Skip to contentSkip to conversation

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

PATCH In review [email protected] 2 participants 4 messages Aug 23, 2022 – Aug 24, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 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 | 53 ++++++++++++++++++++--
 1 file changed, 50 insertions(+), 3 deletions(-)
tools/testing/selftests/landlock/fs_test.c +50 -3
index 21a2ce8fa739..aaece185579d 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,42 @@ 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);
@@ -3404,6 +3442,8 @@ FIXTURE(layout2_overlay) {};
 
 FIXTURE_SETUP(layout2_overlay)
 {
+	int rc;
+
 	prepare_layout(_metadata);
 
 	create_directory(_metadata, LOWER_BASE);
@@ -3431,11 +3471,18 @@ 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
maintainer
#1 lore raw

This looks good overall. You'll find some nitpicking review below.

I found that there is an issue with the skipped test, and especially the teardown parts:

Quoted 4 lines

# RUN layout2_overlay.no_restriction ...
# SKIP overlayfs is not supported
# fs_test.c:3537:no_restriction:Expected 0 (0) == test_open(merge_fl1, O_RDONLY) (2) # fs_test.c:3512:no_restriction:Expected 0 (0) == umount(MERGE_DATA) (-1)

These messages seem OK…

Quoted 2 lines

# fs_test.c:3496:no_restriction:Expected 0 (0) == umount(LOWER_BASE) (-1) # fs_test.c:3507:no_restriction:Expected 0 (0) == umount(UPPER_BASE) (-1)

…but these two should not happen because the tmpfs should still be mounted.

Quoted 4 lines

# OK layout2_overlay.no_restriction
ok 58 # SKIP overlayfs is not supported
# RUN layout2_overlay.same_content_different_file ...
# fs_test.c:3512:no_restriction:Expected 0 (0) == umount(MERGE_DATA) (-1)

These is some inconsistencies here too.

Quoted 11 lines

# fs_test.c:230:no_restriction:Expected 0 (0) == umount(TMP_DIR) (-1)
# SKIP overlayfs is not supported
# fs_test.c:3723:same_content_different_file:Expected 0 (0) == test_open(path_entry, O_RDWR) (2) # fs_test.c:3496:same_content_different_file:Expected 0 (0) == umount(LOWER_BASE) (-1) # fs_test.c:3498:same_content_different_file:Expected 0 (0) == remove_path(LOWER_BASE) (16) # fs_test.c:3507:same_content_different_file:Expected 0 (0) == umount(UPPER_BASE) (-1) # fs_test.c:3509:same_content_different_file:Expected 0 (0) == remove_path(UPPER_BASE) (16) # fs_test.c:3512:same_content_different_file:Expected 0 (0) == umount(MERGE_DATA) (-1) # fs_test.c:230:same_content_different_file:Expected 0 (0) == umount(TMP_DIR) (-1) # fs_test.c:232:same_content_different_file:Expected 0 (0) == remove_path(TMP_DIR) (16) # fs_test.c:3512:same_content_different_file:Expected 0 (0) == umount(MERGE_DATA) (-1)

I guess this is because of the TEST_F_FORK() helper which doesn't handle well the skipped with error tests, hence the parent test process trying to execute the teardown and unmounting something which is not mounted it its namespace, and the duplicated messages.

This may be related to commit 63e6b2a42342 ("selftests/harness: Run TEARDOWN for ASSERT failures").

Can you fix TEST_F_FORK() for skipped tests please?

We should merge TEST_F_FORK() within kselftest_harness.h with a follow up patch though.

Quoted 1 line · On 23/08/2022 03:02, [email protected] wrote:

From: Jeff Xu <[email protected]>

This is not consistent with your Signed-off-by email.

Quoted 4 lines

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.

selftests/landlock: Skip overlayfs tests when not supported

overlayfs can be disabled in the kernel configuration (which is the case for chromeOS), causing related tests to fail. Skip such tests when an overlayfs mount operation failed because the running kernel doesn't support this file system.

Quoted 20 lines
---
  tools/testing/selftests/landlock/fs_test.c | 53 ++++++++++++++++++++--
  1 file changed, 50 insertions(+), 3 deletions(-)
tools/testing/selftests/landlock/fs_test.c +1 -0
index 21a2ce8fa739..aaece185579d 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>

tools/testing/selftests/landlock/fs_test.c static const char dir_s3d1[] = TMP_DIR "/s3d1"; · line 66

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
@@ -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";

No need for this global variable, just use the string in the fopen() call.

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

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
  /*
   * layout1 hierarchy:
   *
@@ -169,6 +171,42 @@ static int remove_path(const char *const path)
  	return err;
  }
 
+static bool fgrep(FILE *inf, const char *str)

const char *const str

s/inf/file/

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

Quoted 3 lines
tools/testing/selftests/landlock/fs_test.c +3 -0
+{
+	char line[32];
+	int slen = strlen(str);

s/slen/str_len/

Quoted 6 lines
tools/testing/selftests/landlock/fs_test.c +6 -0
+
+	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 193

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

No need to set it to "false" (which should be "true" BTW).

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

Quoted 1 line
tools/testing/selftests/landlock/fs_test.c +1 -0
+	FILE *inf = fopen(proc_filesystems, "r");

s/inf/filename/

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

Quoted 5 lines
tools/testing/selftests/landlock/fs_test.c +5 -0
+
+	/*
+	 * If fopen fails, return supported.
+	 * This helps to detect missing file (shall not
+	 * happen).

"A failed attempt to open /proc/filesystems implies that the file system is supported (default behavior). This can help detect such unattended issue (which should not happen)."

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

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

You can remove this newline.

tools/testing/selftests/landlock/fs_test.c FIXTURE(layout2_overlay) {}; · line 3445

Quoted 11 lines
tools/testing/selftests/landlock/fs_test.c +4 -0
+	return ret;
+}
+
  static void prepare_layout(struct __test_metadata *const _metadata)
  {
  	disable_caps(_metadata);
@@ -3404,6 +3442,8 @@ FIXTURE(layout2_overlay) {};
 
  FIXTURE_SETUP(layout2_overlay)
  {
+	int rc;

s/rc/ret/

int ret, err;

Quoted 5 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
+
  	prepare_layout(_metadata);
 
  	create_directory(_metadata, LOWER_BASE);

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

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +4 -3
@@ -3431,11 +3471,18 @@ 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);

err = errno;

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

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +2 -0
  	clear_cap(_metadata, CAP_DAC_OVERRIDE);
  	clear_cap(_metadata, CAP_SYS_ADMIN);
+
+	if (rc < 0) {

if (ret == -1) {

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

Quoted 1 line
tools/testing/selftests/landlock/fs_test.c +1 -0
+		ASSERT_EQ(ENODEV, errno);

ASSERT_EQ(ENODEV, err);

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

Quoted 3 lines
tools/testing/selftests/landlock/fs_test.c +3 -0
+		ASSERT_FALSE(supports_overlayfs());
+		SKIP(return, "overlayfs is not supported");
+	}

ASSERT_EQ(0, ret);

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

base-commit: 50cd95ac46548429e5bba7ca75cc97d11a697947

author
#2 lore raw
Quoted 5 lines

I guess this is because of the TEST_F_FORK() helper which doesn't handle well the skipped with error tests, hence the parent test process trying to execute the teardown and unmounting something which is not mounted it its namespace, and the duplicated messages.

Quoted 7 lines

This may be related to commit 63e6b2a42342 ("selftests/harness: Run TEARDOWN for ASSERT failures").

Can you fix TEST_F_FORK() for skipped tests please?

We should merge TEST_F_FORK() within kselftest_harness.h with a follow up patch though.

OK. That can be after I worked on pt_test.

Would you like me to move supports_overlayfs() to the beginning of layout2_overlay in this Patch ?
then there is nothing to cleanup.

Quoted 4 lines · On 23/08/2022 03:02, [email protected] wrote:
Quoted 1 line

From: Jeff Xu <[email protected]>

This is not consistent with your Signed-off-by email.

Sure, will try to switch to send patch via [email protected]

Quoted 219 lines · Mickaël Salaün

This looks good overall. You'll find some nitpicking review below.

I found that there is an issue with the skipped test, and especially the teardown parts:

Quoted 4 lines

# RUN layout2_overlay.no_restriction ...
# SKIP overlayfs is not supported
# fs_test.c:3537:no_restriction:Expected 0 (0) == test_open(merge_fl1, O_RDONLY) (2) # fs_test.c:3512:no_restriction:Expected 0 (0) == umount(MERGE_DATA) (-1)

These messages seem OK…

Quoted 2 lines

# fs_test.c:3496:no_restriction:Expected 0 (0) == umount(LOWER_BASE) (-1) # fs_test.c:3507:no_restriction:Expected 0 (0) == umount(UPPER_BASE) (-1)

…but these two should not happen because the tmpfs should still be mounted.

Quoted 4 lines

# OK layout2_overlay.no_restriction
ok 58 # SKIP overlayfs is not supported
# RUN layout2_overlay.same_content_different_file ...
# fs_test.c:3512:no_restriction:Expected 0 (0) == umount(MERGE_DATA) (-1)

These is some inconsistencies here too.

Quoted 11 lines

# fs_test.c:230:no_restriction:Expected 0 (0) == umount(TMP_DIR) (-1)
# SKIP overlayfs is not supported
# fs_test.c:3723:same_content_different_file:Expected 0 (0) == test_open(path_entry, O_RDWR) (2) # fs_test.c:3496:same_content_different_file:Expected 0 (0) == umount(LOWER_BASE) (-1) # fs_test.c:3498:same_content_different_file:Expected 0 (0) == remove_path(LOWER_BASE) (16) # fs_test.c:3507:same_content_different_file:Expected 0 (0) == umount(UPPER_BASE) (-1) # fs_test.c:3509:same_content_different_file:Expected 0 (0) == remove_path(UPPER_BASE) (16) # fs_test.c:3512:same_content_different_file:Expected 0 (0) == umount(MERGE_DATA) (-1) # fs_test.c:230:same_content_different_file:Expected 0 (0) == umount(TMP_DIR) (-1) # fs_test.c:232:same_content_different_file:Expected 0 (0) == remove_path(TMP_DIR) (16) # fs_test.c:3512:same_content_different_file:Expected 0 (0) == umount(MERGE_DATA) (-1)

I guess this is because of the TEST_F_FORK() helper which doesn't handle well the skipped with error tests, hence the parent test process trying to execute the teardown and unmounting something which is not mounted it its namespace, and the duplicated messages.

This may be related to commit 63e6b2a42342 ("selftests/harness: Run TEARDOWN for ASSERT failures").

Can you fix TEST_F_FORK() for skipped tests please?

We should merge TEST_F_FORK() within kselftest_harness.h with a follow up patch though.

Quoted 1 line · On 23/08/2022 03:02, [email protected] wrote:

From: Jeff Xu <[email protected]>

This is not consistent with your Signed-off-by email.

Quoted 4 lines

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.

selftests/landlock: Skip overlayfs tests when not supported

overlayfs can be disabled in the kernel configuration (which is the case for chromeOS), causing related tests to fail. Skip such tests when an overlayfs mount operation failed because the running kernel doesn't support this file system.

Quoted 20 lines
---
  tools/testing/selftests/landlock/fs_test.c | 53 ++++++++++++++++++++--
  1 file changed, 50 insertions(+), 3 deletions(-)
tools/testing/selftests/landlock/fs_test.c +1 -0
index 21a2ce8fa739..aaece185579d 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>

tools/testing/selftests/landlock/fs_test.c static const char dir_s3d1[] = TMP_DIR "/s3d1"; · line 66

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
@@ -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";

No need for this global variable, just use the string in the fopen() call.

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

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
  /*
   * layout1 hierarchy:
   *
@@ -169,6 +171,42 @@ static int remove_path(const char *const path)
      return err;
  }
 
+static bool fgrep(FILE *inf, const char *str)

const char *const str

s/inf/file/

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

Quoted 3 lines
tools/testing/selftests/landlock/fs_test.c +3 -0
+{
+     char line[32];
+     int slen = strlen(str);

s/slen/str_len/

Quoted 6 lines
tools/testing/selftests/landlock/fs_test.c +6 -0
+
+     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 193

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

No need to set it to "false" (which should be "true" BTW).

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

Quoted 1 line
tools/testing/selftests/landlock/fs_test.c +1 -0
+     FILE *inf = fopen(proc_filesystems, "r");

s/inf/filename/

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

Quoted 5 lines
tools/testing/selftests/landlock/fs_test.c +5 -0
+
+     /*
+      * If fopen fails, return supported.
+      * This helps to detect missing file (shall not
+      * happen).

"A failed attempt to open /proc/filesystems implies that the file system is supported (default behavior). This can help detect such unattended issue (which should not happen)."

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

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

You can remove this newline.

tools/testing/selftests/landlock/fs_test.c FIXTURE(layout2_overlay) {}; · line 3445

Quoted 11 lines
tools/testing/selftests/landlock/fs_test.c +4 -0
+     return ret;
+}
+
  static void prepare_layout(struct __test_metadata *const _metadata)
  {
      disable_caps(_metadata);
@@ -3404,6 +3442,8 @@ FIXTURE(layout2_overlay) {};
 
  FIXTURE_SETUP(layout2_overlay)
  {
+     int rc;

s/rc/ret/

int ret, err;

Quoted 5 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
+
      prepare_layout(_metadata);
 
      create_directory(_metadata, LOWER_BASE);

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

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +4 -3
@@ -3431,11 +3471,18 @@ 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);

err = errno;

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

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +2 -0
      clear_cap(_metadata, CAP_DAC_OVERRIDE);
      clear_cap(_metadata, CAP_SYS_ADMIN);
+
+     if (rc < 0) {

if (ret == -1) {

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

Quoted 1 line
tools/testing/selftests/landlock/fs_test.c +1 -0
+             ASSERT_EQ(ENODEV, errno);

ASSERT_EQ(ENODEV, err);

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

Quoted 3 lines
tools/testing/selftests/landlock/fs_test.c +3 -0
+             ASSERT_FALSE(supports_overlayfs());
+             SKIP(return, "overlayfs is not supported");
+     }

ASSERT_EQ(0, ret);

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

base-commit: 50cd95ac46548429e5bba7ca75cc97d11a697947

maintainer
#3 lore raw
Quoted 8 lines · Jeff Xu
Quoted 7 lines

I guess this is because of the TEST_F_FORK() helper which doesn't handle well the skipped with error tests, hence the parent test process trying to execute the teardown and unmounting something which is not mounted it its namespace, and the duplicated messages.

This may be related to commit 63e6b2a42342 ("selftests/harness: Run TEARDOWN for ASSERT failures").

Quoted 6 lines
Quoted 4 lines

Can you fix TEST_F_FORK() for skipped tests please?

We should merge TEST_F_FORK() within kselftest_harness.h with a follow up patch though.

OK. That can be after I worked on pt_test.

Right, but that is independent from the TEST_F_FORK() fix which should be easier to do and make backport possible.

Quoted 4 lines

Would you like me to move supports_overlayfs() to the beginning of layout2_overlay in this Patch ?
then there is nothing to cleanup.

I'm not sure this would be enough, and the current approach is good. TEST_F_FORK() needs some investigation.

Quoted 229 lines
Quoted 5 lines · On 23/08/2022 03:02, [email protected] wrote:
Quoted 1 line

From: Jeff Xu <[email protected]>

This is not consistent with your Signed-off-by email.

Sure, will try to switch to send patch via [email protected]

Quoted 219 lines · Mickaël Salaün

This looks good overall. You'll find some nitpicking review below.

I found that there is an issue with the skipped test, and especially the teardown parts:

Quoted 4 lines

# RUN layout2_overlay.no_restriction ...
# SKIP overlayfs is not supported
# fs_test.c:3537:no_restriction:Expected 0 (0) == test_open(merge_fl1, O_RDONLY) (2) # fs_test.c:3512:no_restriction:Expected 0 (0) == umount(MERGE_DATA) (-1)

These messages seem OK…

Quoted 2 lines

# fs_test.c:3496:no_restriction:Expected 0 (0) == umount(LOWER_BASE) (-1) # fs_test.c:3507:no_restriction:Expected 0 (0) == umount(UPPER_BASE) (-1)

…but these two should not happen because the tmpfs should still be mounted.

Quoted 4 lines

# OK layout2_overlay.no_restriction
ok 58 # SKIP overlayfs is not supported
# RUN layout2_overlay.same_content_different_file ...
# fs_test.c:3512:no_restriction:Expected 0 (0) == umount(MERGE_DATA) (-1)

These is some inconsistencies here too.

Quoted 11 lines

# fs_test.c:230:no_restriction:Expected 0 (0) == umount(TMP_DIR) (-1)
# SKIP overlayfs is not supported
# fs_test.c:3723:same_content_different_file:Expected 0 (0) == test_open(path_entry, O_RDWR) (2) # fs_test.c:3496:same_content_different_file:Expected 0 (0) == umount(LOWER_BASE) (-1) # fs_test.c:3498:same_content_different_file:Expected 0 (0) == remove_path(LOWER_BASE) (16) # fs_test.c:3507:same_content_different_file:Expected 0 (0) == umount(UPPER_BASE) (-1) # fs_test.c:3509:same_content_different_file:Expected 0 (0) == remove_path(UPPER_BASE) (16) # fs_test.c:3512:same_content_different_file:Expected 0 (0) == umount(MERGE_DATA) (-1) # fs_test.c:230:same_content_different_file:Expected 0 (0) == umount(TMP_DIR) (-1) # fs_test.c:232:same_content_different_file:Expected 0 (0) == remove_path(TMP_DIR) (16) # fs_test.c:3512:same_content_different_file:Expected 0 (0) == umount(MERGE_DATA) (-1)

I guess this is because of the TEST_F_FORK() helper which doesn't handle well the skipped with error tests, hence the parent test process trying to execute the teardown and unmounting something which is not mounted it its namespace, and the duplicated messages.

This may be related to commit 63e6b2a42342 ("selftests/harness: Run TEARDOWN for ASSERT failures").

Can you fix TEST_F_FORK() for skipped tests please?

We should merge TEST_F_FORK() within kselftest_harness.h with a follow up patch though.

Quoted 1 line · On 23/08/2022 03:02, [email protected] wrote:

From: Jeff Xu <[email protected]>

This is not consistent with your Signed-off-by email.

Quoted 4 lines

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.

selftests/landlock: Skip overlayfs tests when not supported

overlayfs can be disabled in the kernel configuration (which is the case for chromeOS), causing related tests to fail. Skip such tests when an overlayfs mount operation failed because the running kernel doesn't support this file system.

Quoted 20 lines
---
   tools/testing/selftests/landlock/fs_test.c | 53 ++++++++++++++++++++--
   1 file changed, 50 insertions(+), 3 deletions(-)
tools/testing/selftests/landlock/fs_test.c +1 -0
index 21a2ce8fa739..aaece185579d 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>

tools/testing/selftests/landlock/fs_test.c static const char dir_s3d1[] = TMP_DIR "/s3d1"; · line 66

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
@@ -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";

No need for this global variable, just use the string in the fopen() call.

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

Quoted 8 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
   /*
    * layout1 hierarchy:
    *
@@ -169,6 +171,42 @@ static int remove_path(const char *const path)
       return err;
   }
 
+static bool fgrep(FILE *inf, const char *str)

const char *const str

s/inf/file/

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

Quoted 3 lines
tools/testing/selftests/landlock/fs_test.c +3 -0
+{
+     char line[32];
+     int slen = strlen(str);

s/slen/str_len/

Quoted 6 lines
tools/testing/selftests/landlock/fs_test.c +6 -0
+
+     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 193

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

No need to set it to "false" (which should be "true" BTW).

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

Quoted 1 line
tools/testing/selftests/landlock/fs_test.c +1 -0
+     FILE *inf = fopen(proc_filesystems, "r");

s/inf/filename/

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

Quoted 5 lines
tools/testing/selftests/landlock/fs_test.c +5 -0
+
+     /*
+      * If fopen fails, return supported.
+      * This helps to detect missing file (shall not
+      * happen).

"A failed attempt to open /proc/filesystems implies that the file system is supported (default behavior). This can help detect such unattended issue (which should not happen)."

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

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

You can remove this newline.

tools/testing/selftests/landlock/fs_test.c FIXTURE(layout2_overlay) {}; · line 3445

Quoted 11 lines
tools/testing/selftests/landlock/fs_test.c +4 -0
+     return ret;
+}
+
   static void prepare_layout(struct __test_metadata *const _metadata)
   {
       disable_caps(_metadata);
@@ -3404,6 +3442,8 @@ FIXTURE(layout2_overlay) {};
 
   FIXTURE_SETUP(layout2_overlay)
   {
+     int rc;

s/rc/ret/

int ret, err;

Quoted 5 lines
tools/testing/selftests/landlock/fs_test.c +1 -0
+
       prepare_layout(_metadata);
 
       create_directory(_metadata, LOWER_BASE);

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

Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +4 -3
@@ -3431,11 +3471,18 @@ 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);

err = errno;

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

Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +2 -0
       clear_cap(_metadata, CAP_DAC_OVERRIDE);
       clear_cap(_metadata, CAP_SYS_ADMIN);
+
+     if (rc < 0) {

if (ret == -1) {

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

Quoted 1 line
tools/testing/selftests/landlock/fs_test.c +1 -0
+             ASSERT_EQ(ENODEV, errno);

ASSERT_EQ(ENODEV, err);

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

Quoted 3 lines
tools/testing/selftests/landlock/fs_test.c +3 -0
+             ASSERT_FALSE(supports_overlayfs());
+             SKIP(return, "overlayfs is not supported");
+     }

ASSERT_EQ(0, ret);

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

base-commit: 50cd95ac46548429e5bba7ca75cc97d11a697947