FAILED: patch "[PATCH] ovl: do not read the flags of a put upper dentry in lookup" failed to apply to 6.12-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.12-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.12.y
git checkout FETCH_HEAD
git cherry-pick -x 7031366be218f291592a7ad38bc34a7cd1459737
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112530-preplan-eldest-5662@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 7031366be218f291592a7ad38bc34a7cd1459737 Mon Sep 17 00:00:00 2001
From: Amir Goldstein <[email protected]>
Date: Fri, 6 Nov 2026 13:01:26 +0100
Subject: [PATCH] ovl: do not read the flags of a put upper dentry in lookup
ovl_lookup_layers() passes ctx->upperdentry to ovl_get_inode(), which takes over the reference. If an overlay inode for the upper inode is already cached, as for a second hard link, ovl_get_inode() puts the dentry and returns the cached inode. ovl_lookup_layers() then passes the put dentry to ovl_dentry_init_reval(), which reads its d_flags.
With a local upper fs the put dentry usually stays in the dcache, but with a FUSE upper whose entry has timed out, dput() kills it, so this is a use-after-free.
Fix this by taking the revalidate flags from the upper dentry of the overlay inode, which is the one that ovl_dentry_revalidate_common() revalidates.
fs/overlayfs/namei.c +10 -2
@@ -1382,9 +1382,17 @@ static int ovl_lookup_layers(struct ovl_lookup_ctx *ctx, struct ovl_lookup_dataif (metacopy_size >OVL_METACOPY_MIN_SIZE )ovl_set_flag (OVL_HAS_DIGEST , ctx->inode); - } -ovl_dentry_init_reval (dentry, ctx->upperdentry,OVL_I_E (ctx->inode)); +/* +* ovl_get_inode() puts ctx->upperdentry if it finds a cached +* inode, e.g. for another hard link, so use the upper dentry +* of the inode. +*/ +ovl_dentry_init_reval (dentry,ovl_i_dentry_upper (inode), +OVL_I_E (inode)); + }else { +ovl_dentry_init_reval (dentry,NULL ,NULL ); + }return 0 ; }
Sasha Levin
maintainerPATCH 1/4 ovl: use wrapper ovl_revert_creds()
From: Vinicius Costa Gomes <[email protected]>
[ Upstream commit fc5a1d2287bf23f67da1fc7a178cf26c5e6ba9d0 ]
Introduce ovl_revert_creds() wrapper of revert_creds() to match callers of ovl_override_creds().
Stable-dep-of: 7031366be218 ("ovl: do not read the flags of a put upper dentry in lookup")
--- fs/overlayfs/copy_up.c | 2 +- fs/overlayfs/dir.c | 10 +++++----- fs/overlayfs/file.c | 14 +++++++------- fs/overlayfs/inode.c | 20 ++++++++++---------- fs/overlayfs/namei.c | 10 +++++----- fs/overlayfs/overlayfs.h | 1 + fs/overlayfs/readdir.c | 8 ++++---- fs/overlayfs/util.c | 9 +++++++-- fs/overlayfs/xattrs.c | 9 ++++----- 9 files changed, 44 insertions(+), 39 deletions(-)
fs/overlayfs/copy_up.c +1 -1
@@ -1297,7 +1297,7 @@ static int ovl_copy_up_flags(struct dentry *dentry, int flags)dput (parent);dput (next); } -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
fs/overlayfs/dir.c +5 -5
@@ -627,7 +627,7 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode, err =ovl_create_over_whiteout (dentry, inode, attr); out_revert_creds: -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -708,7 +708,7 @@ static int ovl_set_link_redirect(struct dentry *dentry) old_cred =ovl_override_creds (dentry->d_sb); err =ovl_set_redirect (dentry, false); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -918,7 +918,7 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir) err =ovl_remove_upper (dentry, is_dir, &list); else err =ovl_remove_and_whiteout (dentry, &list); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (!err) {if (is_dir)clear_nlink (dentry->d_inode);
@@ -1310,7 +1310,7 @@ static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir, out_unlock:unlock_rename (new_upperdir, old_upperdir); out_revert_creds: -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (update_nlink)ovl_nlink_end (new);else
@@ -1355,7 +1355,7 @@ static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,fput (realfile); } out_revert_creds: -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
fs/overlayfs/file.c +7 -7
@@ -50,7 +50,7 @@ static struct file *ovl_open_realfile(const struct file *file, realfile =backing_file_open (file, flags, realpath,current_cred ()); } -revert_creds (old_cred); +ovl_revert_creds (old_cred); pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", file, file, ovl_whatisit(inode, realinode), file->f_flags,
@@ -214,7 +214,7 @@ static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) old_cred =ovl_override_creds (inode->i_sb); ret =vfs_llseek (fd_file (real), offset, whence); -revert_creds (old_cred); +ovl_revert_creds (old_cred); file->f_pos =fd_file (real)->f_pos;ovl_inode_unlock (inode);
@@ -406,7 +406,7 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)if (file_inode (fd_file (real)) ==ovl_inode_upper (file_inode (file))) { old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =vfs_fsync_range (fd_file (real), start, end, datasync); -revert_creds (old_cred); +ovl_revert_creds (old_cred); }fdput (real);
@@ -446,7 +446,7 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =vfs_fallocate (fd_file (real), mode, offset, len); -revert_creds (old_cred); +ovl_revert_creds (old_cred);/* Update size */ ovl_file_modified (file);
@@ -471,7 +471,7 @@ static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =vfs_fadvise (fd_file (real), offset, len, advice); -revert_creds (old_cred); +ovl_revert_creds (old_cred);fdput (real);
@@ -544,7 +544,7 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, flags);break ; } -revert_creds (old_cred); +ovl_revert_creds (old_cred);/* Update size */ ovl_file_modified (file_out);
@@ -607,7 +607,7 @@ static int ovl_flush(struct file *file, fl_owner_t id)if (fd_file (real)->f_op->flush) { old_cred =ovl_override_creds (file_inode (file)->i_sb); err =fd_file (real)->f_op->flush (fd_file (real), id); -revert_creds (old_cred); +ovl_revert_creds (old_cred); }fdput (real);
fs/overlayfs/inode.c +10 -10
@@ -80,7 +80,7 @@ int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,inode_lock (upperdentry->d_inode); old_cred =ovl_override_creds (dentry->d_sb); err =ovl_do_notify_change (ofs, upperdentry, attr); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (!err)ovl_copyattr (dentry->d_inode);inode_unlock (upperdentry->d_inode);
@@ -280,7 +280,7 @@ int ovl_getattr(struct mnt_idmap *idmap, const struct path *path, stat->nlink = dentry->d_inode->i_nlink; out: -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -317,7 +317,7 @@ int ovl_permission(struct mnt_idmap *idmap, mask |=MAY_READ ; } err =inode_permission (mnt_idmap (realpath.mnt), realinode, mask); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -334,7 +334,7 @@ static const char *ovl_get_link(struct dentry *dentry, old_cred =ovl_override_creds (dentry->d_sb); p =vfs_get_link (ovl_dentry_real (dentry), done); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return p; }
@@ -469,7 +469,7 @@ struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap, old_cred =ovl_override_creds (inode->i_sb); acl =ovl_get_acl_path (&realpath,posix_acl_xattr_name (type), noperm); -revert_creds (old_cred); +ovl_revert_creds (old_cred); }return acl;
@@ -498,7 +498,7 @@ static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode, old_cred =ovl_override_creds (dentry->d_sb); real_acl =vfs_get_acl (mnt_idmap (realpath.mnt), realdentry, acl_name); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (IS_ERR (real_acl)) { err =PTR_ERR (real_acl); goto out;
@@ -523,7 +523,7 @@ static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode, err =ovl_do_set_acl (ofs, realdentry, acl_name, acl); else err =ovl_do_remove_acl (ofs, realdentry, acl_name); -revert_creds (old_cred); +ovl_revert_creds (old_cred);ovl_drop_write (dentry);/* copy c/mtime */
@@ -600,7 +600,7 @@ static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, old_cred =ovl_override_creds (inode->i_sb); err = realinode->i_op->fiemap (realinode, fieinfo, start, len); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -676,7 +676,7 @@ int ovl_fileattr_set(struct mnt_idmap *idmap, err =ovl_set_protattr (inode, upperpath.dentry, fa);if (!err) err =ovl_real_fileattr_set (&upperpath, fa); -revert_creds (old_cred); +ovl_revert_creds (old_cred);ovl_drop_write (dentry);/*
@@ -738,7 +738,7 @@ int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa) old_cred =ovl_override_creds (inode->i_sb); err =ovl_real_fileattr_get (&realpath, fa);ovl_fileattr_prot_flags (inode, fa); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
fs/overlayfs/namei.c +5 -5
@@ -969,7 +969,7 @@ static int ovl_maybe_validate_verity(struct dentry *dentry)if (err ==0 )ovl_set_flag (OVL_VERIFIED_DIGEST , inode); -revert_creds (old_cred); +ovl_revert_creds (old_cred); }ovl_inode_unlock (inode);
@@ -1003,7 +1003,7 @@ static int ovl_maybe_lookup_lowerdata(struct dentry *dentry) old_cred =ovl_override_creds (dentry->d_sb); err =ovl_lookup_data_layers (dentry, redirect, &datapath); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (err) goto out_err;
@@ -1358,7 +1358,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,ovl_dentry_init_reval (dentry, upperdentry,OVL_I_E (inode)); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (origin_path) {dput (origin_path->dentry);kfree (origin_path);
@@ -1382,7 +1382,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,kfree (upperredirect); out:kfree (d.redirect); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return ERR_PTR (err); }
@@ -1439,7 +1439,7 @@ bool ovl_lower_positive(struct dentry *dentry)dput (this); } } -revert_creds (old_cred); +ovl_revert_creds (old_cred);return positive; }
fs/overlayfs/overlayfs.h +1 -0
@@ -439,6 +439,7 @@ int ovl_want_write(struct dentry *dentry);void ovl_drop_write (struct dentry *dentry);struct dentry *ovl_workdir (struct dentry *dentry);const struct cred *ovl_override_creds (struct super_block *sb); +void ovl_revert_creds (const struct cred *old_cred);static inline const struct cred *ovl_creds (struct super_block *sb) {
fs/overlayfs/readdir.c +4 -4
@@ -291,7 +291,7 @@ static int ovl_check_whiteouts(const struct path *path, struct ovl_readdir_data }inode_unlock (dir->d_inode); } -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -829,7 +829,7 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx) } err =0 ; out: -revert_creds (old_cred); +ovl_revert_creds (old_cred);return err; }
@@ -889,7 +889,7 @@ static struct file *ovl_dir_open_realfile(const struct file *file, old_cred =ovl_override_creds (file_inode (file)->i_sb); res =ovl_path_open (realpath, flags); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return res; }
@@ -1017,7 +1017,7 @@ int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list) old_cred =ovl_override_creds (dentry->d_sb); err =ovl_dir_read_merged (dentry, list, &root,NULL ); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (err)return err;
fs/overlayfs/util.c +7 -2
@@ -68,6 +68,11 @@ const struct cred *ovl_override_creds(struct super_block *sb)return override_creds (ofs->creator_cred); } +void ovl_revert_creds (const struct cred *old_cred) +{ +revert_creds (old_cred); +} +/* * Check if underlying fs supports file handles and try to determine encoding * type, in order to deduce maximum inode number used by fs.
@@ -1198,7 +1203,7 @@ int ovl_nlink_start(struct dentry *dentry)* value relative to the upper inode nlink in an upper inode xattr. */ err =ovl_set_nlink_upper (dentry); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (err) goto out_drop_write;
@@ -1223,7 +1228,7 @@ void ovl_nlink_end(struct dentry *dentry) old_cred =ovl_override_creds (dentry->d_sb);ovl_cleanup_index (dentry); -revert_creds (old_cred); +ovl_revert_creds (old_cred); }ovl_inode_unlock (inode);
fs/overlayfs/xattrs.c +4 -5
@@ -47,7 +47,7 @@ static int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const charovl_path_lower (dentry, &realpath); old_cred =ovl_override_creds (dentry->d_sb); err =vfs_getxattr (mnt_idmap (realpath.mnt), realdentry, name,NULL ,0 ); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (err <0 ) goto out; }
@@ -72,7 +72,7 @@ static int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const charWARN_ON (flags !=XATTR_REPLACE ); err =ovl_do_removexattr (ofs, realdentry, name); } -revert_creds (old_cred); +ovl_revert_creds (old_cred);ovl_drop_write (dentry);/* copy c/mtime */
@@ -91,7 +91,7 @@ static int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const charovl_i_path_real (inode, &realpath); old_cred =ovl_override_creds (dentry->d_sb); res =vfs_getxattr (mnt_idmap (realpath.mnt), realpath.dentry, name, value, size); -revert_creds (old_cred); +ovl_revert_creds (old_cred);return res; }
@@ -121,7 +121,7 @@ ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) old_cred =ovl_override_creds (dentry->d_sb); res =vfs_listxattr (realdentry, list, size); -revert_creds (old_cred); +ovl_revert_creds (old_cred);if (res <=0 || size ==0 )return res;
@@ -268,4 +268,3 @@ const struct xattr_handler * const *ovl_xattr_handlers(struct ovl_fs *ofs)return ofs->config.userxattr ? ovl_user_xattr_handlers : ovl_trusted_xattr_handlers; } -
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 ovl: make redirect/metacopy rejection consistent
From: Miklos Szeredi <[email protected]>
[ Upstream commit a6fcfe9bb26df18ba7b5c4d064edd13c80ea2466 ]
When overlayfs finds a file with metacopy and/or redirect attributes and the metacopy and/or redirect features are not enabled, then it refuses to act on those attributes while also issuing a warning.
There was an inconsistency in not checking metacopy found from the index.
And also only warning on an upper metacopy if it found the next file on the lower layer, while always warning for metacopy found on a lower layer.
Fix these inconsistencies and make the logic more straightforward, paving the way for following patches to change when data redirects are allowed.
Stable-dep-of: 7031366be218 ("ovl: do not read the flags of a put upper dentry in lookup")
--- fs/overlayfs/namei.c | 90 +++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 35 deletions(-)
fs/overlayfs/namei.c +55 -35
@@ -18,6 +18,7 @@struct ovl_lookup_data {struct super_block *sb; +struct dentry *dentry;const struct ovl_layer *layer;struct qstr name;bool is_dir;
@@ -26,6 +27,7 @@ struct ovl_lookup_data {bool stop;bool last;char *redirect; +char *upperredirect;int metacopy;/* Referring to last redirect xattr */ bool absolute_redirect;
@@ -1042,6 +1044,31 @@ int ovl_verify_lowerdata(struct dentry *dentry)return ovl_maybe_validate_verity (dentry); } +/* +* Following redirects/metacopy can have security consequences: it's like a +* symlink into the lower layer without the permission checks. +* +* This is only a problem if the upper layer is untrusted (e.g comes from an USB +* drive). This can allow a non-readable file or directory to become readable. +* +* Only following redirects when redirects are enabled disables this attack +* vector when not necessary. +*/ +static bool ovl_check_follow_redirect (struct ovl_lookup_data *d) +{ +struct ovl_fs *ofs =OVL_FS (d->sb); + +if (d->metacopy && !ofs->config.metacopy) { +pr_warn_ratelimited ("refusing to follow metacopy origin for (%pd2)\n" , d->dentry); +return false; + } +if ((d->redirect || d->upperredirect) && !ovl_redirect_follow (ofs)) { +pr_warn_ratelimited ("refusing to follow redirect for (%pd2)\n" , d->dentry); +return false; + } +return true; +} +struct dentry *ovl_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags) {
@@ -1057,7 +1084,6 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,unsigned int ctr =0 ;struct inode *inode =NULL ;bool upperopaque = false; -char *upperredirect =NULL ;struct dentry *this;unsigned int i;int err;
@@ -1065,12 +1091,14 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,int metacopy_size =0 ;struct ovl_lookup_data d = { .sb = dentry->d_sb, + .dentry = dentry, .name = dentry->d_name, .is_dir = false, .opaque = false, .stop = false, .last =ovl_redirect_follow (ofs) ? false : !ovl_numlower (poe), .redirect =NULL , + .upperredirect =NULL , .metacopy =0 , };
@@ -1112,8 +1140,8 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,if (d.redirect) { err = -ENOMEM ; - upperredirect =kstrdup (d.redirect,GFP_KERNEL ); -if (!upperredirect) + d.upperredirect =kstrdup (d.redirect,GFP_KERNEL ); +if (!d.upperredirect) goto out_put_upper;if (d.redirect[0 ] =='/' ) poe = roe;
@@ -1131,6 +1159,11 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,for (i =0 ; !d.stop && i <ovl_numlower (poe); i++) {struct ovl_path lower =ovl_lowerstack (poe)[i]; +if (!ovl_check_follow_redirect (&d)) { + err = -EPERM ; + goto out_put; + } +if (!ovl_redirect_follow (ofs)) d.last = i ==ovl_numlower (poe) -1 ;else if (d.is_dir || !ofs->numdatalayer)
@@ -1144,13 +1177,6 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,if (!this)continue ; -if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) { -dput (this); - err = -EPERM ; -pr_warn_ratelimited ("refusing to follow metacopy origin for (%pd2)\n" , dentry); - goto out_put; - } -/* * If no origin fh is stored in upper of a merge dir, store fh * of lower dir and set upper parent "impure".
@@ -1203,23 +1229,6 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ctr++; } -/* -* Following redirects can have security consequences: it's like -* a symlink into the lower layer without the permission checks. -* This is only a problem if the upper layer is untrusted (e.g -* comes from an USB drive). This can allow a non-readable file -* or directory to become readable. -* -* Only following redirects when redirects are enabled disables -* this attack vector when not necessary. -*/ - err = -EPERM ; -if (d.redirect && !ovl_redirect_follow (ofs)) { -pr_warn_ratelimited ("refusing to follow redirect for (%pd2)\n" , - dentry); - goto out_put; - } -if (d.stop)break ;
@@ -1230,6 +1239,11 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, } } +if (!ovl_check_follow_redirect (&d)) { + err = -EPERM ; + goto out_put; + } +/* Defer lookup of lowerdata in data-only layers to first access */ if (d.metacopy && ctr && ofs->numdatalayer && d.absolute_redirect) { d.metacopy =0 ;
@@ -1316,20 +1330,26 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,/* * It's safe to assign upperredirect here: the previous -* assignment of happens only if upperdentry is non-NULL, and +* assignment happens only if upperdentry is non-NULL, and * this one only if upperdentry is NULL. */ - upperredirect =ovl_get_redirect_xattr (ofs, &upperpath,0 ); -if (IS_ERR (upperredirect)) { - err =PTR_ERR (upperredirect); - upperredirect =NULL ; + d.upperredirect =ovl_get_redirect_xattr (ofs, &upperpath,0 ); +if (IS_ERR (d.upperredirect)) { + err =PTR_ERR (d.upperredirect); + d.upperredirect =NULL ; goto out_free_oe; } + err =ovl_check_metacopy_xattr (ofs, &upperpath,NULL );if (err <0 ) goto out_free_oe; - uppermetacopy = err; + d.metacopy = uppermetacopy = err; metacopy_size = err; + +if (!ovl_check_follow_redirect (&d)) { + err = -EPERM ; + goto out_free_oe; + } }if (upperdentry || ctr) {
@@ -1337,7 +1357,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, .upperdentry = upperdentry, .oe = oe, .index = index, - .redirect = upperredirect, + .redirect = d.upperredirect, };/* Store lowerdata redirect for lazy lookup */
@@ -1379,7 +1399,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,kfree (origin_path); }dput (upperdentry); -kfree (upperredirect); +kfree (d.upperredirect); out:kfree (d.redirect);ovl_revert_creds (old_cred);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 ovl: refactor ovl_lookup()
In reply to Sasha LevinFrom: Christian Brauner <[email protected]>
[ Upstream commit 15da486ad3bde6119d9b6353bebc2ffb6e9165c1 ]
Split the core into a separate helper in preparation of converting the caller to the scoped ovl cred guard.
Stable-dep-of: 7031366be218 ("ovl: do not read the flags of a put upper dentry in lookup")
--- fs/overlayfs/namei.c | 313 ++++++++++++++++++++++--------------------- 1 file changed, 160 insertions(+), 153 deletions(-)
fs/overlayfs/namei.c +160 -153
@@ -1069,56 +1069,43 @@ static bool ovl_check_follow_redirect(struct ovl_lookup_data *d)return true; } -struct dentry *ovl_lookup (struct inode *dir,struct dentry *dentry, -unsigned int flags) +struct ovl_lookup_ctx { +struct dentry *dentry; +struct ovl_entry *oe; +struct ovl_path *stack; +struct ovl_path *origin_path; +struct dentry *upperdentry; +struct dentry *index; +struct inode *inode; +unsigned int ctr; +}; + +static int ovl_lookup_layers (struct ovl_lookup_ctx *ctx,struct ovl_lookup_data *d) { -struct ovl_entry *oe =NULL ; -const struct cred *old_cred; +struct dentry *dentry = ctx->dentry;struct ovl_fs *ofs =OVL_FS (dentry->d_sb);struct ovl_entry *poe =OVL_E (dentry->d_parent);struct ovl_entry *roe =OVL_E (dentry->d_sb->s_root); -struct ovl_path *stack =NULL , *origin_path =NULL ; -struct dentry *upperdir, *upperdentry =NULL ; +struct dentry *upperdir; +struct dentry *this;struct dentry *origin =NULL ; -struct dentry *index =NULL ; -unsigned int ctr =0 ; -struct inode *inode =NULL ;bool upperopaque = false; -struct dentry *this; -unsigned int i; -int err;bool uppermetacopy = false;int metacopy_size =0 ; -struct ovl_lookup_data d = { - .sb = dentry->d_sb, - .dentry = dentry, - .name = dentry->d_name, - .is_dir = false, - .opaque = false, - .stop = false, - .last =ovl_redirect_follow (ofs) ? false : !ovl_numlower (poe), - .redirect =NULL , - .upperredirect =NULL , - .metacopy =0 , - }; - -if (dentry->d_name.len > ofs->namelen) -return ERR_PTR (-ENAMETOOLONG ); +unsigned int i; +int err; - old_cred =ovl_override_creds (dentry->d_sb); upperdir =ovl_dentry_upper (dentry->d_parent);if (upperdir) { - d.layer = &ofs->layers[0 ]; - err =ovl_lookup_layer (upperdir, &d, &upperdentry, true); + d->layer = &ofs->layers[0 ]; + err =ovl_lookup_layer (upperdir, d, &ctx->upperdentry, true);if (err) - goto out; +return err; -if (upperdentry && upperdentry->d_flags &DCACHE_OP_REAL ) { -dput (upperdentry); - err = -EREMOTE ; - goto out; - } -if (upperdentry && !d.is_dir) { +if (ctx->upperdentry && ctx->upperdentry->d_flags &DCACHE_OP_REAL ) +return -EREMOTE ; + +if (ctx->upperdentry && !d->is_dir) {/* * Lookup copy up origin by decoding origin file handle. * We may get a disconnected dentry, which is fine,
@@ -1129,50 +1116,50 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,* number - it's the same as if we held a reference * to a dentry in lower layer that was moved under us. */ - err =ovl_check_origin (ofs, upperdentry, &origin_path); + err =ovl_check_origin (ofs, ctx->upperdentry, &ctx->origin_path);if (err) - goto out_put_upper; +return err; -if (d.metacopy) +if (d->metacopy) uppermetacopy = true; - metacopy_size = d.metacopy; + metacopy_size = d->metacopy; } -if (d.redirect) { +if (d->redirect) { err = -ENOMEM ; - d.upperredirect =kstrdup (d.redirect,GFP_KERNEL ); -if (!d.upperredirect) - goto out_put_upper; -if (d.redirect[0 ] =='/' ) + d->upperredirect =kstrdup (d->redirect,GFP_KERNEL ); +if (!d->upperredirect) +return err; +if (d->redirect[0 ] =='/' ) poe = roe; } - upperopaque = d.opaque; + upperopaque = d->opaque; } -if (!d.stop &&ovl_numlower (poe)) { +if (!d->stop &&ovl_numlower (poe)) { err = -ENOMEM ; - stack =ovl_stack_alloc (ofs->numlayer -1 ); -if (!stack) - goto out_put_upper; + ctx->stack =ovl_stack_alloc (ofs->numlayer -1 ); +if (!ctx->stack) +return err; } -for (i =0 ; !d.stop && i <ovl_numlower (poe); i++) { +for (i =0 ; !d->stop && i <ovl_numlower (poe); i++) {struct ovl_path lower =ovl_lowerstack (poe)[i]; -if (!ovl_check_follow_redirect (&d)) { +if (!ovl_check_follow_redirect (d)) { err = -EPERM ; - goto out_put; +return err; }if (!ovl_redirect_follow (ofs)) - d.last = i ==ovl_numlower (poe) -1 ; -else if (d.is_dir || !ofs->numdatalayer) - d.last = lower.layer->idx ==ovl_numlower (roe); + d->last = i ==ovl_numlower (poe) -1 ; +else if (d->is_dir || !ofs->numdatalayer) + d->last = lower.layer->idx ==ovl_numlower (roe); - d.layer = lower.layer; - err =ovl_lookup_layer (lower.dentry, &d, &this, false); + d->layer = lower.layer; + err =ovl_lookup_layer (lower.dentry, d, &this, false);if (err) - goto out_put; +return err;if (!this)continue ;
@@ -1181,11 +1168,11 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,* If no origin fh is stored in upper of a merge dir, store fh * of lower dir and set upper parent "impure". */ -if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) { - err =ovl_fix_origin (ofs, dentry, this, upperdentry); +if (ctx->upperdentry && !ctx->ctr && !ofs->noxattr && d->is_dir) { + err =ovl_fix_origin (ofs, dentry, this, ctx->upperdentry);if (err) {dput (this); - goto out_put; +return err; } }
@@ -1198,23 +1185,23 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,* matches the dentry found using path based lookup, * otherwise error out. */ -if (upperdentry && !ctr && - ((d.is_dir &&ovl_verify_lower (dentry->d_sb)) || - (!d.is_dir && ofs->config.index && origin_path))) { - err =ovl_verify_origin (ofs, upperdentry, this, false); +if (ctx->upperdentry && !ctx->ctr && + ((d->is_dir &&ovl_verify_lower (dentry->d_sb)) || + (!d->is_dir && ofs->config.index && ctx->origin_path))) { + err =ovl_verify_origin (ofs, ctx->upperdentry, this, false);if (err) {dput (this); -if (d.is_dir) +if (d->is_dir)break ; - goto out_put; +return err; } origin = this; } -if (!upperdentry && !d.is_dir && !ctr && d.metacopy) - metacopy_size = d.metacopy; +if (!ctx->upperdentry && !d->is_dir && !ctx->ctr && d->metacopy) + metacopy_size = d->metacopy; -if (d.metacopy && ctr) { +if (d->metacopy && ctx->ctr) {/* * Do not store intermediate metacopy dentries in * lower chain, except top most lower metacopy dentry.
@@ -1224,30 +1211,30 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,dput (this); this =NULL ; }else { - stack[ctr].dentry = this; - stack[ctr].layer = lower.layer; - ctr++; + ctx->stack[ctx->ctr].dentry = this; + ctx->stack[ctx->ctr].layer = lower.layer; + ctx->ctr++; } -if (d.stop) +if (d->stop)break ; -if (d.redirect && d.redirect[0 ] =='/' && poe != roe) { +if (d->redirect && d->redirect[0 ] =='/' && poe != roe) { poe = roe;/* Find the current layer on the root dentry */ i = lower.layer->idx -1 ; } } -if (!ovl_check_follow_redirect (&d)) { +if (!ovl_check_follow_redirect (d)) { err = -EPERM ; - goto out_put; +return err; }/* Defer lookup of lowerdata in data-only layers to first access */ -if (d.metacopy && ctr && ofs->numdatalayer && d.absolute_redirect) { - d.metacopy =0 ; - ctr++; +if (d->metacopy && ctx->ctr && ofs->numdatalayer && d->absolute_redirect) { + d->metacopy =0 ; + ctx->ctr++; }/*
@@ -1258,20 +1245,20 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,* For metacopy dentry, path based lookup will find lower dentries. * Just make sure a corresponding data dentry has been found. */ -if (d.metacopy || (uppermetacopy && !ctr)) { +if (d->metacopy || (uppermetacopy && !ctx->ctr)) {pr_warn_ratelimited ("metacopy with no lower data found - abort lookup (%pd2)\n" , dentry); err = -EIO ; - goto out_put; - }else if (!d.is_dir && upperdentry && !ctr && origin_path) { -if (WARN_ON (stack !=NULL )) { +return err; + }else if (!d->is_dir && ctx->upperdentry && !ctx->ctr && ctx->origin_path) { +if (WARN_ON (ctx->stack !=NULL )) { err = -EIO ; - goto out_put; +return err; } - stack = origin_path; - ctr =1 ; - origin = origin_path->dentry; - origin_path =NULL ; + ctx->stack = ctx->origin_path; + ctx->ctr =1 ; + origin = ctx->origin_path->dentry; + ctx->origin_path =NULL ; }/*
@@ -1293,38 +1280,39 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,* is enabled and if upper had an ORIGIN xattr. * */ -if (!upperdentry && ctr) - origin = stack[0 ].dentry; +if (!ctx->upperdentry && ctx->ctr) + origin = ctx->stack[0 ].dentry;if (origin &&ovl_indexdir (dentry->d_sb) && - (!d.is_dir ||ovl_index_all (dentry->d_sb))) { - index =ovl_lookup_index (ofs, upperdentry, origin, true); -if (IS_ERR (index)) { - err =PTR_ERR (index); - index =NULL ; - goto out_put; + (!d->is_dir ||ovl_index_all (dentry->d_sb))) { + ctx->index =ovl_lookup_index (ofs, ctx->upperdentry, origin, true); +if (IS_ERR (ctx->index)) { + err =PTR_ERR (ctx->index); + ctx->index =NULL ; +return err; } } -if (ctr) { - oe =ovl_alloc_entry (ctr); +if (ctx->ctr) { + ctx->oe =ovl_alloc_entry (ctx->ctr); err = -ENOMEM ; -if (!oe) - goto out_put; +if (!ctx->oe) +return err; -ovl_stack_cpy (ovl_lowerstack (oe), stack, ctr); +ovl_stack_cpy (ovl_lowerstack (ctx->oe), ctx->stack, ctx->ctr); }if (upperopaque)ovl_dentry_set_opaque (dentry); -if (d.xwhiteouts) +if (d->xwhiteouts)ovl_dentry_set_xwhiteouts (dentry); -if (upperdentry) +if (ctx->upperdentry)ovl_dentry_set_upper_alias (dentry); -else if (index) { +else if (ctx->index) { +char *upperredirect;struct path upperpath = { - .dentry = upperdentry =dget (index), + .dentry = ctx->upperdentry =dget (ctx->index), .mnt =ovl_upper_mnt (ofs), };
@@ -1333,77 +1321,96 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,* assignment happens only if upperdentry is non-NULL, and * this one only if upperdentry is NULL. */ - d.upperredirect =ovl_get_redirect_xattr (ofs, &upperpath,0 ); -if (IS_ERR (d.upperredirect)) { - err =PTR_ERR (d.upperredirect); - d.upperredirect =NULL ; - goto out_free_oe; - } + upperredirect =ovl_get_redirect_xattr (ofs, &upperpath,0 ); +if (IS_ERR (upperredirect)) +return PTR_ERR (upperredirect); + d->upperredirect = upperredirect; err =ovl_check_metacopy_xattr (ofs, &upperpath,NULL );if (err <0 ) - goto out_free_oe; - d.metacopy = uppermetacopy = err; +return err; + d->metacopy = uppermetacopy = err; metacopy_size = err; -if (!ovl_check_follow_redirect (&d)) { +if (!ovl_check_follow_redirect (d)) { err = -EPERM ; - goto out_free_oe; +return err; } } -if (upperdentry || ctr) { +if (ctx->upperdentry || ctx->ctr) { +struct inode *inode;struct ovl_inode_params oip = { - .upperdentry = upperdentry, - .oe = oe, - .index = index, - .redirect = d.upperredirect, + .upperdentry = ctx->upperdentry, + .oe = ctx->oe, + .index = ctx->index, + .redirect = d->upperredirect, };/* Store lowerdata redirect for lazy lookup */ -if (ctr >1 && !d.is_dir && !stack[ctr -1 ].dentry) { - oip.lowerdata_redirect = d.redirect; - d.redirect =NULL ; +if (ctx->ctr >1 && !d->is_dir && !ctx->stack[ctx->ctr -1 ].dentry) { + oip.lowerdata_redirect = d->redirect; + d->redirect =NULL ; } + inode =ovl_get_inode (dentry->d_sb, &oip); - err =PTR_ERR (inode);if (IS_ERR (inode)) - goto out_free_oe; -if (upperdentry && !uppermetacopy) -ovl_set_flag (OVL_UPPERDATA , inode); +return PTR_ERR (inode); + + ctx->inode = inode; +if (ctx->upperdentry && !uppermetacopy) +ovl_set_flag (OVL_UPPERDATA , ctx->inode);if (metacopy_size >OVL_METACOPY_MIN_SIZE ) -ovl_set_flag (OVL_HAS_DIGEST , inode); +ovl_set_flag (OVL_HAS_DIGEST , ctx->inode); } -ovl_dentry_init_reval (dentry, upperdentry,OVL_I_E (inode)); +ovl_dentry_init_reval (dentry, ctx->upperdentry,OVL_I_E (ctx->inode)); + +return 0 ; +} + +struct dentry *ovl_lookup (struct inode *dir,struct dentry *dentry, +unsigned int flags) +{ +const struct cred *old_cred; +struct ovl_fs *ofs =OVL_FS (dentry->d_sb); +struct ovl_entry *poe =OVL_E (dentry->d_parent); +int err; +struct ovl_lookup_ctx ctx = { + .dentry = dentry, + }; +struct ovl_lookup_data d = { + .sb = dentry->d_sb, + .dentry = dentry, + .name = dentry->d_name, + .last =ovl_redirect_follow (ofs) ? false : !ovl_numlower (poe), + }; + +if (dentry->d_name.len > ofs->namelen) +return ERR_PTR (-ENAMETOOLONG ); + + old_cred =ovl_override_creds (dentry->d_sb); + + err =ovl_lookup_layers (&ctx, &d);ovl_revert_creds (old_cred); -if (origin_path) { -dput (origin_path->dentry); -kfree (origin_path); +if (ctx.origin_path) { +dput (ctx.origin_path->dentry); +kfree (ctx.origin_path); } -dput (index); -ovl_stack_free (stack, ctr); +dput (ctx.index); +ovl_stack_free (ctx.stack, ctx.ctr);kfree (d.redirect); -return d_splice_alias (inode, dentry); -out_free_oe: -ovl_free_entry (oe); -out_put: -dput (index); -ovl_stack_free (stack, ctr); -out_put_upper: -if (origin_path) { -dput (origin_path->dentry); -kfree (origin_path); +if (err) { +ovl_free_entry (ctx.oe); +dput (ctx.upperdentry); +kfree (d.upperredirect); +return ERR_PTR (err); } -dput (upperdentry); -kfree (d.upperredirect); -out: -kfree (d.redirect); -ovl_revert_creds (old_cred); -return ERR_PTR (err); + +return d_splice_alias (ctx.inode, dentry); }bool ovl_lower_positive (struct dentry *dentry)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 ovl: do not read the flags of a put upper dentry in lookup
In reply to Sasha LevinFrom: Amir Goldstein <[email protected]>
[ Upstream commit 7031366be218f291592a7ad38bc34a7cd1459737 ]
ovl_lookup_layers() passes ctx->upperdentry to ovl_get_inode(), which takes over the reference. If an overlay inode for the upper inode is already cached, as for a second hard link, ovl_get_inode() puts the dentry and returns the cached inode. ovl_lookup_layers() then passes the put dentry to ovl_dentry_init_reval(), which reads its d_flags.
With a local upper fs the put dentry usually stays in the dcache, but with a FUSE upper whose entry has timed out, dput() kills it, so this is a use-after-free.
Fix this by taking the revalidate flags from the upper dentry of the overlay inode, which is the one that ovl_dentry_revalidate_common() revalidates.
--- fs/overlayfs/namei.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
fs/overlayfs/namei.c +10 -2
@@ -1363,9 +1363,17 @@ static int ovl_lookup_layers(struct ovl_lookup_ctx *ctx, struct ovl_lookup_dataif (metacopy_size >OVL_METACOPY_MIN_SIZE )ovl_set_flag (OVL_HAS_DIGEST , ctx->inode); - } -ovl_dentry_init_reval (dentry, ctx->upperdentry,OVL_I_E (ctx->inode)); +/* +* ovl_get_inode() puts ctx->upperdentry if it finds a cached +* inode, e.g. for another hard link, so use the upper dentry +* of the inode. +*/ +ovl_dentry_init_reval (dentry,ovl_i_dentry_upper (inode), +OVL_I_E (inode)); + }else { +ovl_dentry_init_reval (dentry,NULL ,NULL ); + }return 0 ; }
Signature · 1 line
2.53.0