Debian Codegraph @

landlock/restrict_scoped_test.go

raw
1 //go:build linux
2
3 package landlock_test
4
5 import (
6 "net"
7 "os"
8 "syscall"
9 "testing"
10
11 "github.com/landlock-lsm/go-landlock/landlock"
12 "github.com/landlock-lsm/go-landlock/landlock/lltest"
13 ll "github.com/landlock-lsm/go-landlock/landlock/syscall"
14 )
15
16 func TestRestrictScoped(t *testing.T) {
17 const name = "@abstract/go-landlock/test"
18
19 // Bring up an abstract UNIX Domain Socket service in the
20 // parent process, which the subprocesses can dial.
21 if !lltest.IsRunningInSubprocess() {
22 ls, err := net.Listen("unix", name)
23 if err != nil {
24 t.Fatalf("net.Listen(unix:%q): %v", name, err)
25 }
26 defer ls.Close()
27 }
28
29 for _, tt := range []struct {
30 Name string
31 EnableLandlock func() error
32 RequiredABI int
33 WantDialErr error
34 WantKillErr error
35 }{
36 {
37 Name: "Unrestricted",
38 RequiredABI: 0,
39 EnableLandlock: func() error { return nil },
40 },
41 {
42 Name: "RestrictAbstractUnixSockets",
43 RequiredABI: 6,
44 EnableLandlock: func() error {
45 return landlock.MustConfig(
46 landlock.ScopedSet(ll.ScopeAbstractUnixSocket),
47 ).Restrict()
48 },
49 WantDialErr: syscall.EPERM,
50 },
51 {
52 Name: "RestrictSignal",
53 RequiredABI: 6,
54 EnableLandlock: func() error {
55 return landlock.MustConfig(
56 landlock.ScopedSet(ll.ScopeSignal),
57 ).Restrict()
58 },
59 WantKillErr: syscall.EPERM,
60 },
61 {
62 Name: "RestrictAll",
63 RequiredABI: 6,
64 EnableLandlock: func() error {
65 return landlock.V6.RestrictScoped()
66 },
67 WantDialErr: syscall.EPERM,
68 WantKillErr: syscall.EPERM,
69 },
70 } {
71 t.Run(tt.Name, func(t *testing.T) {
72 lltest.RunInSubprocess(t, func() {
73 lltest.RequireABI(t, tt.RequiredABI)
74
75 err := tt.EnableLandlock()
76 if err != nil {
77 t.Fatalf("Enabling Landlock: %v", err)
78 }
79
80 cs, err := net.Dial("unix", name)
81 if want := tt.WantDialErr; !errEqual(err, want) {
82 t.Errorf("Dial(unix:%q): err=%q, want %q", name, err, want)
83 }
84 if err == nil {
85 defer cs.Close()
86 }
87
88 killErr := syscall.Kill(os.Getppid(), syscall.SIGUSR1)
89 if want := tt.WantKillErr; killErr != want {
90 t.Errorf("Kill(ppid, USR1): err=%q, want %q", killErr, want)
91 }
92 })
93 })
94 }
95 }