[PATCH 1/3] selftests/landlock: Clean up tmp directory even when mount fails

Tingmao Wang m at maowtm.org
Sat May 24 17:56:32 UTC 2025


A typical sequence for someone running this test for the first time might
be:

make kselftest TARGETS="landlock"
(sees a bunch of "Permission denied", realizes that sudo is needed)
sudo make kselftest TARGETS="landlock"
(sees a bunch of "File exists", scratches head)

This ensures that the newly created directory is cleaned up by the first
attempt (and also gives a slightly more helpful message explaining the
cause).

See proposal in patch 3 message for a more generic solution - this might
not be necessary.

Signed-off-by: Tingmao Wang <m at maowtm.org>
---
 tools/testing/selftests/landlock/fs_test.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index 73729382d40f..e65e6cc80e22 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -5,6 +5,7 @@
  * Copyright © 2017-2020 Mickaël Salaün <mic at digikod.net>
  * Copyright © 2020 ANSSI
  * Copyright © 2020-2022 Microsoft Corporation
+ * Copyright © 2025 Tingmao Wang <m at maowtm.org>
  */
 
 #define _GNU_SOURCE
@@ -303,10 +304,9 @@ static void prepare_layout_opt(struct __test_metadata *const _metadata,
 	 * for tests relying on pivot_root(2) and move_mount(2).
 	 */
 	set_cap(_metadata, CAP_SYS_ADMIN);
-	ASSERT_EQ(0, unshare(CLONE_NEWNS | CLONE_NEWCGROUP));
-	ASSERT_EQ(0, mount_opt(mnt, TMP_DIR))
+	ASSERT_EQ(0, unshare(CLONE_NEWNS | CLONE_NEWCGROUP))
 	{
-		TH_LOG("Failed to mount the %s filesystem: %s", mnt->type,
+		TH_LOG("Failed to create new mount namespace: %s",
 		       strerror(errno));
 		/*
 		 * FIXTURE_TEARDOWN() is not called when FIXTURE_SETUP()
@@ -316,6 +316,12 @@ static void prepare_layout_opt(struct __test_metadata *const _metadata,
 		 */
 		remove_path(TMP_DIR);
 	}
+	ASSERT_EQ(0, mount_opt(mnt, TMP_DIR))
+	{
+		TH_LOG("Failed to mount the %s filesystem: %s", mnt->type,
+		       strerror(errno));
+		remove_path(TMP_DIR);
+	}
 	ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL));
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 }
-- 
2.49.0




More information about the Linux-security-module-archive mailing list