crypto: drbg - drop useless check in drbg_get_random_bytes()
PATCH 1/1 crypto: drbg - drop useless check in drbg_get_random_bytes()
drbg_fips_continuous_test() only returns 0 and -EAGAIN, so an early return from drbg_get_random_bytes() could never happen, so we can drop the result check from the *do/while* loop...
Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
---
The patch is against the master branch of Linus Torvalds' linux.git repo (I'm unable to use the other repos on git.kernel.org and I have to update Linus' repo from GitHub).
crypto/drbg.c | 2 -- 1 file changed, 2 deletions(-)
crypto/drbg.c +0 -2
@@ -1067,8 +1067,6 @@ static inline int drbg_get_random_bytes(do {get_random_bytes (entropy, entropylen); ret =drbg_fips_continuous_test (drbg, entropy); -if (ret && ret != -EAGAIN ) -return ret; }while (ret);return 0 ;
Herbert Xu
maintainerQuoted 22 lines · Sergey Shtylyov
drbg_fips_continuous_test()only returns 0 and-EAGAIN, so an early return fromdrbg_get_random_bytes()could never happen, so we can drop the result check from the *do/while* loop...Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
Signed-off-by Sergey Shtylyov <[email protected]>---
The patch is against the master branch of Linus Torvalds' linux.git repo (I'm unable to use the other repos on git.kernel.org and I have to update Linus' repo from GitHub).crypto/drbg.c | 2 -- 1 file changed, 2 deletions(-)Index:
linux/crypto/drbg.c
===================================================================
crypto/drbg.c static inline int drbg_get_random_bytes( · line 1071
Quoted 5 lines
crypto/drbg.c +0
-2@@ -1067,8 +1067,6 @@ static inline int drbg_get_random_bytes( do { get_random_bytes(entropy, entropylen); ret = drbg_fips_continuous_test(drbg, entropy); - if (ret && ret != -EAGAIN) - return ret;
That's a bug waiting to happen. Does the compiler actually fail to optimise away?
If you can show that this makes a difference to the compiled output, then please change the drbg_fips_continuous_test function signature so that it can no longer return an error.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 17 lines · Herbert Xu
Quoted 17 lines
drbg_fips_continuous_test()only returns 0 and-EAGAIN, so an early return fromdrbg_get_random_bytes()could never happen, so we can drop the result check from the *do/while* loop...Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.
Signed-off-by Sergey Shtylyov <[email protected]>---
The patch is against the master branch of Linus Torvalds' linux.git repo (I'm unable to use the other repos on git.kernel.org and I have to update Linus' repo from GitHub).crypto/drbg.c | 2 -- 1 file changed, 2 deletions(-)
Quoted 12 lines
crypto/drbg.c static inline int drbg_get_random_bytes( · line 1071
Quoted 10 lines
Index:
linux/crypto/drbg.c
===================================================================crypto/drbg.c +0
-2@@ -1067,8 +1067,6 @@ static inline int drbg_get_random_bytes( do { get_random_bytes(entropy, entropylen); ret = drbg_fips_continuous_test(drbg, entropy); - if (ret && ret != -EAGAIN) - return ret;That's a bug waiting to happen. Does the compiler actually fail
You mean the change is risky WRT the drbg_fips_continuous_test() changes?
Quoted 1 line
to optimise away?
Looks like both gcc (14.3.1) and clang (19.1.7) are able to optimize away
this *if*...
Quoted 1 line
If you can show that this makes a difference to the compiled output,
It does, however the code (from my cursory look) seems to get even worse,
as *return* -EINVAL generates actual mov, at least with gcc 14.3.1...
However, it's clear that drbg_get_random_bytes() can be made shorter/clearer
(see the other reply), so I'm going to recast.
Quoted 2 lines
then please change the
drbg_fips_continuous_testfunction signature so that it can no longer return an error.
I didn't understand what change you meant here...
Quoted 1 line
Thanks,
MBR, Sergey
Herbert Xu
maintainerQuoted 5 lines · Sergey Shtylyov
Quoted 3 lines
then please change the
drbg_fips_continuous_testfunction signature so that it can no longer return an error.I didn't understand what change you meant here...
Make it return a boolean instead of an int.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 6 lines · Herbert Xu
Quoted 4 lines
Quoted 2 lines
then please change the
drbg_fips_continuous_testfunction signature so that it can no longer return an error.I didn't understand what change you meant here...
Make it return a boolean instead of an int.
That means drbg_get_random_bytes() can be made *void* inst6ead of *int*...
I guess I should do a series 2 patches then?
Quoted 1 line
Cheers,
MBR, Sergey
On 12/12/25 11:30 PM, Sergey Shtylyov wrote:
[...]
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
then please change the
drbg_fips_continuous_testfunction signature so that it can no longer return an error.I didn't understand what change you meant here...
Make it return a boolean instead of an int.
That means
drbg_get_random_bytes()can be made *void* inst6ead of *int*...
I guess I should do a series 2 patches then?
A series of 2 patches, of course. :-)
Quoted 1 line
Quoted 1 line
Cheers,
MBR, Sergey
Hi,
Le 21/09/2025 à 22:33, Sergey Shtylyov a écrit :
crypto/drbg.c static inline int drbg_get_random_bytes( · line 1070
Quoted 11 lines
Index:
linux/crypto/drbg.c
===================================================================crypto/drbg.c +0
-2@@ -1067,8 +1067,6 @@ static inline int drbg_get_random_bytes(do {get_random_bytes (entropy, entropylen); ret =drbg_fips_continuous_test (drbg, entropy); -if (ret && ret != -EAGAIN ) -return ret; }while (ret);
} while (ret == EAGAIN);
return ret;
Signature · 1 line
Yann Droneaud
On 9/22/25 9:19 AM, Yann Droneaud wrote:
[...]
Quoted 14 lines
crypto/drbg.c static inline int drbg_get_random_bytes( · line 1070
Quoted 11 lines
Index:
linux/crypto/drbg.c
===================================================================crypto/drbg.c +0
-2@@ -1067,8 +1067,6 @@ static inline int drbg_get_random_bytes( do { get_random_bytes(entropy, entropylen); ret = drbg_fips_continuous_test(drbg, entropy); - if (ret && ret != -EAGAIN) - return ret; } while (ret);} while (ret == EAGAIN);
Quoted 1 line
return ret;
Yeah, that's more clear, thanks! I'll mention you in the Suggested-by tag
when I recast. :-)
MBR, Sergey