netlink: do not set cb_running if dump's start() errs
Subsystems (2)
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
@@ -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
Dave - this very likely should be queued up for stable.
Jason
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
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
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
@@ -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
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 indump()that were the result of a faulty returnedstart().This fixes the issue by calling
start()_before_ settingcb_runningto
true, so that there's no chance at all of hitting thedump()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 tostart(), 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.Signed-off-by Jason A. Donenfeld <[email protected]>
johannes
Quoted 2 lines · Johannes Berg
Reviewed-by Johannes Berg <[email protected]>
Thanks for the review. Hopefully this can make it into 4.13.6 and 4.14-rc5.
David Miller
maintainerFrom: "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 indump()that were the result of a faulty returnedstart().This fixes the issue by calling
start()_before_ settingcb_runningto true, so that there's no chance at all of hitting thedump()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 tostart(), 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.
Signed-off-by Jason A. Donenfeld <[email protected]>
Applied and queued up for -stable.
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
-0mutex_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