Skip to contentSkip to conversation

Fix mountpoint reference leakage in linkat

PATCH Superseded Oleg Drokin 3 participants 9 messages Jan 31, 2014 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix mountpoint reference leakage in linkat

Recent changes to retry on ESTALE in linkat (commit 442e31ca5a49e398351b2954b51f578353fdf210) introduced a mountpoint reference leak and a small memory leak in case a filesystem link operation returns ESTALE which is pretty normal for distributed filesystems like lustre, nfs and so on.
Free old_path in such a case.

---
 fs/namei.c | 1 +
 1 file changed, 1 insertion(+)
fs/namei.c +1 -0
index bcb838e..e620937 100644
@@ -3931,6 +3931,7 @@ out_dput:
 			goto retry;
 	}
 	if (retry_estale(error, how)) {
+		path_put(&old_path);
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}
Signature · 1 line
1.8.5.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · On Fri, 31 Jan 2014 15:41:58 -0500 Oleg Drokin <[email protected]> wrote:

Recent changes to retry on ESTALE in linkat (commit 442e31ca5a49e398351b2954b51f578353fdf210) introduced a mountpoint reference leak and a small memory leak in case a filesystem link operation returns ESTALE which is pretty normal for distributed filesystems like lustre, nfs and so on.
Free old_path in such a case.

---
 fs/namei.c | 1 +
 1 file changed, 1 insertion(+)
fs/namei.c +0 -0
index bcb838e..e620937 100644

fs/namei.c out_dput: · line 3937

Quoted 7 lines
fs/namei.c +1 -0
@@ -3931,6 +3931,7 @@ out_dput:
 			goto retry;
 	}
 	if (retry_estale(error, how)) {
+		path_put(&old_path);
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}

Nice catch. This should probably also go to stable as well...

maintainer
#2 lore raw
In reply to Oleg Drokin
Quoted 18 lines · Oleg Drokin

Recent changes to retry on ESTALE in linkat (commit 442e31ca5a49e398351b2954b51f578353fdf210) introduced a mountpoint reference leak and a small memory leak in case a filesystem link operation returns ESTALE which is pretty normal for distributed filesystems like lustre, nfs and so on.
Free old_path in such a case.

---
 fs/namei.c | 1 +
 1 file changed, 1 insertion(+)
fs/namei.c +0 -0
index bcb838e..e620937 100644

fs/namei.c out_dput: · line 3937

Quoted 7 lines
fs/namei.c +1 -0
@@ -3931,6 +3931,7 @@ out_dput:
 			goto retry;
 	}
 	if (retry_estale(error, how)) {
+		path_put(&old_path);
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}

Umm... That obviously can't be right - we have another goto retry in the same situation (see in your diff context). I agree that we have a leak there, but you've fixed only a half of it.

#3 lore raw
Quoted 7 lines · Al Viro
Quoted 7 lines
fs/namei.c +0 -0
index bcb838e..e620937 100644
@@ -3931,6 +3931,7 @@ out_dput:
 			goto retry;
 	}
Quoted 8 lines

fs/namei.c out_dput: · line 3937

Quoted 5 lines
fs/namei.c +1 -0
 	if (retry_estale(error, how)) {
+		path_put(&old_path);
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}

Umm... That obviously can't be right - we have another goto retry in the same situation (see in your diff context). I agree that we have a leak there, but you've fixed only a half of it.

Hm, you are right, I did not notice this other one somehow.

So, not to take any guesses, should I convert the other goto retry into retry_deleg similar in style to what happens in rename and unlink, only make retry)deleg label before call to the security_path_link? After the call to the security_path_link?
Or would you prefer to just free old_path in both cases?

Bye,
Oleg

#4 lore raw
Quoted 20 lines · On Fri, 31 Jan 2014 16:13:19 -0500 Oleg Drokin <[email protected]> wrote:
Quoted 7 lines · Al Viro
Quoted 7 lines
fs/namei.c +0 -0
index bcb838e..e620937 100644
@@ -3931,6 +3931,7 @@ out_dput:
 			goto retry;
 	}
Quoted 8 lines

fs/namei.c out_dput: · line 3937

Quoted 5 lines
fs/namei.c +1 -0
 	if (retry_estale(error, how)) {
+		path_put(&old_path);
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}

Umm... That obviously can't be right - we have another goto retry in the same situation (see in your diff context). I agree that we have a leak there, but you've fixed only a half of it.

Hm, you are right, I did not notice this other one somehow.

Quoted 8 lines

So, not to take any guesses, should I convert the other goto retry into retry_deleg similar in style to what happens in rename and unlink, only make retry)deleg label before call to the security_path_link? After the call to the security_path_link?
Or would you prefer to just free old_path in both cases?

Bye,
Oleg

Maybe something like this (untested) instead?

--------------------------8<---------------------------

[PATCH] vfs: fix linkat old_path reference leak

---
 fs/namei.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/namei.c +4 -3
index 46dbd31..e70dd81 100644
@@ -3927,8 +3927,10 @@ retry:
 	new_dentry = user_path_create(newdfd, newname, &new_path,
 					(how & LOOKUP_REVAL));
 	error = PTR_ERR(new_dentry);
-	if (IS_ERR(new_dentry))
+	if (IS_ERR(new_dentry)) {
+		path_put(&old_path);
 		goto out;
+	}
 
 	error = -EXDEV;
 	if (old_path.mnt != new_path.mnt)
@@ -3942,6 +3944,7 @@ retry:
 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
 out_dput:
 	done_path_create(&new_path, new_dentry);
+	path_put(&old_path);
 	if (delegated_inode) {
 		error = break_deleg_wait(&delegated_inode);
 		if (!error)
@@ -3952,8 +3955,6 @@ out_dput:
 		goto retry;
 	}
 out:
-	path_put(&old_path);
-
 	return error;
 }
 
Signature · 1 line
1.8.5.3
#5 lore raw
Quoted 1 line

Maybe something like this (untested) instead?

I think it's just safe to move the out label up along with the old_path freeing.

In all error cases the retry and delegation breaking logic could not be triggered anyway.

Something like this (lightly tested):

From a3b36802a01f4b89a16c773c0645bf374969970a Mon Sep 17 00:00:00 2001
From: Oleg Drokin <[email protected]>
Date: Fri, 31 Jan 2014 15:34:05 -0500
Subject: [PATCH] vfs: Fix mountpoint reference leakage in linkat

Recent changes to retry on ESTALE in linkat (commit 442e31ca5a49e398351b2954b51f578353fdf210) introduced a mountpoint reference leak and a small memory leak in case a filesystem link operation returns ESTALE which is pretty normal for distributed filesystems like lustre, nfs and so on.
Free old_path in such a case.
Also fix a similar leak in case of broken delegation.

---
 fs/namei.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/namei.c +4 -3
index bcb838e..cc3c6c4 100644
@@ -3925,17 +3925,18 @@ retry:
 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
 out_dput:
 	done_path_create(&new_path, new_dentry);
+out:
+	path_put(&old_path);
 	if (delegated_inode) {
 		error = break_deleg_wait(&delegated_inode);
-		if (!error)
+		if (!error) {
 			goto retry;
+		}
 	}
 	if (retry_estale(error, how)) {
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}
-out:
-	path_put(&old_path);
 
 	return error;
 }
Signature · 1 line
1.8.5.3
maintainer
#6 lore raw
In reply to Jeff Layton

fs/namei.c retry: · line 3947

Quoted 2 lines · Jeff Layton
fs/namei.c +1 -0
 	done_path_create(&new_path, new_dentry);
+	path_put(&old_path);

... and the filesystem in question isn't pinned anymore, so it can be unmounted, except that

fs/namei.c retry: · line 3949

Quoted 2 lines
fs/namei.c +0 -0
 	if (delegated_inode) {
 		error = break_deleg_wait(&delegated_inode);

this does an iput() on delegated_inode. And umount really doesn't like finding pinned inodes. Sorry, no go.

I'm going with Oleg's patch with second path_put() added.

#7 lore raw

PATCH v2 1/1 vfs: Fix mountpoint reference leakage in linkat

Recent changes to retry on ESTALE in linkat (commit 442e31ca5a49e398351b2954b51f578353fdf210) introduced a mountpoint reference leak and a small memory leak in case a filesystem link operation returns ESTALE which is pretty normal for distributed filesystems like lustre, nfs and so on.
Free old_path in such a case.
Also fix a similar leak in case of broken delegation.

---
 fs/namei.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/namei.c +4 -1
index bcb838e..31ec503 100644
@@ -3927,10 +3927,13 @@ out_dput:
 	done_path_create(&new_path, new_dentry);
 	if (delegated_inode) {
 		error = break_deleg_wait(&delegated_inode);
-		if (!error)
+		if (!error) {
+			path_put(&old_path);
 			goto retry;
+		}
 	}
 	if (retry_estale(error, how)) {
+		path_put(&old_path);
 		how |= LOOKUP_REVAL;
 		goto retry;
 	}
Signature · 1 line
1.8.5.3
#8 lore raw
In reply to Al Viro
Quoted 8 lines · On Fri, 31 Jan 2014 22:30:14 +0000 Al Viro <[email protected]> wrote:

fs/namei.c retry: · line 3947

Quoted 2 lines · Jeff Layton
fs/namei.c +1 -0
 	done_path_create(&new_path, new_dentry);
+	path_put(&old_path);

... and the filesystem in question isn't pinned anymore, so it can be unmounted, except that

Quoted 7 lines

fs/namei.c retry: · line 3949

Quoted 2 lines
fs/namei.c +0 -0
 	if (delegated_inode) {
 		error = break_deleg_wait(&delegated_inode);

this does an iput() on delegated_inode. And umount really doesn't like finding pinned inodes. Sorry, no go.

I'm going with Oleg's patch with second path_put() added.

Good point. I hadn't considered that. Oleg's second patch looks good to me too.

Signature · 1 line
Jeff Layton <[email protected]>