Debian Codegraph @

landlock/restrict_fs_ioctl_test.go

raw
1 //go:build linux
2
3 package landlock_test
4
5 import (
6 "os"
7 "syscall"
8 "testing"
9
10 "github.com/landlock-lsm/go-landlock/landlock"
11 "github.com/landlock-lsm/go-landlock/landlock/lltest"
12 "golang.org/x/sys/unix"
13 )
14
15 func TestIoctlDev(t *testing.T) {
16 const (
17 path = "/dev/zero"
18 FIONREAD = 0x541b
19 )
20 for _, tt := range []struct {
21 Name string
22 Rule landlock.Rule
23 WantErr error
24 }{
25 {
26 Name: "WithoutIoctlDev",
27 Rule: landlock.RWFiles(path),
28 WantErr: syscall.EACCES,
29 },
30 {
31 Name: "WithIoctlDev",
32 Rule: landlock.RWFiles(path).WithIoctlDev(),
33 // ENOTTY means that the IOCTL was dispatched
34 // to device. (Would be nicer to find an
35 // IOCTL that returns success here, but the
36 // available devices on qemu are limited.)
37 WantErr: syscall.ENOTTY,
38 },
39 } {
40 t.Run(tt.Name, func(t *testing.T) {
41 lltest.RunInSubprocess(t, func() {
42 lltest.RequireABI(t, 5)
43
44 err := landlock.V5.BestEffort().RestrictPaths(tt.Rule)
45 if err != nil {
46 t.Fatalf("Enabling Landlock: %v", err)
47 }
48
49 f, err := os.Open(path)
50 if err != nil {
51 t.Fatalf("os.Open(%q): %v", path, err)
52 }
53 defer func() { f.Close() }()
54
55 _, err = unix.IoctlGetInt(int(f.Fd()), FIONREAD)
56 if !errEqual(err, tt.WantErr) {
57 t.Errorf("ioctl(%v, FIONREAD): got err «%v», want «%v»", f, err, tt.WantErr)
58 }
59 })
60 })
61 }
62 }