Skip to contentSkip to conversation

selftests/landlock: fix test when overlayfs is

PATCH Superseded [email protected] 3 participants 4 messages Dec 29, 2022

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover selftests/landlock: fix test when overlayfs is

From: Jeff Xu <[email protected]>

Overlayfs can be disabled in kernel config, causing related tests to fail. Adding a check for overlayfs’s supportability at runtime, so we can call SKIP() when needed.

v6: In v4, the SKIP() was applied at FIXTURE_SETUP() after mount() fail, however, FIXTURE_TEARDOWN() will fail. It might be complicated for test infra or testcase itself to have cleanup code handing the success/failure of steps in SETUP().

This patch changes the approach, it calls supports_overlay() and SKIP() at the beginning of FIXTURE_SETUP(), FIX_TEARDOWN(), TEST_F_FORK(). Because no modification of system is done by the test, cleanup is not needed.

v4:
https://lore.kernel.org/all/[email protected]/

Jeff Xu (1):
selftests/landlock: skip overlayfs test when kernel not support it

 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)

base-commit: 963a70bee5880640d0fd83ed29dc1e7ec0d2bd4a

Signature · 1 line
2.39.0.314.g84b9a713c41-goog
#1 lore raw

PATCH v6 1/1 selftests/landlock: skip overlayfs test when kernel not support it

From: Jeff Xu <[email protected]>

Overlayfs can be disabled in kernel config, causing related tests to fail. Add check for overlayfs’s supportability at runtime, so we can call SKIP() when needed.

---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
tools/testing/selftests/landlock/fs_test.c +56 -0
index 21a2ce8fa739..2d84c055f86f 100644
@@ -11,6 +11,7 @@
 #include <fcntl.h>
 #include <linux/landlock.h>
 #include <sched.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/capability.h>
 #include <sys/mount.h>
@@ -62,6 +63,7 @@ static const char dir_s3d1[] = TMP_DIR "/s3d1";
 static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
 static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
 
+static const char proc_filesystems[] = "/proc/filesystems";
 /*
  * layout1 hierarchy:
  *
@@ -169,6 +171,48 @@ static int remove_path(const char *const path)
 	return err;
 }
 
+static bool fgrep(FILE *inf, const char *str)
+{
+	char line[32];
+	int slen = strlen(str);
+
+	while (!feof(inf)) {
+		if (!fgets(line, sizeof(line), inf))
+			break;
+		if (strncmp(line, str, slen))
+			continue;
+
+		return true;
+	}
+
+	return false;
+}
+
+static bool supports_overlayfs(void)
+{
+	char *res;
+	bool ret = false;
+	FILE *inf = fopen(proc_filesystems, "r");
+
+	/*
+	 * If fopen failed, return supported.
+	 * This help detect missing file (shall not
+	 * happen).
+	 */
+	if (!inf)
+		return true;
+
+	ret = fgrep(inf, "nodev\toverlay\n");
+
+	if (res)
+		free(res);
+
+	fclose(inf);
+
+	return ret;
+}
+
+
 static void prepare_layout(struct __test_metadata *const _metadata)
 {
 	disable_caps(_metadata);
@@ -3404,6 +3448,9 @@ FIXTURE(layout2_overlay) {};
 
 FIXTURE_SETUP(layout2_overlay)
 {
+	if (!supports_overlayfs())
+		SKIP(return, "overlayfs is not supported");
+
 	prepare_layout(_metadata);
 
 	create_directory(_metadata, LOWER_BASE);
@@ -3440,6 +3487,9 @@ FIXTURE_SETUP(layout2_overlay)
 
 FIXTURE_TEARDOWN(layout2_overlay)
 {
+	if (!supports_overlayfs())
+		SKIP(return, "overlayfs is not supported");
+
 	EXPECT_EQ(0, remove_path(lower_do1_fl3));
 	EXPECT_EQ(0, remove_path(lower_dl1_fl2));
 	EXPECT_EQ(0, remove_path(lower_fl1));
@@ -3471,6 +3521,9 @@ FIXTURE_TEARDOWN(layout2_overlay)
 
 TEST_F_FORK(layout2_overlay, no_restriction)
 {
+	if (!supports_overlayfs())
+		SKIP(return, "overlayfs is not supported");
+
 	ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY));
 	ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY));
 	ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY));
@@ -3634,6 +3687,9 @@ TEST_F_FORK(layout2_overlay, same_content_different_file)
 	size_t i;
 	const char *path_entry;
 
+	if (!supports_overlayfs())
+		SKIP(return, "overlayfs is not supported");
+
 	/* Sets rules on base directories (i.e. outside overlay scope). */
 	ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
 	ASSERT_LE(0, ruleset_fd);
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
#2 lore raw

Re: [PATCH v6 1/1] selftests/landlock: skip overlayfs test when kernel not support it

Quoted 47 lines · On Thu, Dec 29, 2022 at 12:12 PM <[email protected]> wrote:

From: Jeff Xu <[email protected]>

Overlayfs can be disabled in kernel config, causing related tests to fail. Add check for overlayfs’s supportability at runtime, so we can call SKIP() when needed.

---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
tools/testing/selftests/landlock/fs_test.c +13 -0
index 21a2ce8fa739..2d84c055f86f 100644
@@ -11,6 +11,7 @@
 #include <fcntl.h>
 #include <linux/landlock.h>
 #include <sched.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/capability.h>
 #include <sys/mount.h>
@@ -62,6 +63,7 @@ static const char dir_s3d1[] = TMP_DIR "/s3d1";
 static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
 static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
 
+static const char proc_filesystems[] = "/proc/filesystems";
 /*
  * layout1 hierarchy:
  *
@@ -169,6 +171,48 @@ static int remove_path(const char *const path)
        return err;
 }
 
+static bool fgrep(FILE *inf, const char *str)
+{
+       char line[32];
+       int slen = strlen(str);
+
+       while (!feof(inf)) {
+               if (!fgets(line, sizeof(line), inf))
+                       break;
+               if (strncmp(line, str, slen))
+                       continue;
+

tools/testing/selftests/landlock/fs_test.c static int remove_path(const char *const path) · line 194

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +10 -0
+               return true;
+       }
+
+       return false;
+}
+
+static bool supports_overlayfs(void)
+{
+       char *res;
+       bool ret = false;

Unnecessary initialization.

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +4 -0
+       FILE *inf = fopen(proc_filesystems, "r");
+
+       /*
+        * If fopen failed, return supported.

tools/testing/selftests/landlock/fs_test.c static int remove_path(const char *const path) · line 208

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +10 -0
+        * This help detect missing file (shall not
+        * happen).
+        */
+       if (!inf)
+               return true;
+
+       ret = fgrep(inf, "nodev\toverlay\n");
+
+       if (res)
+               free(res);

Either I am missing something, or res is neither initialized nor used.

Guenter

Quoted 53 lines
tools/testing/selftests/landlock/fs_test.c +19 -1
+
+       fclose(inf);
+
+       return ret;
+}
+
+
 static void prepare_layout(struct __test_metadata *const _metadata)
 {
        disable_caps(_metadata);
@@ -3404,6 +3448,9 @@ FIXTURE(layout2_overlay) {};
 
 FIXTURE_SETUP(layout2_overlay)
 {
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        prepare_layout(_metadata);
 
        create_directory(_metadata, LOWER_BASE);
@@ -3440,6 +3487,9 @@ FIXTURE_SETUP(layout2_overlay)
 
 FIXTURE_TEARDOWN(layout2_overlay)
 {
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        EXPECT_EQ(0, remove_path(lower_do1_fl3));
        EXPECT_EQ(0, remove_path(lower_dl1_fl2));
        EXPECT_EQ(0, remove_path(lower_fl1));
@@ -3471,6 +3521,9 @@ FIXTURE_TEARDOWN(layout2_overlay)
 
 TEST_F_FORK(layout2_overlay, no_restriction)
 {
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY));
        ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY));
        ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY));
@@ -3634,6 +3687,9 @@ TEST_F_FORK(layout2_overlay, same_content_different_file)
        size_t i;
        const char *path_entry;
 
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        /* Sets rules on base directories (i.e. outside overlay scope). */
        ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
        ASSERT_LE(0, ruleset_fd);
--

2.39.0.314.g84b9a713c41-goog

#3 lore raw

Re: [PATCH v6 1/1] selftests/landlock: skip overlayfs test when kernel not support it

Quoted 62 lines · Guenter Roeck
Quoted 47 lines · On Thu, Dec 29, 2022 at 12:12 PM <[email protected]> wrote:

From: Jeff Xu <[email protected]>

Overlayfs can be disabled in kernel config, causing related tests to fail. Add check for overlayfs’s supportability at runtime, so we can call SKIP() when needed.

---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
tools/testing/selftests/landlock/fs_test.c +13 -0
index 21a2ce8fa739..2d84c055f86f 100644
@@ -11,6 +11,7 @@
 #include <fcntl.h>
 #include <linux/landlock.h>
 #include <sched.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/capability.h>
 #include <sys/mount.h>
@@ -62,6 +63,7 @@ static const char dir_s3d1[] = TMP_DIR "/s3d1";
 static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
 static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
 
+static const char proc_filesystems[] = "/proc/filesystems";
 /*
  * layout1 hierarchy:
  *
@@ -169,6 +171,48 @@ static int remove_path(const char *const path)
        return err;
 }
 
+static bool fgrep(FILE *inf, const char *str)
+{
+       char line[32];
+       int slen = strlen(str);
+
+       while (!feof(inf)) {
+               if (!fgets(line, sizeof(line), inf))
+                       break;
+               if (strncmp(line, str, slen))
+                       continue;
+

tools/testing/selftests/landlock/fs_test.c static int remove_path(const char *const path) · line 194

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +10 -0
+               return true;
+       }
+
+       return false;
+}
+
+static bool supports_overlayfs(void)
+{
+       char *res;
+       bool ret = false;

Unnecessary initialization.

Quoted 17 lines
Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +4 -0
+       FILE *inf = fopen(proc_filesystems, "r");
+
+       /*
+        * If fopen failed, return supported.

tools/testing/selftests/landlock/fs_test.c static int remove_path(const char *const path) · line 208

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +10 -0
+        * This help detect missing file (shall not
+        * happen).
+        */
+       if (!inf)
+               return true;
+
+       ret = fgrep(inf, "nodev\toverlay\n");
+
+       if (res)
+               free(res);

Either I am missing something, or res is neither initialized nor used.

Hoops, this is a bug, and I'm missing a test with a kernel with overlayfs enabled.
I will send V7.

Quoted 55 lines

Guenter

Quoted 53 lines
tools/testing/selftests/landlock/fs_test.c +19 -1
+
+       fclose(inf);
+
+       return ret;
+}
+
+
 static void prepare_layout(struct __test_metadata *const _metadata)
 {
        disable_caps(_metadata);
@@ -3404,6 +3448,9 @@ FIXTURE(layout2_overlay) {};
 
 FIXTURE_SETUP(layout2_overlay)
 {
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        prepare_layout(_metadata);
 
        create_directory(_metadata, LOWER_BASE);
@@ -3440,6 +3487,9 @@ FIXTURE_SETUP(layout2_overlay)
 
 FIXTURE_TEARDOWN(layout2_overlay)
 {
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        EXPECT_EQ(0, remove_path(lower_do1_fl3));
        EXPECT_EQ(0, remove_path(lower_dl1_fl2));
        EXPECT_EQ(0, remove_path(lower_fl1));
@@ -3471,6 +3521,9 @@ FIXTURE_TEARDOWN(layout2_overlay)
 
 TEST_F_FORK(layout2_overlay, no_restriction)
 {
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY));
        ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY));
        ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY));
@@ -3634,6 +3687,9 @@ TEST_F_FORK(layout2_overlay, same_content_different_file)
        size_t i;
        const char *path_entry;
 
+       if (!supports_overlayfs())
+               SKIP(return, "overlayfs is not supported");
+
        /* Sets rules on base directories (i.e. outside overlay scope). */
        ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
        ASSERT_LE(0, ruleset_fd);
--

2.39.0.314.g84b9a713c41-goog