Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame^] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package fileargs |
| 18 | |
| 19 | import ( |
| 20 | "crypto/rand" |
| 21 | "encoding/hex" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "io/ioutil" |
| 25 | "os" |
| 26 | "path/filepath" |
| 27 | |
| 28 | "golang.org/x/sys/unix" |
| 29 | ) |
| 30 | |
| 31 | // DefaultSize is the default size limit for FileArgs |
| 32 | const DefaultSize = 4 * 1024 * 1024 |
| 33 | |
| 34 | // TempDirectory is the directory where FileArgs will mount the actual files to. Defaults to |
| 35 | // os.TempDir() but can be globally overridden by the application before any FileArgs are used. |
| 36 | var TempDirectory = os.TempDir() |
| 37 | |
| 38 | type FileArgs struct { |
| 39 | path string |
| 40 | lastError error |
| 41 | } |
| 42 | |
| 43 | // New initializes a new set of file-based arguments. Remember to call Close() if you're done |
| 44 | // using it, otherwise this leaks memory and mounts. |
| 45 | func New() (*FileArgs, error) { |
| 46 | return NewWithSize(DefaultSize) |
| 47 | } |
| 48 | |
| 49 | // NewWthSize is the same as new, but with a custom size limit. Please be aware that this data |
| 50 | // cannot be swapped out and using a size limit that's too high can deadlock your kernel. |
| 51 | func NewWithSize(size uint64) (*FileArgs, error) { |
| 52 | randomNameRaw := make([]byte, 128/8) |
| 53 | if _, err := io.ReadFull(rand.Reader, randomNameRaw); err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | tmpPath := filepath.Join(TempDirectory, hex.EncodeToString(randomNameRaw)) |
| 57 | if err := os.MkdirAll(tmpPath, 0700); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | // This uses ramfs instead of tmpfs because we never want to swap this for security reasons |
| 61 | if err := unix.Mount("none", tmpPath, "ramfs", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, fmt.Sprintf("size=%v", size)); err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return &FileArgs{ |
| 65 | path: tmpPath, |
| 66 | }, nil |
| 67 | } |
| 68 | |
| 69 | // ArgPath returns the path of the temporary file for this argument. It names the temporary |
| 70 | // file according to name. |
| 71 | func (f *FileArgs) ArgPath(name string, content []byte) string { |
| 72 | if f.lastError != nil { |
| 73 | return "" |
| 74 | } |
| 75 | |
| 76 | path := filepath.Join(f.path, name) |
| 77 | |
| 78 | if err := ioutil.WriteFile(path, content, 0600); err != nil { |
| 79 | f.lastError = err |
| 80 | return "" |
| 81 | } |
| 82 | |
| 83 | return path |
| 84 | } |
| 85 | |
| 86 | // FileOpt returns a full option with the temporary file name already filled in. |
| 87 | // Example: `FileOpt("--testopt", "test.txt", []byte("hello")) == "--testopt=/tmp/daf8ed.../test.txt"` |
| 88 | func (f *FileArgs) FileOpt(optName, fileName string, content []byte) string { |
| 89 | return fmt.Sprintf("%v=%v", optName, f.ArgPath(fileName, content)) |
| 90 | } |
| 91 | |
| 92 | func (f *FileArgs) Error() error { |
| 93 | return f.lastError |
| 94 | } |
| 95 | |
| 96 | func (f *FileArgs) Close() error { |
| 97 | if err := unix.Unmount(f.path, 0); err != nil { |
| 98 | return err |
| 99 | } |
| 100 | return os.Remove(f.path) |
| 101 | } |