2025-01-22 01:51:10 +09:00
|
|
|
package bwrap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2025-01-25 12:59:11 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/helper/seccomp"
|
2025-01-22 01:51:10 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SyscallPolicy struct {
|
2025-01-25 12:35:47 +09:00
|
|
|
// disable fortify extensions
|
|
|
|
Compat bool `json:"compat"`
|
|
|
|
// deny development syscalls
|
2025-01-22 01:51:10 +09:00
|
|
|
DenyDevel bool `json:"deny_devel"`
|
2025-01-25 12:35:47 +09:00
|
|
|
// deny multiarch/emulation syscalls
|
2025-01-22 01:51:10 +09:00
|
|
|
Multiarch bool `json:"multiarch"`
|
2025-01-25 12:35:47 +09:00
|
|
|
// allow PER_LINUX32
|
|
|
|
Linux32 bool `json:"linux32"`
|
|
|
|
// allow AF_CAN
|
|
|
|
Can bool `json:"can"`
|
|
|
|
// allow AF_BLUETOOTH
|
2025-01-22 01:51:10 +09:00
|
|
|
Bluetooth bool `json:"bluetooth"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type seccompBuilder struct {
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *seccompBuilder) Len() int {
|
|
|
|
if s == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *seccompBuilder) Append(args *[]string, extraFiles *[]*os.File) error {
|
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if f, err := s.config.resolveSeccomp(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
extraFile(args, extraFiles, positionalArgs[Seccomp], f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) resolveSeccomp() (*os.File, error) {
|
|
|
|
if c.Syscall == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve seccomp filter opts
|
|
|
|
var (
|
2025-01-25 12:59:11 +09:00
|
|
|
opts seccomp.SyscallOpts
|
2025-01-22 01:51:10 +09:00
|
|
|
optd []string
|
|
|
|
optCond = [...]struct {
|
|
|
|
v bool
|
2025-01-25 12:59:11 +09:00
|
|
|
o seccomp.SyscallOpts
|
2025-01-22 01:51:10 +09:00
|
|
|
d string
|
|
|
|
}{
|
2025-01-25 12:59:11 +09:00
|
|
|
{!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"},
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
|
|
|
)
|
2025-01-25 12:59:11 +09:00
|
|
|
if seccomp.CPrintln != nil {
|
2025-01-22 01:51:10 +09:00
|
|
|
optd = make([]string, 1, len(optCond)+1)
|
|
|
|
optd[0] = "common"
|
|
|
|
}
|
|
|
|
for _, opt := range optCond {
|
|
|
|
if opt.v {
|
|
|
|
opts |= opt.o
|
|
|
|
if fmsg.Verbose() {
|
|
|
|
optd = append(optd, opt.d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-01-25 12:59:11 +09:00
|
|
|
if seccomp.CPrintln != nil {
|
|
|
|
seccomp.CPrintln(fmt.Sprintf("seccomp flags: %s", optd))
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
|
|
|
|
2025-01-25 12:59:11 +09:00
|
|
|
return seccomp.Export(opts)
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|