hakurei/container/landlock/landlock.go
Clayton Gilmer 7add29b79c
Some checks failed
Test / Create distribution (push) Failing after 52s
Test / Sandbox (push) Successful in 2m22s
Test / Sandbox (race detector) (push) Successful in 4m4s
Test / Planterette (push) Successful in 4m3s
Test / Create distribution (pull_request) Failing after 29s
Test / Sandbox (pull_request) Successful in 40s
Test / Sandbox (race detector) (pull_request) Successful in 41s
Test / Planterette (pull_request) Successful in 41s
Test / Hakurei (push) Failing after 20m46s
Test / Hakurei (race detector) (push) Failing after 22m7s
Test / Flake checks (push) Has been skipped
Test / Hakurei (pull_request) Failing after 34m54s
Test / Hakurei (race detector) (pull_request) Failing after 36m37s
Test / Flake checks (pull_request) Has been skipped
container: optionally isolate host abstract UNIX domain sockets via landlock
2025-08-17 13:27:39 +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
}