Skip to contentSkip to conversation

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

PATCH Superseded [email protected] 2 participants 3 messages Aug 22, 2022 – Aug 23, 2022

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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, 52 insertions(+), 4 deletions(-)
tools/testing/selftests/landlock/fs_test.c +52 -4
index 21a2ce8fa739..0c283d50f222 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,43 @@ 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)
+{
+	bool ret = false;
+	FILE *inf = fopen(proc_filesystems, "r");
+
+	/*
+	 * If fopen fails, return supported.
+	 * This helps to detect missing file (shall not
+	 * happen).
+	 */
+	if (!inf)
+		return true;
+
+	ret = fgrep(inf, "nodev\toverlay\n");
+	fclose(inf);
+
+	return ret;
+}
+
+
 static void prepare_layout(struct __test_metadata *const _metadata)
 {
 	disable_caps(_metadata);
@@ -3397,13 +3436,14 @@ static const char (*merge_sub_files[])[] = {
  *     └── work
  *         └── work
  */
-
 /* clang-format off */
 FIXTURE(layout2_overlay) {};
 /* clang-format on */
 
 FIXTURE_SETUP(layout2_overlay)
 {
+	int rc;
+
 	prepare_layout(_metadata);
 
 	create_directory(_metadata, LOWER_BASE);
@@ -3431,11 +3471,19 @@ FIXTURE_SETUP(layout2_overlay)
 	create_directory(_metadata, MERGE_DATA);
 	set_cap(_metadata, CAP_SYS_ADMIN);
 	set_cap(_metadata, CAP_DAC_OVERRIDE);
-	ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
-			   "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
-			   ",workdir=" UPPER_WORK));
+
+	rc = mount("overlay", MERGE_DATA, "overlay", 0,
+		   "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
+		   ",workdir=" UPPER_WORK);
 	clear_cap(_metadata, CAP_DAC_OVERRIDE);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
+
+	if (rc < 0) {
+		ASSERT_EQ(ENODEV, errno);
+		ASSERT_FALSE(supports_overlayfs());
+		SKIP(return, "overlayfs is not supported");
+	}
+
 }
 
 FIXTURE_TEARDOWN(layout2_overlay)

base-commit: 50cd95ac46548429e5bba7ca75cc97d11a697947

Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · On Mon, Aug 22, 2022 at 9:53 AM <[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.

Couple of nits, otherwise

Quoted 55 lines
---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
 1 file changed, 52 insertions(+), 4 deletions(-)
tools/testing/selftests/landlock/fs_test.c +29 -0
index 21a2ce8fa739..0c283d50f222 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,43 @@ 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)
+{
+       bool ret = false;
+       FILE *inf = fopen(proc_filesystems, "r");
+
+       /*
+        * If fopen fails, return supported.
+        * This helps to detect missing file (shall not
+        * happen).
+        */

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

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +10 -0
+       if (!inf)
+               return true;
+
+       ret = fgrep(inf, "nodev\toverlay\n");
+       fclose(inf);
+
+       return ret;
+}
+
+

double empty line

tools/testing/selftests/landlock/fs_test.c static const char (*merge_sub_files[])[] = { · line 3400

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +0 -1
 static void prepare_layout(struct __test_metadata *const _metadata)
 {
        disable_caps(_metadata);
@@ -3397,13 +3436,14 @@ static const char (*merge_sub_files[])[] = {
  *     └── work
  *         └── work
  */
-

Unnecessary whitespace change

Quoted 21 lines
tools/testing/selftests/landlock/fs_test.c +5 -3
 /* clang-format off */
 FIXTURE(layout2_overlay) {};
 /* clang-format on */
 
 FIXTURE_SETUP(layout2_overlay)
 {
+       int rc;
+
        prepare_layout(_metadata);
 
        create_directory(_metadata, LOWER_BASE);
@@ -3431,11 +3471,19 @@ FIXTURE_SETUP(layout2_overlay)
        create_directory(_metadata, MERGE_DATA);
        set_cap(_metadata, CAP_SYS_ADMIN);
        set_cap(_metadata, CAP_DAC_OVERRIDE);
-       ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
-                          "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
-                          ",workdir=" UPPER_WORK));
+
+       rc = mount("overlay", MERGE_DATA, "overlay", 0,
+                  "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA

tools/testing/selftests/landlock/fs_test.c FIXTURE_SETUP(layout2_overlay) · line 3486

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +8 -0
+                  ",workdir=" UPPER_WORK);
        clear_cap(_metadata, CAP_DAC_OVERRIDE);
        clear_cap(_metadata, CAP_SYS_ADMIN);
+
+       if (rc < 0) {
+               ASSERT_EQ(ENODEV, errno);
+               ASSERT_FALSE(supports_overlayfs());
+               SKIP(return, "overlayfs is not supported");
+       }
+

Unnecessary empty line

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +0 -0
 }
 
 FIXTURE_TEARDOWN(layout2_overlay)

base-commit: 50cd95ac46548429e5bba7ca75cc97d11a697947 --
2.37.1.595.g718a3a8f04-goog

author
#2 lore raw
Quoted 3 lines

Couple of nits, otherwise

Thank you. Updated with V4 patch

Best Regards

Jeff

Quoted 134 lines · Guenter Roeck
Quoted 8 lines · On Mon, Aug 22, 2022 at 9:53 AM <[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.

Couple of nits, otherwise

Quoted 55 lines
---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
 1 file changed, 52 insertions(+), 4 deletions(-)
tools/testing/selftests/landlock/fs_test.c +29 -0
index 21a2ce8fa739..0c283d50f222 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,43 @@ 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)
+{
+       bool ret = false;
+       FILE *inf = fopen(proc_filesystems, "r");
+
+       /*
+        * If fopen fails, return supported.
+        * This helps to detect missing file (shall not
+        * happen).
+        */

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

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +10 -0
+       if (!inf)
+               return true;
+
+       ret = fgrep(inf, "nodev\toverlay\n");
+       fclose(inf);
+
+       return ret;
+}
+
+

double empty line

tools/testing/selftests/landlock/fs_test.c static const char (*merge_sub_files[])[] = { · line 3400

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +0 -1
 static void prepare_layout(struct __test_metadata *const _metadata)
 {
        disable_caps(_metadata);
@@ -3397,13 +3436,14 @@ static const char (*merge_sub_files[])[] = {
  *     └── work
  *         └── work
  */
-

Unnecessary whitespace change

Quoted 21 lines
tools/testing/selftests/landlock/fs_test.c +5 -3
 /* clang-format off */
 FIXTURE(layout2_overlay) {};
 /* clang-format on */
 
 FIXTURE_SETUP(layout2_overlay)
 {
+       int rc;
+
        prepare_layout(_metadata);
 
        create_directory(_metadata, LOWER_BASE);
@@ -3431,11 +3471,19 @@ FIXTURE_SETUP(layout2_overlay)
        create_directory(_metadata, MERGE_DATA);
        set_cap(_metadata, CAP_SYS_ADMIN);
        set_cap(_metadata, CAP_DAC_OVERRIDE);
-       ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
-                          "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
-                          ",workdir=" UPPER_WORK));
+
+       rc = mount("overlay", MERGE_DATA, "overlay", 0,
+                  "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA

tools/testing/selftests/landlock/fs_test.c FIXTURE_SETUP(layout2_overlay) · line 3486

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +8 -0
+                  ",workdir=" UPPER_WORK);
        clear_cap(_metadata, CAP_DAC_OVERRIDE);
        clear_cap(_metadata, CAP_SYS_ADMIN);
+
+       if (rc < 0) {
+               ASSERT_EQ(ENODEV, errno);
+               ASSERT_FALSE(supports_overlayfs());
+               SKIP(return, "overlayfs is not supported");
+       }
+

Unnecessary empty line

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +0 -0
 }
 
 FIXTURE_TEARDOWN(layout2_overlay)

base-commit: 50cd95ac46548429e5bba7ca75cc97d11a697947 --
2.37.1.595.g718a3a8f04-goog