helper/seccomp: separate seccomp package
All checks were successful
Test / Create distribution (push) Successful in 1m39s
Test / Run NixOS test (push) Successful in 3m31s

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-01-25 12:59:11 +09:00
parent 016da20443
commit 163f15e93f
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
7 changed files with 47 additions and 45 deletions

View File

@ -2,9 +2,9 @@ package bwrap
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"git.gensokyo.uk/security/fortify/helper/seccomp"
"git.gensokyo.uk/security/fortify/internal/fmsg" "git.gensokyo.uk/security/fortify/internal/fmsg"
) )
@ -53,24 +53,24 @@ func (c *Config) resolveSeccomp() (*os.File, error) {
// resolve seccomp filter opts // resolve seccomp filter opts
var ( var (
opts syscallOpts opts seccomp.SyscallOpts
optd []string optd []string
optCond = [...]struct { optCond = [...]struct {
v bool v bool
o syscallOpts o seccomp.SyscallOpts
d string d string
}{ }{
{!c.Syscall.Compat, flagExt, "fortify"}, {!c.Syscall.Compat, seccomp.FlagExt, "fortify"},
{!c.UserNS, flagDenyNS, "denyns"}, {!c.UserNS, seccomp.FlagDenyNS, "denyns"},
{c.NewSession, flagDenyTTY, "denytty"}, {c.NewSession, seccomp.FlagDenyTTY, "denytty"},
{c.Syscall.DenyDevel, flagDenyDevel, "denydevel"}, {c.Syscall.DenyDevel, seccomp.FlagDenyDevel, "denydevel"},
{c.Syscall.Multiarch, flagMultiarch, "multiarch"}, {c.Syscall.Multiarch, seccomp.FlagMultiarch, "multiarch"},
{c.Syscall.Linux32, flagLinux32, "linux32"}, {c.Syscall.Linux32, seccomp.FlagLinux32, "linux32"},
{c.Syscall.Can, flagCan, "can"}, {c.Syscall.Can, seccomp.FlagCan, "can"},
{c.Syscall.Bluetooth, flagBluetooth, "bluetooth"}, {c.Syscall.Bluetooth, seccomp.FlagBluetooth, "bluetooth"},
} }
) )
if CPrintln != nil { if seccomp.CPrintln != nil {
optd = make([]string, 1, len(optCond)+1) optd = make([]string, 1, len(optCond)+1)
optd[0] = "common" optd[0] = "common"
} }
@ -82,22 +82,9 @@ func (c *Config) resolveSeccomp() (*os.File, error) {
} }
} }
} }
if CPrintln != nil { if seccomp.CPrintln != nil {
CPrintln(fmt.Sprintf("seccomp flags: %s", optd)) seccomp.CPrintln(fmt.Sprintf("seccomp flags: %s", optd))
} }
// export seccomp filter to tmpfile return seccomp.Export(opts)
if f, err := tmpfile(); err != nil {
return nil, err
} else {
return f, exportAndSeek(f, opts)
}
}
func exportAndSeek(f *os.File, opts syscallOpts) error {
if err := exportFilter(f.Fd(), opts); err != nil {
return err
}
_, err := f.Seek(0, io.SeekStart)
return err
} }

17
helper/seccomp/export.go Normal file
View File

@ -0,0 +1,17 @@
package seccomp
import (
"io"
"os"
)
func Export(opts SyscallOpts) (f *os.File, err error) {
if f, err = tmpfile(); err != nil {
return
}
if err = exportFilter(f.Fd(), opts); err != nil {
return
}
_, err = f.Seek(0, io.SeekStart)
return
}

View File

@ -1,4 +1,4 @@
package bwrap package seccomp
/* /*
#cgo linux pkg-config: --static libseccomp #cgo linux pkg-config: --static libseccomp
@ -25,19 +25,17 @@ var resErr = [...]error{
6: errors.New("seccomp_export_bpf failed"), 6: errors.New("seccomp_export_bpf failed"),
} }
type ( type SyscallOpts = C.f_syscall_opts
syscallOpts = C.f_syscall_opts
)
const ( const (
flagExt syscallOpts = C.F_EXT FlagExt SyscallOpts = C.F_EXT
flagDenyNS syscallOpts = C.F_DENY_NS FlagDenyNS SyscallOpts = C.F_DENY_NS
flagDenyTTY syscallOpts = C.F_DENY_TTY FlagDenyTTY SyscallOpts = C.F_DENY_TTY
flagDenyDevel syscallOpts = C.F_DENY_DEVEL FlagDenyDevel SyscallOpts = C.F_DENY_DEVEL
flagMultiarch syscallOpts = C.F_MULTIARCH FlagMultiarch SyscallOpts = C.F_MULTIARCH
flagLinux32 syscallOpts = C.F_LINUX32 FlagLinux32 SyscallOpts = C.F_LINUX32
flagCan syscallOpts = C.F_CAN FlagCan SyscallOpts = C.F_CAN
flagBluetooth syscallOpts = C.F_BLUETOOTH FlagBluetooth SyscallOpts = C.F_BLUETOOTH
) )
func tmpfile() (*os.File, error) { func tmpfile() (*os.File, error) {
@ -48,7 +46,7 @@ func tmpfile() (*os.File, error) {
return os.NewFile(uintptr(fd), "tmpfile"), err return os.NewFile(uintptr(fd), "tmpfile"), err
} }
func exportFilter(fd uintptr, opts syscallOpts) error { func exportFilter(fd uintptr, opts SyscallOpts) error {
var ( var (
arch C.uint32_t = 0 arch C.uint32_t = 0
multiarch C.uint32_t = 0 multiarch C.uint32_t = 0

View File

@ -8,7 +8,7 @@ import (
"git.gensokyo.uk/security/fortify/fst" "git.gensokyo.uk/security/fortify/fst"
"git.gensokyo.uk/security/fortify/helper" "git.gensokyo.uk/security/fortify/helper"
"git.gensokyo.uk/security/fortify/helper/bwrap" "git.gensokyo.uk/security/fortify/helper/seccomp"
"git.gensokyo.uk/security/fortify/internal" "git.gensokyo.uk/security/fortify/internal"
"git.gensokyo.uk/security/fortify/internal/fmsg" "git.gensokyo.uk/security/fortify/internal/fmsg"
"git.gensokyo.uk/security/fortify/internal/proc" "git.gensokyo.uk/security/fortify/internal/proc"
@ -128,7 +128,7 @@ func Main() {
helper.BubblewrapName = payload.Exec[0] // resolved bwrap path by parent helper.BubblewrapName = payload.Exec[0] // resolved bwrap path by parent
if fmsg.Verbose() { if fmsg.Verbose() {
bwrap.CPrintln = fmsg.Println seccomp.CPrintln = fmsg.Println
} }
if b, err := helper.NewBwrap( if b, err := helper.NewBwrap(
conf, innerInit, conf, innerInit,

View File

@ -16,7 +16,7 @@ import (
"git.gensokyo.uk/security/fortify/dbus" "git.gensokyo.uk/security/fortify/dbus"
"git.gensokyo.uk/security/fortify/fst" "git.gensokyo.uk/security/fortify/fst"
"git.gensokyo.uk/security/fortify/helper/bwrap" "git.gensokyo.uk/security/fortify/helper/seccomp"
"git.gensokyo.uk/security/fortify/internal" "git.gensokyo.uk/security/fortify/internal"
"git.gensokyo.uk/security/fortify/internal/app" "git.gensokyo.uk/security/fortify/internal/app"
"git.gensokyo.uk/security/fortify/internal/fmsg" "git.gensokyo.uk/security/fortify/internal/fmsg"
@ -310,7 +310,7 @@ func runApp(config *fst.Config) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
if fmsg.Verbose() { if fmsg.Verbose() {
bwrap.CPrintln = fmsg.Println seccomp.CPrintln = fmsg.Println
} }
// handle signals for graceful shutdown // handle signals for graceful shutdown