aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/bwrap/arg.go
blob: d419ba50ddc088ac28061e9f563aa76ea3cb97d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package bwrap

import (
	"encoding/gob"
	"os"
	"slices"
	"strconv"

	"git.gensokyo.uk/security/fortify/internal/proc"
)

type Builder interface {
	Len() int
	Append(args *[]string)
}

type FSBuilder interface {
	Path() string
	Builder
}

type FDBuilder interface {
	Len() int
	Append(args *[]string, extraFiles *[]*os.File) error
}

func init() {
	gob.Register(new(pairF))
	gob.Register(new(stringF))
}

type pairF [3]string

func (p *pairF) Path() string {
	return p[2]
}

func (p *pairF) Len() int {
	return len(p) // compiler replaces this with 3
}

func (p *pairF) Append(args *[]string) {
	*args = append(*args, p[0], p[1], p[2])
}

type stringF [2]string

func (s stringF) Path() string {
	return s[1]
}

func (s stringF) Len() int {
	return len(s) // compiler replaces this with 2
}

func (s stringF) Append(args *[]string) {
	*args = append(*args, s[0], s[1])
}

type fileF struct {
	name string
	file *os.File
}

func (f *fileF) Len() int {
	if f.file == nil {
		return 0
	}
	return 2
}

func (f *fileF) Append(args *[]string, extraFiles *[]*os.File) error {
	if f.file == nil {
		return nil
	}
	extraFile(args, extraFiles, f.name, f.file)
	return nil
}

func extraFile(args *[]string, extraFiles *[]*os.File, name string, f *os.File) {
	if f == nil {
		return
	}
	*args = append(*args, name, strconv.Itoa(int(proc.ExtraFileSlice(extraFiles, f))))
}

// Args returns a slice of bwrap args corresponding to c.
func (c *Config) Args() (args []string) {
	builders := []Builder{
		c.boolArgs(),
		c.intArgs(),
		c.stringArgs(),
		c.pairArgs(),
	}

	// copy FSBuilder slice to builder slice
	fb := make([]Builder, len(c.Filesystem)+1)
	for i, f := range c.Filesystem {
		fb[i] = f
	}
	fb[len(fb)-1] = c.Chmod
	builders = append(builders, fb...)

	// accumulate arg count
	argc := 0
	for _, b := range builders {
		argc += b.Len()
	}

	args = make([]string, 0, argc)
	for _, b := range builders {
		b.Append(&args)
	}

	return
}

func (c *Config) FDArgs(syncFd *os.File, extraFiles *[]*os.File) (args []string, err error) {
	builders := []FDBuilder{
		&seccompBuilder{c},
		&fileF{positionalArgs[SyncFd], syncFd},
	}

	argc := 0
	for _, b := range builders {
		argc += b.Len()
	}

	args = make([]string, 0, argc)
	*extraFiles = slices.Grow(*extraFiles, len(builders))

	for _, b := range builders {
		if err = b.Append(&args, extraFiles); err != nil {
			break
		}
	}
	return
}