Skip to contentSkip to conversation

Overlayfs fixes from Chris Mason (batch 1)

PATCH In review Amir Goldstein 1 participant 11 messages Oct 29

Subsystems (4)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

cover Overlayfs fixes from Chris Mason (batch 1)

Hi folks,

The following batch of patches is part of a collection of overlayfs bugs reported by Chris Mason.

These bugs were found by Chris' AI code scanner (kres), but underwent extensive manual auditing and filtering by both Chris and Josef.

I thank them both for handing me a reasonably sized pile of patches, most of them important, correct and ready to merge.

The patches selected in this batch were either simple enough to merge without questions or important enough for me to invest the extra time.

Patches that I changed include a note [amir: ...] with my changes above my signature.

Notable changes from Chris' version:
- Include "fortify ovl_copy_xattr()" and "assertions in dir cache" patches already posted publicly for similar reported issues
- Use LOOKUP_EXCL in "do not copy up a dentry that was removed"
- Add vfs helper try_take_dentry_name_snapshot() instead of open coding it in overlayfs code
- Fix for empty security.capability from Josef adjusted and rebased onto "fortify ovl_copy_xattr()" refactoring

This pile applies cleanly and was tested on top of 7.3 and on top of master of the moment.

Backports down to 6.1.y are either trivial or already prepared in my local tree. I will make those available to the stable maintainers after upstream merge.

PR will follow soon.

Thanks,
Amir.

Amir Goldstein (4):
ovl: fortify ovl_copy_xattr() against underlying layer modifications
ovl: add assertions in dir cache code
vfs: add try_take_dentry_name_snapshot()
ovl: skip copy up of empty security.capability

Chris Mason (12):
ovl: do not copy up an xattr value that was never read
ovl: set OVL_WHITEOUTS before publishing the upper dir
ovl: check that the parents of volatile/dirty are dirs
ovl: take a snapshot of the name for copy up
ovl: do not take a name snapshot in RCU walk
ovl: use the pinned upper dir in ovl_fix_origin()
ovl: hold a reference on the parent in readdir
ovl: do not copy up a dentry that was removed
ovl: fix a torn path in ovl_get_redirect()
ovl: read the nlink xattr of every indexed non-dir
ovl: always encode the origin fh when index is enabled
ovl: set the revalidate flags after d_instantiate()

 fs/dcache.c              |  29 +++++++++--
 fs/overlayfs/copy_up.c   | 110 ++++++++++++++++++++++++---------------
 fs/overlayfs/dir.c       |  22 +++++++-
 fs/overlayfs/inode.c     |  14 +++--
 fs/overlayfs/namei.c     |   8 +--
 fs/overlayfs/overlayfs.h |   8 ++-
 fs/overlayfs/readdir.c   |  61 ++++++++++++++++++----
 fs/overlayfs/super.c     |  21 ++++++--
 include/linux/dcache.h   |   1 +
 9 files changed, 203 insertions(+), 71 deletions(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 1/16 ovl: do not copy up an xattr value that was never read

From: Chris Mason <[email protected]>

Copy-up stores uninitialized kernel memory in an xattr of the upper file, where the user can read it.

ovl_copy_xattr() may only pass bytes to ovl_do_setxattr() that ovl_do_getxattr() has read. When the read fails with -ERANGE the function asks for the size again, and if the new size fits the buffer it stores the buffer although nothing was read into it. The buffer comes from kvmalloc(), or it still holds the value of the previous xattr.

A FUSE lower layer that answers every sized GETXATTR with -ERANGE made copy-up store 4096 bytes that the server never sent, with a kernel pointer in them, as an xattr of the upper file.

Fix this by failing the copy-up with -EIO when the size that follows -ERANGE fits the buffer.

---
 fs/overlayfs/copy_up.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/overlayfs/copy_up.c +5 -1
index e963701b4c877..c220cb5d0b70c 100644
@@ -131,8 +131,12 @@ int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct de
 
 retry:
 		size = ovl_do_getxattr(oldpath, name, value, value_size);
-		if (size == -ERANGE)
+		if (size == -ERANGE) {
 			size = ovl_do_getxattr(oldpath, name, NULL, 0);
+			/* Nothing was read into value, so it must not be used */
+			if (size >= 0 && size <= value_size)
+				size = -EIO;
+		}
 
 		if (size < 0) {
 			error = size;
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 2/16 ovl: fortify ovl_copy_xattr() against underlying layer modifications

In reply to Amir Goldstein

Adapt ovl_getxattr_value() to be used from ovl_copy_xattr(), by allowing to pass it a pre-allocated buffer, which can be resized and by handling the ENODATA/EOPNOTSUPP return codes in the caller.

This replaces the retry loop in ovl_copy_xattr() with a single retry after resizing the buffer. If the value changed again by then, the underlying layer was modified, so return -EIO instead of retrying.

As a side effect, ovl_copy_up_meta_inode_data() now returns -EIO instead of -ERANGE in this case of underlying xattr size changes.

---
 fs/overlayfs/copy_up.c | 68 ++++++++++++++++++++----------------------
 1 file changed, 32 insertions(+), 36 deletions(-)
fs/overlayfs/copy_up.c +32 -36
index c220cb5d0b70c..eee22581b14f1 100644
@@ -72,6 +72,9 @@ static int ovl_copy_acl(struct ovl_fs *ofs, const struct path *path,
 	return err;
 }
 
+static ssize_t ovl_getxattr_value(const struct path *path, char *name,
+				  char **value, ssize_t *value_size);
+
 int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new)
 {
 	struct dentry *old = oldpath->dentry;
@@ -129,34 +132,12 @@ int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct de
 			break;
 		}
 
-retry:
-		size = ovl_do_getxattr(oldpath, name, value, value_size);
-		if (size == -ERANGE) {
-			size = ovl_do_getxattr(oldpath, name, NULL, 0);
-			/* Nothing was read into value, so it must not be used */
-			if (size >= 0 && size <= value_size)
-				size = -EIO;
-		}
-
+		size = ovl_getxattr_value(oldpath, name, &value, &value_size);
 		if (size < 0) {
 			error = size;
 			break;
 		}
 
-		if (size > value_size) {
-			void *new;
-
-			new = kvmalloc(size, GFP_KERNEL);
-			if (!new) {
-				error = -ENOMEM;
-				break;
-			}
-			kvfree(value);
-			value = new;
-			value_size = size;
-			goto retry;
-		}
-
 		error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0);
 		if (error) {
 			if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
@@ -1059,26 +1040,39 @@ static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
 	return true;
 }
 
-static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value)
+static ssize_t ovl_getxattr_value(const struct path *path, char *name,
+				  char **value, ssize_t *value_size)
 {
 	ssize_t res;
 	char *buf;
 
-	res = ovl_do_getxattr(path, name, NULL, 0);
-	if (res == -ENODATA || res == -EOPNOTSUPP)
-		res = 0;
+	res = ovl_do_getxattr(path, name, *value, *value_size);
+	if (res == -ERANGE) {
+		res = ovl_do_getxattr(path, name, NULL, 0);
+		/* Xattr changed underneath us? */
+		if (res >= 0 && res <= *value_size)
+			return -EIO;
+	}
 
-	if (res > 0) {
-		buf = kzalloc(res, GFP_KERNEL);
+	if (res > *value_size) {
+		buf = kvmalloc(res, GFP_KERNEL);
 		if (!buf)
 			return -ENOMEM;
 
 		res = ovl_do_getxattr(path, name, buf, res);
-		if (res < 0)
-			kfree(buf);
-		else
-			*value = buf;
+		if (res < 0) {
+			kvfree(buf);
+			/* Xattr changed underneath us? */
+			if (res == -ERANGE)
+				return -EIO;
+			return res;
+		}
+
+		kvfree(*value);
+		*value = buf;
+		*value_size = res;
 	}
+
 	return res;
 }
 
@@ -1089,7 +1083,7 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
 	struct path upperpath;
 	int err;
 	char *capability = NULL;
-	ssize_t cap_size;
+	ssize_t cap_size = 0;
 
 	ovl_path_upper(c->dentry, &upperpath);
 	if (WARN_ON(upperpath.dentry == NULL))
@@ -1097,7 +1091,9 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
 
 	if (c->stat.size) {
 		err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS,
-						    &capability);
+						    &capability, &cap_size);
+		if (cap_size == -ENODATA || cap_size == -EOPNOTSUPP)
+			cap_size = 0;
 		if (cap_size < 0)
 			goto out;
 	}
@@ -1127,7 +1123,7 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
 	ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry));
 	ovl_set_upperdata(d_inode(c->dentry));
 out_free:
-	kfree(capability);
+	kvfree(capability);
 out:
 	return err;
 }
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH 3/16 ovl: set OVL_WHITEOUTS before publishing the upper dir

In reply to Amir Goldstein

From: Chris Mason <[email protected]>

Listing a directory while it is copied up causes a use-after-free of the readdir cache.

A merge directory must never be iterated as a real upper directory:
that mode frees the cache in OVL_I(inode)->cache without looking at its refcount, while files opened in merged mode still use it. ovl_copy_up_workdir() publishes the upper dentry before it sets OVL_WHITEOUTS, and ovl_dir_open() samples both without a lock, so an open between the two stores finds the upper directory and takes it for a real one.

With a delay between the two stores, an unprivileged process that listed a directory during its copy-up saw only "." and "..", and its next getdents freed the readdir cache of another descriptor.

Fix this by setting OVL_WHITEOUTS before ovl_inode_update(), so that ovl_dir_reset() agrees with ovl_dir_open(), and by testing OVL_TYPE_MERGE() of the path type in ovl_dir_open(), so that an open that observes the upper dentry never iterates a merge dir as a real dir, regardless of the order in which it observes the flag.

Fixes b79e05aaa166 ("ovl: no direct iteration for dir with origin xattr")
[amir: test OVL_TYPE_MERGE() instead of smp_rmb() in ovl_dir_open()]
---
 fs/overlayfs/copy_up.c | 2 +-
 fs/overlayfs/readdir.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
fs/overlayfs/copy_up.c +1 -1
index eee22581b14f1..92b3d08a02349 100644
@@ -819,9 +819,9 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
 
 	if (!c->metacopy)
 		ovl_set_upperdata(inode);
-	ovl_inode_update(inode, temp);
 	if (S_ISDIR(inode->i_mode))
 		ovl_set_flag(OVL_WHITEOUTS, inode);
+	ovl_inode_update(inode, temp);
 out:
 	ovl_end_write(c->dentry);
 
fs/overlayfs/readdir.c +2 -1
index 7d6f7f6022ebe..ba542a1920127 100644
@@ -1055,7 +1055,8 @@ static int ovl_dir_open(struct inode *inode, struct file *file)
 		return PTR_ERR(realfile);
 	}
 	od->realfile = realfile;
-	od->is_real = ovl_dir_is_real(inode);
+	od->is_real = !OVL_TYPE_MERGE(type) &&
+		      !ovl_test_flag(OVL_WHITEOUTS, inode);
 	od->is_upper = OVL_TYPE_UPPER(type);
 	file->private_data = od;
 
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH 4/16 ovl: add assertions in dir cache code

In reply to Amir Goldstein

Overlayfs may keep two different types of directory cache merged (refcounted) and impure (not refcounted) in the inode.

They are mutually exclusive and an overlayfs directory is never expected to switch between merged and impure modes.

Nevertheless it is hard to prove that races in the code are not possible, so add assertions to fortify the code accessing the directory cache to make sure it does not free the unexpected cache type.

If an assertion fires the code may leak dir cache memory but it will not access freed cache memory.

---
 fs/overlayfs/readdir.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
fs/overlayfs/readdir.c +23 -2
index ba542a1920127..b875332107d31 100644
@@ -36,6 +36,7 @@ struct ovl_cache_entry {
 struct ovl_dir_cache {
 	long refcount;
 	u64 version;
+	bool is_refcounted;
 	struct list_head entries;
 	struct rb_root root;
 };
@@ -479,13 +480,31 @@ static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
 	od->cursor = p;
 }
 
+/*
+ * Merged (refcounted) and impure (not refcounted) caches are never
+ * expected to be mixed. On mismatch, detach the cache from the inode
+ * and leak it rather than misuse it. A merged cache is still freed by
+ * its last ovl_cache_put().
+ */
+static struct ovl_dir_cache *ovl_dir_cache_check(struct inode *inode,
+						 bool is_refcounted)
+{
+	struct ovl_dir_cache *cache = ovl_dir_cache(inode);
+
+	if (cache && WARN_ON(cache->is_refcounted != is_refcounted)) {
+		ovl_set_dir_cache(inode, NULL);
+		return NULL;
+	}
+	return cache;
+}
+
 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
 {
 	int res;
 	struct ovl_dir_cache *cache;
 	struct inode *inode = d_inode(dentry);
 
-	cache = ovl_dir_cache(inode);
+	cache = ovl_dir_cache_check(inode, true);
 	if (cache && ovl_inode_version_get(inode) == cache->version) {
 		WARN_ON(!cache->refcount);
 		cache->refcount++;
@@ -498,6 +517,7 @@ static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
 		return ERR_PTR(-ENOMEM);
 
 	cache->refcount = 1;
+	cache->is_refcounted = true;
 	INIT_LIST_HEAD(&cache->entries);
 	cache->root = RB_ROOT;
 
@@ -698,7 +718,7 @@ static struct ovl_dir_cache *ovl_cache_get_impure(const struct path *path)
 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
 	struct ovl_dir_cache *cache;
 
-	cache = ovl_dir_cache(inode);
+	cache = ovl_dir_cache_check(inode, false);
 	if (cache && ovl_inode_version_get(inode) == cache->version)
 		return cache;
 
@@ -710,6 +730,7 @@ static struct ovl_dir_cache *ovl_cache_get_impure(const struct path *path)
 	if (!cache)
 		return ERR_PTR(-ENOMEM);
 
+	cache->is_refcounted = false;
 	res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
 	if (res) {
 		ovl_cache_free(&cache->entries);
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH 5/16 ovl: check that the parents of volatile/dirty are dirs

In reply to Amir Goldstein

From: Chris Mason <[email protected]>

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"")
---
 fs/overlayfs/super.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/overlayfs/super.c +9 -4
index bd0a3f9039d25..18f56d463572a 100644
@@ -621,16 +621,21 @@ static int ovl_check_rename_whiteout(struct ovl_fs *ofs)
 
 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)
 {
 	struct dentry *child;
 
 	child = ovl_start_creating_upper(ofs, parent, &QSTR(name));
 	if (!IS_ERR(child)) {
-		if (!child->d_inode)
+		if (!child->d_inode) {
 			child = ovl_create_real(ofs, parent, child,
 						&QSTR(name),
-						OVL_CATTR(mode));
+						OVL_CATTR(is_dir ? S_IFDIR :
+								   S_IFREG));
+		} else if (is_dir && !d_can_lookup(child)) {
+			end_creating(child);
+			child = ERR_PTR(-ENOTDIR);
+		}
 		end_creating_keep(child);
 	}
 	dput(parent);
@@ -652,7 +657,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
2.55.0
author maintainer
#6 lore raw

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

In reply to Amir Goldstein

From: Chris Mason <[email protected]>

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 | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
fs/overlayfs/copy_up.c +12 -6
index 92b3d08a02349..12000e45fefc4 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;
Signature · 1 line
2.55.0
author maintainer
#7 lore raw

PATCH 7/16 vfs: add try_take_dentry_name_snapshot()

In reply to Amir Goldstein

take_dentry_name_snapshot() requires the caller to keep the dentry alive. The external name of a dead dentry has a zero refcount that never changes, so atomic_inc_not_zero() fails on every retry and the function never returns.

A caller in RCU walk holds no reference on the dentry. Add a variant that fails instead of retrying when it cannot get a reference on the external name, so such a caller can return -ECHILD.

---
 fs/dcache.c            | 29 ++++++++++++++++++++++++++---
 include/linux/dcache.h |  1 +
 2 files changed, 27 insertions(+), 3 deletions(-)
fs/dcache.c +26 -3
index f44c7b39c3b38..21ac036b31675 100644
@@ -358,7 +358,8 @@ static inline int dname_external(const struct dentry *dentry)
 	return dentry->d_name.name != dentry->d_shortname.string;
 }
 
-void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
+static bool __take_dentry_name_snapshot(struct name_snapshot *name,
+					struct dentry *dentry, bool retry)
 {
 	unsigned seq;
 	const unsigned char *s;
@@ -375,8 +376,12 @@ void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry
 		struct external_name *p;
 		p = container_of(s, struct external_name, name[0]);
 		// get a valid reference
-		if (unlikely(!atomic_inc_not_zero(&p->count)))
-			goto retry;
+		if (unlikely(!atomic_inc_not_zero(&p->count))) {
+			if (retry)
+				goto retry;
+			rcu_read_unlock();
+			return false;
+		}
 		name->name.name = s;
 	}
 	if (read_seqcount_retry(&dentry->d_seq, seq)) {
@@ -384,9 +389,27 @@ void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry
 		goto retry;
 	}
 	rcu_read_unlock();
+	return true;
+}
+
+void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
+{
+	__take_dentry_name_snapshot(name, dentry, true);
 }
 EXPORT_SYMBOL(take_dentry_name_snapshot);
 
+/*
+ * For a caller that holds no reference on @dentry, e.g. in RCU walk.
+ * The external name of a dead dentry has a zero refcount forever, so
+ * take_dentry_name_snapshot() would retry without end.  Fail instead.
+ */
+bool try_take_dentry_name_snapshot(struct name_snapshot *name,
+				   struct dentry *dentry)
+{
+	return __take_dentry_name_snapshot(name, dentry, false);
+}
+EXPORT_SYMBOL(try_take_dentry_name_snapshot);
+
 void release_dentry_name_snapshot(struct name_snapshot *name)
 {
 	if (unlikely(name->name.name != name->inline_name.string)) {
include/linux/dcache.h +1 -0
index 4b1ff99608e01..a4be11ca2df18 100644
@@ -629,6 +629,7 @@ struct name_snapshot {
 	union shortname_store inline_name;
 };
 void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
+bool try_take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
 void release_dentry_name_snapshot(struct name_snapshot *);
 
 static inline struct dentry *d_first_child(const struct dentry *dentry)
Signature · 1 line
2.55.0
author maintainer
#8 lore raw

PATCH 8/16 ovl: do not take a name snapshot in RCU walk

In reply to Amir Goldstein

From: Chris Mason <[email protected]>

A path lookup on an overlay over FUSE loops in the kernel for ever and stalls RCU.

take_dentry_name_snapshot() needs a dentry that the caller keeps alive, because for a dead dentry the refcount of an external name stays at zero and the function retries without end. In RCU walk ovl_revalidate_real() holds no reference on the real dentry, and the eviction of the overlay inode on another CPU can drop the last one.

With a delay in ovl_revalidate_real(), a stat() loop on a 48 byte name in an overlay over FUSE, raced with drop_caches, spun for ever in take_dentry_name_snapshot() and stalled RCU.

Fix this by using try_take_dentry_name_snapshot() in RCU walk, and by returning -ECHILD when it fails.

Fixes 5be1fa8abd7b ("Pass parent directory inode and expected name to ->d_revalidate()")
[amir: use try_take_dentry_name_snapshot() instead of open coded snapshot]
---
 fs/overlayfs/super.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/overlayfs/super.c +7 -1
index 18f56d463572a..cc4b0959faf61 100644
@@ -100,11 +100,17 @@ static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
 			dir = d_inode_rcu(parent);
 			if (!dir)
 				return -ECHILD;
+			/*
+			 * Nothing pins d in RCU walk: eviction of the overlay
+			 * inode may drop the last reference at any time.
+			 */
+			if (!try_take_dentry_name_snapshot(&n, d))
+				return -ECHILD;
 		} else {
 			parent = dget_parent(d);
 			dir = d_inode(parent);
+			take_dentry_name_snapshot(&n, d);
 		}
-		take_dentry_name_snapshot(&n, d);
 		ret = d->d_op->d_revalidate(dir, &n.name, d, flags);
 		release_dentry_name_snapshot(&n);
 		if (!(flags & LOOKUP_RCU))
Signature · 1 line
2.55.0
author maintainer
#9 lore raw

PATCH 9/16 ovl: use the pinned upper dir in ovl_fix_origin()

In reply to Amir Goldstein

From: Chris Mason <[email protected]>

Renames made directly on the upper filesystem during a lookup crash the kernel.

A dentry that is passed to vfs_setxattr() must be pinned. ovl_fix_origin() passes upper->d_parent to ovl_set_impure() without a reference, and the lookup holds no lock of the upper directory. A rename on the upper filesystem changes d_parent, and only the child pins the new parent, so a second rename and an rmdir free it.

With delays in ovl_fix_origin() and ovl_set_impure(), renaming the upper directory twice and removing the intermediate parent during the lookup of a merge directory without an origin xattr gave a KASAN use-after-free in vfs_setxattr().

Fix this by passing the upper dentry that the overlay parent holds.

Fixes 9678e6303057 ("ovl: fix inconsistent d_ino for legacy merge dir")
[amir: add helper ovl_set_impure_dir()]
---
 fs/overlayfs/namei.c     | 6 ++++--
 fs/overlayfs/overlayfs.h | 6 ++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
fs/overlayfs/namei.c +4 -2
index ca899fdfaafd2..cf7908a39f614 100644
@@ -942,8 +942,10 @@ static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
 		goto out;
 
 	err = ovl_set_origin_fh(ofs, fh, upper);
-	if (!err)
-		err = ovl_set_impure(dentry->d_parent, upper->d_parent);
+	if (!err) {
+		/* upper->d_parent is not stable, use the overlay parent */
+		err = ovl_set_impure_dir(dentry->d_parent);
+	}
 
 	ovl_drop_write(dentry);
 out:
fs/overlayfs/overlayfs.h +6 -0
index 7f3558372c599..c89739e5338b4 100644
@@ -572,6 +572,12 @@ int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
 		       enum ovl_xattr ox, const void *value, size_t size,
 		       int xerr);
 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
+
+static inline int ovl_set_impure_dir(struct dentry *dentry)
+{
+	return ovl_set_impure(dentry, ovl_dentry_upper(dentry));
+}
+
 bool ovl_inuse_trylock(struct dentry *dentry);
 void ovl_inuse_unlock(struct dentry *dentry);
 bool ovl_is_inuse(struct dentry *dentry);
Signature · 1 line
2.55.0
author maintainer
#10 lore raw
In reply to Amir Goldstein
Quoted 8 lines · Amir Goldstein

Hi folks,

The following batch of patches is part of a collection of overlayfs bugs reported by Chris Mason.

These bugs were found by Chris' AI code scanner (kres), but underwent extensive manual auditing and filtering by both Chris and Josef.

Sorry guys, I meant to CC you on this cover letter but missed.

Also apologies to the fuse list for spamming them with these overlayfs patches by mistake.

Amir.

Quoted 65 lines

I thank them both for handing me a reasonably sized pile of patches, most of them important, correct and ready to merge.

The patches selected in this batch were either simple enough to merge without questions or important enough for me to invest the extra time.

Patches that I changed include a note [amir: ...] with my changes above my signature.

Notable changes from Chris' version:
- Include "fortify ovl_copy_xattr()" and "assertions in dir cache" patches already posted publicly for similar reported issues
- Use LOOKUP_EXCL in "do not copy up a dentry that was removed"
- Add vfs helper try_take_dentry_name_snapshot() instead of open coding it in overlayfs code
- Fix for empty security.capability from Josef adjusted and rebased onto "fortify ovl_copy_xattr()" refactoring

This pile applies cleanly and was tested on top of 7.3 and on top of master of the moment.

Backports down to 6.1.y are either trivial or already prepared in my local tree. I will make those available to the stable maintainers after upstream merge.

PR will follow soon.

Thanks,
Amir.

Amir Goldstein (4):
ovl: fortify ovl_copy_xattr() against underlying layer modifications
ovl: add assertions in dir cache code
vfs: add try_take_dentry_name_snapshot()
ovl: skip copy up of empty security.capability

Chris Mason (12):
ovl: do not copy up an xattr value that was never read
ovl: set OVL_WHITEOUTS before publishing the upper dir
ovl: check that the parents of volatile/dirty are dirs
ovl: take a snapshot of the name for copy up
ovl: do not take a name snapshot in RCU walk
ovl: use the pinned upper dir in ovl_fix_origin()
ovl: hold a reference on the parent in readdir
ovl: do not copy up a dentry that was removed
ovl: fix a torn path in ovl_get_redirect()
ovl: read the nlink xattr of every indexed non-dir
ovl: always encode the origin fh when index is enabled
ovl: set the revalidate flags after d_instantiate()

 fs/dcache.c              |  29 +++++++++--
 fs/overlayfs/copy_up.c   | 110 ++++++++++++++++++++++++---------------
 fs/overlayfs/dir.c       |  22 +++++++-
 fs/overlayfs/inode.c     |  14 +++--
 fs/overlayfs/namei.c     |   8 +--
 fs/overlayfs/overlayfs.h |   8 ++-
 fs/overlayfs/readdir.c   |  61 ++++++++++++++++++----
 fs/overlayfs/super.c     |  21 ++++++--
 include/linux/dcache.h   |   1 +
 9 files changed, 203 insertions(+), 71 deletions(-)

--
2.55.0