aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/bwrap/arg.go
blob: 6d79a8b2140a6d0765168e38c2d2e033e10ce412 (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
package bwrap

import (
	"os"
	"slices"

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

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

type FSBuilder interface {
	Path() string
	Builder
}

type FDBuilder interface {
	proc.File
	Builder
}

// Args returns a slice of bwrap args corresponding to c.
func (c *Config) Args(syncFd *os.File, extraFiles *proc.ExtraFilesPre, files *[]proc.File) (args []string) {
	builders := []Builder{
		c.boolArgs(),
		c.intArgs(),
		c.stringArgs(),
		c.pairArgs(),
		c.seccompArgs(),
		newFile(SyncFd.String(), syncFd),
	}

	builders = slices.Grow(builders, len(c.Filesystem)+1)
	for _, f := range c.Filesystem {
		builders = append(builders, f)
	}
	builders = append(builders, c.Chmod)

	argc := 0
	fc := 0
	for _, b := range builders {
		l := b.Len()
		if l < 1 {
			continue
		}
		argc += l

		if f, ok := b.(FDBuilder); ok {
			fc++
			proc.InitFile(f, extraFiles)
		}
	}
	fc++ // allocate extra slot for stat fd

	args = make([]string, 0, argc)
	*files = slices.Grow(*files, fc)
	for _, b := range builders {
		if b.Len() < 1 {
			continue
		}
		b.Append(&args)

		if f, ok := b.(FDBuilder); ok {
			*files = append(*files, f)
		}
	}

	return
}