blob: d3be1f7cf722390d313023996673c715ff7047a3 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brun71936202019-11-18 10:22:57 +01002// SPDX-License-Identifier: Apache-2.0
Lorenz Brun71936202019-11-18 10:22:57 +01003
4package fileargs
5
6import (
7 "crypto/rand"
8 "encoding/hex"
9 "fmt"
10 "io"
Lorenz Brun71936202019-11-18 10:22:57 +010011 "os"
12 "path/filepath"
13
14 "golang.org/x/sys/unix"
15)
16
17// DefaultSize is the default size limit for FileArgs
18const DefaultSize = 4 * 1024 * 1024
19
Serge Bazanski216fe7b2021-05-21 18:36:16 +020020// 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 Brun71936202019-11-18 10:22:57 +010023var TempDirectory = os.TempDir()
24
25type FileArgs struct {
26 path string
27 lastError error
28}
29
Serge Bazanski216fe7b2021-05-21 18:36:16 +020030// 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 Brun71936202019-11-18 10:22:57 +010032func New() (*FileArgs, error) {
33 return NewWithSize(DefaultSize)
34}
35
Tim Windelschmidt51daf252024-04-18 23:18:43 +020036// NewWithSize is the same as new, but with a custom size limit. Please be aware
Serge Bazanski216fe7b2021-05-21 18:36:16 +020037// that this data cannot be swapped out and using a size limit that's too high
38// can deadlock your kernel.
Lorenz Brun71936202019-11-18 10:22:57 +010039func 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 Bazanski216fe7b2021-05-21 18:36:16 +020048 // This uses ramfs instead of tmpfs because we never want to swap this for
49 // security reasons
Lorenz Brun71936202019-11-18 10:22:57 +010050 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 Bazanski216fe7b2021-05-21 18:36:16 +020058// ArgPath returns the path of the temporary file for this argument. It names
59// the temporary file according to name.
Lorenz Brun71936202019-11-18 10:22:57 +010060func (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 Brun764a2de2021-11-22 16:26:36 +010067 if err := os.WriteFile(path, content, 0600); err != nil {
Lorenz Brun71936202019-11-18 10:22:57 +010068 f.lastError = err
69 return ""
70 }
71
72 return path
73}
74
Serge Bazanski216fe7b2021-05-21 18:36:16 +020075// 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 Brun71936202019-11-18 10:22:57 +010080func (f *FileArgs) FileOpt(optName, fileName string, content []byte) string {
81 return fmt.Sprintf("%v=%v", optName, f.ArgPath(fileName, content))
82}
83
84func (f *FileArgs) Error() error {
85 return f.lastError
86}
87
88func (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}