| 1 |
//go:build linux |
| 2 |
|
| 3 |
package landlock_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"net" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"syscall" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/landlock-lsm/go-landlock/landlock" |
| 13 |
"github.com/landlock-lsm/go-landlock/landlock/lltest" |
| 14 |
) |
| 15 |
|
| 16 |
// TestResolveUnix verifies that the "resolve unix" access right |
| 17 |
// (Landlock ABI v9) restricts connect(2) on pathname UNIX domain |
| 18 |
// sockets created outside the Landlock domain, and permits it on |
| 19 |
// paths which are explicitly allowed for it. |
| 20 |
func TestResolveUnix(t *testing.T) { |
| 21 |
// The socket is created in the parent process before the test |
| 22 |
// subprocess starts, so that from the subprocess's point of |
| 23 |
// view, it is a UNIX server socket that was created outside of |
| 24 |
// its Landlock domain. The path is passed to the subprocess |
| 25 |
// via an environment variable. |
| 26 |
const sockEnv = "LANDLOCK_TEST_RESOLVE_UNIX_SOCK" |
| 27 |
sockPath := os.Getenv(sockEnv) |
| 28 |
if !lltest.IsRunningInSubprocess() { |
| 29 |
sockPath = filepath.Join(t.TempDir(), "sock") |
| 30 |
t.Setenv(sockEnv, sockPath) |
| 31 |
ls, err := net.Listen("unix", sockPath) |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("net.Listen(unix:%q): %v", sockPath, err) |
| 34 |
} |
| 35 |
defer ls.Close() |
| 36 |
} |
| 37 |
|
| 38 |
for _, tt := range []struct { |
| 39 |
Name string |
| 40 |
Rule landlock.Rule |
| 41 |
WantErr error |
| 42 |
}{ |
| 43 |
{ |
| 44 |
Name: "WithoutResolveUnix", |
| 45 |
Rule: landlock.RWFiles(sockPath), |
| 46 |
WantErr: syscall.EACCES, |
| 47 |
}, |
| 48 |
{ |
| 49 |
Name: "WithResolveUnix", |
| 50 |
Rule: landlock.RWFiles(sockPath).WithResolveUnix(), |
| 51 |
WantErr: nil, |
| 52 |
}, |
| 53 |
} { |
| 54 |
t.Run(tt.Name, func(t *testing.T) { |
| 55 |
lltest.RunInSubprocess(t, func() { |
| 56 |
lltest.RequireABI(t, 9) |
| 57 |
|
| 58 |
if err := landlock.V9.RestrictPaths(tt.Rule); err != nil { |
| 59 |
t.Fatalf("Enabling Landlock: %v", err) |
| 60 |
} |
| 61 |
|
| 62 |
cs, err := net.Dial("unix", sockPath) |
| 63 |
if !errEqual(err, tt.WantErr) { |
| 64 |
t.Errorf("Dial(unix:%q): got err %q, want %q", sockPath, err, tt.WantErr) |
| 65 |
} |
| 66 |
if err == nil { |
| 67 |
cs.Close() |
| 68 |
} |
| 69 |
}) |
| 70 |
}) |
| 71 |
} |
| 72 |
} |