| 1 |
//go:build linux |
| 2 |
|
| 3 |
package landlock_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/landlock-lsm/go-landlock/landlock" |
| 9 |
"github.com/landlock-lsm/go-landlock/landlock/lltest" |
| 10 |
ll "github.com/landlock-lsm/go-landlock/landlock/syscall" |
| 11 |
) |
| 12 |
|
| 13 |
func TestCustomConfig(t *testing.T) { |
| 14 |
lltest.RunInSubprocess(t, func() { |
| 15 |
lltest.RequireABI(t, 1) |
| 16 |
|
| 17 |
pathRO := MakeSomeFile(t) |
| 18 |
pathNoAccess := MakeSomeFile(t) |
| 19 |
|
| 20 |
readFile := landlock.AccessFSSet(ll.AccessFSReadFile) |
| 21 |
if err := landlock.MustConfig(readFile).RestrictPaths( |
| 22 |
landlock.PathAccess(readFile, pathRO), |
| 23 |
); err != nil { |
| 24 |
t.Fatalf("Could not restrict paths: %v", err) |
| 25 |
} |
| 26 |
|
| 27 |
if err := openForRead(pathRO); err != nil { |
| 28 |
t.Errorf("openForRead(%q): %v", pathRO, err) |
| 29 |
} |
| 30 |
if err := openForRead(pathNoAccess); err == nil { |
| 31 |
t.Errorf("openForRead(%q) successful, want error", pathNoAccess) |
| 32 |
} |
| 33 |
}) |
| 34 |
} |
| 35 |
|
| 36 |
func TestRestrictWithLoggingFlags(t *testing.T) { |
| 37 |
lltest.RunInSubprocess(t, func() { |
| 38 |
cfg := landlock.V9. |
| 39 |
DisableLoggingForOriginatingProcess(). |
| 40 |
EnableLoggingForSubprocesses(). |
| 41 |
DisableLoggingForSubdomains(). |
| 42 |
BestEffort() |
| 43 |
|
| 44 |
// Should always work, because "best effort" is set. |
| 45 |
if err := cfg.Restrict(); err != nil { |
| 46 |
t.Errorf("%v.Restrict(): unexpected error: %v", cfg, err) |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
func TestAbsurdDowngradeCase(t *testing.T) { |
| 52 |
// This is a regression test for a bug where: |
| 53 |
// |
| 54 |
// - we run on a kernel that supports Landlock but does not |
| 55 |
// support the truncate access right |
| 56 |
// - Go-Landlock will "downgrade" the filesystem rule to "no access rights", |
| 57 |
// because the requested access right "truncate" is not supported. |
| 58 |
// - It should not try to add that rule (but it used to). |
| 59 |
if v, err := ll.LandlockGetABIVersion(); err != nil || v <= 0 || v > 2 { |
| 60 |
t.Skipf("Requires Landlock version 1 or 2, got V%v (err=%v)", v, err) |
| 61 |
} |
| 62 |
|
| 63 |
lltest.RunInSubprocess(t, func() { |
| 64 |
cfg := landlock.MustConfig( |
| 65 |
landlock.AccessFSSet(ll.AccessFSTruncate | ll.AccessFSMakeDir), |
| 66 |
).BestEffort() |
| 67 |
|
| 68 |
path := MakeSomeFile(t) |
| 69 |
err := cfg.Restrict(landlock.PathAccess(ll.AccessFSTruncate, path)) |
| 70 |
if err != nil { |
| 71 |
t.Errorf("Landlock restriction error: %v", err) |
| 72 |
} |
| 73 |
}) |
| 74 |
|
| 75 |
} |