| 1 |
//go:build linux |
| 2 |
|
| 3 |
package landlock |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"runtime" |
| 9 |
"syscall" |
| 10 |
|
| 11 |
ll "github.com/landlock-lsm/go-landlock/landlock/syscall" |
| 12 |
"golang.org/x/sys/unix" |
| 13 |
) |
| 14 |
|
| 15 |
// downgrade calculates the actual ruleset to be enforced given the |
| 16 |
// current kernel's Landlock ABI level. |
| 17 |
// |
| 18 |
// It establishes that rule.compatibleWithConfig(c) and c.compatibleWithABI(abi). |
| 19 |
func downgrade(c Config, rules []Rule, abi abiInfo) (Config, []Rule) { |
| 20 |
c = c.restrictTo(abi) |
| 21 |
|
| 22 |
resRules := make([]Rule, 0, len(rules)) |
| 23 |
for _, rule := range rules { |
| 24 |
rule, ok := rule.downgrade(c) |
| 25 |
if !ok { |
| 26 |
return v0, nil // Use "ABI V0" (do nothing) |
| 27 |
} |
| 28 |
resRules = append(resRules, rule) |
| 29 |
} |
| 30 |
return c, resRules |
| 31 |
} |
| 32 |
|
| 33 |
// restrict is the actual implementation which sets up Landlock. |
| 34 |
func restrict(c Config, rules ...Rule) error { |
| 35 |
abi := getSupportedABIVersion() |
| 36 |
useTsync := abi.version >= 8 |
| 37 |
if !useTsync { |
| 38 |
// Work around https://github.com/landlock-lsm/go-landlock/issues/39 |
| 39 |
rules = maybeWorkaroundBug39(c, rules) |
| 40 |
} |
| 41 |
|
| 42 |
// Check validity of rules early. |
| 43 |
for _, rule := range rules { |
| 44 |
if !rule.compatibleWithConfig(c) { |
| 45 |
return fmt.Errorf("incompatible rule %v: %w", rule, unix.EINVAL) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if c.bestEffort { |
| 50 |
c, rules = downgrade(c, rules, abi) |
| 51 |
} |
| 52 |
if !c.compatibleWithABI(abi) { |
| 53 |
return fmt.Errorf("missing kernel Landlock support. Got Landlock ABI v%v, wanted %v", abi.version, c) |
| 54 |
} |
| 55 |
|
| 56 |
// TODO: This might be incorrect - the "refer" permission is |
| 57 |
// always implicit, even in Landlock V1. So enabling Landlock |
| 58 |
// on a Landlock V1 kernel without any handled access rights |
| 59 |
// will still forbid linking files between directories. |
| 60 |
if c.handledAccessFS.isEmpty() && c.handledAccessNet.isEmpty() && c.scoped.isEmpty() { |
| 61 |
return nil // Success: Nothing to restrict. |
| 62 |
} |
| 63 |
|
| 64 |
rulesetAttr := ll.RulesetAttr{ |
| 65 |
HandledAccessFS: uint64(c.handledAccessFS), |
| 66 |
HandledAccessNet: uint64(c.handledAccessNet), |
| 67 |
Scoped: uint64(c.scoped), |
| 68 |
} |
| 69 |
fd, err := ll.LandlockCreateRuleset(&rulesetAttr, 0) |
| 70 |
if err != nil { |
| 71 |
if errors.Is(err, syscall.ENOSYS) || errors.Is(err, syscall.EOPNOTSUPP) { |
| 72 |
err = errors.New("landlock is not supported by kernel or not enabled at boot time") |
| 73 |
} |
| 74 |
if errors.Is(err, syscall.EINVAL) { |
| 75 |
err = errors.New("unknown flags, unknown access, or too small size") |
| 76 |
} |
| 77 |
// Bug, because these should have been caught up front with the ABI version check. |
| 78 |
return bug(fmt.Errorf("landlock_create_ruleset: %w", err)) |
| 79 |
} |
| 80 |
defer syscall.Close(fd) |
| 81 |
|
| 82 |
for _, rule := range rules { |
| 83 |
if err := rule.addToRuleset(fd, c); err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if !useTsync { |
| 89 |
if err := ll.AllThreadsPrctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { |
| 90 |
// This prctl invocation should always work. |
| 91 |
return bug(fmt.Errorf("prctl(PR_SET_NO_NEW_PRIVS): %v", err)) |
| 92 |
} |
| 93 |
|
| 94 |
if err := ll.AllThreadsLandlockRestrictSelf(fd, uint32(c.flags)); err != nil { |
| 95 |
if errors.Is(err, syscall.E2BIG) { |
| 96 |
// Other errors than E2BIG should never happen. |
| 97 |
return fmt.Errorf("the maximum number of stacked rulesets is reached for the current thread: %w", err) |
| 98 |
} |
| 99 |
return bug(fmt.Errorf("landlock_restrict_self: %w", err)) |
| 100 |
} |
| 101 |
return nil |
| 102 |
} |
| 103 |
|
| 104 |
runtime.LockOSThread() |
| 105 |
defer runtime.UnlockOSThread() |
| 106 |
|
| 107 |
if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { |
| 108 |
return bug(fmt.Errorf("prctl(PR_SET_NO_NEW_PRIVS): %v", err)) |
| 109 |
} |
| 110 |
if err := ll.LandlockRestrictSelf(fd, uint32(c.flags)|ll.FlagRestrictSelfTSync); err != nil { |
| 111 |
if errors.Is(err, syscall.E2BIG) { |
| 112 |
return fmt.Errorf("the maximum number of stacked rulesets is reached for the current thread: %w", err) |
| 113 |
} |
| 114 |
return bug(fmt.Errorf("landlock_restrict_self: %w", err)) |
| 115 |
} |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
// Denotes an error that should not have happened. |
| 120 |
// If such an error occurs anyway, please try upgrading the library |
| 121 |
// and file a bug to github.com/landlock-lsm/go-landlock if the issue persists. |
| 122 |
func bug(err error) error { |
| 123 |
return fmt.Errorf("BUG(go-landlock): This should not have happened: %w", err) |
| 124 |
} |