2024-10-08 18:02:38 +09:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
2025-02-13 23:15:34 +09:00
|
|
|
"context"
|
2024-10-08 18:02:38 +09:00
|
|
|
"errors"
|
|
|
|
"io"
|
2024-12-06 04:21:37 +09:00
|
|
|
"os"
|
2025-02-13 23:15:34 +09:00
|
|
|
"slices"
|
2024-10-08 18:02:38 +09:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2024-12-20 00:20:02 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/helper/bwrap"
|
2025-02-13 23:15:34 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/helper/proc"
|
2024-10-08 18:02:38 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// BubblewrapName is the file name or path to bubblewrap.
|
|
|
|
var BubblewrapName = "bwrap"
|
|
|
|
|
|
|
|
type bubblewrap struct {
|
2025-02-13 23:15:34 +09:00
|
|
|
// final args fd of bwrap process
|
|
|
|
argsFd uintptr
|
2024-10-08 18:02:38 +09:00
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
// name of the command to run in bwrap
|
|
|
|
name string
|
2024-10-08 18:02:38 +09:00
|
|
|
|
|
|
|
lock sync.RWMutex
|
2025-02-13 23:15:34 +09:00
|
|
|
*helperCmd
|
2024-10-08 18:02:38 +09:00
|
|
|
}
|
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
func (b *bubblewrap) Start(ctx context.Context, stat bool) error {
|
2024-10-08 18:02:38 +09:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
// Check for doubled Start calls before we defer failure cleanup. If the prior
|
|
|
|
// call to Start succeeded, we don't want to spuriously close its pipes.
|
2025-02-13 23:15:34 +09:00
|
|
|
if b.Cmd != nil && b.Cmd.Process != nil {
|
2024-10-08 18:02:38 +09:00
|
|
|
return errors.New("exec: already started")
|
|
|
|
}
|
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
args := b.finalise(ctx, stat)
|
|
|
|
b.Cmd.Args = slices.Grow(b.Cmd.Args, 4+len(args))
|
|
|
|
b.Cmd.Args = append(b.Cmd.Args, "--args", strconv.Itoa(int(b.argsFd)), "--", b.name)
|
|
|
|
b.Cmd.Args = append(b.Cmd.Args, args...)
|
|
|
|
return proc.Fulfill(ctx, b.Cmd, b.files, b.extraFiles)
|
2024-10-08 18:02:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2025-01-19 18:38:13 +09:00
|
|
|
func MustNewBwrap(
|
|
|
|
conf *bwrap.Config, name string,
|
|
|
|
wt io.WriterTo, argF func(argsFD, statFD int) []string,
|
2025-01-22 01:51:10 +09:00
|
|
|
extraFiles []*os.File,
|
|
|
|
syncFd *os.File,
|
2025-01-19 18:38:13 +09:00
|
|
|
) Helper {
|
2025-01-22 01:51:10 +09:00
|
|
|
b, err := NewBwrap(conf, name, wt, argF, extraFiles, syncFd)
|
2024-10-08 18:02:38 +09:00
|
|
|
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.
|
2025-01-19 18:38:13 +09:00
|
|
|
func NewBwrap(
|
|
|
|
conf *bwrap.Config, name string,
|
2025-02-13 23:15:34 +09:00
|
|
|
wt io.WriterTo, argF func(argsFd, statFd int) []string,
|
2025-01-22 01:51:10 +09:00
|
|
|
extraFiles []*os.File,
|
|
|
|
syncFd *os.File,
|
2025-01-19 18:38:13 +09:00
|
|
|
) (Helper, error) {
|
2024-10-08 18:02:38 +09:00
|
|
|
b := new(bubblewrap)
|
|
|
|
|
|
|
|
b.name = name
|
2025-02-13 23:15:34 +09:00
|
|
|
b.helperCmd = newHelperCmd(b, BubblewrapName, wt, argF, extraFiles)
|
2025-01-22 01:51:10 +09:00
|
|
|
|
2025-02-14 18:13:06 +09:00
|
|
|
if v, err := NewCheckedArgs(conf.Args(syncFd, b.extraFiles, &b.files)); err != nil {
|
2025-01-22 01:51:10 +09:00
|
|
|
return nil, err
|
|
|
|
} else {
|
2025-02-13 23:15:34 +09:00
|
|
|
f := proc.NewWriterTo(v)
|
|
|
|
b.argsFd = proc.InitFile(f, b.extraFiles)
|
|
|
|
b.files = append(b.files, f)
|
2025-01-22 01:51:10 +09:00
|
|
|
}
|
2024-10-08 18:02:38 +09:00
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|