From: Eugene Syromiatnikov Date: Wed, 10 Sep 2025 08:03:11 +0000 (+0200) Subject: crypto/bio/bio_print.c: avoid signed int overow in padlen calculation in fmtstr X-Git-Tag: openssl-3.6.0-beta1~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ff5df1014205bc0b45a12163b2e0b31492bf641;p=thirdparty%2Fopenssl.git crypto/bio/bio_print.c: avoid signed int overow in padlen calculation in fmtstr In a highly unlikely situation of str being longer than INT_MAX, a signed integer overflow in padlen calculation can be triggered. Avoid it by reworking the check for the need of padlen calculation. Fixes: 230c691a5218 "Fix fmtstr for BIO_printf() et al" Signed-off-by: Eugene Syromiatnikov Reviewed-by: Neil Horman Reviewed-by: Saša Nedvědický Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/28502) --- diff --git a/crypto/bio/bio_print.c b/crypto/bio/bio_print.c index c2db1fa6c11..ddc5bc6deee 100644 --- a/crypto/bio/bio_print.c +++ b/crypto/bio/bio_print.c @@ -476,9 +476,11 @@ fmtstr(struct pr_desc *desc, const char *value, int flags, int min, int max) strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max); - padlen = (int)(min - strln); - if (min < 0 || padlen < 0) - padlen = 0; + if (min >= 0 && strln < INT_MAX) { + padlen = min - (int)strln; + if (padlen < 0) + padlen = 0; + } if (max >= 0) { /* * Calculate the maximum output including padding.