Skip to contentSkip to conversation

netlink: do not set cb_running if dump's start() errs

PATCH Superseded Jason A. Donenfeld 3 participants 9 messages Oct 9, 2017

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

PATCH 1/1 netlink: do not set cb_running if dump's start() errs

It turns out that multiple places can call netlink_dump(), which means it's still possible to dereference partially initialized values in dump() that were the result of a faulty returned start().

This fixes the issue by calling start() _before_ setting cb_running to true, so that there's no chance at all of hitting the dump() function through any indirect paths.

In testing this with several different pieces of tricky code to trigger these issues, this commit fixes all avenues that I'm aware of.

---
 net/netlink/af_netlink.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
net/netlink/af_netlink.c +7 -6
index 94c11cf0459d..f34750691c5c 100644
@@ -2266,16 +2266,17 @@ int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 	cb->min_dump_alloc = control->min_dump_alloc;
 	cb->skb = skb;
 
+	if (cb->start) {
+		ret = cb->start(cb);
+		if (ret)
+			goto error_unlock;
+	}
+
 	nlk->cb_running = true;
 
 	mutex_unlock(nlk->cb_mutex);
 
-	ret = 0;
-	if (cb->start)
-		ret = cb->start(cb);
-
-	if (!ret)
-		ret = netlink_dump(sk);
+	ret = netlink_dump(sk);
 
 	sock_put(sk);
 
Signature · 1 line
2.14.2
#2 lore raw
In reply to Jason A. Donenfeld
Quoted 4 lines · Jason A. Donenfeld
net/netlink/af_netlink.c +0 -0
@@ -2266,16 +2266,17 @@ int __netlink_dump_start(struct sock *ssk,
 struct sk_buff *skb,
 	cb->min_dump_alloc = control->min_dump_alloc;
 	cb->skb = skb;

net/netlink/af_netlink.c int __netlink_dump_start(struct sock *ssk, · line 2278

Quoted 10 lines
net/netlink/af_netlink.c +6 -0
 
+	if (cb->start) {
+		ret = cb->start(cb);
+		if (ret)
+			goto error_unlock;
+	}
+
 	nlk->cb_running = true;
 
 	mutex_unlock(nlk->cb_mutex);

Hmm. Now start is invoked with the mutex held, I'm not sure it actually _matters_, but that should probably be reviewed and mentioned in the commit log?

johannes

#3 lore raw

Hi Johannes,

Yes, indeed, and I think that's actually a good thing. It means that the starts aren't ever called in parallel, which could be useful if drivers have any ordering requirements. The change doesn't negatively effect any existing drivers. I'll resubmit with a larger commit message explaining this.

Jason

#4 lore raw

PATCH v2 1/1 netlink: do not set cb_running if dump's start() errs

It turns out that multiple places can call netlink_dump(), which means it's still possible to dereference partially initialized values in dump() that were the result of a faulty returned start().

This fixes the issue by calling start() _before_ setting cb_running to true, so that there's no chance at all of hitting the dump() function through any indirect paths.

It also moves the call to start() to be when the mutex is held. This has the nice side effect of serializing invocations to start(), which is likely desirable anyway. It also prevents any possible other races that might come out of this logic.

In testing this with several different pieces of tricky code to trigger these issues, this commit fixes all avenues that I'm aware of.

---
 net/netlink/af_netlink.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
net/netlink/af_netlink.c +7 -6
index 94c11cf0459d..f34750691c5c 100644
@@ -2266,16 +2266,17 @@ int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 	cb->min_dump_alloc = control->min_dump_alloc;
 	cb->skb = skb;
 
+	if (cb->start) {
+		ret = cb->start(cb);
+		if (ret)
+			goto error_unlock;
+	}
+
 	nlk->cb_running = true;
 
 	mutex_unlock(nlk->cb_mutex);
 
-	ret = 0;
-	if (cb->start)
-		ret = cb->start(cb);
-
-	if (!ret)
-		ret = netlink_dump(sk);
+	ret = netlink_dump(sk);
 
 	sock_put(sk);
 
Signature · 1 line
2.14.2
Reviewed-by on v2 1/1
#5 lore raw
Quoted 10 lines · Jason A. Donenfeld

It turns out that multiple places can call netlink_dump(), which means
it's still possible to dereference partially initialized values in dump() that were the result of a faulty returned start().

This fixes the issue by calling start() _before_ setting cb_running to
true, so that there's no chance at all of hitting the dump() function through any indirect paths.

Quoted 6 lines

It also moves the call to start() to be when the mutex is held. This has
the nice side effect of serializing invocations to start(), which is likely desirable anyway. It also prevents any possible other races that
might come out of this logic.

I'm not necessarily sure it's _nice_, but I do think it doesn't matter, so that's just splitting hairs. If you do have a genl family with parallel_ops, you'd better be prepared to handle parallel things, and then this could also be in parallel :-)

Quoted 5 lines

In testing this with several different pieces of tricky code to trigger
these issues, this commit fixes all avenues that I'm aware of.

johannes

maintainer
#7 lore raw
In reply to Jason A. Donenfeld

From: "Jason A. Donenfeld" <[email protected]>
Date: Mon, 9 Oct 2017 14:14:51 +0200

Quoted 13 lines

It turns out that multiple places can call netlink_dump(), which means it's still possible to dereference partially initialized values in dump() that were the result of a faulty returned start().

This fixes the issue by calling start() _before_ setting cb_running to true, so that there's no chance at all of hitting the dump() function through any indirect paths.

It also moves the call to start() to be when the mutex is held. This has the nice side effect of serializing invocations to start(), which is likely desirable anyway. It also prevents any possible other races that might come out of this logic.

Quoted 4 lines

In testing this with several different pieces of tricky code to trigger these issues, this commit fixes all avenues that I'm aware of.

Applied and queued up for -stable.

#8 lore raw
In reply to Johannes Berg

Just decided to take another look:

Quoted 15 lines · Johannes Berg
Quoted 13 lines · Jason A. Donenfeld
net/netlink/af_netlink.c +6 -0
@@ -2266,16 +2266,17 @@ int __netlink_dump_start(struct sock *ssk,
 struct sk_buff *skb,
 	cb->min_dump_alloc = control->min_dump_alloc;
 	cb->skb = skb;
 
+	if (cb->start) {
+		ret = cb->start(cb);
+		if (ret)
+			goto error_unlock;
+	}
+
 	nlk->cb_running = true;
 
Quoted 5 lines

net/netlink/af_netlink.c int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, · line 2277

Quoted 1 line
net/netlink/af_netlink.c +0 -0
 	mutex_unlock(nlk->cb_mutex);

Hmm. Now start is invoked with the mutex held, I'm not sure it actually _matters_, but that should probably be reviewed and mentioned in the commit log?

It sort of seems designed to run ->start outside the lock, otherwise we wouldn't really have to acquire it again in netlink_dump() but could just keep it across the call (with some locking changes in netlink_recvmsg())?

Then again, clearly none of the (few) existing users actually care.

Btw - we should (separately) also remove "start" from struct netlink_callback, it's only ever used within this function and we can use control->start instead of cb->start here.

johannes