Coverity: __sock_gen_cookie(): Error handling issues
Subsystems (4)
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scan
You're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
*** CID 1527347: Error handling issues (CHECKED_RETURN) net/core/sock_diag.c:33 in __sock_gen_cookie()
27 {
28 u64 res = atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new = gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33 atomic64_try_cmpxchg(&sk->sk_cookie, &res, new); 34 } 35 return res; 36 }
37
38 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
If this is a false positive, please let us know so we can mark it as such, or teach the Coverity rules to be smarter. If not, please make sure fixes get into linux-next. :) For patches fixing this, please include these lines (but double-check the "Fixes" first):
Addresses-Coverity-ID: 1527347 ("Error handling issues")
Thanks for your attention!
Signature · 1 line
Coverity-bot
Quoted 15 lines · coverity-bot
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scanYou're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
Quoted 11 lines
*** CID 1527347: Error handling issues (
CHECKED_RETURN)net/core/sock_diag.c:33in__sock_gen_cookie()
27 {
28 u64 res =atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new =gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);
Hmmm. for some reason I thought @res was always updated...
A fix would be to read sk->sk_cookie, but I guess your tool will still complain we do not care
of atomic64_try_cmpxchg() return value ?
net/core/sock_diag.c +0 -0
100644
net/core/sock_diag.c +4 -0
@@ -31,6 +31,10 @@ u64 __sock_gen_cookie(struct sock *sk)u64 new =gen_cookie_next (&sock_cookie);atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +/* Another cpu/thread might have won the race, +* reload the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }
Quoted 41 lines · Eric Dumazet
Quoted 15 lines · coverity-bot
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scanYou're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
Quoted 11 lines
*** CID 1527347: Error handling issues (
CHECKED_RETURN)net/core/sock_diag.c:33in__sock_gen_cookie()
27 {
28 u64 res =atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new =gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);Hmmm. for some reason I thought @res was always updated...
A fix would be to read
sk->sk_cookie, but I guess your tool will still complain we do not care
ofatomic64_try_cmpxchg()return value ?net/core/sock_diag.c +0
-0100644
net/core/sock_diag.c u64 __sock_gen_cookie(struct sock *sk) · line 40
Quoted 10 lines
net/core/sock_diag.c +4
-0@@ -31,6 +31,10 @@ u64 __sock_gen_cookie(struct sock *sk)u64 new =gen_cookie_next (&sock_cookie);atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +/* Another cpu/thread might have won the race, +* reload the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }
I think it's saying it was expecting an update loop -- i.e. to make sure the value actually got swapped (the "try" part...)?
Signature · 1 line
Kees Cook
Quoted 45 lines · Kees Cook
Quoted 43 lines · Eric Dumazet
Quoted 15 lines · coverity-bot
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scanYou're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
Quoted 11 lines
*** CID 1527347: Error handling issues (
CHECKED_RETURN)net/core/sock_diag.c:33in__sock_gen_cookie()
27 {
28 u64 res =atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new =gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);Hmmm. for some reason I thought @res was always updated...
A fix would be to read
sk->sk_cookie, but I guess your tool will still complain we do not care
ofatomic64_try_cmpxchg()return value ?net/core/sock_diag.c +0
-0100644
net/core/sock_diag.c +0
-0@@ -31,6 +31,10 @@ u64 __sock_gen_cookie(struct sock *sk)u64 new =gen_cookie_next (&sock_cookie);
Quoted 11 lines
net/core/sock_diag.c u64 __sock_gen_cookie(struct sock *sk) · line 40
Quoted 8 lines
net/core/sock_diag.c +4
-0atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +/* Another cpu/thread might have won the race, +* reload the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }I think it's saying it was expecting an update loop -- i.e. to make sure the value actually got swapped (the "try" part...)?
The value has been updated, either by us or someone else.
We do not particularly care who won the race, since the value is updated once only.
Quoted 58 lines · Eric Dumazet
Quoted 45 lines · Kees Cook
Quoted 43 lines · Eric Dumazet
Quoted 15 lines · coverity-bot
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scanYou're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
Quoted 11 lines
*** CID 1527347: Error handling issues (
CHECKED_RETURN)net/core/sock_diag.c:33in__sock_gen_cookie()
27 {
28 u64 res =atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new =gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);Hmmm. for some reason I thought @res was always updated...
A fix would be to read
sk->sk_cookie, but I guess your tool will still complain we do not care
ofatomic64_try_cmpxchg()return value ?net/core/sock_diag.c +0
-0100644
net/core/sock_diag.c +0
-0@@ -31,6 +31,10 @@ u64 __sock_gen_cookie(struct sock *sk)u64 new =gen_cookie_next (&sock_cookie);Quoted 11 lines
net/core/sock_diag.c u64 __sock_gen_cookie(struct sock *sk) · line 40
Quoted 8 lines
net/core/sock_diag.c +4
-0atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +/* Another cpu/thread might have won the race, +* reload the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }I think it's saying it was expecting an update loop -- i.e. to make sure the value actually got swapped (the "try" part...)?
Quoted 4 lines
The value has been updated, either by us or someone else.
We do not particularly care who won the race, since the value is updated once only.
Ah! Okay, now I understand the added comment. Thanks :)
Signature · 1 line
Kees Cook
Quoted 60 lines · Kees Cook
Quoted 58 lines · Eric Dumazet
Quoted 56 lines · Kees Cook
Quoted 41 lines · Eric Dumazet
Quoted 15 lines · coverity-bot
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scanYou're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
Quoted 11 lines
*** CID 1527347: Error handling issues (
CHECKED_RETURN)net/core/sock_diag.c:33in__sock_gen_cookie()
27 {
28 u64 res =atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new =gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);Hmmm. for some reason I thought @res was always updated...
A fix would be to read
sk->sk_cookie, but I guess your tool will still complain we do not care
ofatomic64_try_cmpxchg()return value ?net/core/sock_diag.c +0
-0100644
net/core/sock_diag.c u64 __sock_gen_cookie(struct sock *sk) · line 40
Quoted 10 lines
net/core/sock_diag.c +4
-0@@ -31,6 +31,10 @@ u64 __sock_gen_cookie(struct sock *sk)u64 new =gen_cookie_next (&sock_cookie);atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +/* Another cpu/thread might have won the race, +* reload the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }I think it's saying it was expecting an update loop -- i.e. to make sure the value actually got swapped (the "try" part...)?
Quoted 6 lines
Quoted 4 lines
The value has been updated, either by us or someone else.
We do not particularly care who won the race, since the value is updated once only.
Ah! Okay, now I understand the added comment. Thanks :)
I guess we could simply go back to atomic64_cmpxchg() to avoid a false positive.
This boils to avoid the loop we had prior to 4ebf802cf1c6
net/core/sock_diag.c +0 -0
100644
net/core/sock_diag.c +5 -1
@@ -30,7 +30,11 @@ 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 cpu/thread might have won the race, +* load the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }
Quoted 61 lines · Eric Dumazet
Quoted 60 lines · Kees Cook
Quoted 58 lines · Eric Dumazet
Quoted 56 lines · Kees Cook
Quoted 41 lines · Eric Dumazet
Quoted 15 lines · coverity-bot
Hello!
This is an experimental semi-automated report about issues detected by Coverity from a scan of next-20221117 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scanYou're getting this email because you were associated with the identified lines of code (noted below) that were touched by commits:
Wed Nov 16 12:42:01 2022 +0000
4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup")Coverity reported the following:
Quoted 11 lines
*** CID 1527347: Error handling issues (
CHECKED_RETURN)net/core/sock_diag.c:33in__sock_gen_cookie()
27 {
28 u64 res =atomic64_read(&sk->sk_cookie);
29
30 if (!res) {
31 u64 new =gen_cookie_next(&sock_cookie);
32
vvv CID 1527347: Error handling issues (CHECKED_RETURN)
vvv Calling "atomic64_try_cmpxchg" without checking return value (as is done elsewhere 8 out of 9 times).
33atomic64_try_cmpxchg(&sk->sk_cookie, &res, new);Hmmm. for some reason I thought @res was always updated...
A fix would be to read
sk->sk_cookie, but I guess your tool will still complain we do not care
ofatomic64_try_cmpxchg()return value ?net/core/sock_diag.c +0
-0100644
net/core/sock_diag.c u64 __sock_gen_cookie(struct sock *sk) · line 40
Quoted 10 lines
net/core/sock_diag.c +4
-0@@ -31,6 +31,10 @@ u64 __sock_gen_cookie(struct sock *sk)u64 new =gen_cookie_next (&sock_cookie);atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +/* Another cpu/thread might have won the race, +* reload the final value. +*/ + res =atomic64_read (&sk->sk_cookie); }return res; }I think it's saying it was expecting an update loop -- i.e. to make sure the value actually got swapped (the "try" part...)?
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
The value has been updated, either by us or someone else.
We do not particularly care who won the race, since the value is updated once only.
Ah! Okay, now I understand the added comment. Thanks :)
I guess we could simply go back to
atomic64_cmpxchg()to avoid a false positive.
It looks like the existing code already works as intended, so no need to silence the warning. The comment and reload might be nice to add, just to clarify for anyone looking at it again in the future, though.
Signature · 1 line
Kees Cook
Quoted 4 lines · Kees Cook
It looks like the existing code already works as intended, so no need to silence the warning. The comment and reload might be nice to add, just to clarify for anyone looking at it again in the future, though.
The current code in net-next is broken, because if we succeed to change sk->sk_cookie,
we return 0 (instead of @new). So your report was not a false positive.
Thanks.