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

import (
	"context"
	"io"
	"os"
	"os/exec"
	"slices"
	"strconv"
	"syscall"

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

// BubblewrapName is the file name or path to bubblewrap.
var BubblewrapName = "bwrap"

// MustNewBwrap initialises a new Bwrap instance with wt as the null-terminated argument writer.
// If wt is nil, the child process spawned by bwrap will not get an argument pipe.
// Function argF returns an array of arguments passed directly to the child process.
func MustNewBwrap(
	ctx context.Context,
	name string,
	wt io.WriterTo,
	stat bool,
	argF func(argsFd, statFd int) []string,
	cmdF func(cmd *exec.Cmd),
	conf *bwrap.Config,
	setpgid bool,
	extraFiles []*os.File,
	syncFd *os.File,
) Helper {
	b, err := NewBwrap(ctx, name, wt, stat, argF, cmdF, conf, setpgid, extraFiles, syncFd)
	if err != nil {
		panic(err.Error())
	} else {
		return b
	}
}

// NewBwrap initialises a new Bwrap instance with wt as the null-terminated argument writer.
// If wt is nil, the child process spawned by bwrap will not get an argument pipe.
// Function argF returns an array of arguments passed directly to the child process.
func NewBwrap(
	ctx context.Context,
	name string,
	wt io.WriterTo,
	stat bool,
	argF func(argsFd, statFd int) []string,
	cmdF func(cmd *exec.Cmd),
	conf *bwrap.Config,
	setpgid bool,
	extraFiles []*os.File,
	syncFd *os.File,
) (Helper, error) {
	b, args := newHelperCmd(ctx, BubblewrapName, wt, stat, argF, extraFiles)
	if setpgid {
		b.Cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
	}

	var argsFd uintptr
	if v, err := NewCheckedArgs(conf.Args(syncFd, b.extraFiles, &b.files)); err != nil {
		return nil, err
	} else {
		f := proc.NewWriterTo(v)
		argsFd = proc.InitFile(f, b.extraFiles)
		b.files = append(b.files, f)
	}

	b.Args = slices.Grow(b.Args, 4+len(args))
	b.Args = append(b.Args, "--args", strconv.Itoa(int(argsFd)), "--", name)
	b.Args = append(b.Args, args...)
	if cmdF != nil {
		cmdF(b.Cmd)
	}
	return b, nil
}