Skip to contentSkip to conversation

lockd: fix race in nlm_release()

PATCH In review J. Bruce Fields 4 participants 8 messages Feb 20, 2008 – Feb 21, 2008 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lockd: fix race in nlm_release()

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +4 -6
index c3f1194..960911c 100644
@@ -529,12 +529,10 @@ nsm_release(struct nsm_handle *nsm)
 {
 	if (!nsm)
 		return;
+	mutex_lock(&nsm_mutex);
 	if (atomic_dec_and_test(&nsm->sm_count)) {
-		mutex_lock(&nsm_mutex);
-		if (atomic_read(&nsm->sm_count) == 0) {
-			list_del(&nsm->sm_link);
-			kfree(nsm);
-		}
-		mutex_unlock(&nsm_mutex);
+		list_del(&nsm->sm_link);
+		kfree(nsm);
 	}
+	mutex_unlock(&nsm_mutex);
 }
Signature · 1 line
1.5.4.rc2.60.gb2e62
#1 lore raw
Quoted 28 lines · J. Bruce Fields

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +1 -1
index c3f1194..960911c 100644
@@ -529,12 +529,10 @@ nsm_release(struct nsm_handle *nsm)
 {
 	if (!nsm)
 		return;
+	mutex_lock(&nsm_mutex);
 	if (atomic_dec_and_test(&nsm->sm_count)) {
-		mutex_lock(&nsm_mutex);

fs/lockd/host.c nsm_release(struct nsm_handle *nsm) · line 538

Quoted 10 lines
fs/lockd/host.c +3 -5
-		if (atomic_read(&nsm->sm_count) == 0) {
-			list_del(&nsm->sm_link);
-			kfree(nsm);
-		}
-		mutex_unlock(&nsm_mutex);
+		list_del(&nsm->sm_link);
+		kfree(nsm);
 	}
+	mutex_unlock(&nsm_mutex);
 }

It would be nice to get rid of that mutex. That should really be either a spinlock or an rcu-protected list...

Trond

#3 lore raw
In reply to a message not in the archive
Quoted 35 lines · Trond Myklebust
Quoted 33 lines · J. Bruce Fields

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +1 -6
index c3f1194..960911c 100644
@@ -529,12 +529,10 @@ nsm_release(struct nsm_handle *nsm)
 {
 	if (!nsm)
 		return;
+	mutex_lock(&nsm_mutex);
 	if (atomic_dec_and_test(&nsm->sm_count)) {
-		mutex_lock(&nsm_mutex);
-		if (atomic_read(&nsm->sm_count) == 0) {
-			list_del(&nsm->sm_link);
-			kfree(nsm);
-		}
-		mutex_unlock(&nsm_mutex);
Quoted 8 lines

fs/lockd/host.c nsm_release(struct nsm_handle *nsm) · line 538

Quoted 5 lines
fs/lockd/host.c +3 -0
+		list_del(&nsm->sm_link);
+		kfree(nsm);
 	}
+	mutex_unlock(&nsm_mutex);
 }

It would be nice to get rid of that mutex. That should really be either a spinlock or an rcu-protected list...

OK, I'll look into doing that next.

If you've got any other suggestions while I'm in the general area, I'm all ears.

--b.

#4 lore raw
Quoted 45 lines · J. Bruce Fields
Quoted 35 lines · Trond Myklebust
Quoted 33 lines · J. Bruce Fields

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +1 -6
index c3f1194..960911c 100644
@@ -529,12 +529,10 @@ nsm_release(struct nsm_handle *nsm)
 {
 	if (!nsm)
 		return;
+	mutex_lock(&nsm_mutex);
 	if (atomic_dec_and_test(&nsm->sm_count)) {
-		mutex_lock(&nsm_mutex);
-		if (atomic_read(&nsm->sm_count) == 0) {
-			list_del(&nsm->sm_link);
-			kfree(nsm);
-		}
-		mutex_unlock(&nsm_mutex);
Quoted 8 lines

fs/lockd/host.c nsm_release(struct nsm_handle *nsm) · line 538

Quoted 5 lines
fs/lockd/host.c +3 -0
+		list_del(&nsm->sm_link);
+		kfree(nsm);
 	}
+	mutex_unlock(&nsm_mutex);
 }

It would be nice to get rid of that mutex. That should really be either a spinlock or an rcu-protected list...

Quoted 4 lines

OK, I'll look into doing that next.

If you've got any other suggestions while I'm in the general area, I'm all ears.

Just the usual plea to replace the host->h_server flag with 2 separate
lists: one list of client nlm_hosts, and one list of server nlm_hosts :-)

...Oh and a minor optimisation: If we're using a loopback mount, I don't think we'll ever need to monitor 'localhost' :-)

#6 lore raw
In reply to a message not in the archive
Quoted 46 lines · Trond Myklebust
Quoted 45 lines · J. Bruce Fields
Quoted 43 lines · Trond Myklebust
Quoted 28 lines · J. Bruce Fields

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +1 -1
index c3f1194..960911c 100644
@@ -529,12 +529,10 @@ nsm_release(struct nsm_handle *nsm)
 {
 	if (!nsm)
 		return;
+	mutex_lock(&nsm_mutex);
 	if (atomic_dec_and_test(&nsm->sm_count)) {
-		mutex_lock(&nsm_mutex);

fs/lockd/host.c nsm_release(struct nsm_handle *nsm) · line 538

Quoted 10 lines
fs/lockd/host.c +3 -5
-		if (atomic_read(&nsm->sm_count) == 0) {
-			list_del(&nsm->sm_link);
-			kfree(nsm);
-		}
-		mutex_unlock(&nsm_mutex);
+		list_del(&nsm->sm_link);
+		kfree(nsm);
 	}
+	mutex_unlock(&nsm_mutex);
 }

It would be nice to get rid of that mutex. That should really be either a spinlock or an rcu-protected list...

Quoted 8 lines
Quoted 4 lines

OK, I'll look into doing that next.

If you've got any other suggestions while I'm in the general area, I'm all ears.

Just the usual plea to replace the host->h_server flag with 2 separate
lists: one list of client nlm_hosts, and one list of server nlm_hosts :-)

I have no objection to that, but my NLM IPv6 patches will be significantly affected by such a change right at this point. Can we hold off until the IPv6 work is integrated, or make this change part of the IPv6 work itself?

--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com

#8 lore raw
In reply to a message not in the archive
Quoted 26 lines · Trond Myklebust
Quoted 24 lines · J. Bruce Fields
Quoted 23 lines · Trond Myklebust
Quoted 21 lines · J. Bruce Fields

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +0 -0
index c3f1194..960911c 100644

...

Quoted 6 lines
Quoted 2 lines

If you've got any other suggestions while I'm in the general area, I'm all ears.

Just the usual plea to replace the host->h_server flag with 2 separate
lists: one list of client nlm_hosts, and one list of server nlm_hosts :-)

OK, I'm looking at that.

Quoted 2 lines

...Oh and a minor optimisation: If we're using a loopback mount, I don't think we'll ever need to monitor 'localhost' :-)

I assumed one of the only uses for loopback mounts was for testing and development, in which case it's better to keep the loopback behavior similar to non-loopback behavior, even if it's a little silly. No?

--b.

#7 lore raw
In reply to Chuck Lever
Quoted 9 lines · Chuck Lever
Quoted 3 lines · Trond Myklebust

Just the usual plea to replace the host->h_server flag with 2 separate
lists: one list of client nlm_hosts, and one list of server nlm_hosts :-)

I have no objection to that, but my NLM IPv6 patches will be significantly affected by such a change right at this point. Can we hold off until the IPv6 work is integrated, or make this change part of the IPv6 work itself?

Just speak up before I try to merge it, and we'll work something out....

--b.

#9 lore raw
In reply to J. Bruce Fields
Quoted 43 lines · J. Bruce Fields
Quoted 28 lines · Trond Myklebust
Quoted 26 lines · J. Bruce Fields
Quoted 24 lines · Trond Myklebust
Quoted 22 lines · J. Bruce Fields

From: J. Bruce Fields <[email protected]>

The sm_count is decremented to zero but left on the nsm_handles list. So in the space between decrementing sm_count and acquiring nsm_mutex, it is possible for another task to find this nsm_handle, increment the use count and then enter nsm_release itself.

Thus there's nothing to prevent the nsm being freed before we acquire nsm_mutex here.

---
 fs/lockd/host.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

Am I missing something here?--b.

fs/lockd/host.c +0 -0
index c3f1194..960911c 100644

...

Quoted 7 lines
Quoted 3 lines

If you've got any other suggestions while I'm in the general area, I'm all ears.

Just the usual plea to replace the host->h_server flag with 2 separate
lists: one list of client nlm_hosts, and one list of server nlm_hosts :-)

OK, I'm looking at that.

Quoted 7 lines
Quoted 3 lines

...Oh and a minor optimisation: If we're using a loopback mount, I don't think we'll ever need to monitor 'localhost' :-)

I assumed one of the only uses for loopback mounts was for testing and development, in which case it's better to keep the loopback behavior similar to non-loopback behavior, even if it's a little silly. No?

We have encountered a number of customers, who are trying to deploy in a cluster architecture, who mount over loopback. We have tried to convince them that this is a bad idea and/or that they should use something like bind mounts instead, but they insist.

We try not to look too surprised when they encounter issues... :-)

ps