Skip to content

Commit

Permalink
netlink: do not set cb_running if dump's start() errs
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
zx2c4 authored and davem330 committed Oct 9, 2017
1 parent 6df4d17 commit 41c8742
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions net/netlink/af_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

1 comment on commit 41c8742

@sebbekarlsson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.