blob: 88863da793d885ec230905a62c4fec01d4c6497e [file] [log] [blame]
Lorenz Brun71936202019-11-18 10:22:57 +01001// 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
17package fileargs
18
19import (
20 "crypto/rand"
21 "encoding/hex"
22 "fmt"
23 "io"
Lorenz Brun71936202019-11-18 10:22:57 +010024 "os"
25 "path/filepath"
26
27 "golang.org/x/sys/unix"
28)
29
30// DefaultSize is the default size limit for FileArgs
31const DefaultSize = 4 * 1024 * 1024
32
Serge Bazanski216fe7b2021-05-21 18:36:16 +020033// 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 Brun71936202019-11-18 10:22:57 +010036var TempDirectory = os.TempDir()
37
38type FileArgs struct {
39 path string
40 lastError error
41}
42
Serge Bazanski216fe7b2021-05-21 18:36:16 +020043// 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 Brun71936202019-11-18 10:22:57 +010045func New() (*FileArgs, error) {
46 return NewWithSize(DefaultSize)
47}
48
Serge Bazanski216fe7b2021-05-21 18:36:16 +020049// 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 Brun71936202019-11-18 10:22:57 +010052func 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 Bazanski216fe7b2021-05-21 18:36:16 +020061 // This uses ramfs instead of tmpfs because we never want to swap this for
62 // security reasons
Lorenz Brun71936202019-11-18 10:22:57 +010063 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 Bazanski216fe7b2021-05-21 18:36:16 +020071// ArgPath returns the path of the temporary file for this argument. It names
72// the temporary file according to name.
Lorenz Brun71936202019-11-18 10:22:57 +010073func (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 Brun764a2de2021-11-22 16:26:36 +010080 if err := os.WriteFile(path, content, 0600); err != nil {
Lorenz Brun71936202019-11-18 10:22:57 +010081 f.lastError = err
82 return ""
83 }
84
85 return path
86}
87
Serge Bazanski216fe7b2021-05-21 18:36:16 +020088// 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 Brun71936202019-11-18 10:22:57 +010093func (f *FileArgs) FileOpt(optName, fileName string, content []byte) string {
94 return fmt.Sprintf("%v=%v", optName, f.ArgPath(fileName, content))
95}
96
97func (f *FileArgs) Error() error {
98 return f.lastError
99}
100
101func (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}