Skip to content

Commit

Permalink
Update hkdf.c to avoid potentially vulnerable code pattern
Browse files Browse the repository at this point in the history
The expression "if (a+b>c) a=c-b" is incorrect if "a+b" overflows.
It should be replaced by "if (a>c-b) a=c-b", which avoids the
potential overflow and is much easier to understand.

This pattern is the root cause of CVE-2022-37454, a buffer overflow
vulnerability in the "official" SHA-3 implementation.

It has been confirmed that the addition in
https://github.com/openssl/openssl/blob/master/providers/implementations/kdfs/hkdf.c#L534
cannot overflow. So this is only a minor change proposal to avoid
a potentially vulnerable code pattern and to improve readability.
More information: github/codeql#12036 (comment)

CLA: trivial

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #20990)

(cherry picked from commit 56a51b5)
  • Loading branch information
nmouha authored and t8m committed May 19, 2023
1 parent 205e7e3 commit b0973c1
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion providers/implementations/kdfs/hkdf.c
Expand Up @@ -586,7 +586,7 @@ static int HKDF_Expand(const EVP_MD *evp_md,
if (!HMAC_Final(hmac, prev, NULL))
goto err;

copy_len = (done_len + dig_len > okm_len) ?
copy_len = (dig_len > okm_len - done_len) ?
okm_len - done_len :
dig_len;

Expand Down

0 comments on commit b0973c1

Please sign in to comment.