Skip to contentSkip to conversation

ovl: support idmapped overlay mounts

PATCH In review Christian Brauner 3 participants 16 messages Jun 15 – Oct 5

Subsystems (4)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover ovl: support idmapped overlay mounts

I've been asked about this feature multiple times and had actually implemented it right when I did the initial idmapped layer support. So here's a new version of this series.

This series lets the merged overlayfs mount itself be idmapped via mount_setattr(MOUNT_ATTR_IDMAP), in addition to the already supported idmapped lower and upper layers. It allows the same overlay tree to be exposed under a different ownership view, e.g. a container seeing the merged tree as 0:0 regardless of the on-disk or per-layer ids.

Overlayfs already normalizes every underlying id through the relevant layer idmap when it copies attributes into the overlay inode (ovl_copyattr()), so the overlay inode's i_{u,g}id are "overlay-final" ids. The overlay mount idmap O composes on top of that pivot:

    caller view  <--O-->  overlay-final id  <--layer idmap-->  on-disk

O is applied at the overlay-inode boundary only - permission, getattr, setattr, the ACL owner checks and inode_init_owner() - while the underlying layers keep being accessed with the mounter's credentials through their own, possibly idmapped, mounts. The mount idmap thus only changes how the caller sees the overlay inode and never widens the mounter's access to the layers: the second, mounter-credential check in ovl_permission() against the real inode stays on the layer idmap.

Most paths need no change because the VFS applies O to the overlay inode before overlayfs runs (sticky/rename/unlink/create gating) or after it returns at the syscall boundary (the POSIX ACL get/set translation). Overlayfs only acts where it bypasses the generic path:
getattr (it stats the real path), the setattr forward to the upper (the requested id is rebased from O into overlay-final space before being handed down), inode_init_owner() on create/tmpfile, and its own owner checks in ovl_set_acl().

FS_ALLOW_IDMAP is set last, once every operation honors O, so there is no bisection point where the mount is idmappable but an operation still ignores the idmapping.

Note that fstests will be broken by this temporarily as they rely on the ability to create whiteouts for unprivileged device node creation. That can be adjusted.

---
Christian Brauner (10):
ovl: handle idmapped mounts in ovl_create_object() and ovl_tmpfile()
ovl: handle idmapped mounts in ovl_permission()
ovl: handle idmapped mounts in ovl_setattr()
ovl: handle idmapped mounts in ovl_getattr()
ovl: handle idmapped mounts in ovl_set_acl()
ovl: allow idmapping overlay mounts
docs: document idmapped overlay mounts selftests/filesystems/overlayfs: fix set_layers_via_fds link error selftests/filesystems/overlayfs: test idmapped overlay mounts
ovl: document security.capability idmapping on the xattr forward paths

 Documentation/filesystems/overlayfs.rst            |  16 +
 fs/overlayfs/dir.c                                 |  16 +-
 fs/overlayfs/inode.c                               |  26 +-
 fs/overlayfs/overlayfs.h                           |   1 +
 fs/overlayfs/super.c                               |   2 +-
 fs/overlayfs/xattrs.c                              |   1 +
 .../selftests/filesystems/overlayfs/.gitignore     |   1 +
 .../selftests/filesystems/overlayfs/Makefile       |   2 +
 .../filesystems/overlayfs/idmapped_mounts.c        | 501 +++++++++++++++++++++
 .../filesystems/overlayfs/set_layers_via_fds.c     |  16 +-
 10 files changed, 560 insertions(+), 22 deletions(-)

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260615-work-idmapped-overlayfs-0b05e79bd216

author maintainer
#1 lore raw

PATCH 1/10 ovl: handle idmapped mounts in ovl_create_object() and ovl_tmpfile()

In preparation for allowing the overlay mount itself to be idmapped, thread the mount's struct mnt_idmap into the inode creation path and use it for inode_init_owner() instead of the hardcoded &nop_mnt_idmap.

The preallocated overlay inode's i_{u,g}id are copied into the override credentials by ovl_override_creator_creds() to create the real upper inode, so honoring the overlay mount idmap here makes newly created files, directories, special files, symlinks and tmpfiles get the caller's mapped fs{u,g}id once overlay mounts can be idmapped.

No functional change: until FS_ALLOW_IDMAP is set on ovl_fs_type the overlay mount idmap is always &nop_mnt_idmap, so inode_init_owner() behaves exactly as before.

---
 fs/overlayfs/dir.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
fs/overlayfs/dir.c +8 -8
index a033743dbf51..a7a393b04277 100644
@@ -689,8 +689,8 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 	return err;
 }
 
-static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
-			     const char *link)
+static int ovl_create_object(struct mnt_idmap *idmap, struct dentry *dentry,
+			     int mode, dev_t rdev, const char *link)
 {
 	int err;
 	struct inode *inode;
@@ -717,7 +717,7 @@ static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
 	inode_state_set(inode, I_CREATING);
 	spin_unlock(&inode->i_lock);
 
-	inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode);
+	inode_init_owner(idmap, inode, dentry->d_parent->d_inode, mode);
 	attr.mode = inode->i_mode;
 
 	err = ovl_create_or_link(dentry, inode, &attr, false);
@@ -734,13 +734,13 @@ static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir,
 		      struct dentry *dentry, umode_t mode, bool excl)
 {
-	return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
+	return ovl_create_object(idmap, dentry, (mode & 07777) | S_IFREG, 0, NULL);
 }
 
 static struct dentry *ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir,
 				struct dentry *dentry, umode_t mode)
 {
-	return ERR_PTR(ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL));
+	return ERR_PTR(ovl_create_object(idmap, dentry, (mode & 07777) | S_IFDIR, 0, NULL));
 }
 
 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
@@ -750,13 +750,13 @@ static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
 	if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
 		return -EPERM;
 
-	return ovl_create_object(dentry, mode, rdev, NULL);
+	return ovl_create_object(idmap, dentry, mode, rdev, NULL);
 }
 
 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,
 		       struct dentry *dentry, const char *link)
 {
-	return ovl_create_object(dentry, S_IFLNK, 0, link);
+	return ovl_create_object(idmap, dentry, S_IFLNK, 0, link);
 }
 
 static int ovl_set_link_redirect(struct dentry *dentry)
@@ -1444,7 +1444,7 @@ static int ovl_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
 	if (!inode)
 		goto drop_write;
 
-	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
+	inode_init_owner(idmap, inode, dir, mode);
 	err = ovl_create_tmpfile(file, dentry, inode, inode->i_mode);
 	if (err)
 		goto put_inode;
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/10 ovl: handle idmapped mounts in ovl_permission()

In reply to Christian Brauner

When the overlay mount is idmapped, the permission check on the overlay inode must account for the mount's idmapping. Use the struct mnt_idmap passed in by the VFS instead of the hardcoded &nop_mnt_idmap when checking the overlay inode against the caller's credentials.

The second check, which verifies that the mounter may access the underlying real inode, continues to use the real layer's idmap mnt_idmap(realpath.mnt) under the mounter's credentials and is deliberately left unchanged. The overlay mount idmap only affects how the caller views the overlay inode, not the mounter's access to the layers, so it cannot widen access to the real files.

No functional change until FS_ALLOW_IDMAP is set on ovl_fs_type; until then the overlay mount idmap is always &nop_mnt_idmap.

---
 fs/overlayfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/inode.c +1 -1
index 00c69707bda9..f59db57dfd55 100644
@@ -306,7 +306,7 @@ int ovl_permission(struct mnt_idmap *idmap,
 	 * Check overlay inode with the creds of task and underlying inode
 	 * with creds of mounter
 	 */
-	err = generic_permission(&nop_mnt_idmap, inode, mask);
+	err = generic_permission(idmap, inode, mask);
 	if (err)
 		return err;
 
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 3/10 ovl: handle idmapped mounts in ovl_setattr()

In reply to Christian Brauner

Pass the mount's struct mnt_idmap to setattr_prepare() so that the permission checks for a chown/chmod performed through an idmapped overlay mount are evaluated in the mount's id space.

The ownership requested in @attr is expressed relative to the overlay mount idmap. Before forwarding the change to the upper layer via ovl_do_notify_change() - whose notify_change() applies the upper layer idmap in turn - rebase ia_vfsuid/ia_vfsgid into the overlay's own id space, i.e. the same space as the overlay inode's i_{u,g}id established by ovl_copyattr(). Without this rebase the upper layer would interpret the caller's mount-relative id as an upper-relative one and store the wrong owner on disk, or reject it with -EOVERFLOW.

from_vfsuid() returns INVALID_UID for an id that the overlay mount idmap does not map; that invalid id is carried faithfully into the forwarded iattr and rejected by the upper notify_change() via vfsuid_has_fsmapping(), so no bogus owner can be written.

No functional change until FS_ALLOW_IDMAP is set on ovl_fs_type; until then the overlay mount idmap is &nop_mnt_idmap and from_vfsuid() is the identity.

---
 fs/overlayfs/inode.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/overlayfs/inode.c +9 -1
index f59db57dfd55..33734ca971e1 100644
@@ -26,10 +26,18 @@ int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	bool full_copy_up = false;
 	struct dentry *upperdentry;
 
-	err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
+	err = setattr_prepare(idmap, dentry, attr);
 	if (err)
 		return err;
 
+	/* Rebase ownership from the mount idmap into overlay id space. */
+	if (attr->ia_valid & ATTR_UID)
+		attr->ia_vfsuid = VFSUIDT_INIT(from_vfsuid(idmap,
+				i_user_ns(d_inode(dentry)), attr->ia_vfsuid));
+	if (attr->ia_valid & ATTR_GID)
+		attr->ia_vfsgid = VFSGIDT_INIT(from_vfsgid(idmap,
+				i_user_ns(d_inode(dentry)), attr->ia_vfsgid));
+
 	if (attr->ia_valid & ATTR_SIZE) {
 		/* Truncate should trigger data copy up as well */
 		full_copy_up = true;
Signature · 1 line
2.47.3
maintainer
#4 lore raw

Re: [PATCH 03/10] ovl: handle idmapped mounts in ovl_setattr()

Quoted 43 lines · Christian Brauner

Pass the mount's struct mnt_idmap to setattr_prepare() so that the permission checks for a chown/chmod performed through an idmapped overlay mount are evaluated in the mount's id space.

The ownership requested in @attr is expressed relative to the overlay mount idmap. Before forwarding the change to the upper layer via ovl_do_notify_change() - whose notify_change() applies the upper layer idmap in turn - rebase ia_vfsuid/ia_vfsgid into the overlay's own id space, i.e. the same space as the overlay inode's i_{u,g}id established by ovl_copyattr(). Without this rebase the upper layer would interpret the caller's mount-relative id as an upper-relative one and store the wrong owner on disk, or reject it with -EOVERFLOW.

from_vfsuid() returns INVALID_UID for an id that the overlay mount idmap does not map; that invalid id is carried faithfully into the forwarded iattr and rejected by the upper notify_change() via vfsuid_has_fsmapping(), so no bogus owner can be written.

No functional change until FS_ALLOW_IDMAP is set on ovl_fs_type; until then the overlay mount idmap is &nop_mnt_idmap and from_vfsuid() is the identity.

---
 fs/overlayfs/inode.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/overlayfs/inode.c +2 -1
index f59db57dfd55..33734ca971e1 100644
@@ -26,10 +26,18 @@ int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
        bool full_copy_up = false;
        struct dentry *upperdentry;
 
-       err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
+       err = setattr_prepare(idmap, dentry, attr);
        if (err)
                return err;
 
+       /* Rebase ownership from the mount idmap into overlay id space. */

fs/overlayfs/inode.c int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry, · line 43

Quoted 10 lines
fs/overlayfs/inode.c +7 -0
+       if (attr->ia_valid & ATTR_UID)
+               attr->ia_vfsuid = VFSUIDT_INIT(from_vfsuid(idmap,
+                               i_user_ns(d_inode(dentry)), attr->ia_vfsuid));
+       if (attr->ia_valid & ATTR_GID)
+               attr->ia_vfsgid = VFSGIDT_INIT(from_vfsgid(idmap,
+                               i_user_ns(d_inode(dentry)), attr->ia_vfsgid));
+
        if (attr->ia_valid & ATTR_SIZE) {
                /* Truncate should trigger data copy up as well */
                full_copy_up = true;

Won't insist on this as part of your work, but would be nice to take Joanne's patch [1] first and make this change in backing_inode_setattr()

In any case, Joanne, watch out!
Fuse already has FS_ALLOW_IDMAP, so your passthrough inode series must take this into account - now you have the template here ;)

Thanks,
Amir.

[1] https://lore.kernel.org/fuse-devel/[email protected]/

author maintainer
#5 lore raw

PATCH 4/10 ovl: handle idmapped mounts in ovl_getattr()

In reply to Christian Brauner

ovl_getattr() fetches attributes from the real (upper or lower) path via vfs_getattr(), so the returned stat->uid/stat->gid are already mapped through the real layer idmap but not through the overlay mount idmap. Unlike the generic path, the VFS does not re-apply the accessing mount's idmap after ->getattr returns - generic_fillattr() does it, but overlayfs bypasses it - so overlayfs has to do it itself.

Map stat->uid/stat->gid through the overlay mount idmap before returning, mirroring generic_fillattr(). The owner reported through an idmapped overlay mount is thus the overlay-final id translated by the mount idmap.

No functional change until FS_ALLOW_IDMAP is set on ovl_fs_type; until then make_vfsuid()/make_vfsgid() are the identity for &nop_mnt_idmap.

---
 fs/overlayfs/inode.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/overlayfs/inode.c +8 -0
index 33734ca971e1..45b2a5c3d978 100644
@@ -180,6 +180,8 @@ int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
 	int fsid = 0;
 	int err;
 	bool metacopy_blocks = false;
+	vfsuid_t vfsuid;
+	vfsgid_t vfsgid;
 
 	metacopy_blocks = ovl_is_metacopy_dentry(dentry);
 
@@ -292,6 +294,12 @@ int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
 	if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
 		stat->nlink = dentry->d_inode->i_nlink;
 
+	/* Map ownership of the real inode through the overlay mount idmap. */
+	vfsuid = make_vfsuid(idmap, i_user_ns(inode), stat->uid);
+	vfsgid = make_vfsgid(idmap, i_user_ns(inode), stat->gid);
+	stat->uid = vfsuid_into_kuid(vfsuid);
+	stat->gid = vfsgid_into_kgid(vfsgid);
+
 	return err;
 }
 
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

PATCH 5/10 ovl: handle idmapped mounts in ovl_set_acl()

In reply to Christian Brauner

The two checks ovl_set_acl() performs on the overlay inode itself - inode_owner_or_capable() and the setgid-stripping test - were done with &nop_mnt_idmap. On an idmapped overlay mount this compares the overlay inode's id space directly against the caller's: it denies the rightful owner (as seen through the mount idmap) the right to set an ACL, and evaluates the setgid-drop decision in the wrong id space, which can mis-set the mode on the upper inode.

Use the struct mnt_idmap passed in by the VFS for both. Fold the open-coded "caller not in group and not CAP_FSETID privileged" test into in_group_or_capable() with i_gid_into_vfsgid(), the same idmap-aware helpers used by setattr_should_drop_sgid().

The subsequent internal forced setgid-kill via ovl_setattr() stays on &nop_mnt_idmap: it carries only ATTR_KILL_SGID with no uid/gid to translate and is overlayfs' own mode change, not a user-driven operation through the mount.

No functional change until FS_ALLOW_IDMAP is set on ovl_fs_type.

---
 fs/overlayfs/inode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/overlayfs/inode.c +3 -3
index 45b2a5c3d978..57aa74de5bc4 100644
@@ -550,7 +550,7 @@ int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		return -EOPNOTSUPP;
 	if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
 		return acl ? -EACCES : 0;
-	if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
+	if (!inode_owner_or_capable(idmap, inode))
 		return -EPERM;
 
 	/*
@@ -558,8 +558,8 @@ int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 	 * be done with mounter's capabilities and so that won't do it for us).
 	 */
 	if (unlikely(inode->i_mode & S_ISGID) && type == ACL_TYPE_ACCESS &&
-	    !in_group_p(inode->i_gid) &&
-	    !capable_wrt_inode_uidgid(&nop_mnt_idmap, inode, CAP_FSETID)) {
+	    !in_group_or_capable(idmap, inode,
+				 i_gid_into_vfsgid(idmap, inode))) {
 		struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
 
 		err = ovl_setattr(&nop_mnt_idmap, dentry, &iattr);
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 6/10 ovl: allow idmapping overlay mounts

In reply to Christian Brauner

Now that every overlay inode operation honors the overlay mount idmap, allow the merged overlay mount itself to be idmapped by setting FS_ALLOW_IDMAP on ovl_fs_type. mount_setattr(MOUNT_ATTR_IDMAP) can then apply an idmapping to an overlay mount, exposing the merged tree under a different ownership view.

The composition is clean because overlayfs already normalizes every underlying id through the relevant layer idmap when it copies attributes into the overlay inode (ovl_copyattr()); the overlay inode's i_{u,g}id are thus "overlay-final" ids. The overlay mount idmap composes on top of that pivot: it is applied to (getattr) and removed from (setattr, create) those ids at the overlay-inode boundary - permission, getattr, setattr, ACL owner checks and inode_init_owner() - while the underlying layers keep being accessed with the mounter's credentials through their own, possibly idmapped, mounts. The mount idmap therefore only changes how the caller sees the overlay inode and never widens the mounter's access to the layers.

This is deliberately the final code patch of the series: only once every operation honors the mount idmap is it safe to make overlay mounts idmappable.

---
 fs/overlayfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/super.c +1 -1
index 60f0b7ceef0a..f2889cf9bc07 100644
@@ -1573,7 +1573,7 @@ struct file_system_type ovl_fs_type = {
 	.name			= "overlay",
 	.init_fs_context	= ovl_init_fs_context,
 	.parameters		= ovl_parameter_spec,
-	.fs_flags		= FS_USERNS_MOUNT,
+	.fs_flags		= FS_USERNS_MOUNT | FS_ALLOW_IDMAP,
 	.kill_sb		= kill_anon_super,
 };
 MODULE_ALIAS_FS("overlay");
Signature · 1 line
2.47.3
author maintainer
#8 lore raw

PATCH 7/10 docs: document idmapped overlay mounts

In reply to Christian Brauner

Describe that the merged overlay mount itself can be turned into an idmapped mount with mount_setattr(2), how the overlay mount idmapping composes with any layer idmappings, and that the mounter's access to the underlying layers is unaffected.

---
 Documentation/filesystems/overlayfs.rst | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Documentation/filesystems/overlayfs.rst +16 -0
index eb846518e6ac..1a29a5afabd7 100644
@@ -347,6 +347,22 @@ The resulting access permissions should be the same.  The difference is in
 the time of copy (on-demand vs. up-front).
 
 
+Idmapped mounts
+---------------
+
+The overlay mount itself can be turned into an idmapped mount by applying an
+idmapping to it with mount_setattr(2) and MOUNT_ATTR_IDMAP, just like for
+other filesystems that support idmapped mounts.
+
+The mount idmapping only changes how ownership and permissions of the overlay
+inodes are presented to and interpreted for the caller.  It does not change
+how overlayfs accesses the underlying layers: those are still accessed with
+the stashed mounter's credentials through their own mounts, which may
+themselves be idmapped.  The overlay mount idmapping and any layer idmapping
+compose, an underlying id is first mapped according to the relevant layer
+idmapping and then according to the overlay mount idmapping.
+
+
 Multiple lower layers
 ---------------------
 
Signature · 1 line
2.47.3
author maintainer
#9 lore raw

PATCH 8/10 selftests/filesystems/overlayfs: fix set_layers_via_fds link error

In reply to Christian Brauner

set_layers_via_fds.c calls open_tree() directly, but that wrapper is not provided by all C libraries, so the test fails to link with "undefined reference to open_tree" on toolchains without it.

Use sys_open_tree() from ../wrappers.h, the syscall wrapper already used for move_mount(), fsmount() and friends here. This is also a prerequisite for building the new idmapped_mounts test added to this directory.

---
 .../selftests/filesystems/overlayfs/set_layers_via_fds.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c +8 -8
index 3c0b93183348..7a293544233d 100644
@@ -624,7 +624,7 @@ TEST_F(set_layers_via_fds, set_layers_via_detached_mount_fds)
 
 	ASSERT_EQ(sys_move_mount(fd_tmpfs, "", -EBADF, "/set_layers_via_fds_tmpfs", MOVE_MOUNT_F_EMPTY_PATH), 0);
 
-	fd_tmp = open_tree(fd_tmpfs, "u", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	fd_tmp = sys_open_tree(fd_tmpfs, "u", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(fd_tmp, 0);
 
 	layer_fds[0] = openat(fd_tmp, "upper", O_CLOEXEC | O_DIRECTORY | O_PATH);
@@ -633,25 +633,25 @@ TEST_F(set_layers_via_fds, set_layers_via_detached_mount_fds)
 	layer_fds[1] = openat(fd_tmp, "work", O_CLOEXEC | O_DIRECTORY | O_PATH);
 	ASSERT_GE(layer_fds[1], 0);
 
-	layer_fds[2] = open_tree(fd_tmpfs, "l1", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[2] = sys_open_tree(fd_tmpfs, "l1", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[2], 0);
 
-	layer_fds[3] = open_tree(fd_tmpfs, "l2", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[3] = sys_open_tree(fd_tmpfs, "l2", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[3], 0);
 
-	layer_fds[4] = open_tree(fd_tmpfs, "l3", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[4] = sys_open_tree(fd_tmpfs, "l3", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[4], 0);
 
-	layer_fds[5] = open_tree(fd_tmpfs, "l4", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[5] = sys_open_tree(fd_tmpfs, "l4", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[5], 0);
 
-	layer_fds[6] = open_tree(fd_tmpfs, "d1", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[6] = sys_open_tree(fd_tmpfs, "d1", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[6], 0);
 
-	layer_fds[7] = open_tree(fd_tmpfs, "d2", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[7] = sys_open_tree(fd_tmpfs, "d2", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[7], 0);
 
-	layer_fds[8] = open_tree(fd_tmpfs, "d3", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+	layer_fds[8] = sys_open_tree(fd_tmpfs, "d3", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
 	ASSERT_GE(layer_fds[8], 0);
 
 	ASSERT_EQ(close(fd_tmpfs), 0);
Signature · 1 line
2.47.3
author maintainer
#10 lore raw

PATCH 9/10 selftests/filesystems/overlayfs: test idmapped overlay mounts

In reply to Christian Brauner

Add a selftest for idmapping the merged overlay mount itself. It applies an idmapping to a freshly created (still detached) overlay mount with mount_setattr(MOUNT_ATTR_IDMAP) and checks that:

- getattr reports ownership mapped through the mount idmap;
- a file created through the idmapped mount is stored on the upper

    layer with the corresponding overlay-final id;

- chown through the idmapped mount round-trips;
- an nfs_export overlay can be idmapped and decodable file handles round-trip through it with correctly mapped ownership.

The layers live on a private tmpfs and are owned by the host id so that they map to id 0 through the test idmapping, allowing the root caller to operate on them.

---
 .../selftests/filesystems/overlayfs/.gitignore     |   1 +
 .../selftests/filesystems/overlayfs/Makefile       |   2 +
 .../filesystems/overlayfs/idmapped_mounts.c        | 501 +++++++++++++++++++++
 3 files changed, 504 insertions(+)
tools/testing/selftests/filesystems/overlayfs/.gitignore +1 -0
index e23a18c8b37f..077f7a128168 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 dev_in_maps
 set_layers_via_fds
+idmapped_mounts
tools/testing/selftests/filesystems/overlayfs/Makefile +2 -0
index d3ad4a77db9b..b3185f684add 100644
@@ -8,7 +8,9 @@ LOCAL_HDRS += ../wrappers.h log.h
 
 TEST_GEN_PROGS := dev_in_maps
 TEST_GEN_PROGS += set_layers_via_fds
+TEST_GEN_PROGS += idmapped_mounts
 
 include ../../lib.mk
 
 $(OUTPUT)/set_layers_via_fds: ../utils.c
+$(OUTPUT)/idmapped_mounts: ../utils.c
tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c new file +501 -0

501 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#11 lore raw

Re: [PATCH 09/10] selftests/filesystems/overlayfs: test idmapped overlay mounts

Quoted 138 lines · Christian Brauner

Add a selftest for idmapping the merged overlay mount itself. It applies an idmapping to a freshly created (still detached) overlay mount with mount_setattr(MOUNT_ATTR_IDMAP) and checks that:

- getattr reports ownership mapped through the mount idmap;
- a file created through the idmapped mount is stored on the upper

    layer with the corresponding overlay-final id;

- chown through the idmapped mount round-trips;
- an nfs_export overlay can be idmapped and decodable file handles round-trip through it with correctly mapped ownership.

The layers live on a private tmpfs and are owned by the host id so that they map to id 0 through the test idmapping, allowing the root caller to operate on them.

---
 .../selftests/filesystems/overlayfs/.gitignore     |   1 +
 .../selftests/filesystems/overlayfs/Makefile       |   2 +
 .../filesystems/overlayfs/idmapped_mounts.c        | 501 +++++++++++++++++++++
 3 files changed, 504 insertions(+)
tools/testing/selftests/filesystems/overlayfs/.gitignore +1 -0
index e23a18c8b37f..077f7a128168 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 dev_in_maps
 set_layers_via_fds
+idmapped_mounts
tools/testing/selftests/filesystems/overlayfs/Makefile +2 -0
index d3ad4a77db9b..b3185f684add 100644
@@ -8,7 +8,9 @@ LOCAL_HDRS += ../wrappers.h log.h
 
 TEST_GEN_PROGS := dev_in_maps
 TEST_GEN_PROGS += set_layers_via_fds
+TEST_GEN_PROGS += idmapped_mounts
 
 include ../../lib.mk
 
 $(OUTPUT)/set_layers_via_fds: ../utils.c
+$(OUTPUT)/idmapped_mounts: ../utils.c
tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c new file +86 -0
new file mode 100644
index 000000000000..44a75839f4ed
@@ -0,0 +1,501 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+
+#include <fcntl.h>
+#include <limits.h>
+#include <sched.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <linux/mount.h>
+#include <linux/types.h>
+
+#include "kselftest_harness.h"
+#include "../wrappers.h"
+#include "../utils.h"
+
+/*
+ * An idmapping that maps the mount-visible id range [0, ID_RANGE) onto the
+ * host/overlay-final id range [ID_HOST, ID_HOST + ID_RANGE).  Through such an
+ * idmapped overlay mount, an overlay-final id of ID_HOST + n is reported as n,
+ * and an id of n requested through the mount is stored as ID_HOST + n.
+ */
+#define ID_NS   0
+#define ID_HOST         10000
+#define ID_RANGE 10000
+
+/*
+ * For the composition test the lower layer's on-disk ids live in a
+ * separate range and are mapped by an idmapped lower layer onto the
+ * overlay-final range [ID_HOST, ID_HOST + ID_RANGE).
+ */
+#define LAYER_HOST 20000
+
+#ifndef MOUNT_ATTR_IDMAP
+#define MOUNT_ATTR_IDMAP 0x00100000
+#endif
+
+#ifndef __NR_mount_setattr
+#define __NR_mount_setattr 442
+#endif
+
+static inline int sys_mount_setattr(int dfd, const char *path,
+                                   unsigned int flags,
+                                   struct mount_attr *attr, size_t size)
+{
+       return syscall(__NR_mount_setattr, dfd, path, flags, attr, size);
+}
+
+static bool ovl_supported(void)
+{
+       int fd = sys_fsopen("overlay", 0);
+
+       if (fd < 0)
+               return false;
+       close(fd);
+       return true;
+}
+
+/* base/{l,u,w} owned by ID_HOST so they map to ID_NS through the idmap. */
+static int setup_layers(const char *base)
+{
+       static const char *sub[] = { "", "/l", "/u", "/w" };
+       char path[PATH_MAX];
+
+       for (size_t i = 0; i < ARRAY_SIZE(sub); i++) {
+               snprintf(path, sizeof(path), "%s%s", base, sub[i]);
+               if (mkdir(path, 0755) && errno != EEXIST)
+                       return -1;
+               if (i && chown(path, ID_HOST, ID_HOST))
+                       return -1;
+       }
+       return 0;
+}
+
+static int ovl_mount(const char *base, bool nfs_export)
+{
+       char lower[PATH_MAX], upper[PATH_MAX], work[PATH_MAX];
+       int fsfd, ovl;
+
+       snprintf(lower, sizeof(lower), "%s/l", base);
+       snprintf(upper, sizeof(upper), "%s/u", base);
+       snprintf(work, sizeof(work), "%s/w", base);
+
+       fsfd = sys_fsopen("overlay", 0);

tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c line 96

Quoted 10 lines
tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c new file +10 -0
+       if (fsfd < 0)
+               return -1;
+
+       if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) ||
+           sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", lower, 0) ||
+           sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", upper, 0) ||
+           sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", work, 0))
+               goto err;
+       if (nfs_export &&
+           (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "index", "on", 0) ||

index is implied by nfs_export.

Quoted 383 lines
tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c new file +383 -0
+            sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "nfs_export", "on", 0)))
+               goto err;
+       if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0))
+               goto err;
+
+       ovl = sys_fsmount(fsfd, 0, 0);
+       close(fsfd);
+       return ovl;
+err:
+       close(fsfd);
+       return -1;
+}
+
+/* Idmap the (still detached, not yet visible) overlay mount @mfd. */
+static int ovl_idmap(int mfd)
+{
+       struct mount_attr attr = {
+               .attr_set = MOUNT_ATTR_IDMAP,
+       };
+       int ret, userns_fd;
+
+       /*
+        * get_userns_fd(fs_id, mount_id, range): a file whose filesystem id
+        * is fs_id + n is shown through the idmapped mount as mount_id + n.
+        * Here the overlay-final (fs side) range is [ID_HOST, ..) and the
+        * caller-visible (mount side) range is [ID_NS, ..).
+        */
+       userns_fd = get_userns_fd(ID_HOST, ID_NS, ID_RANGE);
+       if (userns_fd < 0)
+               return -1;
+
+       attr.userns_fd = userns_fd;
+       ret = sys_mount_setattr(mfd, "", AT_EMPTY_PATH, &attr, sizeof(attr));
+       close(userns_fd);
+       return ret;
+}
+
+/* Clone @path into a detached, idmapped mount usable as an overlay layer. */
+static int idmapped_layer_fd(const char *path, int nsid, int hostid, int range)
+{
+       struct mount_attr attr = {
+               .attr_set = MOUNT_ATTR_IDMAP,
+       };
+       int fd_tree, userns_fd;
+
+       fd_tree = sys_open_tree(AT_FDCWD, path,
+                              OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
+       if (fd_tree < 0)
+               return -1;
+       userns_fd = get_userns_fd(nsid, hostid, range);
+       if (userns_fd < 0) {
+               close(fd_tree);
+               return -1;
+       }
+       attr.userns_fd = userns_fd;
+       if (sys_mount_setattr(fd_tree, "", AT_EMPTY_PATH, &attr,
+                             sizeof(attr))) {
+               close(userns_fd);
+               close(fd_tree);
+               return -1;
+       }
+       close(userns_fd);
+       return fd_tree;
+}
+
+/* Overlay with a layer passed by fd (idmapped) plus a plain upper/work. */
+static int ovl_mount_lower_fd(const char *upper, const char *work, int fd_lower)
+{
+       int fsfd, ovl;
+
+       fsfd = sys_fsopen("overlay", 0);
+       if (fsfd < 0)
+               return -1;
+
+       if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) ||
+           sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", upper, 0) ||
+           sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", work, 0) ||
+           sys_fsconfig(fsfd, FSCONFIG_SET_FD, "lowerdir+", NULL, fd_lower) ||
+           sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0))
+               goto err;
+
+       ovl = sys_fsmount(fsfd, 0, 0);
+       close(fsfd);
+       return ovl;
+err:
+       close(fsfd);
+       return -1;
+}
+
+/*
+ * Mount an overlay inside user namespace @u1 (so the overlay sb's s_user_ns is
+ * not the initial namespace) and idmap that overlay mount with @u2.  Runs in a
+ * child that joins @u1; returns 0 on success.
+ */
+static int userns_overlay_child(int u1)
+{
+       struct mount_attr attr = {
+               .attr_set = MOUNT_ATTR_IDMAP,
+       };
+       struct stat st;
+       int ovl, u2;
+
+       /* Become root in the overlay sb's user namespace u1. */
+       if (!switch_userns(u1, 0, 0, false))
+               return fprintf(stderr, "userns: switch_userns: %m\n"), -1;
+       if (unshare(CLONE_NEWNS) ||
+           sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL))
+               return fprintf(stderr, "userns: unshare/slave: %m\n"), -1;
+       if (sys_mount("tmpfs", "/tmp", "tmpfs", 0, NULL))
+               return fprintf(stderr, "userns: mount tmpfs: %m\n"), -1;
+       if (setup_layers("/tmp/ovl"))
+               return fprintf(stderr, "userns: setup_layers: %m\n"), -1;
+       if (mknod("/tmp/ovl/l/file", S_IFREG | 0644, 0) ||
+           chown("/tmp/ovl/l/file", ID_HOST + 5, ID_HOST + 5))
+               return fprintf(stderr, "userns: lower file: %m\n"), -1;
+
+       ovl = ovl_mount("/tmp/ovl", false);
+       if (ovl < 0)
+               return fprintf(stderr, "userns: ovl_mount: %m\n"), -1;
+
+       /*
+        * mount_setattr() requires CAP_SYS_ADMIN over the idmap user
+        * namespace, so it must be a child of u1.  Create it now, from
+        * inside u1.
+        */
+       u2 = get_userns_fd(ID_HOST, ID_NS, ID_RANGE);
+       if (u2 < 0)
+               return fprintf(stderr, "userns: get_userns_fd: %m\n"), -1;
+       attr.userns_fd = u2;
+       if (sys_mount_setattr(ovl, "", AT_EMPTY_PATH, &attr, sizeof(attr)))
+               return fprintf(stderr, "userns: mount_setattr: %m\n"), -1;
+       close(u2);
+
+       if (fstatat(ovl, "file", &st, 0))
+               return fprintf(stderr, "userns: fstatat: %m\n"), -1;
+       if (st.st_uid != ID_NS + 5 || st.st_gid != ID_NS + 5) {
+               fprintf(stderr, "userns: got %u:%u expected %u:%u\n",
+                       st.st_uid, st.st_gid, ID_NS + 5, ID_NS + 5);
+               return -1;
+       }
+       return 0;
+}
+
+FIXTURE(idmapped_overlay) {
+       char base[64];
+};
+
+FIXTURE_SETUP(idmapped_overlay)
+{
+       /* Private mount namespace so test mounts need no cleanup. */
+       ASSERT_EQ(unshare(CLONE_NEWNS), 0);
+       ASSERT_EQ(sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL), 0);
+
+       /* tmpfs for the layers so we can chown them to arbitrary ids. */
+       ASSERT_EQ(sys_mount("tmpfs", "/tmp", "tmpfs", 0, NULL), 0);
+
+       snprintf(self->base, sizeof(self->base), "/tmp/ovl");
+       ASSERT_EQ(setup_layers(self->base), 0);
+}
+
+FIXTURE_TEARDOWN(idmapped_overlay)
+{
+}
+
+/* A file owned by ID_HOST + 5 is reported as ID_NS + 5 through the idmap. */
+TEST_F(idmapped_overlay, getattr)
+{
+       char path[PATH_MAX];
+       struct stat st;
+       int ovl;
+
+       if (!ovl_supported())
+               SKIP(return, "overlayfs not supported");
+
+       snprintf(path, sizeof(path), "%s/l/file", self->base);
+       ASSERT_EQ(mknod(path, S_IFREG | 0644, 0), 0);
+       ASSERT_EQ(chown(path, ID_HOST + 5, ID_HOST + 5), 0);
+
+       ovl = ovl_mount(self->base, false);
+       ASSERT_GE(ovl, 0);
+       ASSERT_EQ(ovl_idmap(ovl), 0);
+
+       ASSERT_EQ(fstatat(ovl, "file", &st, 0), 0);
+       EXPECT_EQ(st.st_uid, ID_NS + 5);
+       EXPECT_EQ(st.st_gid, ID_NS + 5);
+
+       EXPECT_EQ(close(ovl), 0);
+}
+
+/*
+ * Every creation path initializes the new owner through the mount idmap:
+ * created as caller id ID_NS, stored on the upper layer as overlay-final
+ * ID_HOST.  Covers ovl_create() (regular file), ovl_mkdir(), ovl_mknod()
+ * and ovl_symlink() (which share ovl_create_object()), plus the separate
+ * ovl_tmpfile() path.
+ */
+TEST_F(idmapped_overlay, create)
+{
+       static const char *names[] = { "reg", "dir", "fifo", "lnk" };
+       char path[PATH_MAX];
+       struct stat st;
+       int ovl, fd;
+
+       if (!ovl_supported())
+               SKIP(return, "overlayfs not supported");
+
+       ovl = ovl_mount(self->base, false);
+       ASSERT_GE(ovl, 0);
+       ASSERT_EQ(ovl_idmap(ovl), 0);
+
+       /* One object per creation operation, all as caller id ID_NS. */
+       fd = openat(ovl, "reg", O_CREAT | O_WRONLY | O_EXCL, 0644);
+       ASSERT_GE(fd, 0);
+       EXPECT_EQ(close(fd), 0);
+       ASSERT_EQ(mkdirat(ovl, "dir", 0755), 0);
+       ASSERT_EQ(mknodat(ovl, "fifo", S_IFIFO | 0644, 0), 0);
+       ASSERT_EQ(symlinkat("target", ovl, "lnk"), 0);
+
+       for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
+               /* Reported as ID_NS through the idmapped mount ... */
+               ASSERT_EQ(fstatat(ovl, names[i], &st, AT_SYMLINK_NOFOLLOW), 0);
+               EXPECT_EQ(st.st_uid, ID_NS);
+               EXPECT_EQ(st.st_gid, ID_NS);
+               /* ... and stored as ID_HOST on the upper layer. */
+               snprintf(path, sizeof(path), "%s/u/%s", self->base, names[i]);
+               ASSERT_EQ(lstat(path, &st), 0);
+               EXPECT_EQ(st.st_uid, ID_HOST);
+               EXPECT_EQ(st.st_gid, ID_HOST);
+       }
+
+       /* O_TMPFILE goes through the separate ovl_tmpfile() path. */
+       fd = openat(ovl, ".", O_TMPFILE | O_WRONLY, 0644);
+       ASSERT_GE(fd, 0);
+       /* Inside the mount: caller id ID_NS. */
+       ASSERT_EQ(fstat(fd, &st), 0);
+       EXPECT_EQ(st.st_uid, ID_NS);
+       EXPECT_EQ(st.st_gid, ID_NS);
+       /* Link it in so the upper backing file can be inspected too. */
+       ASSERT_EQ(linkat(fd, "", ovl, "tmp", AT_EMPTY_PATH), 0);
+       EXPECT_EQ(close(fd), 0);
+       snprintf(path, sizeof(path), "%s/u/tmp", self->base);
+       ASSERT_EQ(lstat(path, &st), 0);
+       EXPECT_EQ(st.st_uid, ID_HOST);
+       EXPECT_EQ(st.st_gid, ID_HOST);
+
+       EXPECT_EQ(close(ovl), 0);
+}
+
+/* chown through the idmapped mount round-trips: ID_NS + 5 <-> ID_HOST + 5. */
+TEST_F(idmapped_overlay, chown)
+{
+       char path[PATH_MAX];
+       struct stat st;
+       int ovl, fd;
+
+       if (!ovl_supported())
+               SKIP(return, "overlayfs not supported");
+
+       ovl = ovl_mount(self->base, false);
+       ASSERT_GE(ovl, 0);
+       ASSERT_EQ(ovl_idmap(ovl), 0);
+
+       fd = openat(ovl, "f", O_CREAT | O_WRONLY | O_EXCL, 0644);
+       ASSERT_GE(fd, 0);
+       EXPECT_EQ(close(fd), 0);
+
+       ASSERT_EQ(fchownat(ovl, "f", ID_NS + 5, ID_NS + 5, 0), 0);
+
+       ASSERT_EQ(fstatat(ovl, "f", &st, 0), 0);
+       EXPECT_EQ(st.st_uid, ID_NS + 5);
+       EXPECT_EQ(st.st_gid, ID_NS + 5);
+
+       snprintf(path, sizeof(path), "%s/u/f", self->base);
+       ASSERT_EQ(stat(path, &st), 0);
+       EXPECT_EQ(st.st_uid, ID_HOST + 5);
+       EXPECT_EQ(st.st_gid, ID_HOST + 5);
+
+       EXPECT_EQ(close(ovl), 0);
+}
+
+/*
+ * Composition: an idmapped lower layer underneath an idmapped overlay mount.
+ * An on-disk id is mapped by the layer idmap into the overlay-final range and
+ * then by the mount idmap into the caller's range:
+ *
+ *   on-disk LAYER_HOST+7  --layer-->  ID_HOST+7  --mount-->  ID_NS+7
+ */
+TEST_F(idmapped_overlay, composition)
+{
+       char lower[PATH_MAX], upper[PATH_MAX], work[PATH_MAX], path[PATH_MAX];
+       struct stat st;
+       int ovl, fd_lower;
+
+       if (!ovl_supported())
+               SKIP(return, "overlayfs not supported");
+
+       snprintf(lower, sizeof(lower), "%s/l", self->base);
+       snprintf(upper, sizeof(upper), "%s/u", self->base);
+       snprintf(work, sizeof(work), "%s/w", self->base);
+
+       /* Put the lower layer's ids in the on-disk [LAYER_HOST, ..) range. */
+       ASSERT_EQ(chown(lower, LAYER_HOST, LAYER_HOST), 0);
+       snprintf(path, sizeof(path), "%s/l/file", self->base);
+       ASSERT_EQ(mknod(path, S_IFREG | 0644, 0), 0);
+       ASSERT_EQ(chown(path, LAYER_HOST + 7, LAYER_HOST + 7), 0);
+
+       /* Idmapped lower: on-disk LAYER_HOST <-> overlay-final ID_HOST. */
+       fd_lower = idmapped_layer_fd(lower, LAYER_HOST, ID_HOST, ID_RANGE);
+       ASSERT_GE(fd_lower, 0);
+
+       ovl = ovl_mount_lower_fd(upper, work, fd_lower);
+       ASSERT_GE(ovl, 0);
+       EXPECT_EQ(close(fd_lower), 0);
+
+       /* Idmap the overlay mount: overlay-final ID_HOST <-> caller ID_NS. */
+       ASSERT_EQ(ovl_idmap(ovl), 0);
+
+       ASSERT_EQ(fstatat(ovl, "file", &st, 0), 0);
+       EXPECT_EQ(st.st_uid, ID_NS + 7);
+       EXPECT_EQ(st.st_gid, ID_NS + 7);
+
+       EXPECT_EQ(close(ovl), 0);
+}
+
+/* An idmapped overlay mount whose sb lives inside a user namespace. */
+TEST_F(idmapped_overlay, userns)
+{
+       int u1;
+       pid_t pid;
+
+       if (!ovl_supported())
+               SKIP(return, "overlayfs not supported");
+
+       /* u1 backs the overlay sb: identity-mapped, but not the init ns. */
+       u1 = get_userns_fd(0, 0, 65536);
+       if (u1 < 0)
+               SKIP(return, "user namespaces not available");
+
+       pid = fork();
+       ASSERT_GE(pid, 0);
+       if (pid == 0) {
+               int ret = userns_overlay_child(u1);
+
+               _exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
+       }
+       EXPECT_EQ(wait_for_pid(pid), 0);
+
+       EXPECT_EQ(close(u1), 0);
+}
+
+/*
+ * An nfs_export overlay can be idmapped, and decodable file handles round-trip
+ * through the idmapped mount with correctly mapped ownership.  Overlay file
+ * handles encode object identity, not ownership, so the mount idmap does not
+ * affect them; it only maps the owner reported once a handle is reopened.
+ */
+TEST_F(idmapped_overlay, nfs_export_handles)
+{
+       char path[PATH_MAX], mnt[128];
+       union {
+               struct file_handle fh;
+               char buf[sizeof(struct file_handle) + MAX_HANDLE_SZ];
+       } fhu;
+       struct file_handle *fh = &fhu.fh;
+       struct stat st;
+       int ovl, mfd, fd, mount_id;
+
+       if (!ovl_supported())
+               SKIP(return, "overlayfs not supported");
+
+       snprintf(path, sizeof(path), "%s/l/file", self->base);
+       ASSERT_EQ(mknod(path, S_IFREG | 0644, 0), 0);
+       ASSERT_EQ(chown(path, ID_HOST + 7, ID_HOST + 7), 0);
+
+       /* nfs_export=on gives decodable overlay file handles. */
+       ovl = ovl_mount(self->base, true);
+       if (ovl < 0)
+               SKIP(return, "overlayfs nfs_export not supported");
+       ASSERT_EQ(ovl_idmap(ovl), 0);
+
+       /* Attach the idmapped mount so handles can be resolved against it. */
+       snprintf(mnt, sizeof(mnt), "%s/mnt", self->base);
+       ASSERT_EQ(mkdir(mnt, 0755), 0);

tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c line 489

Quoted 10 lines
tools/testing/selftests/filesystems/overlayfs/idmapped_mounts.c new file +10 -0
+       ASSERT_EQ(sys_move_mount(ovl, "", AT_FDCWD, mnt,
+                                MOVE_MOUNT_F_EMPTY_PATH), 0);
+
+       snprintf(path, sizeof(path), "%s/file", mnt);
+       fh->handle_bytes = MAX_HANDLE_SZ;
+       ASSERT_EQ(name_to_handle_at(AT_FDCWD, path, fh, &mount_id, 0), 0);
+
+       mfd = open(mnt, O_RDONLY | O_DIRECTORY);
+       ASSERT_GE(mfd, 0);
+       fd = open_by_handle_at(mfd, fh, O_RDONLY);

This is likely exercising the code path ovl_lookup_inode() => d_find_any_alias() I think it is more interesting for this patch set to exercising the code path of ovl_obtain_alias() via ovl mount cycle or something.

Thanks,
Amir.

author maintainer
#12 lore raw

PATCH 10/10 ovl: document security.capability idmapping on the xattr forward paths

In reply to Christian Brauner

Now that an overlay mount can itself be idmapped, every id exposed at the overlay-inode boundary is mapped through the overlay mount idmap. security.capability is the one id-bearing xattr that overlayfs does not translate in its own boundary code: the embedded rootid of a v3 fscap is instead mapped by the capability LSM inside vfs_getxattr() and vfs_setxattr().

It still composes correctly only because the xattr read and write forwards go through the security-aware vfs_getxattr() / vfs_setxattr() rather than the raw __vfs_*xattr() variants: commoncap maps the rootid through the layer idmap at the overlay-to-real forward, while the overlay mount idmap is applied by the outer vfs_getxattr() at the syscall boundary, mirroring the layer-then-mount composition used by ovl_getattr().

The raw __vfs_*xattr() variants skip the security hooks and would silently drop the rootid mapping. Comment both forwards - the read in ovl_xattr_get() and the write in ovl_do_setxattr() - so they are not converted to them by accident.

No functional change.

---
 fs/overlayfs/overlayfs.h | 1 +
 fs/overlayfs/xattrs.c    | 1 +
 2 files changed, 2 insertions(+)
fs/overlayfs/overlayfs.h +1 -0
index b75df37f70ac..e0d8c6152e9f 100644
@@ -320,6 +320,7 @@ static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
 				  const char *name, const void *value,
 				  size_t size, int flags)
 {
+	/* Use vfs_setxattr(), not __vfs_setxattr(): it idmaps the security.capability rootid. */
 	int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name,
 			       value, size, flags);
 
fs/overlayfs/xattrs.c +1 -0
index aa95855c7023..811c94d2d9e9 100644
@@ -84,6 +84,7 @@ static int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char
 	struct path realpath;
 
 	ovl_i_path_real(inode, &realpath);
+	/* Use vfs_getxattr(), not __vfs_getxattr(): it idmaps the security.capability rootid. */
 	with_ovl_creds(dentry->d_sb)
 		return vfs_getxattr(mnt_idmap(realpath.mnt), realpath.dentry, name, value, size);
 }
Signature · 1 line
2.47.3
maintainer Reviewed-by on the v1 series
#13 lore raw
In reply to Christian Brauner
Quoted 40 lines · Christian Brauner

I've been asked about this feature multiple times and had actually implemented it right when I did the initial idmapped layer support. So here's a new version of this series.

This series lets the merged overlayfs mount itself be idmapped via mount_setattr(MOUNT_ATTR_IDMAP), in addition to the already supported idmapped lower and upper layers. It allows the same overlay tree to be exposed under a different ownership view, e.g. a container seeing the merged tree as 0:0 regardless of the on-disk or per-layer ids.

Overlayfs already normalizes every underlying id through the relevant layer idmap when it copies attributes into the overlay inode (ovl_copyattr()), so the overlay inode's i_{u,g}id are "overlay-final" ids. The overlay mount idmap O composes on top of that pivot:

    caller view  <--O-->  overlay-final id  <--layer idmap-->  on-disk

O is applied at the overlay-inode boundary only - permission, getattr, setattr, the ACL owner checks and inode_init_owner() - while the underlying layers keep being accessed with the mounter's credentials through their own, possibly idmapped, mounts. The mount idmap thus only changes how the caller sees the overlay inode and never widens the mounter's access to the layers: the second, mounter-credential check in ovl_permission() against the real inode stays on the layer idmap.

Most paths need no change because the VFS applies O to the overlay inode before overlayfs runs (sticky/rename/unlink/create gating) or after it returns at the syscall boundary (the POSIX ACL get/set translation). Overlayfs only acts where it bypasses the generic path:
getattr (it stats the real path), the setattr forward to the upper (the requested id is rebased from O into overlay-final space before being handed down), inode_init_owner() on create/tmpfile, and its own owner checks in ovl_set_acl().

FS_ALLOW_IDMAP is set last, once every operation honors O, so there is no bisection point where the mount is idmappable but an operation still ignores the idmapping.

Quoted 6 lines

Note that fstests will be broken by this temporarily as they rely on the ability to create whiteouts for unprivileged device node creation. That can be adjusted.

---

Apart from the minor comments on the test, the entire series looks good to me, so you may add:

The problem is that my review can only assert that your changes make sense.

I am not even close to being able to prove that this is risk free.

This is the first time that we will have a combination of FS_ALLOW_IDMAP | FS_USERNS_MOUNT with fs which is a proxy to another fs, which to me sounds like Vulnerability Scanners heaven.

Could we maybe ask our friends in Anthropic to unleash Claude Mythos on this work to try to beat the AI kiddies to it before we merge this work?

Sorry for my lack of faith in human kind ;)

Amir.

author maintainer
#14 lore raw
Quoted 54 lines · Amir Goldstein
Quoted 40 lines · Christian Brauner

I've been asked about this feature multiple times and had actually implemented it right when I did the initial idmapped layer support. So here's a new version of this series.

This series lets the merged overlayfs mount itself be idmapped via mount_setattr(MOUNT_ATTR_IDMAP), in addition to the already supported idmapped lower and upper layers. It allows the same overlay tree to be exposed under a different ownership view, e.g. a container seeing the merged tree as 0:0 regardless of the on-disk or per-layer ids.

Overlayfs already normalizes every underlying id through the relevant layer idmap when it copies attributes into the overlay inode (ovl_copyattr()), so the overlay inode's i_{u,g}id are "overlay-final" ids. The overlay mount idmap O composes on top of that pivot:

    caller view  <--O-->  overlay-final id  <--layer idmap-->  on-disk

O is applied at the overlay-inode boundary only - permission, getattr, setattr, the ACL owner checks and inode_init_owner() - while the underlying layers keep being accessed with the mounter's credentials through their own, possibly idmapped, mounts. The mount idmap thus only changes how the caller sees the overlay inode and never widens the mounter's access to the layers: the second, mounter-credential check in ovl_permission() against the real inode stays on the layer idmap.

Most paths need no change because the VFS applies O to the overlay inode before overlayfs runs (sticky/rename/unlink/create gating) or after it returns at the syscall boundary (the POSIX ACL get/set translation). Overlayfs only acts where it bypasses the generic path:
getattr (it stats the real path), the setattr forward to the upper (the requested id is rebased from O into overlay-final space before being handed down), inode_init_owner() on create/tmpfile, and its own owner checks in ovl_set_acl().

FS_ALLOW_IDMAP is set last, once every operation honors O, so there is no bisection point where the mount is idmappable but an operation still ignores the idmapping.

Quoted 6 lines

Note that fstests will be broken by this temporarily as they rely on the ability to create whiteouts for unprivileged device node creation. That can be adjusted.

---

Apart from the minor comments on the test, the entire series looks good to me, so you may add:

The problem is that my review can only assert that your changes make sense.

Quoted 8 lines

I am not even close to being able to prove that this is risk free.

This is the first time that we will have a combination of FS_ALLOW_IDMAP | FS_USERNS_MOUNT with fs which is a proxy to another fs, which to me sounds like Vulnerability Scanners heaven.

Could we maybe ask our friends in Anthropic to unleash Claude Mythos on this work to try to beat the AI kiddies to it before we merge this work?

I would be happy if they did.

Quoted 1 line

Sorry for my lack of faith in human kind ;)

I will take this personally and I will seek in-person revenge in September.

#15 lore raw
In reply to Amir Goldstein
Quoted 51 lines · Amir Goldstein
Quoted 39 lines · Christian Brauner

I've been asked about this feature multiple times and had actually implemented it right when I did the initial idmapped layer support. So here's a new version of this series.

This series lets the merged overlayfs mount itself be idmapped via mount_setattr(MOUNT_ATTR_IDMAP), in addition to the already supported idmapped lower and upper layers. It allows the same overlay tree to be exposed under a different ownership view, e.g. a container seeing the merged tree as 0:0 regardless of the on-disk or per-layer ids.

Overlayfs already normalizes every underlying id through the relevant layer idmap when it copies attributes into the overlay inode (ovl_copyattr()), so the overlay inode's i_{u,g}id are "overlay-final" ids. The overlay mount idmap O composes on top of that pivot:

    caller view  <--O-->  overlay-final id  <--layer idmap-->  on-disk

O is applied at the overlay-inode boundary only - permission, getattr, setattr, the ACL owner checks and inode_init_owner() - while the underlying layers keep being accessed with the mounter's credentials through their own, possibly idmapped, mounts. The mount idmap thus only changes how the caller sees the overlay inode and never widens the mounter's access to the layers: the second, mounter-credential check in ovl_permission() against the real inode stays on the layer idmap.

Most paths need no change because the VFS applies O to the overlay inode before overlayfs runs (sticky/rename/unlink/create gating) or after it returns at the syscall boundary (the POSIX ACL get/set translation). Overlayfs only acts where it bypasses the generic path:
getattr (it stats the real path), the setattr forward to the upper (the requested id is rebased from O into overlay-final space before being handed down), inode_init_owner() on create/tmpfile, and its own owner checks in ovl_set_acl().

FS_ALLOW_IDMAP is set last, once every operation honors O, so there is no bisection point where the mount is idmappable but an operation still ignores the idmapping.

Quoted 6 lines

Note that fstests will be broken by this temporarily as they rely on the ability to create whiteouts for unprivileged device node creation. That can be adjusted.

---

Apart from the minor comments on the test, the entire series looks good to me, so you may add:

Quoted 7 lines

The problem is that my review can only assert that your changes make sense.

I am not even close to being able to prove that this is risk free.

This is the first time that we will have a combination of FS_ALLOW_IDMAP | FS_USERNS_MOUNT with fs which is a proxy to another fs, which to me sounds like Vulnerability Scanners heaven.

I randomly decided to point my human brain at overlayfs again, and noticed this change. :P

I've noticed that the may_linkat() checks for sysctl_protected_hardlinks only happen in filename_linkat(); not in vfs_link(), which overlayfs uses for operating on its backing filesystem. If idmap shenanigans allow being capable over the overlayfs view of an inode without being capable over the backing inode, that might break the hardlink protection. But getting a proper understanding of idmaps into my brain is taking some time, maybe later this week I'll understand whether this is actually a problem or not...