linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: <jeffxu@google.com>
To: mic@digikod.net
Cc: jorgelo@chromium.org, keescook@chromium.org,
	linux-security-module@vger.kernel.org, groeck@chromium.org,
	Jeff Xu <jeffxu@google.com>, Jeff Xu <jeffxu@chromium.org>
Subject: [PATCH] selftests/landlock: skip overlayfs test when kernel not support it
Date: Sat, 20 Aug 2022 00:45:47 +0000	[thread overview]
Message-ID: <20220820004547.2135627-1-jeffxu@google.com> (raw)

From: Jeff Xu <jeffxu@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@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


             reply	other threads:[~2022-08-20  0:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-20  0:45 jeffxu [this message]
2022-08-22 12:55 ` [PATCH] selftests/landlock: skip overlayfs test when kernel not support it Mickaël Salaün

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220820004547.2135627-1-jeffxu@google.com \
    --to=jeffxu@google.com \
    --cc=groeck@chromium.org \
    --cc=jeffxu@chromium.org \
    --cc=jorgelo@chromium.org \
    --cc=keescook@chromium.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).