[PATCH v3 2/2] landlock: Add a test for REFER layer composition with network layer

From: pandaWall

Date: Thu Nov 12 2026 - 01:59:19 EST


Check that stacking a network-only ruleset (i.e. a layer handling no
filesystem access right) on top of a filesystem ruleset still allows
different-parent renames and exchanges allowed by the filesystem layer.
Before the fix, such a layer implicitly handled
LANDLOCK_ACCESS_FS_REFER and denied these operations with EXDEV.

A single rule on dir_s1d2 is enough: it covers its subdirectory
dir_s1d3, and grants all the filesystem access rights handled by the
domain on both parents of the tested renames.

Signed-off-by: pandaWall <mangosteen922@xxxxxxx>
---
tools/testing/selftests/landlock/fs_test.c | 37 +++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index f725882fa..94b339252 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -2569,7 +2569,6 @@ const struct rule layer_dir_s2d1_execute[] = {
},
{},
};
-
/*
* Tests precedence over renames: denied by default for different parent
* directories, *with* a rule matching a parent directory, but not directly
@@ -2612,6 +2611,42 @@ TEST_F_FORK(layout1, refer_denied_by_default4)
layer_dir_s1d1_refer);
}

+/*
+ * Tests that a layer not handling filesystem access rights does not
+ * implicitly handle LANDLOCK_ACCESS_FS_REFER, which would otherwise deny
+ * different-parent renames and links stacked on top of a filesystem ruleset.
+ */
+TEST_F_FORK(layout1, refer_with_net_layer)
+{
+ const struct rule layer_fs[] = {
+ {
+ .path = dir_s1d2,
+ .access = LANDLOCK_ACCESS_FS_REFER,
+ },
+ {},
+ };
+ const struct landlock_ruleset_attr ruleset_net = {
+ .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP,
+ };
+
+ /*
+ * layer_fs alone: different-parent renames are allowed. A single
+ * rule on dir_s1d2 covers its subdirectory dir_s1d3 as well.
+ */
+ enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REFER |
+ LANDLOCK_ACCESS_FS_MAKE_REG,
+ layer_fs);
+ ASSERT_EQ(0, test_rename(file1_s1d2, file1_s1d3));
+ ASSERT_EQ(0, test_rename(file1_s1d3, file1_s1d2));
+ ASSERT_EQ(0, test_exchange(file2_s1d2, file2_s1d3));
+
+ /* Stacks a network-only layer: still allowed. */
+ drop_access_rights(_metadata, &ruleset_net);
+ ASSERT_EQ(0, test_rename(file1_s1d2, file1_s1d3));
+ ASSERT_EQ(0, test_rename(file1_s1d3, file1_s1d2));
+ ASSERT_EQ(0, test_exchange(file2_s1d2, file2_s1d3));
+}
+
/*
* Tests walking through a denied root mount.
*/
--
2.56.0