Skip to contentSkip to conversation

namei: fix use-after-free and adjust calling conventions

PATCH Superseded Stephen Brennan 2 participants 6 messages Sep 7, 2021 VFS

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover namei: fix use-after-free and adjust calling conventions

Drawing from the comments on the last two patches from me and Dmitry, the concensus is that __filename_parentat() is inherently buggy, and should be removed. But there's some nice consistency to the way that the other functions (filename_create, filename_lookup) are named which would get broken.

I looked at the callers of filename_create and filename_lookup. All are small functions which are trivial to modify to include a putname(). It seems to me that adding a few more lines to these functions is a good traedoff for better clarity on lifetimes (as it's uncommon for functions to drop references to their parameters) and better consistency.

This small series combines the UAF fix from me, and the removal of __filename_parentat() from Dmitry as patch 1. Then I standardize filename_create() and filename_lookup() and their callers.

v2: Fix double getname in user_path_create()

Stephen Brennan (3):
namei: Fix use after free in kern_path_locked
namei: Standardize callers of filename_lookup()
namei: Standardize callers of filename_create()

 fs/fs_parser.c |   1 -
 fs/namei.c     | 126 ++++++++++++++++++++++++++-----------------------
 2 files changed, 66 insertions(+), 61 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/3 namei: Fix use after free in kern_path_locked

In 0ee50b47532a ("namei: change filename_parentat() calling conventions"), filename_parentat() was made to always call putname() on the filename before returning, and kern_path_locked() was migrated to this calling convention. However, kern_path_locked() uses the "last" parameter to lookup and potentially create a new dentry. The last parameter contains the last component of the path and points within the filename, which was recently freed at the end of filename_parentat(). Thus, when kern_path_locked() calls __lookup_hash(), it is using the filename after it has already been freed.

In this case, filename_parentat() is fundamentally broken due to this use after free. So, remove it, and rename __filename_parentat to filename_parentat, migrating all callers. Adjust kern_path_locked to put the filename once all users are done with it.

---
 fs/namei.c | 47 ++++++++++++++++++++++-------------------------
 1 file changed, 22 insertions(+), 25 deletions(-)
fs/namei.c +22 -25
index d049d3972695..f2af301cc79f 100644
@@ -2514,9 +2514,10 @@ static int path_parentat(struct nameidata *nd, unsigned flags,
 	return err;
 }
 
-static int __filename_parentat(int dfd, struct filename *name,
-				unsigned int flags, struct path *parent,
-				struct qstr *last, int *type)
+/* Note: this does not consume "name" */
+static int filename_parentat(int dfd, struct filename *name,
+			     unsigned int flags, struct path *parent,
+			     struct qstr *last, int *type)
 {
 	int retval;
 	struct nameidata nd;
@@ -2538,30 +2539,24 @@ static int __filename_parentat(int dfd, struct filename *name,
 	return retval;
 }
 
-static int filename_parentat(int dfd, struct filename *name,
-				unsigned int flags, struct path *parent,
-				struct qstr *last, int *type)
-{
-	int retval = __filename_parentat(dfd, name, flags, parent, last, type);
-
-	putname(name);
-	return retval;
-}
-
 /* does lookup, returns the object with parent locked */
 struct dentry *kern_path_locked(const char *name, struct path *path)
 {
+	struct filename *filename;
 	struct dentry *d;
 	struct qstr last;
 	int type, error;
 
-	error = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
-				    &last, &type);
-	if (error)
-		return ERR_PTR(error);
+	filename = getname_kernel(name);
+	error = filename_parentat(AT_FDCWD, filename, 0, path, &last, &type);
+	if (error) {
+		d = ERR_PTR(error);
+		goto out;
+	}
 	if (unlikely(type != LAST_NORM)) {
 		path_put(path);
-		return ERR_PTR(-EINVAL);
+		d = ERR_PTR(-EINVAL);
+		goto out;
 	}
 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
 	d = __lookup_hash(&last, path->dentry, 0);
@@ -2569,6 +2564,8 @@ struct dentry *kern_path_locked(const char *name, struct path *path)
 		inode_unlock(path->dentry->d_inode);
 		path_put(path);
 	}
+out:
+	putname(filename);
 	return d;
 }
 
@@ -3634,7 +3631,7 @@ static struct dentry *__filename_create(int dfd, struct filename *name,
 	 */
 	lookup_flags &= LOOKUP_REVAL;
 
-	error = __filename_parentat(dfd, name, lookup_flags, path, &last, &type);
+	error = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
 	if (error)
 		return ERR_PTR(error);
 
@@ -3996,7 +3993,7 @@ int do_rmdir(int dfd, struct filename *name)
 	int type;
 	unsigned int lookup_flags = 0;
 retry:
-	error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
+	error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
 	if (error)
 		goto exit1;
 
@@ -4135,7 +4132,7 @@ int do_unlinkat(int dfd, struct filename *name)
 	struct inode *delegated_inode = NULL;
 	unsigned int lookup_flags = 0;
 retry:
-	error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
+	error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
 	if (error)
 		goto exit1;
 
@@ -4683,13 +4680,13 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
 		target_flags = 0;
 
 retry:
-	error = __filename_parentat(olddfd, from, lookup_flags, &old_path,
-					&old_last, &old_type);
+	error = filename_parentat(olddfd, from, lookup_flags, &old_path,
+				  &old_last, &old_type);
 	if (error)
 		goto put_names;
 
-	error = __filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
-				&new_type);
+	error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
+				  &new_type);
 	if (error)
 		goto exit1;
 
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/3 namei: Standardize callers of filename_lookup()

In reply to Stephen Brennan

filename_lookup() has two variants, one which drops the caller's reference to filename (filename_lookup), and one which does not (__filename_lookup). This can be confusing as it's unusual to drop a caller's reference. Remove filename_lookup, rename __filename_lookup to filename_lookup, and convert all callers. The cost is a few slightly longer functions, but the clarity is greater.

---
 fs/fs_parser.c |  1 -
 fs/namei.c     | 41 ++++++++++++++++++++++++-----------------
 2 files changed, 24 insertions(+), 18 deletions(-)
fs/fs_parser.c +0 -1
index 980d44fd3a36..3df07c0e32b3 100644
@@ -165,7 +165,6 @@ int fs_lookup_param(struct fs_context *fc,
 		return invalf(fc, "%s: not usable as path", param->key);
 	}
 
-	f->refcnt++; /* filename_lookup() drops our ref. */
 	ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
 	if (ret < 0) {
 		errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
fs/namei.c +24 -17

79 lines not on this page · raw message

Signature · 1 line
2.30.2
#3 lore raw

PATCH v2 3/3 namei: Standardize callers of filename_create()

In reply to Stephen Brennan

filename_create() has two variants, one which drops the caller's reference to filename (filename_create) and one which does not (__filename_create). This can be confusing as it's unusual to drop a caller's reference. Remove filename_create, rename __filename_create to filename_create, and convert all callers.

---
 fs/namei.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)
fs/namei.c +20 -18
index 76871b7f127a..0d3ce04c55ad 100644
@@ -3622,8 +3622,8 @@ struct file *do_file_open_root(const struct path *root,
 	return file;
 }
 
-static struct dentry *__filename_create(int dfd, struct filename *name,
-				struct path *path, unsigned int lookup_flags)
+static struct dentry *filename_create(int dfd, struct filename *name,
+				      struct path *path, unsigned int lookup_flags)
 {
 	struct dentry *dentry = ERR_PTR(-EEXIST);
 	struct qstr last;
@@ -3691,20 +3691,16 @@ static struct dentry *__filename_create(int dfd, struct filename *name,
 	return dentry;
 }
 
-static inline struct dentry *filename_create(int dfd, struct filename *name,
-				struct path *path, unsigned int lookup_flags)
-{
-	struct dentry *res = __filename_create(dfd, name, path, lookup_flags);
-
-	putname(name);
-	return res;
-}
-
 struct dentry *kern_path_create(int dfd, const char *pathname,
 				struct path *path, unsigned int lookup_flags)
 {
-	return filename_create(dfd, getname_kernel(pathname),
-				path, lookup_flags);
+	struct filename *filename;
+	struct dentry *dentry;
+
+	filename = getname_kernel(pathname);
+	dentry = filename_create(dfd, filename, path, lookup_flags);
+	putname(filename);
+	return dentry;
 }
 EXPORT_SYMBOL(kern_path_create);
 
@@ -3720,7 +3716,13 @@ EXPORT_SYMBOL(done_path_create);
 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
 				struct path *path, unsigned int lookup_flags)
 {
-	return filename_create(dfd, getname(pathname), path, lookup_flags);
+	struct filename *filename;
+	struct dentry *dentry;
+
+	filename = getname(pathname);
+	dentry = filename_create(dfd, pathname, path, lookup_flags);
+	putname(filename);
+	return dentry;
 }
 EXPORT_SYMBOL(user_path_create);
 
@@ -3801,7 +3803,7 @@ static int do_mknodat(int dfd, struct filename *name, umode_t mode,
 	if (error)
 		goto out1;
 retry:
-	dentry = __filename_create(dfd, name, &path, lookup_flags);
+	dentry = filename_create(dfd, name, &path, lookup_flags);
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto out1;
@@ -3901,7 +3903,7 @@ int do_mkdirat(int dfd, struct filename *name, umode_t mode)
 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
 
 retry:
-	dentry = __filename_create(dfd, name, &path, lookup_flags);
+	dentry = filename_create(dfd, name, &path, lookup_flags);
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto out_putname;
@@ -4268,7 +4270,7 @@ int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
 		goto out_putnames;
 	}
 retry:
-	dentry = __filename_create(newdfd, to, &path, lookup_flags);
+	dentry = filename_create(newdfd, to, &path, lookup_flags);
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto out_putnames;
@@ -4432,7 +4434,7 @@ int do_linkat(int olddfd, struct filename *old, int newdfd,
 	if (error)
 		goto out_putnames;
 
-	new_dentry = __filename_create(newdfd, new, &new_path,
+	new_dentry = filename_create(newdfd, new, &new_path,
 					(how & LOOKUP_REVAL));
 	error = PTR_ERR(new_dentry);
 	if (IS_ERR(new_dentry))
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v2 3/3] namei: Standardize callers of filename_create()

Thank you for the patch! Yet something to improve:

Hi Stephen,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on next-20210907]
[cannot apply to vfs/for-next linux/master v5.14]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Stephen-Brennan/namei-fix-use-after-free-and-adjust-calling-conventions/20210908-045151
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git a2b28235335fee2586b4bd16448fb59ed6c80eef
config: nios2-defconfig (attached as .config)
compiler: nios2-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/a598a6633bb421e9b3d9d0e3f038cbd0ab0b90a1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Stephen-Brennan/namei-fix-use-after-free-and-adjust-calling-conventions/20210908-045151 git checkout a598a6633bb421e9b3d9d0e3f038cbd0ab0b90a1
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=nios2

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

fs/namei.c: In function 'user_path_create':

>> fs/namei.c:3723:39: error: passing argument 2 of 'filename_create' from incompatible pointer type [-Werror=incompatible-pointer-types]
    3723 |         dentry = filename_create(dfd, pathname, path, lookup_flags);
         |                                       ^~~~~~~~
         |                                       |
         |                                       const char *

fs/namei.c:3625:65: note: expected 'struct filename *' but argument is of type 'const char *'
3625 | static struct dentry *filename_create(int dfd, struct filename *name,
| ~~~~~~~~~~~~~~~~~^~~~
cc1: all warnings being treated as errors

vim +/filename_create +3723 fs/namei.c

3715
3716 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3717 struct path *path, unsigned int lookup_flags)
3718 {
3719 struct filename *filename;
3720 struct dentry *dentry;
3721
3722 filename = getname(pathname);

> 3723		dentry = filename_create(dfd, pathname, path, lookup_flags);

3724 putname(filename);
3725 return dentry;
3726 }
3727 EXPORT_SYMBOL(user_path_create);
3728

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw
In reply to Stephen Brennan
Quoted 16 lines · Stephen Brennan

Drawing from the comments on the last two patches from me and Dmitry, the concensus is that __filename_parentat() is inherently buggy, and should be removed. But there's some nice consistency to the way that the other functions (filename_create, filename_lookup) are named which would get broken.

I looked at the callers of filename_create and filename_lookup. All are small functions which are trivial to modify to include a putname(). It seems to me that adding a few more lines to these functions is a good traedoff for better clarity on lifetimes (as it's uncommon for functions to drop references to their parameters) and better consistency.

This small series combines the UAF fix from me, and the removal of __filename_parentat() from Dmitry as patch 1. Then I standardize filename_create() and filename_lookup() and their callers.

Quoted 6 lines

v2: Fix double getname in user_path_create()

Stephen Brennan (3):
namei: Fix use after free in kern_path_locked
namei: Standardize callers of filename_lookup()
namei: Standardize callers of filename_create()

Please ignore this! I missed some feedback from Al and will send a v3.

Quoted 7 lines
 fs/fs_parser.c |   1 -
 fs/namei.c     | 126 ++++++++++++++++++++++++++-----------------------
 2 files changed, 66 insertions(+), 61 deletions(-)
Signature · 1 line
2.30.2