ovl: backports of fixes that failed to apply
cover ovl: backports of fixes that failed to apply
Greg,
These are backports of three overlayfs fixes from ovl-fixes-7.3 that failed to apply to the stable trees:
e34d2ef818e4 ("ovl: check that the parents of volatile/dirty are dirs") 16d319e12328 ("ovl: hold a reference on the parent in readdir") f635192ad223 ("ovl: do not copy up a dentry that was removed")
The same backports are expected to apply cleanly to 6.18, 6.12, 6.6 and 6.1, so please apply this series to all four trees.
The patches do not depend on each other and can be applied in any order.
Thanks,
Amir.
Chris Mason (3):
ovl: check that the parents of volatile/dirty are dirs
ovl: hold a reference on the parent in readdir
ovl: do not copy up a dentry that was removed
fs/overlayfs/copy_up.c | 7 +++++++ fs/overlayfs/readdir.c | 26 +++++++++++++++++++------- fs/overlayfs/super.c | 16 ++++++++++++---- 3 files changed, 38 insertions(+), 11 deletions(-)
Signature · 1 line
backport
PATCH 1/3 ovl: check that the parents of volatile/dirty are dirs
From: Chris Mason <[email protected]>
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.
[backport: no ovl_start_creating_upper(), check the child of
ovl_lookup_upper() in ovl_lookup_or_create() instead]
--- fs/overlayfs/super.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
fs/overlayfs/super.c +12 -4
@@ -636,17 +636,25 @@ static int ovl_check_rename_whiteout(struct ovl_fs *ofs)return err; }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);
@@ -667,7 +675,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); }
Signature · 1 line
backport
PATCH 2/3 ovl: hold a reference on the parent in readdir
In reply to Amir GoldsteinFrom: Chris Mason <[email protected]>
commit 16d319e12328acafeafd3eecdf09c46d7fb43452 upstream.
Listing a directory on an overlay uses a freed dentry and crashes the kernel when a lookup moves the directory at the same time.
Code that can sleep may only use d_parent without a reference while something stops a move of the dentry. ovl_iterate_real(), ovl_need_adjust_d_ino() and ovl_cache_update() use the d_parent of the listed directory and hold only the lock of that directory. When a lookup finds the directory under another parent after a change to a layer, d_splice_alias() moves it without that lock and drops its reference on the old parent.
With a delay in ovl_iterate_real(), getdents64 on an overlay directory raced a rename in the lower layer and a lookup that moved its dentry, and after an rmdir of the old parent KASAN reported a use-after-free in security_inode_getattr().
Fix this by using dget_parent() in the three functions.
[backport: no ovl_need_adjust_d_ino(), take the parent with dget_parent()
in ovl_iterate() and test a local flag]
--- fs/overlayfs/readdir.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
fs/overlayfs/readdir.c +19 -7
@@ -572,8 +572,8 @@ static int ovl_cache_update(const struct path *path, struct ovl_cache_entry *p, goto get; }if (p->len ==2 && p->name[1 ] =='.' ) { -/* we shall not be moved */ - this =dget (dir->d_parent); +/* d_splice_alias() can move even a locked dir */ + this =dget_parent (dir); goto get; } }
@@ -802,6 +802,7 @@ static int ovl_iterate_real(struct file *file, struct dir_context *ctx)struct dentry *dir = file->f_path.dentry;struct ovl_fs *ofs =OVL_FS (dir->d_sb);const struct ovl_layer *lower_layer =ovl_layer_lower (dir); +struct dentry *parent;struct ovl_readdir_translate rdt = { .ctx.actor = ovl_fill_real, .ctx.count = ctx->count,
@@ -813,18 +814,26 @@ static int ovl_iterate_real(struct file *file, struct dir_context *ctx)if (rdt.xinobits && lower_layer) rdt.fsid = lower_layer->fsid; -if (OVL_TYPE_MERGE (ovl_path_type (dir->d_parent))) { +/* +* The lock on dir does not stop d_splice_alias() from moving it to +* another parent, which drops the reference dir holds on this one. +*/ + parent =dget_parent (dir); +if (OVL_TYPE_MERGE (ovl_path_type (parent))) {struct kstat stat;struct path statpath = file->f_path; - statpath.dentry = dir->d_parent; + statpath.dentry = parent; err =vfs_getattr (&statpath, &stat,STATX_INO ,0 ); -if (err) +if (err) { +dput (parent);return err; + }WARN_ON_ONCE (dir->d_sb->s_dev != stat.dev); rdt.parent_ino = stat.ino; } +dput (parent);if (ovl_is_impure_dir (file)) { rdt.cache =ovl_cache_get_impure (&file->f_path);
@@ -852,16 +861,19 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)if (!ctx->pos)ovl_dir_reset (file);if (od->is_real) { +struct dentry *parent =dget_parent (dentry); +bool parent_merge =OVL_TYPE_MERGE (ovl_path_type (parent)); + +dput (parent);/* * If parent is merge, then need to adjust d_ino for '..', if * dir is impure then need to adjust d_ino for copied up * entries. */ if (ovl_xino_bits (ofs) || (ovl_same_fs (ofs) && - (ovl_is_impure_dir (file) || -OVL_TYPE_MERGE (ovl_path_type (dentry->d_parent))))) { + (ovl_is_impure_dir (file) || parent_merge))) { err =ovl_iterate_real (file, ctx); }else { err =iterate_dir (od->realfile, ctx);
Signature · 1 line
backport
PATCH 3/3 ovl: do not copy up a dentry that was removed
In reply to Amir GoldsteinFrom: Chris Mason <[email protected]>
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.
[backport: no start_renaming_dentry() and LOOKUP_EXCL, fail with -EEXIST if
the target of ovl_lookup_upper() is positive]
--- fs/overlayfs/copy_up.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/overlayfs/copy_up.c +7 -0
@@ -831,7 +831,14 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) err =PTR_ERR (upper);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);
Signature · 1 line
backport