Skip to content

Commit 2a794ee

Browse files
committed
selftests/landlock: Add layout1.umount_sandboxer tests
Check that a domain is not tied to the executable file that created it. For instance, that could happen if a Landlock domain took a reference to a struct path. Move global path names to common.h and replace copy_binary() with a more generic copy_file() helper. Test coverage for security/landlock is 92.7% of 1133 lines according to gcc/gcov-14. Cc: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20250108154338.1129069-23-mic@digikod.net [mic: Update date and add test coverage] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 5147779 commit 2a794ee

File tree

5 files changed

+213
-10
lines changed

5 files changed

+213
-10
lines changed

tools/testing/selftests/landlock/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ src_test := $(wildcard *_test.c)
1010

1111
TEST_GEN_PROGS := $(src_test:.c=)
1212

13-
TEST_GEN_PROGS_EXTENDED := true
13+
TEST_GEN_PROGS_EXTENDED := true sandbox-and-launch wait-pipe
1414

1515
# Short targets:
1616
$(TEST_GEN_PROGS): LDLIBS += -lcap -lpthread

tools/testing/selftests/landlock/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
/* TEST_F_FORK() should not be used for new tests. */
2929
#define TEST_F_FORK(fixture_name, test_name) TEST_F(fixture_name, test_name)
3030

31+
static const char bin_sandbox_and_launch[] = "./sandbox-and-launch";
32+
static const char bin_wait_pipe[] = "./wait-pipe";
33+
3134
static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
3235
{
3336
cap_t cap_p;

tools/testing/selftests/landlock/fs_test.c

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int open_tree(int dfd, const char *filename, unsigned int flags)
5959
#define RENAME_EXCHANGE (1 << 1)
6060
#endif
6161

62-
#define BINARY_PATH "./true"
62+
static const char bin_true[] = "./true";
6363

6464
/* Paths (sibling number and depth) */
6565
static const char dir_s1d1[] = TMP_DIR "/s1d1";
@@ -1965,8 +1965,8 @@ TEST_F_FORK(layout1, relative_chroot_chdir)
19651965
test_relative_path(_metadata, REL_CHROOT_CHDIR);
19661966
}
19671967

1968-
static void copy_binary(struct __test_metadata *const _metadata,
1969-
const char *const dst_path)
1968+
static void copy_file(struct __test_metadata *const _metadata,
1969+
const char *const src_path, const char *const dst_path)
19701970
{
19711971
int dst_fd, src_fd;
19721972
struct stat statbuf;
@@ -1976,11 +1976,10 @@ static void copy_binary(struct __test_metadata *const _metadata,
19761976
{
19771977
TH_LOG("Failed to open \"%s\": %s", dst_path, strerror(errno));
19781978
}
1979-
src_fd = open(BINARY_PATH, O_RDONLY | O_CLOEXEC);
1979+
src_fd = open(src_path, O_RDONLY | O_CLOEXEC);
19801980
ASSERT_LE(0, src_fd)
19811981
{
1982-
TH_LOG("Failed to open \"" BINARY_PATH "\": %s",
1983-
strerror(errno));
1982+
TH_LOG("Failed to open \"%s\": %s", src_path, strerror(errno));
19841983
}
19851984
ASSERT_EQ(0, fstat(src_fd, &statbuf));
19861985
ASSERT_EQ(statbuf.st_size,
@@ -2028,9 +2027,9 @@ TEST_F_FORK(layout1, execute)
20282027
create_ruleset(_metadata, rules[0].access, rules);
20292028

20302029
ASSERT_LE(0, ruleset_fd);
2031-
copy_binary(_metadata, file1_s1d1);
2032-
copy_binary(_metadata, file1_s1d2);
2033-
copy_binary(_metadata, file1_s1d3);
2030+
copy_file(_metadata, bin_true, file1_s1d1);
2031+
copy_file(_metadata, bin_true, file1_s1d2);
2032+
copy_file(_metadata, bin_true, file1_s1d3);
20342033

20352034
enforce_ruleset(_metadata, ruleset_fd);
20362035
ASSERT_EQ(0, close(ruleset_fd));
@@ -2048,6 +2047,83 @@ TEST_F_FORK(layout1, execute)
20482047
test_execute(_metadata, 0, file1_s1d3);
20492048
}
20502049

2050+
TEST_F_FORK(layout1, umount_sandboxer)
2051+
{
2052+
int pipe_child[2], pipe_parent[2];
2053+
char buf_parent;
2054+
pid_t child;
2055+
int status;
2056+
2057+
copy_file(_metadata, bin_sandbox_and_launch, file1_s3d3);
2058+
ASSERT_EQ(0, pipe2(pipe_child, 0));
2059+
ASSERT_EQ(0, pipe2(pipe_parent, 0));
2060+
2061+
child = fork();
2062+
ASSERT_LE(0, child);
2063+
if (child == 0) {
2064+
char pipe_child_str[12], pipe_parent_str[12];
2065+
char *const argv[] = { (char *)file1_s3d3,
2066+
(char *)bin_wait_pipe, pipe_child_str,
2067+
pipe_parent_str, NULL };
2068+
2069+
/* Passes the pipe FDs to the executed binary and its child. */
2070+
EXPECT_EQ(0, close(pipe_child[0]));
2071+
EXPECT_EQ(0, close(pipe_parent[1]));
2072+
snprintf(pipe_child_str, sizeof(pipe_child_str), "%d",
2073+
pipe_child[1]);
2074+
snprintf(pipe_parent_str, sizeof(pipe_parent_str), "%d",
2075+
pipe_parent[0]);
2076+
2077+
/*
2078+
* We need bin_sandbox_and_launch (copied inside the mount as
2079+
* file1_s3d3) to execute bin_wait_pipe (outside the mount) to
2080+
* make sure the mount point will not be EBUSY because of
2081+
* file1_s3d3 being in use. This avoids a potential race
2082+
* condition between the following read() and umount() calls.
2083+
*/
2084+
ASSERT_EQ(0, execve(argv[0], argv, NULL))
2085+
{
2086+
TH_LOG("Failed to execute \"%s\": %s", argv[0],
2087+
strerror(errno));
2088+
};
2089+
_exit(1);
2090+
return;
2091+
}
2092+
2093+
EXPECT_EQ(0, close(pipe_child[1]));
2094+
EXPECT_EQ(0, close(pipe_parent[0]));
2095+
2096+
/* Waits for the child to sandbox itself. */
2097+
EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1));
2098+
2099+
/* Tests that the sandboxer is tied to its mount point. */
2100+
set_cap(_metadata, CAP_SYS_ADMIN);
2101+
EXPECT_EQ(-1, umount(dir_s3d2));
2102+
EXPECT_EQ(EBUSY, errno);
2103+
clear_cap(_metadata, CAP_SYS_ADMIN);
2104+
2105+
/* Signals the child to launch a grandchild. */
2106+
EXPECT_EQ(1, write(pipe_parent[1], ".", 1));
2107+
2108+
/* Waits for the grandchild. */
2109+
EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1));
2110+
2111+
/* Tests that the domain's sandboxer is not tied to its mount point. */
2112+
set_cap(_metadata, CAP_SYS_ADMIN);
2113+
EXPECT_EQ(0, umount(dir_s3d2))
2114+
{
2115+
TH_LOG("Failed to umount \"%s\": %s", dir_s3d2,
2116+
strerror(errno));
2117+
};
2118+
clear_cap(_metadata, CAP_SYS_ADMIN);
2119+
2120+
/* Signals the grandchild to terminate. */
2121+
EXPECT_EQ(1, write(pipe_parent[1], ".", 1));
2122+
ASSERT_EQ(child, waitpid(child, &status, 0));
2123+
ASSERT_EQ(1, WIFEXITED(status));
2124+
ASSERT_EQ(0, WEXITSTATUS(status));
2125+
}
2126+
20512127
TEST_F_FORK(layout1, link)
20522128
{
20532129
const struct rule layer1[] = {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Sandbox itself and execute another program (in a different mount point).
4+
*
5+
* Used by layout1.umount_sandboxer from fs_test.c
6+
*
7+
* Copyright © 2024-2025 Microsoft Corporation
8+
*/
9+
10+
#define _GNU_SOURCE
11+
#include <errno.h>
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
#include <sys/prctl.h>
15+
#include <unistd.h>
16+
17+
#include "wrappers.h"
18+
19+
int main(int argc, char *argv[])
20+
{
21+
struct landlock_ruleset_attr ruleset_attr = {
22+
.scoped = LANDLOCK_SCOPE_SIGNAL,
23+
};
24+
int pipe_child, pipe_parent, ruleset_fd;
25+
char buf;
26+
27+
/*
28+
* The first argument must be the file descriptor number of a pipe.
29+
* The second argument must be the program to execute.
30+
*/
31+
if (argc != 4) {
32+
fprintf(stderr, "Wrong number of arguments (not three)\n");
33+
return 1;
34+
}
35+
36+
pipe_child = atoi(argv[2]);
37+
pipe_parent = atoi(argv[3]);
38+
39+
ruleset_fd =
40+
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
41+
if (ruleset_fd < 0) {
42+
perror("Failed to create ruleset");
43+
return 1;
44+
}
45+
46+
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
47+
perror("Failed to call prctl()");
48+
return 1;
49+
}
50+
51+
if (landlock_restrict_self(ruleset_fd, 0)) {
52+
perror("Failed to restrict self");
53+
return 1;
54+
}
55+
56+
if (close(ruleset_fd)) {
57+
perror("Failed to close ruleset");
58+
return 1;
59+
}
60+
61+
/* Signals that we are sandboxed. */
62+
errno = 0;
63+
if (write(pipe_child, ".", 1) != 1) {
64+
perror("Failed to write to the second argument");
65+
return 1;
66+
}
67+
68+
/* Waits for the parent to try to umount. */
69+
if (read(pipe_parent, &buf, 1) != 1) {
70+
perror("Failed to write to the third argument");
71+
return 1;
72+
}
73+
74+
/* Shifts arguments. */
75+
argv[0] = argv[1];
76+
argv[1] = argv[2];
77+
argv[2] = argv[3];
78+
argv[3] = NULL;
79+
execve(argv[0], argv, NULL);
80+
perror("Failed to execute the provided binary");
81+
return 1;
82+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Write in a pipe and wait.
4+
*
5+
* Used by layout1.umount_sandboxer from fs_test.c
6+
*
7+
* Copyright © 2024-2025 Microsoft Corporation
8+
*/
9+
10+
#define _GNU_SOURCE
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <unistd.h>
14+
15+
int main(int argc, char *argv[])
16+
{
17+
int pipe_child, pipe_parent;
18+
char buf;
19+
20+
/* The first argument must be the file descriptor number of a pipe. */
21+
if (argc != 3) {
22+
fprintf(stderr, "Wrong number of arguments (not two)\n");
23+
return 1;
24+
}
25+
26+
pipe_child = atoi(argv[1]);
27+
pipe_parent = atoi(argv[2]);
28+
29+
/* Signals that we are waiting. */
30+
if (write(pipe_child, ".", 1) != 1) {
31+
perror("Failed to write to first argument");
32+
return 1;
33+
}
34+
35+
/* Waits for the parent do its test. */
36+
if (read(pipe_parent, &buf, 1) != 1) {
37+
perror("Failed to write to the second argument");
38+
return 1;
39+
}
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)