Skip to content

Commit 12264f7

Browse files
committed
selftests/landlock: Add test to check partial access in a mount tree
Add layout1.refer_part_mount_tree_is_allowed to test the masked logical issue regarding collect_domain_accesses() calls followed by the is_access_to_paths_allowed() check in current_check_refer_path(). See previous commit. This test should work without the previous fix as well, but it enables us to make sure future changes will not have impact regarding this behavior. Cc: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20250108154338.1129069-13-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 058518c commit 12264f7

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ static const char file1_s3d1[] = TMP_DIR "/s3d1/f1";
8585
/* dir_s3d2 is a mount point. */
8686
static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
8787
static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
88+
static const char file1_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3/f1";
89+
static const char dir_s3d4[] = TMP_DIR "/s3d1/s3d2/s3d4";
90+
static const char file1_s3d4[] = TMP_DIR "/s3d1/s3d2/s3d4/f1";
8891

8992
/*
9093
* layout1 hierarchy:
@@ -108,8 +111,11 @@ static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
108111
* │   └── f2
109112
* └── s3d1
110113
*    ├── f1
111-
* └── s3d2
112-
* └── s3d3
114+
* └── s3d2 [mount point]
115+
*    ├── s3d3
116+
*    │ └── f1
117+
*    └── s3d4
118+
*    └── f1
113119
*/
114120

115121
static bool fgrep(FILE *const inf, const char *const str)
@@ -358,7 +364,8 @@ static void create_layout1(struct __test_metadata *const _metadata)
358364
ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2));
359365
clear_cap(_metadata, CAP_SYS_ADMIN);
360366

361-
ASSERT_EQ(0, mkdir(dir_s3d3, 0700));
367+
create_file(_metadata, file1_s3d3);
368+
create_file(_metadata, file1_s3d4);
362369
}
363370

364371
static void remove_layout1(struct __test_metadata *const _metadata)
@@ -378,7 +385,8 @@ static void remove_layout1(struct __test_metadata *const _metadata)
378385
EXPECT_EQ(0, remove_path(dir_s2d2));
379386

380387
EXPECT_EQ(0, remove_path(file1_s3d1));
381-
EXPECT_EQ(0, remove_path(dir_s3d3));
388+
EXPECT_EQ(0, remove_path(file1_s3d3));
389+
EXPECT_EQ(0, remove_path(file1_s3d4));
382390
set_cap(_metadata, CAP_SYS_ADMIN);
383391
umount(dir_s3d2);
384392
clear_cap(_metadata, CAP_SYS_ADMIN);
@@ -2444,6 +2452,44 @@ TEST_F_FORK(layout1, refer_mount_root_deny)
24442452
EXPECT_EQ(0, close(root_fd));
24452453
}
24462454

2455+
TEST_F_FORK(layout1, refer_part_mount_tree_is_allowed)
2456+
{
2457+
const struct rule layer1[] = {
2458+
{
2459+
/* Parent mount point. */
2460+
.path = dir_s3d1,
2461+
.access = LANDLOCK_ACCESS_FS_REFER |
2462+
LANDLOCK_ACCESS_FS_MAKE_REG,
2463+
},
2464+
{
2465+
/*
2466+
* Removing the source file is allowed because its
2467+
* access rights are already a superset of the
2468+
* destination.
2469+
*/
2470+
.path = dir_s3d4,
2471+
.access = LANDLOCK_ACCESS_FS_REFER |
2472+
LANDLOCK_ACCESS_FS_MAKE_REG |
2473+
LANDLOCK_ACCESS_FS_REMOVE_FILE,
2474+
},
2475+
{},
2476+
};
2477+
int ruleset_fd;
2478+
2479+
ASSERT_EQ(0, unlink(file1_s3d3));
2480+
ruleset_fd = create_ruleset(_metadata,
2481+
LANDLOCK_ACCESS_FS_REFER |
2482+
LANDLOCK_ACCESS_FS_MAKE_REG |
2483+
LANDLOCK_ACCESS_FS_REMOVE_FILE,
2484+
layer1);
2485+
2486+
ASSERT_LE(0, ruleset_fd);
2487+
enforce_ruleset(_metadata, ruleset_fd);
2488+
ASSERT_EQ(0, close(ruleset_fd));
2489+
2490+
ASSERT_EQ(0, rename(file1_s3d4, file1_s3d3));
2491+
}
2492+
24472493
TEST_F_FORK(layout1, reparent_link)
24482494
{
24492495
const struct rule layer1[] = {

0 commit comments

Comments
 (0)