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