Commit 486ec1f606 for openssl.org
commit 486ec1f606c989dc4bd350eeb7f30715078073bd
Author: Bob Beck <beck@openssl.org>
Date: Wed Jun 17 19:14:40 2026 -0600
Convert BIO_snprintf() callers that detect truncation to snprintf().
These sites used the return value to detect truncation, then used the
buffer as is. Convert them to detect truncation in the standard
snprintf() way.
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Andrew Dinh <andrewd@openssl.org>
MergeDate: Wed Aug 26 16:20:13 2026
(Merged from https://github.com/openssl/openssl/pull/31640)
diff --git a/apps/s_client.c b/apps/s_client.c
index f5aecec132..9207230cec 100644
--- a/apps/s_client.c
+++ b/apps/s_client.c
@@ -172,8 +172,8 @@ static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
/*
* lookup PSK identity and PSK key based on the given identity hint here
*/
- ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
- if (ret < 0 || (unsigned int)ret > max_identity_len)
+ ret = snprintf(identity, max_identity_len, "%s", psk_identity);
+ if (ret < 0 || (unsigned int)ret >= max_identity_len)
goto out_err;
if (c_debug)
BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity,
diff --git a/apps/s_server.c b/apps/s_server.c
index 2856a1b9f5..50926e4377 100644
--- a/apps/s_server.c
+++ b/apps/s_server.c
@@ -1623,17 +1623,18 @@ static int ech_load_dir(SSL_CTX *lctx, const char *thedir,
int r;
#ifdef OPENSSL_SYS_VMS
- r = BIO_snprintf(filepath, sizeof(filepath), "%s%s", thedir, thisfile);
+ r = snprintf(filepath, sizeof(filepath), "%s%s", thedir, thisfile);
#else
- r = BIO_snprintf(filepath, sizeof(filepath), "%s/%s", thedir, thisfile);
+ r = snprintf(filepath, sizeof(filepath), "%s/%s", thedir, thisfile);
#endif
+ if (r < 0 || (size_t)r >= sizeof(filepath))
+ continue;
if (app_isdir(filepath) > 0) {
if (s_debug)
BIO_printf(bio_err, "Skipping directory: %s\n", filepath);
continue;
}
- if (r < 0
- || (in = BIO_new_file(filepath, "r")) == NULL
+ if ((in = BIO_new_file(filepath, "r")) == NULL
|| OSSL_ECHSTORE_read_pem(es, in, for_retry) != 1) {
BIO_printf(bio_err, "Failed reading from: %s\n", filepath);
continue;
diff --git a/apps/s_time.c b/apps/s_time.c
index d68418445c..9a2a967f5e 100644
--- a/apps/s_time.c
+++ b/apps/s_time.c
@@ -307,9 +307,10 @@ int s_time_main(int argc, char **argv)
goto end;
if (www_path != NULL) {
- buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
+ buf_len = snprintf(buf, sizeof(buf), fmt_http_get_cmd,
www_path);
- if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
+ if (buf_len <= 0 || (size_t)buf_len >= sizeof(buf)
+ || SSL_write(scon, buf, buf_len) <= 0)
goto end;
while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
bytes_read += i;
@@ -360,8 +361,9 @@ next:
}
if (www_path != NULL) {
- buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
- if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
+ buf_len = snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
+ if (buf_len <= 0 || (size_t)buf_len >= sizeof(buf)
+ || SSL_write(scon, buf, buf_len) <= 0)
goto end;
while (SSL_read(scon, buf, sizeof(buf)) > 0)
continue;
@@ -388,9 +390,10 @@ next:
goto end;
if (www_path != NULL) {
- buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
+ buf_len = snprintf(buf, sizeof(buf), fmt_http_get_cmd,
www_path);
- if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
+ if (buf_len <= 0 || (size_t)buf_len >= sizeof(buf)
+ || SSL_write(scon, buf, buf_len) <= 0)
goto end;
while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
bytes_read += i;
diff --git a/crypto/asn1/asn1_parse.c b/crypto/asn1/asn1_parse.c
index f78e0b36c9..9a602267b7 100644
--- a/crypto/asn1/asn1_parse.c
+++ b/crypto/asn1/asn1_parse.c
@@ -28,7 +28,7 @@ static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
const char *p;
int pop_f_prefix = 0;
long saved_indent = -1;
- int i = 0;
+ int i = 0, n;
BIO *bio = NULL;
if (constructed & V_ASN1_CONSTRUCTED)
@@ -36,16 +36,14 @@ static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
else
p = "prim: ";
if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
- if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
- offset, depth, (long)hl, len, p)
- <= 0)
- goto err;
+ n = snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
+ offset, depth, (long)hl, len, p);
} else {
- if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf %s",
- offset, depth, (long)hl, p)
- <= 0)
- goto err;
+ n = snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf %s",
+ offset, depth, (long)hl, p);
}
+ if (n <= 0 || (size_t)n >= sizeof(str))
+ goto err;
if (bp != NULL) {
if (BIO_set_prefix(bp, str) <= 0) {
if ((bio = BIO_new(BIO_f_prefix())) == NULL
diff --git a/providers/implementations/signature/rsa_sig.c b/providers/implementations/signature/rsa_sig.c
index 7b35fec028..6a7ab4a150 100644
--- a/providers/implementations/signature/rsa_sig.c
+++ b/providers/implementations/signature/rsa_sig.c
@@ -13,6 +13,7 @@
*/
#include "internal/deprecated.h"
+#include <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/core_dispatch.h>
@@ -1489,10 +1490,10 @@ static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX;
break;
default: {
- int len = BIO_snprintf(p.slen->data, p.slen->data_size, "%d",
+ int len = snprintf(p.slen->data, p.slen->data_size, "%d",
prsactx->saltlen);
- if (len <= 0)
+ if (len <= 0 || (size_t)len >= p.slen->data_size)
return 0;
p.slen->return_size = len;
break;
diff --git a/test/endecode_test.c b/test/endecode_test.c
index a2c5d4009d..ec02b86a02 100644
--- a/test/endecode_test.c
+++ b/test/endecode_test.c
@@ -658,11 +658,15 @@ static int check_params_PEM(const char *file, const int line,
{
static char expected_pem_header[80];
- return TEST_FL_int_gt(BIO_snprintf(expected_pem_header,
- sizeof(expected_pem_header),
- "-----BEGIN %s PARAMETERS-----", type),
- 0)
- && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header));
+ {
+ int n = snprintf(expected_pem_header, sizeof(expected_pem_header),
+ "-----BEGIN %s PARAMETERS-----", type);
+
+ return TEST_FL_int_gt(n, 0)
+ && TEST_FL_true((size_t)n < sizeof(expected_pem_header))
+ && TEST_FL_strn_eq(data, expected_pem_header,
+ strlen(expected_pem_header));
+ }
}
static int test_params_via_DER(const char *type, EVP_PKEY *key)
@@ -690,11 +694,15 @@ static int check_unprotected_legacy_PEM(const char *file, const int line,
{
static char expected_pem_header[80];
- return TEST_FL_int_gt(BIO_snprintf(expected_pem_header,
- sizeof(expected_pem_header),
- "-----BEGIN %s PRIVATE KEY-----", type),
- 0)
- && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header));
+ {
+ int n = snprintf(expected_pem_header, sizeof(expected_pem_header),
+ "-----BEGIN %s PRIVATE KEY-----", type);
+
+ return TEST_FL_int_gt(n, 0)
+ && TEST_FL_true((size_t)n < sizeof(expected_pem_header))
+ && TEST_FL_strn_eq(data, expected_pem_header,
+ strlen(expected_pem_header));
+ }
}
static int test_unprotected_via_legacy_PEM(const char *type, EVP_PKEY *key)
@@ -810,12 +818,16 @@ static int check_protected_legacy_PEM(const char *file, const int line,
{
static char expected_pem_header[80];
- return TEST_FL_int_gt(BIO_snprintf(expected_pem_header,
- sizeof(expected_pem_header),
- "-----BEGIN %s PRIVATE KEY-----", type),
- 0)
- && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header))
- && TEST_FL_ptr(strstr(data, "\nDEK-Info: "));
+ {
+ int n = snprintf(expected_pem_header, sizeof(expected_pem_header),
+ "-----BEGIN %s PRIVATE KEY-----", type);
+
+ return TEST_FL_int_gt(n, 0)
+ && TEST_FL_true((size_t)n < sizeof(expected_pem_header))
+ && TEST_FL_strn_eq(data, expected_pem_header,
+ strlen(expected_pem_header))
+ && TEST_FL_ptr(strstr(data, "\nDEK-Info: "));
+ }
}
static int test_protected_via_legacy_PEM(const char *type, EVP_PKEY *key)
diff --git a/test/ossl_store_test.c b/test/ossl_store_test.c
index 104ae3ad61..4480411cbd 100644
--- a/test/ossl_store_test.c
+++ b/test/ossl_store_test.c
@@ -208,8 +208,12 @@ static int test_store_get_params(int idx)
urifmt = "%s%s-params.pem";
}
#endif
- if (!TEST_true(BIO_snprintf(uri, sizeof(uri), urifmt, datadir, type)))
- return 0;
+ {
+ int n = snprintf(uri, sizeof(uri), urifmt, datadir, type);
+
+ if (!TEST_true(n > 0 && (size_t)n < sizeof(uri)))
+ return 0;
+ }
TEST_info("Testing uri: %s", uri);
if (!TEST_true(get_params(uri, type)))
diff --git a/test/ssl_old_test.c b/test/ssl_old_test.c
index ce90169979..dbfb020931 100644
--- a/test/ssl_old_test.c
+++ b/test/ssl_old_test.c
@@ -2974,8 +2974,8 @@ static unsigned int psk_client_callback(SSL *ssl, const char *hint,
int ret;
unsigned int psk_len = 0;
- ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
- if (ret < 0)
+ ret = snprintf(identity, max_identity_len, "Client_identity");
+ if (ret < 0 || (unsigned int)ret >= max_identity_len)
goto out_err;
if (debug)
fprintf(stderr, "client: created identity '%s' len=%d\n", identity,