Skip to content

Commit 0055f53

Browse files
committed
selftests/landlock: Add layout1.refer_mount_root
Add tests to check error codes when linking or renaming a mount root directory. This previously triggered a kernel warning, but it is fixed with the previous commit. Cc: Günther Noack <gnoack@google.com> Cc: Paul Moore <paul@paul-moore.com> Link: https://lore.kernel.org/r/20240516181935.1645983-3-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 88da52c commit 0055f53

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* See https://sourceware.org/glibc/wiki/Synchronizing_Headers.
3636
*/
3737
#include <linux/fs.h>
38+
#include <linux/mount.h>
3839

3940
#include "common.h"
4041

@@ -47,6 +48,13 @@ int renameat2(int olddirfd, const char *oldpath, int newdirfd,
4748
}
4849
#endif
4950

51+
#ifndef open_tree
52+
int open_tree(int dfd, const char *filename, unsigned int flags)
53+
{
54+
return syscall(__NR_open_tree, dfd, filename, flags);
55+
}
56+
#endif
57+
5058
#ifndef RENAME_EXCHANGE
5159
#define RENAME_EXCHANGE (1 << 1)
5260
#endif
@@ -2400,6 +2408,43 @@ TEST_F_FORK(layout1, refer_denied_by_default4)
24002408
layer_dir_s1d1_refer);
24012409
}
24022410

2411+
/*
2412+
* Tests walking through a denied root mount.
2413+
*/
2414+
TEST_F_FORK(layout1, refer_mount_root_deny)
2415+
{
2416+
const struct landlock_ruleset_attr ruleset_attr = {
2417+
.handled_access_fs = LANDLOCK_ACCESS_FS_MAKE_DIR,
2418+
};
2419+
int root_fd, ruleset_fd;
2420+
2421+
/* Creates a mount object from a non-mount point. */
2422+
set_cap(_metadata, CAP_SYS_ADMIN);
2423+
root_fd =
2424+
open_tree(AT_FDCWD, dir_s1d1,
2425+
AT_EMPTY_PATH | OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
2426+
clear_cap(_metadata, CAP_SYS_ADMIN);
2427+
ASSERT_LE(0, root_fd);
2428+
2429+
ruleset_fd =
2430+
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
2431+
ASSERT_LE(0, ruleset_fd);
2432+
2433+
ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
2434+
ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
2435+
EXPECT_EQ(0, close(ruleset_fd));
2436+
2437+
/* Link denied by Landlock: EACCES. */
2438+
EXPECT_EQ(-1, linkat(root_fd, ".", root_fd, "does_not_exist", 0));
2439+
EXPECT_EQ(EACCES, errno);
2440+
2441+
/* renameat2() always returns EBUSY. */
2442+
EXPECT_EQ(-1, renameat2(root_fd, ".", root_fd, "does_not_exist", 0));
2443+
EXPECT_EQ(EBUSY, errno);
2444+
2445+
EXPECT_EQ(0, close(root_fd));
2446+
}
2447+
24032448
TEST_F_FORK(layout1, reparent_link)
24042449
{
24052450
const struct rule layer1[] = {

0 commit comments

Comments
 (0)