]> git.hungrycats.org Git - linux/commitdiff
ovl: check that the parents of volatile/dirty are dirs
authorChris Mason <mason@kernel.org>
Wed, 4 Nov 2026 18:45:14 +0000 (19:45 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:11 +0000 (12:06 -0500)
commit e34d2ef818e4c7676d8d8e1bf43e6a1ed39efb2a upstream.

An unprivileged user crashes the kernel by mounting overlayfs with the
volatile option.

A dentry that serves as the parent of a lookup must be a directory.
ovl_create_volatile_dirty() walks work/incompat/volatile/dirty by name,
and ovl_lookup_or_create() returns an existing entry whatever its type.
A file or a symlink that another task creates during the mount becomes
the parent of the next lookup, which calls a NULL ->lookup.

An unprivileged user mounted overlayfs with "volatile" in a user
namespace while a second process created a symlink named work/incompat,
and the mount task oopsed on a NULL ->lookup call.

Fix this by returning -ENOTDIR from ovl_lookup_or_create() when the
caller asked for a directory and the existing entry is a non-dir.

Fixes: c86243b090bc ("ovl: provide a mount option "volatile"")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
[backport: no ovl_start_creating_upper(), check the child of
 ovl_lookup_upper() in ovl_lookup_or_create() instead]
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/overlayfs/super.c

index d727e56ef4b1fc1960bd6ae7783089342248795b..6a733632a6475537864be4cb9b049d26e8a47ede 100644 (file)
@@ -624,15 +624,23 @@ cleanup_temp:
 
 static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
                                           struct dentry *parent,
-                                          const char *name, umode_t mode)
+                                          const char *name, bool is_dir)
 {
        size_t len = strlen(name);
        struct dentry *child;
 
        inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
        child = ovl_lookup_upper(ofs, name, parent, len);
-       if (!IS_ERR(child) && !child->d_inode)
-               child = ovl_create_real(ofs, parent, child, OVL_CATTR(mode));
+       if (!IS_ERR(child)) {
+               if (!child->d_inode) {
+                       child = ovl_create_real(ofs, parent, child,
+                                               OVL_CATTR(is_dir ? S_IFDIR :
+                                                                  S_IFREG));
+               } else if (is_dir && !d_can_lookup(child)) {
+                       dput(child);
+                       child = ERR_PTR(-ENOTDIR);
+               }
+       }
        inode_unlock(parent->d_inode);
        dput(parent);
 
@@ -653,7 +661,7 @@ static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
        const char *const *name = volatile_path;
 
        for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
-               d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
+               d = ovl_lookup_or_create(ofs, d, *name, ctr > 1);
                if (IS_ERR(d))
                        return PTR_ERR(d);
        }