helper/seccomp: seccomp_load on negative fd
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
df266527f1
commit
be16970e77
@ -11,6 +11,9 @@ import (
|
|||||||
// New returns an inactive Encoder instance.
|
// New returns an inactive Encoder instance.
|
||||||
func New(opts SyscallOpts) *Encoder { return &Encoder{newExporter(opts)} }
|
func New(opts SyscallOpts) *Encoder { return &Encoder{newExporter(opts)} }
|
||||||
|
|
||||||
|
// Load loads a filter into the kernel.
|
||||||
|
func Load(opts SyscallOpts) error { return buildFilter(-1, opts) }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
An Encoder writes a BPF program to an output stream.
|
An Encoder writes a BPF program to an output stream.
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ func (e *exporter) prepare() error {
|
|||||||
|
|
||||||
ec := make(chan error, 1)
|
ec := make(chan error, 1)
|
||||||
go func(fd uintptr) {
|
go func(fd uintptr) {
|
||||||
ec <- exportFilter(fd, e.opts)
|
ec <- buildFilter(int(fd), e.opts)
|
||||||
close(ec)
|
close(ec)
|
||||||
_ = e.closeWrite()
|
_ = e.closeWrite()
|
||||||
runtime.KeepAlive(e.w)
|
runtime.KeepAlive(e.w)
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#define _GNU_SOURCE // CLONE_NEWUSER
|
#define _GNU_SOURCE // CLONE_NEWUSER
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "seccomp-export.h"
|
#include "seccomp-build.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -48,7 +48,7 @@ struct f_syscall_act {
|
|||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
int32_t f_export_bpf(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts opts) {
|
int32_t f_build_filter(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts opts) {
|
||||||
int32_t res = 0; // refer to resErr for meaning
|
int32_t res = 0; // refer to resErr for meaning
|
||||||
int allow_multiarch = opts & F_MULTIARCH;
|
int allow_multiarch = opts & F_MULTIARCH;
|
||||||
int allowed_personality = PER_LINUX;
|
int allowed_personality = PER_LINUX;
|
||||||
@ -285,11 +285,20 @@ int32_t f_export_bpf(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts o
|
|||||||
// Blocklist the rest
|
// Blocklist the rest
|
||||||
seccomp_rule_add_exact(ctx, SCMP_ACT_ERRNO(EAFNOSUPPORT), SCMP_SYS(socket), 1, SCMP_A0(SCMP_CMP_GE, last_allowed_family + 1));
|
seccomp_rule_add_exact(ctx, SCMP_ACT_ERRNO(EAFNOSUPPORT), SCMP_SYS(socket), 1, SCMP_A0(SCMP_CMP_GE, last_allowed_family + 1));
|
||||||
|
|
||||||
ret = seccomp_export_bpf(ctx, fd);
|
if (fd < 0) {
|
||||||
if (ret != 0) {
|
ret = seccomp_load(ctx);
|
||||||
res = 6;
|
if (ret != 0) {
|
||||||
errno = -ret;
|
res = 7;
|
||||||
goto out;
|
errno = -ret;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret = seccomp_export_bpf(ctx, fd);
|
||||||
|
if (ret != 0) {
|
||||||
|
res = 6;
|
||||||
|
errno = -ret;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
@ -20,4 +20,4 @@ typedef enum {
|
|||||||
} f_syscall_opts;
|
} f_syscall_opts;
|
||||||
|
|
||||||
extern void F_println(char *v);
|
extern void F_println(char *v);
|
||||||
int32_t f_export_bpf(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts opts);
|
int32_t f_build_filter(int fd, uint32_t arch, uint32_t multiarch, f_syscall_opts opts);
|
@ -3,7 +3,7 @@ package seccomp
|
|||||||
/*
|
/*
|
||||||
#cgo linux pkg-config: --static libseccomp
|
#cgo linux pkg-config: --static libseccomp
|
||||||
|
|
||||||
#include "seccomp-export.h"
|
#include "seccomp-build.h"
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
@ -22,6 +22,7 @@ var resErr = [...]error{
|
|||||||
4: errors.New("internal libseccomp failure"),
|
4: errors.New("internal libseccomp failure"),
|
||||||
5: errors.New("seccomp_rule_add failed"),
|
5: errors.New("seccomp_rule_add failed"),
|
||||||
6: errors.New("seccomp_export_bpf failed"),
|
6: errors.New("seccomp_export_bpf failed"),
|
||||||
|
7: errors.New("seccomp_load failed"),
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyscallOpts = C.f_syscall_opts
|
type SyscallOpts = C.f_syscall_opts
|
||||||
@ -46,7 +47,7 @@ const (
|
|||||||
FlagBluetooth SyscallOpts = C.F_BLUETOOTH
|
FlagBluetooth SyscallOpts = C.F_BLUETOOTH
|
||||||
)
|
)
|
||||||
|
|
||||||
func exportFilter(fd uintptr, opts SyscallOpts) error {
|
func buildFilter(fd int, 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
|
||||||
@ -70,7 +71,7 @@ func exportFilter(fd uintptr, opts SyscallOpts) error {
|
|||||||
opts |= flagVerbose
|
opts |= flagVerbose
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := C.f_export_bpf(C.int(fd), arch, multiarch, opts)
|
res, err := C.f_build_filter(C.int(fd), arch, multiarch, opts)
|
||||||
if re := resErr[res]; re != nil {
|
if re := resErr[res]; re != nil {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return re
|
return re
|
||||||
|
Loading…
Reference in New Issue
Block a user