hakurei/container/landlock/landlock.go
Clayton Gilmer 2cf3077c07
Some checks failed
Test / Create distribution (push) Failing after 32s
Test / Create distribution (pull_request) Failing after 35s
Test / Hakurei (push) Failing after 50s
Test / Hakurei (pull_request) Failing after 50s
Test / Hakurei (race detector) (push) Failing after 54s
Test / Sandbox (pull_request) Failing after 54s
Test / Hakurei (race detector) (pull_request) Failing after 57s
Test / Sandbox (push) Failing after 1m8s
Test / Sandbox (race detector) (pull_request) Failing after 1m6s
Test / Hpkg (pull_request) Failing after 1m8s
Test / Flake checks (pull_request) Has been skipped
Test / Sandbox (race detector) (push) Failing after 1m22s
Test / Hpkg (push) Failing after 1m24s
Test / Flake checks (push) Has been skipped
container: optionally isolate host abstract UNIX domain sockets via landlock
2025-08-18 11:57:21 +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
}