Skip to contentSkip to conversation

vfs: Fix use-after-free of vfsmount by mnt_drop_write()

PATCH In review OGAWA Hirofumi 2 participants 3 messages Feb 22, 2010 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfs: Fix use-after-free of vfsmount by mnt_drop_write()

Hi,

Please review this. (This originally was for #untested branch, so this is not minimal change perfectly.)

Those might be assuming that the nd->path.mnt refcnt is keeped by filp after nameidata_to_filp().

It's wrong if nameidata_to_filp() returned the error. (nd->path and filp are invalid)

Instead to use deep knowledge of nameidata_to_filp() internal, this thinks the nd->path is invalid after nameidata_to_filp(). So, this just take refcnt for mnt_want/drop_write().

---

 fs/namei.c |   35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
fs/namei.c renamed from fs/namei.c~nameidata_to_filp-mount-writer-fix +23 -12
diff -puN fs/namei.c~nameidata_to_filp-mount-writer-fix fs/namei.c
@@ -1617,6 +1617,7 @@ struct file *do_filp_open(int dfd, const
 	struct path path;
 	struct dentry *dir;
 	int count = 0;
+	struct vfsmount *uninitialized_var(mnt_writer);
 	int will_truncate;
 	int flag = open_to_namei_flags(open_flag);
 	int force_reval = 0;
@@ -1733,16 +1734,20 @@ do_last:
 		 * a permanent write count is taken through
 		 * the 'struct file' in nameidata_to_filp().
 		 */
-		error = mnt_want_write(nd.path.mnt);
+		mnt_writer = nd.path.mnt;
+		error = mnt_want_write(mnt_writer);
 		if (error)
 			goto exit_mutex_unlock;
 		error = __open_namei_create(&nd, &path, flag, mode);
 		if (error) {
-			mnt_drop_write(nd.path.mnt);
+			mnt_drop_write(mnt_writer);
 			goto exit;
 		}
+		mntget(mnt_writer);
+		/* nd->path is invalid after this */
 		filp = nameidata_to_filp(&nd);
-		mnt_drop_write(nd.path.mnt);
+		mnt_drop_write(mnt_writer);
+		mntput(mnt_writer);
 		if (nd.root.mnt)
 			path_put(&nd.root);
 		if (!IS_ERR(filp)) {
@@ -1794,16 +1799,20 @@ ok:
 	 */
 	will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
 	if (will_truncate) {
-		error = mnt_want_write(nd.path.mnt);
+		mnt_writer = nd.path.mnt;
+		error = mnt_want_write(mnt_writer);
 		if (error)
 			goto exit;
 	}
 	error = may_open(&nd.path, acc_mode, flag);
 	if (error) {
 		if (will_truncate)
-			mnt_drop_write(nd.path.mnt);
+			mnt_drop_write(mnt_writer);
 		goto exit;
 	}
+	if (will_truncate)
+		mntget(mnt_writer);
+	/* nd->path is invalid after this */
 	filp = nameidata_to_filp(&nd);
 	if (!IS_ERR(filp)) {
 		error = ima_file_check(filp, acc_mode);
@@ -1814,10 +1823,10 @@ ok:
 	}
 	if (!IS_ERR(filp)) {
 		if (acc_mode & MAY_WRITE)
-			vfs_dq_init(nd.path.dentry->d_inode);
+			vfs_dq_init(filp->f_path.dentry->d_inode);
 
 		if (will_truncate) {
-			error = handle_truncate(&nd.path);
+			error = handle_truncate(&filp->f_path);
 			if (error) {
 				fput(filp);
 				filp = ERR_PTR(error);
@@ -1825,12 +1834,14 @@ ok:
 		}
 	}
 	/*
-	 * It is now safe to drop the mnt write
-	 * because the filp has had a write taken
-	 * on its behalf.
+	 * It is now safe to drop the mnt write because the filp has
+	 * had a write taken on its behalf. (NOTE: since O_TRUNC can
+	 * be used with O_RDONLY, this needs to cover truncate path)
 	 */
-	if (will_truncate)
-		mnt_drop_write(nd.path.mnt);
+	if (will_truncate) {
+		mnt_drop_write(mnt_writer);
+		mntput(mnt_writer);
+	}
 	if (nd.root.mnt)
 		path_put(&nd.root);
 	return filp;

_

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#1 lore raw
Quoted 10 lines · OGAWA Hirofumi

Hi,

Please review this. (This originally was for #untested branch, so this is not minimal change perfectly.)

Those might be assuming that the nd->path.mnt refcnt is keeped by filp after nameidata_to_filp().

Quoted 6 lines

It's wrong if nameidata_to_filp() returned the error. (nd->path and filp are invalid)

Instead to use deep knowledge of nameidata_to_filp() internal, this thinks the nd->path is invalid after nameidata_to_filp(). So, this just take refcnt for mnt_want/drop_write().

You might take a look at the patch I just posted:

Subject: [RFC PATCH] VFS: Simplify truncate logic in do_filp_open()

If that patch is correct, it will considerably simplify the second part of your patch.

-VAL

Quoted 106 lines

---

 fs/namei.c |   35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff -puN fs/namei.c~nameidata_to_filp-mount-writer-fix fs/namei.c

fs/namei.c renamed from fs/namei.c~nameidata_to_filp-mount-writer-fix +23 -12
@@ -1617,6 +1617,7 @@ struct file *do_filp_open(int dfd, const
 	struct path path;
 	struct dentry *dir;
 	int count = 0;
+	struct vfsmount *uninitialized_var(mnt_writer);
 	int will_truncate;
 	int flag = open_to_namei_flags(open_flag);
 	int force_reval = 0;
@@ -1733,16 +1734,20 @@ do_last:
 		 * a permanent write count is taken through
 		 * the 'struct file' in nameidata_to_filp().
 		 */
-		error = mnt_want_write(nd.path.mnt);
+		mnt_writer = nd.path.mnt;
+		error = mnt_want_write(mnt_writer);
 		if (error)
 			goto exit_mutex_unlock;
 		error = __open_namei_create(&nd, &path, flag, mode);
 		if (error) {
-			mnt_drop_write(nd.path.mnt);
+			mnt_drop_write(mnt_writer);
 			goto exit;
 		}
+		mntget(mnt_writer);
+		/* nd->path is invalid after this */
 		filp = nameidata_to_filp(&nd);
-		mnt_drop_write(nd.path.mnt);
+		mnt_drop_write(mnt_writer);
+		mntput(mnt_writer);
 		if (nd.root.mnt)
 			path_put(&nd.root);
 		if (!IS_ERR(filp)) {
@@ -1794,16 +1799,20 @@ ok:
 	 */
 	will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
 	if (will_truncate) {
-		error = mnt_want_write(nd.path.mnt);
+		mnt_writer = nd.path.mnt;
+		error = mnt_want_write(mnt_writer);
 		if (error)
 			goto exit;
 	}
 	error = may_open(&nd.path, acc_mode, flag);
 	if (error) {
 		if (will_truncate)
-			mnt_drop_write(nd.path.mnt);
+			mnt_drop_write(mnt_writer);
 		goto exit;
 	}
+	if (will_truncate)
+		mntget(mnt_writer);
+	/* nd->path is invalid after this */
 	filp = nameidata_to_filp(&nd);
 	if (!IS_ERR(filp)) {
 		error = ima_file_check(filp, acc_mode);
@@ -1814,10 +1823,10 @@ ok:
 	}
 	if (!IS_ERR(filp)) {
 		if (acc_mode & MAY_WRITE)
-			vfs_dq_init(nd.path.dentry->d_inode);
+			vfs_dq_init(filp->f_path.dentry->d_inode);
 
 		if (will_truncate) {
-			error = handle_truncate(&nd.path);
+			error = handle_truncate(&filp->f_path);
 			if (error) {
 				fput(filp);
 				filp = ERR_PTR(error);
@@ -1825,12 +1834,14 @@ ok:
 		}
 	}
 	/*
-	 * It is now safe to drop the mnt write
-	 * because the filp has had a write taken
-	 * on its behalf.
+	 * It is now safe to drop the mnt write because the filp has
+	 * had a write taken on its behalf. (NOTE: since O_TRUNC can
+	 * be used with O_RDONLY, this needs to cover truncate path)
 	 */
-	if (will_truncate)
-		mnt_drop_write(nd.path.mnt);
+	if (will_truncate) {
+		mnt_drop_write(mnt_writer);
+		mntput(mnt_writer);
+	}
 	if (nd.root.mnt)
 		path_put(&nd.root);
 	return filp;

_

Signature · 5 lines
OGAWA Hirofumi <[email protected]>
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 16 lines · Valerie Aurora
Quoted 9 lines

Those might be assuming that the nd->path.mnt refcnt is keeped by filp after nameidata_to_filp().

It's wrong if nameidata_to_filp() returned the error. (nd->path and filp are invalid)

Instead to use deep knowledge of nameidata_to_filp() internal, this thinks the nd->path is invalid after nameidata_to_filp(). So, this just take refcnt for mnt_want/drop_write().

You might take a look at the patch I just posted:

Subject: [RFC PATCH] VFS: Simplify truncate logic in do_filp_open()

If that patch is correct, it will considerably simplify the second part of your patch.

[...]

Quoted 6 lines
Quoted 6 lines
fs/namei.c renamed from fs/namei.c~nameidata_to_filp-mount-writer-fix +2 -3
 	/*
-	 * It is now safe to drop the mnt write
-	 * because the filp has had a write taken
-	 * on its behalf.
+	 * It is now safe to drop the mnt write because the filp has
+	 * had a write taken on its behalf. (NOTE: since O_TRUNC can
Quoted 8 lines

fs/namei.c ok: · line 1844

Quoted 8 lines
fs/namei.c renamed from fs/namei.c~nameidata_to_filp-mount-writer-fix +5 -2
+	 * be used with O_RDONLY, this needs to cover truncate path)
 	 */
-	if (will_truncate)
-		mnt_drop_write(nd.path.mnt);
+	if (will_truncate) {
+		mnt_drop_write(mnt_writer);
+		mntput(mnt_writer);
+	}

Unfortunately, no. I already noted that we can't it simply. ;)

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>