hakurei/container/landlock/landlock.go
Clayton Gilmer c9eeafbbf0
Some checks failed
Test / Create distribution (pull_request) Failing after 32s
Test / Sandbox (pull_request) Failing after 51s
Test / Sandbox (race detector) (pull_request) Failing after 54s
Test / Hpkg (pull_request) Failing after 56s
Test / Hakurei (pull_request) Failing after 1m9s
Test / Hakurei (race detector) (push) Failing after 1m0s
Test / Hakurei (push) Failing after 1m11s
Test / Hakurei (race detector) (pull_request) Failing after 1m18s
Test / Flake checks (pull_request) Has been skipped
Test / Create distribution (push) Failing after 30s
Test / Sandbox (push) Failing after 49s
Test / Hpkg (push) Failing after 48s
Test / Sandbox (race detector) (push) Failing after 51s
Test / Flake checks (push) Has been skipped
container: optionally isolate host abstract UNIX domain sockets via landlock
2025-08-18 11:50:05 +09:00

56 lines
1.4 KiB
Go

package landlock
/*
#include <linux/landlock.h>
#include <sys/syscall.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
SYS_LANDLOCK_RESTRICT_SELF = C.SYS_landlock_restrict_self
)
type LandlockRulesetAttr = C.struct_landlock_ruleset_attr
// ScopeAbstract calls landlock_restrict_self and must be called from a goroutine wired to an m
// with the process starting from the same goroutine.
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))
r, _, err := syscall.Syscall(SYS_LANDLOCK_RESTRICT_SELF, fd, 0, 0)
if r != 0 {
return fmt.Errorf("could not restrict self via landlock: errno %v", err)
}
return nil
}