| 1 |
//go:build linux |
| 2 |
|
| 3 |
package syscall |
| 4 |
|
| 5 |
import ( |
| 6 |
"syscall" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"golang.org/x/sys/unix" |
| 10 |
) |
| 11 |
|
| 12 |
// LandlockCreateRuleset creates a ruleset file descriptor with the |
| 13 |
// given attributes. |
| 14 |
func LandlockCreateRuleset(attr *RulesetAttr, flags int) (fd int, err error) { |
| 15 |
r0, _, e1 := syscall.Syscall(unix.SYS_LANDLOCK_CREATE_RULESET, uintptr(unsafe.Pointer(attr)), unsafe.Sizeof(*attr), uintptr(flags)) |
| 16 |
fd = int(r0) |
| 17 |
if e1 != 0 { |
| 18 |
err = syscall.Errno(e1) |
| 19 |
} |
| 20 |
return |
| 21 |
} |
| 22 |
|
| 23 |
// LandlockGetABIVersion returns the supported Landlock ABI version (starting at 1). |
| 24 |
func LandlockGetABIVersion() (version int, err error) { |
| 25 |
r0, _, e1 := syscall.Syscall(unix.SYS_LANDLOCK_CREATE_RULESET, 0, 0, unix.LANDLOCK_CREATE_RULESET_VERSION) |
| 26 |
version = int(r0) |
| 27 |
if e1 != 0 { |
| 28 |
err = syscall.Errno(e1) |
| 29 |
} |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
// LandlockGetErrata returns the fixed Landlock errata as an integer. |
| 34 |
func LandlockGetErrata() (errata int, err error) { |
| 35 |
r0, _, e1 := syscall.Syscall(unix.SYS_LANDLOCK_CREATE_RULESET, 0, 0, unix.LANDLOCK_CREATE_RULESET_ERRATA) |
| 36 |
errata = int(r0) |
| 37 |
if errata < 0 { |
| 38 |
err = syscall.Errno(e1) |
| 39 |
} |
| 40 |
return |
| 41 |
} |
| 42 |
|
| 43 |
// Landlock rule types. |
| 44 |
const ( |
| 45 |
RuleTypePathBeneath = unix.LANDLOCK_RULE_PATH_BENEATH |
| 46 |
RuleTypeNetPort = 2 // TODO: Use it from sys/unix when available. |
| 47 |
) |
| 48 |
|
| 49 |
// LandlockAddPathBeneathRule adds a rule of type "path beneath" to |
| 50 |
// the given ruleset fd. attr defines the rule parameters. flags must |
| 51 |
// currently be 0. |
| 52 |
func LandlockAddPathBeneathRule(rulesetFd int, attr *PathBeneathAttr, flags int) error { |
| 53 |
return LandlockAddRule(rulesetFd, RuleTypePathBeneath, unsafe.Pointer(attr), flags) |
| 54 |
} |
| 55 |
|
| 56 |
// LandlockAddNetPortRule adds a rule of type "net port" to the given ruleset FD. |
| 57 |
// attr defines the rule parameters. flags must currently be 0. |
| 58 |
func LandlockAddNetPortRule(rulesetFD int, attr *NetPortAttr, flags int) error { |
| 59 |
return LandlockAddRule(rulesetFD, RuleTypeNetPort, unsafe.Pointer(attr), flags) |
| 60 |
} |
| 61 |
|
| 62 |
// LandlockAddRule is the generic landlock_add_rule syscall. |
| 63 |
func LandlockAddRule(rulesetFd int, ruleType int, ruleAttr unsafe.Pointer, flags int) (err error) { |
| 64 |
_, _, e1 := syscall.Syscall6(unix.SYS_LANDLOCK_ADD_RULE, uintptr(rulesetFd), uintptr(ruleType), uintptr(ruleAttr), uintptr(flags), 0, 0) |
| 65 |
if e1 != 0 { |
| 66 |
err = syscall.Errno(e1) |
| 67 |
} |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
// LandlockRestrictSelf is the landlock_restrict_self(2) system call. |
| 72 |
// |
| 73 |
// If the [FlagRestrictSelfTSync] flag is provided in flags, the |
| 74 |
// Landlock policy is applied to all threads of the current process, |
| 75 |
// and the no_new_privs attribute is also synchronized across all |
| 76 |
// threads (if it was set for the current thread). |
| 77 |
// |
| 78 |
// Without this flag, the policy is only applied to the current OS thread. |
| 79 |
// |
| 80 |
// See https://docs.kernel.org/userspace-api/landlock.html#c.sys_landlock_restrict_self |
| 81 |
func LandlockRestrictSelf(rulesetFd int, flags uint32) (err error) { |
| 82 |
_, _, e1 := syscall.Syscall(unix.SYS_LANDLOCK_RESTRICT_SELF, uintptr(rulesetFd), uintptr(flags), 0) |
| 83 |
if e1 != 0 { |
| 84 |
err = syscall.Errno(e1) |
| 85 |
} |
| 86 |
return |
| 87 |
} |