| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 3 | |
| 4 | package fileargs |
| 5 | |
| 6 | import ( |
| 7 | "crypto/rand" |
| 8 | "encoding/hex" |
| 9 | "fmt" |
| 10 | "io" |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 11 | "os" |
| 12 | "path/filepath" |
| 13 | |
| 14 | "golang.org/x/sys/unix" |
| 15 | ) |
| 16 | |
| 17 | // DefaultSize is the default size limit for FileArgs |
| 18 | const DefaultSize = 4 * 1024 * 1024 |
| 19 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 20 | // TempDirectory is the directory where FileArgs will mount the actual files |
| 21 | // to. Defaults to os.TempDir() but can be globally overridden by the |
| 22 | // application before any FileArgs are used. |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 23 | var TempDirectory = os.TempDir() |
| 24 | |
| 25 | type FileArgs struct { |
| 26 | path string |
| 27 | lastError error |
| 28 | } |
| 29 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 30 | // New initializes a new set of file-based arguments. Remember to call Close() |
| 31 | // if you're done using it, otherwise this leaks memory and mounts. |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 32 | func New() (*FileArgs, error) { |
| 33 | return NewWithSize(DefaultSize) |
| 34 | } |
| 35 | |
| Tim Windelschmidt | 51daf25 | 2024-04-18 23:18:43 +0200 | [diff] [blame] | 36 | // NewWithSize is the same as new, but with a custom size limit. Please be aware |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 37 | // that this data cannot be swapped out and using a size limit that's too high |
| 38 | // can deadlock your kernel. |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 39 | func NewWithSize(size uint64) (*FileArgs, error) { |
| 40 | randomNameRaw := make([]byte, 128/8) |
| 41 | if _, err := io.ReadFull(rand.Reader, randomNameRaw); err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | tmpPath := filepath.Join(TempDirectory, hex.EncodeToString(randomNameRaw)) |
| 45 | if err := os.MkdirAll(tmpPath, 0700); err != nil { |
| 46 | return nil, err |
| 47 | } |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 48 | // This uses ramfs instead of tmpfs because we never want to swap this for |
| 49 | // security reasons |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 50 | if err := unix.Mount("none", tmpPath, "ramfs", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, fmt.Sprintf("size=%v", size)); err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | return &FileArgs{ |
| 54 | path: tmpPath, |
| 55 | }, nil |
| 56 | } |
| 57 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 58 | // ArgPath returns the path of the temporary file for this argument. It names |
| 59 | // the temporary file according to name. |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 60 | func (f *FileArgs) ArgPath(name string, content []byte) string { |
| 61 | if f.lastError != nil { |
| 62 | return "" |
| 63 | } |
| 64 | |
| 65 | path := filepath.Join(f.path, name) |
| 66 | |
| Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 67 | if err := os.WriteFile(path, content, 0600); err != nil { |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 68 | f.lastError = err |
| 69 | return "" |
| 70 | } |
| 71 | |
| 72 | return path |
| 73 | } |
| 74 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 75 | // FileOpt returns a full option with the temporary file name already filled |
| 76 | // in. Example: |
| 77 | // |
| 78 | // option := FileOpt("--testopt", "test.txt", []byte("hello")) |
| 79 | // option == "--testopt=/tmp/daf8ed.../test.txt" |
| Lorenz Brun | 7193620 | 2019-11-18 10:22:57 +0100 | [diff] [blame] | 80 | func (f *FileArgs) FileOpt(optName, fileName string, content []byte) string { |
| 81 | return fmt.Sprintf("%v=%v", optName, f.ArgPath(fileName, content)) |
| 82 | } |
| 83 | |
| 84 | func (f *FileArgs) Error() error { |
| 85 | return f.lastError |
| 86 | } |
| 87 | |
| 88 | func (f *FileArgs) Close() error { |
| 89 | if err := unix.Unmount(f.path, 0); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | return os.Remove(f.path) |
| 93 | } |