| 1 |
//go:build linux |
| 2 |
|
| 3 |
package landlock_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/landlock-lsm/go-landlock/landlock" |
| 13 |
"github.com/landlock-lsm/go-landlock/landlock/lltest" |
| 14 |
"golang.org/x/sys/unix" |
| 15 |
) |
| 16 |
|
| 17 |
func MustWriteFile(t testing.TB, path string) { |
| 18 |
t.Helper() |
| 19 |
|
| 20 |
if err := os.WriteFile(path, []byte("somecontent"), 0600); err != nil { |
| 21 |
t.Fatalf("os.WriteFile(%q, ...): %v", path, err) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func MustMkdir(t testing.TB, path string) { |
| 26 |
t.Helper() |
| 27 |
|
| 28 |
if err := os.Mkdir(path, 0700); err != nil { |
| 29 |
t.Fatalf("os.Mkdir(%q): %v", path, err) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func MakeSomeFile(t testing.TB) string { |
| 34 |
t.Helper() |
| 35 |
fpath := filepath.Join(lltest.TempDir(t), "somefile") |
| 36 |
MustWriteFile(t, fpath) |
| 37 |
return fpath |
| 38 |
} |
| 39 |
|
| 40 |
func TestPathDoesNotExist(t *testing.T) { |
| 41 |
lltest.RequireABI(t, 1) |
| 42 |
|
| 43 |
doesNotExistPath := filepath.Join(t.TempDir(), "does_not_exist") |
| 44 |
|
| 45 |
err := landlock.V1.RestrictPaths( |
| 46 |
landlock.RODirs(doesNotExistPath), |
| 47 |
) |
| 48 |
if !errors.Is(err, os.ErrNotExist) { |
| 49 |
t.Errorf("expected 'not exist' error, got: %v", err) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestPathDoesNotExist_Ignored(t *testing.T) { |
| 54 |
lltest.RunInSubprocess(t, func() { |
| 55 |
lltest.RequireABI(t, 1) |
| 56 |
|
| 57 |
doesNotExistPath := filepath.Join(lltest.TempDir(t), "does_not_exist") |
| 58 |
|
| 59 |
err := landlock.V1.RestrictPaths( |
| 60 |
landlock.RODirs(doesNotExistPath).IgnoreIfMissing(), |
| 61 |
) |
| 62 |
if err != nil { |
| 63 |
t.Errorf("expected no error, got: %v", err) |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
|
| 68 |
func TestRestrictingPlainFileWithDirectoryFlags(t *testing.T) { |
| 69 |
lltest.RequireABI(t, 1) |
| 70 |
|
| 71 |
fpath := MakeSomeFile(t) |
| 72 |
|
| 73 |
err := landlock.V1.RestrictPaths( |
| 74 |
landlock.RODirs(fpath), |
| 75 |
) |
| 76 |
if !errors.Is(err, unix.EINVAL) { |
| 77 |
t.Errorf("expected 'invalid argument' error, got: %v", err) |
| 78 |
} |
| 79 |
if isGoLandlockBug(err) { |
| 80 |
t.Errorf("should not be marked as a go-landlock bug, but was: %v", err) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func isGoLandlockBug(err error) bool { |
| 85 |
return strings.Contains(err.Error(), "BUG(go-landlock)") |
| 86 |
} |
| 87 |
|
| 88 |
func TestEmptyAccessRights(t *testing.T) { |
| 89 |
lltest.RequireABI(t, 1) |
| 90 |
|
| 91 |
lltest.RunInSubprocess(t, func() { |
| 92 |
fpath := MakeSomeFile(t) |
| 93 |
|
| 94 |
err := landlock.V1.RestrictPaths( |
| 95 |
landlock.PathAccess(0, fpath), |
| 96 |
) |
| 97 |
if err != nil { |
| 98 |
t.Errorf("expected success, got: %v", err) |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
func TestOverlyBroadFSRule(t *testing.T) { |
| 104 |
lltest.RequireABI(t, 1) |
| 105 |
|
| 106 |
handled := landlock.AccessFSSet(0b011) |
| 107 |
excempt := landlock.AccessFSSet(0b111) // superset of handled! |
| 108 |
err := landlock.MustConfig(handled).RestrictPaths( |
| 109 |
landlock.PathAccess(excempt, "/tmp"), |
| 110 |
) |
| 111 |
if !errors.Is(err, unix.EINVAL) { |
| 112 |
t.Errorf("expected 'invalid argument' error, got: %v", err) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestReferNotPermittedInStrictV1(t *testing.T) { |
| 117 |
lltest.RequireABI(t, 1) |
| 118 |
|
| 119 |
// 'refer' is incompatible with Landlock ABI V1. |
| 120 |
// Users should use Landlock V2 instead or construct a custom |
| 121 |
// config that handles the 'refer' access right. |
| 122 |
// You can technically also just enable V1 best-effort mode, |
| 123 |
// but that combination always falls back to "no enforcement". |
| 124 |
for _, rule := range []landlock.Rule{ |
| 125 |
landlock.RWDirs("/etc").WithRefer(), |
| 126 |
landlock.PathAccess(0, "/etc").WithRefer(), |
| 127 |
} { |
| 128 |
err := landlock.V1.RestrictPaths(rule) |
| 129 |
if !errors.Is(err, unix.EINVAL) { |
| 130 |
t.Errorf("expected 'invalid argument' error, got: %v", err) |
| 131 |
} |
| 132 |
if !strings.Contains(err.Error(), "incompatible rule") { |
| 133 |
t.Errorf("expected a 'incompatible rule' error, got: %v", err) |
| 134 |
} |
| 135 |
} |
| 136 |
} |