Skip to contentSkip to conversation

ovl: restrict pseudo filesystems as overlayfs layers

PATCH In review Amir Goldstein 2 participants 10 messages Nov 18

Subsystems (4)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

cover ovl: restrict pseudo filesystems as overlayfs layers

Christian,

These patches are based on vfs-7.5.pseudofs.fixes and replace the two patches I sent on top of "ovl: refuse data-only layers on pseudo filesystems".

Pseudo filesystems are refused as upper layer and allowed as lower layers only in legacy setups.

Thanks,
Amir.

Amir Goldstein (6):
ovl: do not change redirect_dir=follow to on in a non-upper mount
ovl: do not allow offline lower changes with data-only layers
ovl: refuse pseudo filesystem as upper layer
ovl: refuse pseudo lower layers unless offline lower changes are
allowed
selftests/ovl: test pseudo filesystem upper and lower layers selftests/ovl: use legacy setups for pseudo lower layer tests

 Documentation/filesystems/overlayfs.rst       |   7 +-
 fs/overlayfs/overlayfs.h                      |   4 +-
 fs/overlayfs/params.c                         |  19 +--
 fs/overlayfs/super.c                          |   9 +-
 .../overlayfs/automount_in_layer.c            |   6 +-
 .../filesystems/overlayfs/pseudo_datadir.c    | 111 ++++++++++++++++--
 6 files changed, 131 insertions(+), 25 deletions(-)
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 1/6 ovl: do not change redirect_dir=follow to on in a non-upper mount

In a non-upper mount, redirect_dir=follow was changed to redirect_dir=on to simplify the metacopy -> redirect_dir dependency check.

The two modes behave the same in a non-upper mount, because redirects are only created in upper, but the resulting mode is visible to users in mount options and to ovl_allow_offline_changes(), which treats redirect_dir=on as one of the features that do not allow offline changes to the lower layers.

Drop the conversion and let the metacopy dependency check accept redirect_dir=follow in a non-upper mount, where metacopy only needs to follow redirects.

---
 fs/overlayfs/params.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
fs/overlayfs/params.c +8 -8
index d1ca4a9094f28..0c7bfff7f06dc 100644
@@ -936,14 +936,12 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
 		config->redirect_mode = follow_mode;
 
 	/*
-	 * This is to make the logic below simpler.  It doesn't make any other
-	 * difference, since redirect_dir=on is only used for upper.
+	 * metacopy -> redirect_dir dependency.
+	 * metacopy needs to create redirects in upper and to follow redirects
+	 * in a non-upper mount.
 	 */
-	if (!config->upperdir && config->redirect_mode == OVL_REDIRECT_FOLLOW)
-		config->redirect_mode = OVL_REDIRECT_ON;
-
-	/* metacopy -> redirect_dir dependency */
-	if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON) {
+	if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON &&
+	    (config->upperdir || config->redirect_mode != OVL_REDIRECT_FOLLOW)) {
 		if (set.metacopy && set.redirect) {
 			pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
 			       ovl_redirect_mode_name(redirect_opt));
@@ -959,7 +957,9 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
 			config->metacopy = false;
 		} else {
 			/* Automatically enable redirect otherwise. */
-			config->redirect_mode = OVL_REDIRECT_ON;
+			config->redirect_mode = config->upperdir ?
+						OVL_REDIRECT_ON :
+						OVL_REDIRECT_FOLLOW;
 		}
 	}
 
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/6 ovl: do not allow offline lower changes with data-only layers

In reply to Amir Goldstein

Offline changes to the lower layers are allowed only if none of the features that depend on the lower layers being unchanged are used, e.g. metacopy=on.

Commit 5ef7bcdeecc9 ("ovl: relax redirect/metacopy requirements for lower -> data redirect") allowed data-only layers without metacopy=on. Data-only layers imply lower metacopy files, but without metacopy=on, ovl_allow_offline_changes() would still allow offline changes.

Use the presence of data-only layers as an indication that offline changes are not allowed.

The only practical implication of this change is that an unprivileged overlayfs mount with data-only layers and implicit metacopy will create a new upper directory with user.overlay.opaque xattr to be consistent with the behavior of privileged mounts with explicit metacopy=on.

Set ofs->numdatalayer before ovl_get_layers(), so that the check in ovl_lower_uuid_ok() is done with the number of data-only layers.

Fixes 5ef7bcdeecc9 ("ovl: relax redirect/metacopy requirements for lower -> data redirect")
---
 Documentation/filesystems/overlayfs.rst | 4 ++--
 fs/overlayfs/overlayfs.h                | 4 +++-
 fs/overlayfs/super.c                    | 3 ++-
 3 files changed, 7 insertions(+), 4 deletions(-)
Documentation/filesystems/overlayfs.rst +2 -2
index e01f7810c0271..688ab108f7478 100644
@@ -679,8 +679,8 @@ a crash or deadlock.
 
 Offline changes, when the overlay is not mounted, are allowed to the
 upper tree.  Offline changes to the lower tree are only allowed if the
-"metacopy", "index", "xino" and "redirect_dir" features
-have not been used.  If the lower tree is modified and any of these
+"metacopy", "index", "xino", "redirect_dir" and data-only lower layers
+features have not been used.  If the lower tree is modified and any of these
 features has been used, the behavior of the overlay is undefined,
 though it will not result in a crash or deadlock.
 
fs/overlayfs/overlayfs.h +3 -1
index 646f8ffc40aef..943291da8b7fe 100644
@@ -697,11 +697,13 @@ static inline bool ovl_is_volatile(struct ovl_config *config)
 /*
  * To avoid regressions in existing setups with overlay lower offline changes,
  * we allow lower changes only if none of the new features are used.
+ * Data-only layers imply lower metacopy files, even without metacopy=on.
  */
 static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs)
 {
 	return (!ofs->config.index && !ofs->config.metacopy &&
-		!ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs));
+		!ofs->numdatalayer && !ovl_redirect_dir(ofs) &&
+		!ovl_xino_warn(ofs));
 }
 
 /* All layers on same fs? */
fs/overlayfs/super.c +2 -1
index 990a470112c5c..58e4cc22d4de2 100644
@@ -1255,6 +1255,8 @@ static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
 		return ERR_PTR(-EINVAL);
 	}
 
+	/* Needed by ovl_allow_offline_changes() */
+	ofs->numdatalayer = ctx->nr_data;
 	err = -EINVAL;
 	for (i = 0; i < ctx->nr; i++) {
 		l = &ctx->lower[i];
@@ -1296,7 +1298,6 @@ static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
 		lowerstack[i].dentry = dget(l->path.dentry);
 		lowerstack[i].layer = &ofs->layers[i + 1];
 	}
-	ofs->numdatalayer = ctx->nr_data;
 
 	return oe;
 }
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 3/6 ovl: refuse pseudo filesystem as upper layer

In reply to Amir Goldstein

Using pseudo filesystems as overlayfs layers is a questionable practice. Copy up requires that the size of the lower file correctly represents the expected data size, which is not always the case with pseudo filesystems.

None of the pseudo filesystems can act as a functional upper layer. Most of them fail to create the work dir and the overlayfs mount falls back to read-only (e.g. sysfs, cgroup, debugfs). Others (bpffs, configfs, tracefs) create the work dir, but fail the mount later on. So let's stop pretending and explicitly deny pseudo fs as upper layer. Setups that used to fall back to a read-only mount now fail to mount.

---
 fs/overlayfs/params.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/overlayfs/params.c +2 -1
index 0c7bfff7f06dc..8da739b9fb883 100644
@@ -347,7 +347,8 @@ static int ovl_mount_dir_check(struct fs_context *fc, const struct path *path,
 	 * as the mount attributes could change.
 	 */
 	if (upper) {
-		if (path->dentry->d_flags & DCACHE_OP_REAL)
+		if ((path->dentry->d_flags & DCACHE_OP_REAL) ||
+		    (path->dentry->d_sb->s_iflags & SB_I_PSEUDO))
 			return invalfc(fc, "filesystem on %s not supported as upperdir", name);
 		if (__mnt_is_readonly(path->mnt))
 			return invalfc(fc, "filesystem on %s is read-only", name);
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH 4/6 ovl: refuse pseudo lower layers unless offline lower changes are allowed

In reply to Amir Goldstein

The content of pseudo filesystems is unstable by nature, so pseudo filesystems cannot be expected to behave as stable lower layers.

Overlayfs features such as metacopy, index, redirect_dir=on, xino=on and data-only layers do not allow offline changes to the lower layers.

Allow pseudo filesystems as lower layers only in legacy setups, where offline changes to the lower layers are allowed, to avoid regressions in existing setups, and refuse them along with any of the new features.

This generalizes the refusal of pseudo lower layers as data-only layers.

---
 Documentation/filesystems/overlayfs.rst | 3 +++
 fs/overlayfs/super.c                    | 6 +++---
 2 files changed, 6 insertions(+), 3 deletions(-)
Documentation/filesystems/overlayfs.rst +3 -0
index 688ab108f7478..235dc06c83a3c 100644
@@ -684,6 +684,9 @@ features have not been used.  If the lower tree is modified and any of these
 features has been used, the behavior of the overlay is undefined,
 though it will not result in a crash or deadlock.
 
+When offline changes to the lower tree are not allowed, the lower tree
+is also not allowed to be a pseudo kernel filesystem (e.g. sysfs).
+
 When the overlay NFS export feature is enabled, overlay filesystems
 behavior on offline changes of the underlying lower layer is different
 than the behavior when NFS export is disabled.
fs/overlayfs/super.c +3 -3
index 58e4cc22d4de2..cd4ad88ecaa06 100644
@@ -1261,10 +1261,10 @@ static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
 	for (i = 0; i < ctx->nr; i++) {
 		l = &ctx->lower[i];
 
-		/* Pseudo filesystems don't hold data for metacopy files. */
-		if (i >= ctx->nr - ctx->nr_data &&
+		/* Pseudo filesystems don't mix with new overlayfs features */
+		if (!ovl_allow_offline_changes(ofs) &&
 		    (l->path.dentry->d_sb->s_iflags & SB_I_PSEUDO)) {
-			pr_err("filesystem on %s not supported as data-only layer\n",
+			pr_err("pseudo filesystem on %s not supported with metacopy, index, redirect_dir=on, xino=on or data-only layers\n",
 			       l->name);
 			return ERR_PTR(-EINVAL);
 		}
Signature · 1 line
2.56.1
#5 lore raw

Re: [PATCH 4/6] ovl: refuse pseudo lower layers unless offline lower changes are allowed

Review

Quoted 5 lines · Amir Goldstein
(quoted diff) +2 -2
-		/* Pseudo filesystems don't hold data for metacopy files. */
-		if (i >= ctx->nr - ctx->nr_data &&
+		/* Pseudo filesystems don't mix with new overlayfs features */
+		if (!ovl_allow_offline_changes(ofs) &&
 		    (l->path.dentry->d_sb->s_iflags & SB_I_PSEUDO)) {

sashiko.dev <[email protected]>:

[Severity: Medium]
Does checking ovl_allow_offline_changes(ofs) here run before ofs->config.index has been finalized?

[...]

Because most pseudo filesystems do not implement export operations, evaluating ovl_allow_offline_changes(ofs) before those fallbacks run causes a regression where mounting with an upperdir over a pseudo lower layer fails with -EINVAL instead of falling back to index=off, and makes multi-layer mounts depend on whether a non-exportfs lower layer appears before or after the pseudo layer in lowerdir.

via: https://sashiko.dev/#/message/[email protected]

I think that's right. ovl_lower_dir(), ovl_get_fsid() and the !workdir case in ovl_fill_super_creds() all turn the index off.

So with index=on sysfs is refused when it comes first and accepted behind a lower layer without file handles.

Same for sashiko's 6/6 report.

So I would fold the following diff so we check the layers once they're set up but before ovl_init_uuid_xattr() and ovl_get_indexdir() write to upper. I can fold this into 4/6, plus tests into 6/6, if you agree:

fs/overlayfs/super.c +31 -8
index 2c08dc07bd149..4d803b16f3298 100644
@@ -1260,14 +1260,6 @@ static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
 	for (i = 0; i < ctx->nr; i++) {
 		l = &ctx->lower[i];
 
-		/* Pseudo filesystems don't mix with new overlayfs features */
-		if (!ovl_allow_offline_changes(ofs) &&
-		    (l->path.dentry->d_sb->s_iflags & SB_I_PSEUDO)) {
-			pr_err("pseudo filesystem on %s not supported with metacopy, index, redirect_dir=on, xino=on or data-only layers\n",
-			       l->name);
-			return ERR_PTR(-EINVAL);
-		}
-
 		err = ovl_lower_dir(l->name, &l->path, ofs, &sb->s_stack_depth);
 		if (err)
 			return ERR_PTR(err);
@@ -1374,6 +1366,28 @@ static int ovl_check_overlapping_layers(struct super_block *sb,
 	return 0;
 }
 
+/*
+ * Pseudo filesystems don't mix with new overlayfs features.  Check the layers
+ * only after setting them up, as that may have turned off the index.
+ */
+static int ovl_check_pseudo_layers(struct ovl_fs *ofs)
+{
+	unsigned int i;
+
+	if (ovl_allow_offline_changes(ofs))
+		return 0;
+
+	for (i = 1; i < ofs->numlayer; i++) {
+		if (ofs->layers[i].mnt->mnt_sb->s_iflags & SB_I_PSEUDO) {
+			pr_err("pseudo filesystem on %s not supported with metacopy, index, redirect_dir=on, xino=on or data-only layers\n",
+			       ofs->config.lowerdirs[i]);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
 static struct dentry *ovl_get_root(struct super_block *sb,
 				   struct dentry *upperdentry,
 				   struct ovl_entry *oe)
@@ -1538,6 +1552,15 @@ static int ovl_fill_super_creds(struct fs_context *fc, struct super_block *sb)
 	if (!ovl_upper_mnt(ofs))
 		sb->s_flags |= SB_RDONLY;
 
+	/* A mount without a workdir doesn't use the index */
+	if (!ofs->workdir)
+		ofs->config.index = false;
+
+	/* Before the uuid and the index dir are set up on the upper layer */
+	err = ovl_check_pseudo_layers(ofs);
+	if (err)
+		goto out_free_oe;
+
 	if (ovl_has_fsid(ofs) && ovl_upper_mnt(ofs)) {
 		/* Use per instance persistent uuid/fsid */
 		ovl_init_uuid_xattr(sb, ofs, &ctx->upper);
author maintainer
#6 lore raw

Re: [PATCH 4/6] ovl: refuse pseudo lower layers unless offline lower changes are allowed

Quoted 14 lines · Christian Brauner

Review

Quoted 5 lines · Amir Goldstein
(quoted diff) +2 -2
-             /* Pseudo filesystems don't hold data for metacopy files. */
-             if (i >= ctx->nr - ctx->nr_data &&
+             /* Pseudo filesystems don't mix with new overlayfs features */
+             if (!ovl_allow_offline_changes(ofs) &&
                  (l->path.dentry->d_sb->s_iflags & SB_I_PSEUDO)) {

sashiko.dev <[email protected]>:

[Severity: Medium]
Does checking ovl_allow_offline_changes(ofs) here run before ofs->config.index has been finalized?

Quoted 8 lines

[...]

Because most pseudo filesystems do not implement export operations, evaluating ovl_allow_offline_changes(ofs) before those fallbacks run causes a regression where mounting with an upperdir over a pseudo lower layer fails with -EINVAL instead of falling back to index=off, and makes multi-layer mounts depend on whether a non-exportfs lower layer appears before or after the pseudo layer in lowerdir.

This has the opposite effect of what I want to achieve with this patch set.

If I could ban pseudo layers completely I would have but I am afraid to cause a regression to some legacy use case.

index=on which falls back to index=off is not a legacy use case.

Thanks,
Amir.

#7 lore raw

Re: [PATCH 4/6] ovl: refuse pseudo lower layers unless offline lower changes are allowed

Quoted 24 lines · Amir Goldstein
Quoted 14 lines · Christian Brauner

Review

Quoted 5 lines · Amir Goldstein
(quoted diff) +2 -2
-             /* Pseudo filesystems don't hold data for metacopy files. */
-             if (i >= ctx->nr - ctx->nr_data &&
+             /* Pseudo filesystems don't mix with new overlayfs features */
+             if (!ovl_allow_offline_changes(ofs) &&
                  (l->path.dentry->d_sb->s_iflags & SB_I_PSEUDO)) {

sashiko.dev <[email protected]>:

[Severity: Medium]
Does checking ovl_allow_offline_changes(ofs) here run before ofs->config.index has been finalized?

Quoted 8 lines

[...]

Because most pseudo filesystems do not implement export operations, evaluating ovl_allow_offline_changes(ofs) before those fallbacks run causes a regression where mounting with an upperdir over a pseudo lower layer fails with -EINVAL instead of falling back to index=off, and makes multi-layer mounts depend on whether a non-exportfs lower layer appears before or after the pseudo layer in lowerdir.

Quoted 6 lines

This has the opposite effect of what I want to achieve with this patch set.

If I could ban pseudo layers completely I would have but I am afraid to cause a regression to some legacy use case.

index=on which falls back to index=off is not a legacy use case.

So it's stricter your way. Very nice.

author maintainer
#8 lore raw

PATCH 5/6 selftests/ovl: test pseudo filesystem upper and lower layers

In reply to Amir Goldstein

A pseudo filesystem lower layer is now refused along with features that do not allow offline lower changes, such as data-only layers and redirect_dir=on, and a pseudo filesystem is refused as upper layer.

Split the lowerdir_sysfs test, so that sysfs as a regular lower layer is still expected to mount, but sysfs as a lower layer along with a data-only layer is expected to fail.

Add a test that sysfs as a lower layer is refused with redirect_dir=on.

Add tests that sysfs as upperdir is refused via legacy mount options and via fsconfig by path and by file descriptor, and a tmpfs upperdir control test.

---
 .../filesystems/overlayfs/pseudo_datadir.c    | 103 +++++++++++++++++-
 1 file changed, 97 insertions(+), 6 deletions(-)
tools/testing/selftests/filesystems/overlayfs/pseudo_datadir.c +97 -6
index 3b59fd3e9f5c2..954a9f25cd1c5 100644
@@ -22,17 +22,19 @@ static int setup_tmp(void)
 	if (sys_mount("tmpfs", "/tmp", "tmpfs", 0, NULL))
 		return -errno;
 	if (mkdir("/tmp/lower", 0755) || mkdir("/tmp/data", 0755) ||
+	    mkdir("/tmp/upper", 0755) || mkdir("/tmp/work", 0755) ||
 	    mkdir("/tmp/merged", 0755))
 		return -errno;
 	return 0;
 }
 
-/* Mount an overlayfs with the legacy "lowerdir=" option. */
-static int mount_lowerdir(const char *lowerdir)
+/* Mount an overlayfs with the legacy "lowerdir=" option and extra options. */
+static int mount_lowerdir_opts(const char *lowerdir, const char *extra)
 {
 	char opts[256];
 
-	snprintf(opts, sizeof(opts), "lowerdir=%s", lowerdir);
+	snprintf(opts, sizeof(opts), "lowerdir=%s%s%s", lowerdir,
+		 extra ? "," : "", extra ? extra : "");
 	if (sys_mount("overlay", "/tmp/merged", "overlay", MS_RDONLY, opts))
 		return -errno;
 	if (umount2("/tmp/merged", 0))
@@ -40,6 +42,11 @@ static int mount_lowerdir(const char *lowerdir)
 	return 0;
 }
 
+static int mount_lowerdir(const char *lowerdir)
+{
+	return mount_lowerdir_opts(lowerdir, NULL);
+}
+
 static int set_layer(int fd_context, const char *key, const char *path,
 		     bool by_fd)
 {
@@ -60,6 +67,54 @@ static int set_layer(int fd_context, const char *key, const char *path,
 	return ret;
 }
 
+/* Mount an overlayfs with the legacy "upperdir=" and "workdir=" options. */
+static int mount_upperdir(const char *upper, const char *work)
+{
+	char opts[256];
+
+	snprintf(opts, sizeof(opts), "lowerdir=/tmp/lower,upperdir=%s,workdir=%s",
+		 upper, work);
+	if (sys_mount("overlay", "/tmp/merged", "overlay", 0, opts))
+		return -errno;
+	if (umount2("/tmp/merged", 0))
+		return -errno;
+	return 0;
+}
+
+/* Create an overlayfs with "upperdir" and "workdir" through fsconfig. */
+static int fsmount_upper(const char *upper, const char *work, bool by_fd)
+{
+	int fd_context, fd_mnt, ret;
+
+	fd_context = sys_fsopen("overlay", FSOPEN_CLOEXEC);
+	if (fd_context < 0)
+		return -errno;
+
+	ret = set_layer(fd_context, "lowerdir+", "/tmp/lower", by_fd);
+	if (ret)
+		goto out;
+	ret = set_layer(fd_context, "upperdir", upper, by_fd);
+	if (ret)
+		goto out;
+	ret = set_layer(fd_context, "workdir", work, by_fd);
+	if (ret)
+		goto out;
+
+	if (sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0)) {
+		ret = -errno;
+		goto out;
+	}
+
+	fd_mnt = sys_fsmount(fd_context, FSMOUNT_CLOEXEC, 0);
+	if (fd_mnt < 0)
+		ret = -errno;
+	else
+		close(fd_mnt);
+out:
+	close(fd_context);
+	return ret;
+}
+
 /* Create an overlayfs with "lowerdir+" and "datadir+" through fsconfig. */
 static int fsmount_layers(const char *lower, const char *data, bool by_fd)
 {
@@ -123,9 +178,45 @@ TEST(lowerdir_sysfs)
 	ASSERT_EQ(setup_tmp(), 0);
 
 	EXPECT_EQ(mount_lowerdir("/sys/kernel:/tmp/lower"), 0);
-	EXPECT_EQ(mount_lowerdir("/sys/kernel::/tmp/data"), 0);
-	EXPECT_EQ(fsmount_layers("/sys/kernel", "/tmp/data", false), 0);
-	EXPECT_EQ(fsmount_layers("/sys/kernel", "/tmp/data", true), 0);
+}
+
+TEST(lowerdir_sysfs_redirect_dir_on)
+{
+	ASSERT_EQ(setup_tmp(), 0);
+
+	EXPECT_EQ(mount_lowerdir_opts("/sys/kernel:/tmp/lower",
+				      "redirect_dir=on"), -EINVAL);
+}
+
+TEST(lowerdir_sysfs_with_datadir)
+{
+	ASSERT_EQ(setup_tmp(), 0);
+
+	EXPECT_EQ(mount_lowerdir("/sys/kernel::/tmp/data"), -EINVAL);
+	EXPECT_EQ(fsmount_layers("/sys/kernel", "/tmp/data", false), -EINVAL);
+	EXPECT_EQ(fsmount_layers("/sys/kernel", "/tmp/data", true), -EINVAL);
+}
+
+/*
+ * Without the check, sysfs as upperdir falls back to a read-only mount
+ * because the work dir cannot be created.
+ */
+TEST(upperdir_sysfs)
+{
+	ASSERT_EQ(setup_tmp(), 0);
+
+	EXPECT_EQ(mount_upperdir("/sys/block", "/sys/bus"), -EINVAL);
+	EXPECT_EQ(fsmount_upper("/sys/block", "/sys/bus", false), -EINVAL);
+	EXPECT_EQ(fsmount_upper("/sys/block", "/sys/bus", true), -EINVAL);
+}
+
+TEST(upperdir_tmpfs)
+{
+	ASSERT_EQ(setup_tmp(), 0);
+
+	EXPECT_EQ(mount_upperdir("/tmp/upper", "/tmp/work"), 0);
+	EXPECT_EQ(fsmount_upper("/tmp/upper", "/tmp/work", false), 0);
+	EXPECT_EQ(fsmount_upper("/tmp/upper", "/tmp/work", true), 0);
 }
 
 TEST_HARNESS_MAIN
Signature · 1 line
2.56.1
author maintainer
#9 lore raw

PATCH 6/6 selftests/ovl: use legacy setups for pseudo lower layer tests

In reply to Amir Goldstein

Pseudo filesystems are only allowed as overlayfs lower layers in legacy setups, where offline changes to the lower layers are allowed.

Kernel config defaults such as CONFIG_OVERLAY_FS_REDIRECT_DIR=y or CONFIG_OVERLAY_FS_METACOPY=y turn a lower-only mount into a non-legacy setup, so mounts of sysfs or debugfs lower layers that are expected to succeed would fail on such kernels.

Explicitly set redirect_dir=off,metacopy=off in the lowerdir_sysfs test and in the automount_in_layer fixture, to make them independent of the kernel config.

---
 .../selftests/filesystems/overlayfs/automount_in_layer.c  | 6 ++++--
 .../selftests/filesystems/overlayfs/pseudo_datadir.c      | 8 +++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)
tools/testing/selftests/filesystems/overlayfs/automount_in_layer.c +4 -2
index 0476c4582bdfc..c5bb12d524e5a 100644
@@ -82,7 +82,7 @@ FIXTURE(layer) {
 
 FIXTURE_SETUP(layer)
 {
-	char lower[PATH_LEN], other[PATH_LEN], ovl[PATH_LEN], opts[2 * PATH_LEN + 16];
+	char lower[PATH_LEN], other[PATH_LEN], ovl[PATH_LEN], opts[2 * PATH_LEN + 64];
 	struct fanotify_event_metadata *ev;
 	struct pollfd pfd;
 	char buf[4096];
@@ -111,7 +111,9 @@ FIXTURE_SETUP(layer)
 	ASSERT_EQ(mkdir(other, 0755), 0);
 	ASSERT_EQ(mkdir(ovl, 0755), 0);
 	ASSERT_EQ(mount("debugfs", lower, "debugfs", 0, NULL), 0);
-	snprintf(opts, sizeof(opts), "lowerdir=%s:%s", lower, other);
+	/* Pseudo lower layers are only allowed in legacy setups */
+	snprintf(opts, sizeof(opts),
+		 "lowerdir=%s:%s,redirect_dir=off,metacopy=off", lower, other);
 	ASSERT_EQ(mount("overlay", ovl, "overlay", MS_RDONLY, opts), 0);
 
 	self->fan = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK | FAN_CLOEXEC,
tools/testing/selftests/filesystems/overlayfs/pseudo_datadir.c +7 -1
index 954a9f25cd1c5..fbf32c3ea88c1 100644
@@ -173,11 +173,17 @@ TEST(datadir_tmpfs)
 	EXPECT_EQ(fsmount_layers("/tmp/lower", "/tmp/data", true), 0);
 }
 
+/*
+ * Pseudo lower layers are only allowed in legacy setups.  Explicitly turn off
+ * the features that may be enabled by kernel config defaults.
+ */
+#define LEGACY_OPTS "redirect_dir=off,metacopy=off"
+
 TEST(lowerdir_sysfs)
 {
 	ASSERT_EQ(setup_tmp(), 0);
 
-	EXPECT_EQ(mount_lowerdir("/sys/kernel:/tmp/lower"), 0);
+	EXPECT_EQ(mount_lowerdir_opts("/sys/kernel:/tmp/lower", LEGACY_OPTS), 0);
 }
 
 TEST(lowerdir_sysfs_redirect_dir_on)
Signature · 1 line
2.56.1