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