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

import (
	"fmt"
	"strconv"

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

type SyscallPolicy struct {
	// disable fortify extensions
	Compat bool `json:"compat"`
	// deny development syscalls
	DenyDevel bool `json:"deny_devel"`
	// deny multiarch/emulation syscalls
	Multiarch bool `json:"multiarch"`
	// allow PER_LINUX32
	Linux32 bool `json:"linux32"`
	// allow AF_CAN
	Can bool `json:"can"`
	// allow AF_BLUETOOTH
	Bluetooth bool `json:"bluetooth"`
}

func (c *Config) seccompArgs() FDBuilder {
	// explicitly disable syscall filter
	if c.Syscall == nil {
		// nil File skips builder
		return new(seccompBuilder)
	}

	var (
		opts    seccomp.SyscallOpts
		optd    []string
		optCond = [...]struct {
			v bool
			o seccomp.SyscallOpts
			d string
		}{
			{!c.Syscall.Compat, seccomp.FlagExt, "fortify"},
			{!c.UserNS, seccomp.FlagDenyNS, "denyns"},
			{c.NewSession, seccomp.FlagDenyTTY, "denytty"},
			{c.Syscall.DenyDevel, seccomp.FlagDenyDevel, "denydevel"},
			{c.Syscall.Multiarch, seccomp.FlagMultiarch, "multiarch"},
			{c.Syscall.Linux32, seccomp.FlagLinux32, "linux32"},
			{c.Syscall.Can, seccomp.FlagCan, "can"},
			{c.Syscall.Bluetooth, seccomp.FlagBluetooth, "bluetooth"},
		}
	)
	scmpPrintln := seccomp.GetOutput()
	if scmpPrintln != nil {
		optd = make([]string, 1, len(optCond)+1)
		optd[0] = "common"
	}
	for _, opt := range optCond {
		if opt.v {
			opts |= opt.o
			if scmpPrintln != nil {
				optd = append(optd, opt.d)
			}
		}
	}
	if scmpPrintln != nil {
		scmpPrintln(fmt.Sprintf("seccomp flags: %s", optd))
	}

	return &seccompBuilder{seccomp.NewFile(opts)}
}

type seccompBuilder struct{ proc.File }

func (s *seccompBuilder) Len() int {
	if s == nil || s.File == nil {
		return 0
	}
	return 2
}

func (s *seccompBuilder) Append(args *[]string) {
	if s == nil || s.File == nil {
		return
	}

	*args = append(*args, Seccomp.String(), strconv.Itoa(int(s.Fd())))
}