hakurei/container/landlock/landlock.go
Clayton Gilmer 2de7c2d07d
Some checks failed
Test / Sandbox (push) Successful in 2m17s
Test / Hakurei (race detector) (pull_request) Failing after 1h0m59s
Test / Flake checks (pull_request) Has been skipped
Test / Sandbox (pull_request) Successful in 1m29s
Test / Hpkg (push) Successful in 4m12s
Test / Hpkg (pull_request) Successful in 1m56s
Test / Sandbox (race detector) (push) Successful in 4m17s
Test / Sandbox (race detector) (pull_request) Successful in 2m5s
Test / Create distribution (pull_request) Failing after 28s
Test / Hakurei (pull_request) Failing after 20m30s
Test / Hakurei (race detector) (push) Failing after 22m21s
Test / Hakurei (push) Failing after 39m46s
Test / Flake checks (push) Has been skipped
Test / Create distribution (push) Failing after 46s
container: optionally isolate host abstract UNIX domain sockets via landlock
2025-08-17 16:05:49 +09:00

58 lines
1.3 KiB
Go

package landlock
/*
#cgo linux pkg-config: --static libpsx
#include <linux/landlock.h>
#include <sys/syscall.h>
#include "landlock-helper.h"
*/
import "C"
import (
"fmt"
"syscall"
"unsafe"
)
const (
LANDLOCK_CREATE_RULESET_VERSION = C.LANDLOCK_CREATE_RULESET_VERSION
LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET = C.LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
SYS_LANDLOCK_CREATE_RULESET = C.SYS_landlock_create_ruleset
)
type LandlockRulesetAttr = C.struct_landlock_ruleset_attr
func ScopeAbstract() error {
abi, _, err := syscall.Syscall(SYS_LANDLOCK_CREATE_RULESET, 0, 0, LANDLOCK_CREATE_RULESET_VERSION)
if err != 0 {
return fmt.Errorf("could not fetch landlock ABI: errno %v", err)
}
if abi < 6 {
return fmt.Errorf("landlock ABI must be >= 6, got %d", abi)
}
attrs := LandlockRulesetAttr{
scoped: LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
}
fd, _, err := syscall.Syscall(SYS_LANDLOCK_CREATE_RULESET, uintptr(unsafe.Pointer(&attrs)), unsafe.Sizeof(attrs), 0)
if err != 0 {
return fmt.Errorf("could not create landlock ruleset: errno %v", err)
}
defer syscall.Close(int(fd))
var errno C.int
if rv := C.hakurei_scope_abstract_unix_sockets(&errno, C.int(fd)); rv != 0 {
return fmt.Errorf("could not restrict self via landlock: errno %v", errno)
}
return nil
}