Skip to content

Commit 364f095

Browse files
igus68t8m
authored andcommitted
Avoid possible buffer overflow in buf2hex conversion
Fixes CVE-2026-31789 Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.foundation> MergeDate: Mon Apr 6 19:39:23 2026 (cherry picked from commit 3244aa4)
1 parent 5d3ccf1 commit 364f095

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

‎crypto/o_str.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
296296
int has_sep = (sep != CH_ZERO);
297297
size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2;
298298

299+
if (buflen > (has_sep ? SIZE_MAX / 3 : (SIZE_MAX - 1) / 2)) {
300+
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES);
301+
return 0;
302+
}
303+
299304
if (len == 0)
300305
++len;
301306
if (strlength != NULL)
@@ -339,7 +344,13 @@ char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
339344
if (buflen == 0)
340345
return OPENSSL_zalloc(1);
341346

342-
tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
347+
if ((sep != CH_ZERO && (size_t)buflen > SIZE_MAX / 3)
348+
|| (sep == CH_ZERO && (size_t)buflen > (SIZE_MAX - 1) / 2)) {
349+
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES);
350+
return NULL;
351+
}
352+
353+
tmp_n = (sep != CH_ZERO) ? (size_t)buflen * 3 : 1 + (size_t)buflen * 2;
343354
if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
344355
return NULL;
345356

0 commit comments

Comments
 (0)