diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-03-14 17:51:29 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-03-14 17:55:55 +0900 |
| commit | 4bb5d9780f3c46e942dc778d93c28391caee33c7 (patch) | |
| tree | be07ad4378fa9dda5f2615a9d5e07b71bd17c1e5 /ldd/exec.go | |
| parent | f41fd946284aa30e7529bd92193632b20d77a2dd (diff) | |
ldd: run in native sandbox
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'ldd/exec.go')
| -rw-r--r-- | ldd/exec.go | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/ldd/exec.go b/ldd/exec.go index 5c51c6a2..4f77ae30 100644 --- a/ldd/exec.go +++ b/ldd/exec.go @@ -7,8 +7,7 @@ import ( "os/exec" "time" - "git.gensokyo.uk/security/fortify/helper" - "git.gensokyo.uk/security/fortify/helper/bwrap" + "git.gensokyo.uk/security/fortify/internal/sandbox" ) const lddTimeout = 2 * time.Second @@ -18,34 +17,31 @@ var ( msgStaticGlibc = []byte("not a dynamic executable") ) -func Exec(ctx context.Context, p string) ([]*Entry, error) { - var h helper.Helper - - if toolPath, err := exec.LookPath("ldd"); err != nil { - return nil, err - } else if h, err = helper.NewBwrap( - (&bwrap.Config{ - Hostname: "fortify-ldd", - Chdir: "/", - Syscall: &bwrap.SyscallPolicy{DenyDevel: true, Multiarch: true}, - NewSession: true, - DieWithParent: true, - }).Bind("/", "/").DevTmpfs("/dev"), toolPath, false, - nil, func(_, _ int) []string { return []string{p} }, - nil, nil, - ); err != nil { - return nil, err - } - - stdout, stderr := new(bytes.Buffer), new(bytes.Buffer) - h.Stdout(stdout).Stderr(stderr) +func Exec(ctx context.Context, p string) ([]*Entry, error) { return ExecFilter(ctx, nil, nil, p) } +func ExecFilter(ctx context.Context, + commandContext func(context.Context) *exec.Cmd, + f func([]byte) []byte, + p string) ([]*Entry, error) { c, cancel := context.WithTimeout(ctx, lddTimeout) defer cancel() - if err := h.Start(c, false); err != nil { + container := sandbox.New(c, "ldd", p) + container.Hostname = "fortify-ldd" + stdout, stderr := new(bytes.Buffer), new(bytes.Buffer) + container.Stdout = stdout + container.Stderr = stderr + container.Bind("/", "/", 0).Dev("/dev") + + if commandContext != nil { + container.CommandContext = commandContext + } + + if err := container.Start(); err != nil { + return nil, err + } else if err = container.Serve(); err != nil { return nil, err } - if err := h.Wait(); err != nil { + if err := container.Wait(); err != nil { m := stderr.Bytes() if bytes.Contains(m, append([]byte(p+": "), msgStatic...)) || bytes.Contains(m, msgStaticGlibc) { @@ -56,5 +52,9 @@ func Exec(ctx context.Context, p string) ([]*Entry, error) { return nil, err } - return Parse(stdout) + v := stdout.Bytes() + if f != nil { + v = f(v) + } + return Parse(v) } |
