Skip to contentSkip to conversation

FAILED: patch "[PATCH] ovl: take a snapshot of the name for copy up" failed to apply to 6.1-stable tree

PATCH In review [email protected] 3 participants 4 messages Nov 4 – Nov 5

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 65bf000962a33ec9d2488e4e1797b6cf630abcbb
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026110415-dragster-unsubtly-6084@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'

Possible dependencies:

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 65bf000962a33ec9d2488e4e1797b6cf630abcbb Mon Sep 17 00:00:00 2001
From: Chris Mason <[email protected]>
Date: Wed, 7 Oct 2026 10:34:08 +0000
Subject: [PATCH] ovl: take a snapshot of the name for copy up

The copy up of a directory reads a freed dentry name when a lookup moves the directory at the same time.

A pointer to the name of a dentry is only valid while something stops a move of the dentry. ovl_do_copy_up() keeps a plain copy of c->dentry->d_name in c->destname, and ovl_copy_up_start() only keeps ovl_rename() away. When a lookup finds the same directory under another name after a change to the lower layer, d_splice_alias() moves the dentry and frees its external name.

With a delay in ovl_do_copy_up(), a chmod of a lower-only directory with a 60 byte name raced with a rename of that directory in the lower layer and a stat of the new name, and KASAN reported a use-after-free in lookup_noperm_common().

Fix this by using take_dentry_name_snapshot() for c->destname.

Fixes 829c28be9bb9 ("ovl: use d_splice_alias() in place of d_add() in lookup")
fs/overlayfs/copy_up.c +12 -6
index 92b3d08a0234..12000e45fefc 100644
@@ -920,6 +920,7 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
 	struct dentry *origin = c->lowerpath.dentry;
 	struct ovl_fh *fh = NULL;
+	struct name_snapshot name;
 	bool to_index = false;
 
 	/*
@@ -957,12 +958,15 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 		goto out_free_fh;
 	} else {
 		/*
-		 * c->dentry->d_name is stabilzed by ovl_copy_up_start(),
-		 * because if we got here, it means that c->dentry has no upper
-		 * alias and changing ->d_name means going through ovl_rename()
-		 * that will call ovl_copy_up() on source and target dentry.
+		 * ovl_copy_up_start() keeps ovl_rename() away from c->dentry,
+		 * because ovl_rename() calls ovl_copy_up() on source and target
+		 * dentry.  It does not stop d_splice_alias() from moving a
+		 * directory that a lookup found under another name after a
+		 * change to the lower layer.  That move frees an external name,
+		 * so take a snapshot.
 		 */
-		c->destname = c->dentry->d_name;
+		take_dentry_name_snapshot(&name, c->dentry);
+		c->destname = name.name;
 		/*
 		 * Mark parent "impure" because it may now contain non-pure
 		 * upper
@@ -971,7 +975,7 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 		err = ovl_set_impure(c->parent, c->destdir);
 		ovl_end_write(c->dentry);
 		if (err)
-			goto out_free_fh;
+			goto out;
 	}
 
 	/* Should we copyup with O_TMPFILE or with workdir? */
@@ -1005,6 +1009,8 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 out:
 	if (to_index)
 		kfree(c->destname.name);
+	else
+		release_dentry_name_snapshot(&name);
 out_free_fh:
 	kfree(fh);
 	return err;
maintainer
#1 lore raw

PATCH 1/1 ovl: take a snapshot of the name for copy up

From: Chris Mason <[email protected]>

[ Upstream commit 65bf000962a33ec9d2488e4e1797b6cf630abcbb ]

The copy up of a directory reads a freed dentry name when a lookup moves the directory at the same time.

A pointer to the name of a dentry is only valid while something stops a move of the dentry. ovl_do_copy_up() keeps a plain copy of c->dentry->d_name in c->destname, and ovl_copy_up_start() only keeps ovl_rename() away. When a lookup finds the same directory under another name after a change to the lower layer, d_splice_alias() moves the dentry and frees its external name.

With a delay in ovl_do_copy_up(), a chmod of a lower-only directory with a 60 byte name raced with a rename of that directory in the lower layer and a stat of the new name, and KASAN reported a use-after-free in lookup_noperm_common().

Fix this by using take_dentry_name_snapshot() for c->destname.

Fixes 829c28be9bb9 ("ovl: use d_splice_alias() in place of d_add() in lookup")
[ sashal: moved destination-name initialization from ovl_copy_up_one() into ovl_do_copy_up() to accommodate the older code layout ]
---
 fs/overlayfs/copy_up.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/overlayfs/copy_up.c +14 -2
index 0f0c02121936e..a873bf4bac814 100644
@@ -804,6 +804,7 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 {
 	int err;
 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
+	struct name_snapshot name;
 	bool to_index = false;
 
 	/*
@@ -832,13 +833,23 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 		/* Disconnected dentry must be copied up to index dir */
 		return -EIO;
 	} else {
+		/*
+		 * ovl_copy_up_start() keeps ovl_rename() away from c->dentry,
+		 * because ovl_rename() calls ovl_copy_up() on source and target
+		 * dentry.  It does not stop d_splice_alias() from moving a
+		 * directory that a lookup found under another name after a
+		 * change to the lower layer.  That move frees an external name,
+		 * so take a snapshot.
+		 */
+		take_dentry_name_snapshot(&name, c->dentry);
+		c->destname = name.name;
 		/*
 		 * Mark parent "impure" because it may now contain non-pure
 		 * upper
 		 */
 		err = ovl_set_impure(c->parent, c->destdir);
 		if (err)
-			return err;
+			goto out;
 	}
 
 	/* Should we copyup with O_TMPFILE or with workdir? */
@@ -870,6 +881,8 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 out:
 	if (to_index)
 		kfree(c->destname.name);
+	else
+		release_dentry_name_snapshot(&name);
 	return err;
 }
 
@@ -1005,7 +1018,6 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
 	if (parent) {
 		ovl_path_upper(parent, &parentpath);
 		ctx.destdir = parentpath.dentry;
-		ctx.destname = dentry->d_name;
 
 		err = vfs_getattr(&parentpath, &ctx.pstat,
 				  STATX_ATIME | STATX_MTIME,
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH 6.1.y] ovl: take a snapshot of the name for copy up

Quoted 22 lines · Sasha Levin

From: Chris Mason <[email protected]>

[ Upstream commit 65bf000962a33ec9d2488e4e1797b6cf630abcbb ]

The copy up of a directory reads a freed dentry name when a lookup moves the directory at the same time.

A pointer to the name of a dentry is only valid while something stops a move of the dentry. ovl_do_copy_up() keeps a plain copy of c->dentry->d_name in c->destname, and ovl_copy_up_start() only keeps ovl_rename() away. When a lookup finds the same directory under another name after a change to the lower layer, d_splice_alias() moves the dentry and frees its external name.

With a delay in ovl_do_copy_up(), a chmod of a lower-only directory with a 60 byte name raced with a rename of that directory in the lower layer and a stat of the new name, and KASAN reported a use-after-free in lookup_noperm_common().

Fix this by using take_dentry_name_snapshot() for c->destname.

Quoted 7 lines
Fixes 829c28be9bb9 ("ovl: use d_splice_alias() in place of d_add() in lookup")
[ sashal: moved destination-name initialization from ovl_copy_up_one() into ovl_do_copy_up() to accommodate the older code layout ]

This is the same change as the one I asked Greg to apply to 6.6 and 6.1

https://lore.kernel.org/stable/[email protected]/

Can take whichever to 6.1

Sorry for not following protocol.

Thanks,
Amir.

Quoted 61 lines
---
 fs/overlayfs/copy_up.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/overlayfs/copy_up.c +14 -3
index 0f0c02121936e..a873bf4bac814 100644
@@ -804,6 +804,7 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 {
        int err;
        struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
+       struct name_snapshot name;
        bool to_index = false;
 
        /*
@@ -832,13 +833,23 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
                /* Disconnected dentry must be copied up to index dir */
                return -EIO;
        } else {
+               /*
+                * ovl_copy_up_start() keeps ovl_rename() away from c->dentry,
+                * because ovl_rename() calls ovl_copy_up() on source and target
+                * dentry.  It does not stop d_splice_alias() from moving a
+                * directory that a lookup found under another name after a
+                * change to the lower layer.  That move frees an external name,
+                * so take a snapshot.
+                */
+               take_dentry_name_snapshot(&name, c->dentry);
+               c->destname = name.name;
                /*
                 * Mark parent "impure" because it may now contain non-pure
                 * upper
                 */
                err = ovl_set_impure(c->parent, c->destdir);
                if (err)
-                       return err;
+                       goto out;
        }
 
        /* Should we copyup with O_TMPFILE or with workdir? */
@@ -870,6 +881,8 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
 out:
        if (to_index)
                kfree(c->destname.name);
+       else
+               release_dentry_name_snapshot(&name);
        return err;
 }
 
@@ -1005,7 +1018,6 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
        if (parent) {
                ovl_path_upper(parent, &parentpath);
                ctx.destdir = parentpath.dentry;
-               ctx.destname = dentry->d_name;
 
                err = vfs_getattr(&parentpath, &ctx.pstat,
                                  STATX_ATIME | STATX_MTIME,
--

2.53.0

maintainer
#3 lore raw

Re: [PATCH 6.1.y] ovl: take a snapshot of the name for copy up

Quoted 3 lines

This is the same change as the one I asked Greg to apply to 6.6 and 6.1

Can take whichever to 6.1

Thanks, both of your patches are queued for 6.1.

Signature · 2 lines
Thanks,
Sasha