]> git.hungrycats.org Git - linux/commitdiff
ovl: do not copy up a dentry that was removed
authorChris Mason <mason@kernel.org>
Wed, 4 Nov 2026 18:45:16 +0000 (19:45 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:11 +0000 (12:06 -0500)
commit f635192ad223d2009ee640b63adf5ce3198aa338 upstream.

A change to a removed file through an open descriptor replaces whatever
has its old name in the upper layer.

Copy up must only create a name in the upper directory, never replace
one. After an unlink, an rmdir or a rename over a lower dentry, a
dentry that an open file keeps alive still has its old parent and name,
but in the upper directory that name is now a whiteout or a new object.
ovl_copy_up_workdir() renames the copy over it, which brings back the
removed object and destroys whatever took its name.

An unprivileged user opened a lower FIFO and unlinked it, root created
a file at that name, and fchmod on the old descriptor replaced the file
of root with the FIFO.

Fix this by looking up the target name of the rename in
ovl_copy_up_workdir() with LOOKUP_EXCL, so that copy up fails with
-EEXIST if the name is taken, as copy up with O_TMPFILE already does
when it links the copy to the name.

Fixes: e9be9d5e76e3 ("overlay filesystem")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
[amir: use LOOKUP_EXCL instead of relying on d_unhashed()]
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
[backport: no start_renaming_dentry() and LOOKUP_EXCL, fail with -EEXIST if
 the target of ovl_lookup_upper() is positive]
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/overlayfs/copy_up.c

index 488ce3cc3fc79d22d80b8458b9a807327cd20e5e..6f95fbd96340d7189bfa08185909f30b7390512d 100644 (file)
@@ -830,6 +830,13 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
        if (IS_ERR(upper))
                goto cleanup;
 
+       /* The name may have been taken since lookup, never replace it */
+       err = -EEXIST;
+       if (d_is_positive(upper)) {
+               dput(upper);
+               goto cleanup;
+       }
+
        err = ovl_do_rename(ofs, c->workdir, temp, c->destdir, upper, 0);
        unlock_rename(c->workdir, c->destdir);
        dput(upper);