aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/seccomp
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-13 23:15:34 +0900
committerOphestra <cat@gensokyo.uk>2025-02-13 23:34:15 +0900
commitfe7d208cf76fa6f24bb9d12ba29b5ed61d837ce3 (patch)
treecac02af50a13b2078739a8f5a74d219f3b60833d /helper/seccomp
parent60c287375048b21eab2bd82f1e7d43e36dcfb3a2 (diff)
helper: use generic extra files interface
This replaces the pipes object and integrates context into helper process lifecycle. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper/seccomp')
-rw-r--r--helper/seccomp/api.go47
-rw-r--r--helper/seccomp/seccomp-export.c8
-rw-r--r--helper/seccomp/seccomp-export.h1
-rw-r--r--helper/seccomp/seccomp.go9
4 files changed, 32 insertions, 33 deletions
diff --git a/helper/seccomp/api.go b/helper/seccomp/api.go
index 5b4a42c3..2799fc90 100644
--- a/helper/seccomp/api.go
+++ b/helper/seccomp/api.go
@@ -1,22 +1,15 @@
package seccomp
import (
+ "context"
"errors"
- "io"
- "os"
"syscall"
+
+ "git.gensokyo.uk/security/fortify/helper/proc"
)
-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
-}
+// New returns an inactive Encoder instance.
+func New(opts SyscallOpts) *Encoder { return &Encoder{newExporter(opts)} }
/*
An Encoder writes a BPF program to an output stream.
@@ -45,7 +38,31 @@ func (e *Encoder) Close() error {
return errors.Join(e.closeWrite(), <-e.exportErr)
}
-// New returns an inactive Encoder instance.
-func New(opts SyscallOpts) *Encoder {
- return &Encoder{newExporter(opts)}
+// NewFile returns an instance of exporter implementing [proc.File].
+func NewFile(opts SyscallOpts) proc.File { return &File{opts: opts} }
+
+// File implements [proc.File] and provides access to the read end of exporter pipe.
+type File struct {
+ opts SyscallOpts
+ proc.BaseFile
+}
+
+func (f *File) ErrCount() int { return 2 }
+func (f *File) Fulfill(ctx context.Context, dispatchErr func(error)) error {
+ e := newExporter(f.opts)
+ if err := e.prepare(); err != nil {
+ return err
+ }
+ f.Set(e.r)
+ go func() {
+ select {
+ case err := <-e.exportErr:
+ dispatchErr(nil)
+ dispatchErr(err)
+ case <-ctx.Done():
+ dispatchErr(e.closeWrite())
+ dispatchErr(<-e.exportErr)
+ }
+ }()
+ return nil
}
diff --git a/helper/seccomp/seccomp-export.c b/helper/seccomp/seccomp-export.c
index 855c3bf0..5a3a56be 100644
--- a/helper/seccomp/seccomp-export.c
+++ b/helper/seccomp/seccomp-export.c
@@ -48,14 +48,6 @@ struct f_syscall_act {
} \
} while (0)
-
-int f_tmpfile_fd() {
- FILE *f = tmpfile();
- if (f == NULL)
- return -1;
- return fileno(f);
-}
-
int32_t f_export_bpf(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts opts) {
int32_t res = 0; // refer to resErr for meaning
int allow_multiarch = opts & F_MULTIARCH;
diff --git a/helper/seccomp/seccomp-export.h b/helper/seccomp/seccomp-export.h
index 3a28b127..df158d91 100644
--- a/helper/seccomp/seccomp-export.h
+++ b/helper/seccomp/seccomp-export.h
@@ -20,5 +20,4 @@ typedef enum {
} f_syscall_opts;
extern void F_println(char *v);
-int f_tmpfile_fd();
int32_t f_export_bpf(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts opts); \ No newline at end of file
diff --git a/helper/seccomp/seccomp.go b/helper/seccomp/seccomp.go
index affe369c..a8cf8bb2 100644
--- a/helper/seccomp/seccomp.go
+++ b/helper/seccomp/seccomp.go
@@ -9,7 +9,6 @@ import "C"
import (
"errors"
"fmt"
- "os"
"runtime"
)
@@ -47,14 +46,6 @@ const (
FlagBluetooth SyscallOpts = C.F_BLUETOOTH
)
-func tmpfile() (*os.File, error) {
- fd, err := C.f_tmpfile_fd()
- if err != nil {
- return nil, err
- }
- return os.NewFile(uintptr(fd), "tmpfile"), err
-}
-
func exportFilter(fd uintptr, opts SyscallOpts) error {
var (
arch C.uint32_t = 0