2024-10-15 02:15:55 +09:00
|
|
|
package bwrap
|
|
|
|
|
2025-01-22 01:51:10 +09:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"slices"
|
|
|
|
|
2025-02-08 13:03:45 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/helper/proc"
|
2025-01-22 01:51:10 +09:00
|
|
|
)
|
2024-10-15 02:15:55 +09:00
|
|
|
|
|
|
|
type Builder interface {
|
|
|
|
Len() int
|
|
|
|
Append(args *[]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type FSBuilder interface {
|
|
|
|
Path() string
|
|
|
|
Builder
|
|
|
|
}
|
|
|
|
|
2025-01-22 01:51:10 +09:00
|
|
|
type FDBuilder interface {
|
2025-02-13 23:15:34 +09:00
|
|
|
proc.File
|
|
|
|
Builder
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
|
|
|
|
2024-10-15 02:15:55 +09:00
|
|
|
// Args returns a slice of bwrap args corresponding to c.
|
|
|
|
func (c *Config) Args() (args []string) {
|
|
|
|
builders := []Builder{
|
|
|
|
c.boolArgs(),
|
|
|
|
c.intArgs(),
|
|
|
|
c.stringArgs(),
|
|
|
|
c.pairArgs(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy FSBuilder slice to builder slice
|
|
|
|
fb := make([]Builder, len(c.Filesystem)+1)
|
|
|
|
for i, f := range c.Filesystem {
|
|
|
|
fb[i] = f
|
|
|
|
}
|
|
|
|
fb[len(fb)-1] = c.Chmod
|
|
|
|
builders = append(builders, fb...)
|
|
|
|
|
|
|
|
// accumulate arg count
|
|
|
|
argc := 0
|
|
|
|
for _, b := range builders {
|
|
|
|
argc += b.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
args = make([]string, 0, argc)
|
|
|
|
for _, b := range builders {
|
|
|
|
b.Append(&args)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2025-01-22 01:51:10 +09:00
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
func (c *Config) FDArgs(syncFd *os.File, args *[]string, extraFiles *proc.ExtraFilesPre, files *[]proc.File) {
|
2025-01-22 01:51:10 +09:00
|
|
|
builders := []FDBuilder{
|
2025-02-13 23:15:34 +09:00
|
|
|
c.seccompArgs(),
|
|
|
|
newFile(positionalArgs[SyncFd], syncFd),
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
argc := 0
|
2025-02-13 23:15:34 +09:00
|
|
|
fc := 0
|
2025-01-22 01:51:10 +09:00
|
|
|
for _, b := range builders {
|
2025-02-13 23:15:34 +09:00
|
|
|
l := b.Len()
|
|
|
|
if l < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
argc += l
|
|
|
|
fc++
|
|
|
|
|
|
|
|
proc.InitFile(b, extraFiles)
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
fc++ // allocate extra slot for stat fd
|
|
|
|
*args = slices.Grow(*args, argc)
|
|
|
|
*files = slices.Grow(*files, fc)
|
2025-01-22 01:51:10 +09:00
|
|
|
|
|
|
|
for _, b := range builders {
|
2025-02-13 23:15:34 +09:00
|
|
|
if b.Len() < 1 {
|
|
|
|
continue
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
2025-02-13 23:15:34 +09:00
|
|
|
|
|
|
|
b.Append(args)
|
|
|
|
*files = append(*files, b)
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|