Skip to content

Commit

Permalink
net: fix __sock_gen_cookie()
Browse files Browse the repository at this point in the history
I was mistaken how atomic64_try_cmpxchg(&sk_cookie, &res, new)
is working.

I was assuming @res would contain the final sk_cookie value,
regardless of the success of our cmpxchg()

We could do something like:

if (atomic64_try_cmpxchg(&sk_cookie, &res, new)
	res = new;

But we can avoid a conditional and read sk_cookie again.

atomic64_cmpxchg(&sk_cookie, res, new);
res = atomic64_read(&sk_cookie);

Reported-by: coverity-bot <keescook+coverity-bot@chromium.org>
Addresses-Coverity-ID: 1527347 ("Error handling issues")
Fixes: 4ebf802 ("net: __sock_gen_cookie() cleanup")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://lore.kernel.org/r/20221118043843.3703186-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Eric Dumazet authored and Jakub Kicinski committed Nov 22, 2022
1 parent 2c45455 commit 3263481
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion net/core/sock_diag.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ u64 __sock_gen_cookie(struct sock *sk)
if (!res) {
u64 new = gen_cookie_next(&sock_cookie);

atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);
atomic64_cmpxchg(&sk->sk_cookie, res, new);

/* Another thread might have changed sk_cookie before us. */
res = atomic64_read(&sk->sk_cookie);
}
return res;
}
Expand Down

0 comments on commit 3263481

Please sign in to comment.