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

jeffxu at google.com jeffxu at google.com
Sat Aug 20 00:45:47 UTC 2022


From: Jeff Xu <jeffxu at google.com>

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.

Signed-off-by: Jeff Xu <jeffxu at chromium.org>
---
 tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index 21a2ce8fa739..f604165dbd21 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -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>
@@ -3398,12 +3399,53 @@ static const char (*merge_sub_files[])[] = {
  *         └── work
  */
 
+static char *fgrep(FILE *inf, const char *str)
+{
+	char line[256];
+	int slen = strlen(str);
+
+	while (!feof(inf)) {
+		if (!fgets(line, 256, inf))
+			break;
+		if (strncmp(line, str, slen))
+			continue;
+
+		return strdup(line);
+	}
+
+	return NULL;
+}
+
+static bool check_overlayfs_support(void)
+{
+	FILE *inf = fopen("/proc/filesystems", "r");
+	char *res;
+	bool ret = false;
+
+	if (!inf)
+		return false;
+
+	res = fgrep(inf, "nodev\toverlay\n");
+
+	if (res) {
+		ret = true;
+		free(res);
+	}
+
+	fclose(inf);
+
+	return ret;
+}
+
 /* clang-format off */
 FIXTURE(layout2_overlay) {};
 /* clang-format on */
 
 FIXTURE_SETUP(layout2_overlay)
 {
+	int rc;
+	bool support;
+
 	prepare_layout(_metadata);
 
 	create_directory(_metadata, LOWER_BASE);
@@ -3431,9 +3473,17 @@ 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);
+	if (rc < 0) {
+		TH_LOG("mount overlay failed: errorno=%s", strerror(errno));
+		support = check_overlayfs_support();
+		ASSERT_FALSE(support);
+		SKIP(return, "overlayfs is not supported");
+	}
+
 	clear_cap(_metadata, CAP_DAC_OVERRIDE);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 }
-- 
2.37.1.595.g718a3a8f04-goog



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