Commit bce996082b for openssl.org
commit bce996082b17ea05b559352371145b22e8bd0234
Author: Nicky Mouha <nicky@mouha.be>
Date: Fri Oct 30 21:41:44 2026 -0400
Avoid potential overflow in if-statements
[NSF]
The expression if (a+b>c) a=c-b is incorrect if a+b overflows.
It is replaced by if (a>c-b) a=c-b, which avoids the potential overflow
and is much easier to understand.
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Milan Broz <gmazyland@gmail.com>
Merge-date: Mon Nov 16 17:56:37 2026
Merged-from: https://github.com/openssl/openssl/pull/33463
diff --git a/crypto/bio/bss_bio.c b/crypto/bio/bss_bio.c
index 8f7c0fcb3d..96da427a0a 100644
--- a/crypto/bio/bss_bio.c
+++ b/crypto/bio/bss_bio.c
@@ -199,7 +199,7 @@ static int bio_read(BIO *bio, char *buf, int size_)
static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
{
struct bio_bio_st *b, *peer_b;
- ossl_ssize_t num;
+ size_t num;
BIO_clear_retry_flags(bio);
@@ -223,14 +223,14 @@ static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
}
num = peer_b->len;
- if (peer_b->size < peer_b->offset + num)
+ if (num > peer_b->size - peer_b->offset)
/* no ring buffer wrap-around for non-copying interface */
num = peer_b->size - peer_b->offset;
assert(num > 0);
if (buf != NULL)
*buf = peer_b->buf + peer_b->offset;
- return num;
+ return (ossl_ssize_t)num;
}
static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
@@ -373,7 +373,7 @@ static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
write_offset = b->offset + b->len;
if (write_offset >= b->size)
write_offset -= b->size;
- if (write_offset + num > b->size)
+ if (num > b->size - write_offset)
/*
* no ring buffer wrap-around for non-copying interface (to fulfil
* the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
diff --git a/include/internal/hashtable.h b/include/internal/hashtable.h
index 9aad653097..281a7d01e0 100644
--- a/include/internal/hashtable.h
+++ b/include/internal/hashtable.h
@@ -11,6 +11,7 @@
#define OPENSSL_HASHTABLE_H
#pragma once
+#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
@@ -141,7 +142,8 @@ typedef struct ht_config_st {
*/
static ossl_inline ossl_unused int ossl_key_raw_copy(HT_KEY *key, const uint8_t *buf, size_t len)
{
- if (key->keysize + len > key->bufsize)
+ assert(key->keysize <= key->bufsize);
+ if (len > key->bufsize - key->keysize)
return 0;
memcpy(&key->keybuf[key->keysize], buf, len);
key->keysize += len;
@@ -165,6 +167,7 @@ static ossl_inline ossl_unused int ossl_key_raw_copy(HT_KEY *key, const uint8_t
#define HT_COPY_RAW_KEY_CASE(key, buf, len) \
do { \
size_t tmplen = (size_t)(len); \
+ assert((key)->keysize <= (key)->bufsize); \
if (tmplen > (key)->bufsize - (key)->keysize) \
tmplen = (key)->bufsize - (key)->keysize; \
ossl_ht_strcase((key), (char *)&((key)->keybuf[(key)->keysize]), buf, tmplen); \
@@ -182,6 +185,7 @@ static ossl_inline ossl_unused int ossl_key_raw_copy(HT_KEY *key, const uint8_t
HT_INIT_KEY((key)); \
(key)->key_header.keybuf = (buf); \
(key)->key_header.keysize = (len); \
+ (key)->key_header.bufsize = (len); \
} while (0)
#define HT_KEY_GET_HASH(key) (key)->key_header.cached_hash
@@ -354,8 +358,11 @@ static ossl_inline ossl_unused void ossl_ht_strcase(HT_KEY *key, char *tgt, cons
* so check that we don't overflow here, and truncate if
* we copy more space than we have available
*/
- if (key != NULL && key->keysize + len > key->bufsize)
- len = (size_t)(key->bufsize - key->keysize);
+ if (key != NULL) {
+ assert(key->keysize <= key->bufsize);
+ if (len > key->bufsize - key->keysize)
+ len = key->bufsize - key->keysize;
+ }
for (i = 0; i < len && src[i] != '\0'; i++)
tgt[i] = case_adjust & src[i];
diff --git a/ssl/quic/quic_sstream.c b/ssl/quic/quic_sstream.c
index dd5ab13fb9..314104addc 100644
--- a/ssl/quic/quic_sstream.c
+++ b/ssl/quic/quic_sstream.c
@@ -144,7 +144,7 @@ int ossl_quic_sstream_get_stream_frame(QUIC_SSTREAM *qss,
assert(i < 2);
- if (total_len + src_len > max_len)
+ if (src_len > max_len - total_len)
src_len = (size_t)(max_len - total_len);
iov[num_iov_].buf = src;
diff --git a/test/evp_xof_test.c b/test/evp_xof_test.c
index 4efd6e6b1e..ca0e48d4c2 100644
--- a/test/evp_xof_test.c
+++ b/test/evp_xof_test.c
@@ -460,7 +460,7 @@ static int xof_absorb_test(int tstid)
sz = 0;
for (i = 0; i < total; i += sz) {
sz += stride;
- if ((i + sz) > total)
+ if (sz > total - i)
sz = total - i;
if (!TEST_true(EVP_DigestUpdate(ctx, td->in + i, sz)))
goto err;
@@ -547,7 +547,7 @@ static int do_xof_squeeze_test(const TEST_DATA *td,
goto err;
while (i < expected_outlen) {
- if ((i + sz) > expected_outlen)
+ if (sz > expected_outlen - i)
sz = expected_outlen - i;
if (!TEST_true(EVP_DigestSqueeze(ctx, out + i, sz)))
goto err;
@@ -621,7 +621,7 @@ static int do_xof_squeeze_dup_test(const TEST_DATA *td, size_t dupoffset,
goto err;
while (i < expected_outlen) {
- if ((i + sz) > expected_outlen)
+ if (sz > expected_outlen - i)
sz = expected_outlen - i;
if (!TEST_true(EVP_DigestSqueeze(cur, out + i, sz)))
goto err;
diff --git a/test/helpers/noisydgrambio.c b/test/helpers/noisydgrambio.c
index 8413d74588..c2961ffc2f 100644
--- a/test/helpers/noisydgrambio.c
+++ b/test/helpers/noisydgrambio.c
@@ -285,7 +285,7 @@ static void flip_bits(unsigned char *msg, size_t msg_len, uint16_t flip,
/* None of these border conditions should happen but check them anyway */
if (msg_len < 2)
return;
- if (msg_len < flip_offset + 2)
+ if (flip_offset > msg_len - 2)
flip_offset = msg_len - 2;
#ifdef OSSL_NOISY_DGRAM_DEBUG
diff --git a/test/quic_stream_test.c b/test/quic_stream_test.c
index e36a70899a..35498a24b7 100644
--- a/test/quic_stream_test.c
+++ b/test/quic_stream_test.c
@@ -594,7 +594,7 @@ static int test_rstream_random(int idx)
if (off > 50)
off -= 50;
size = test_random() % 100 + 1;
- if (off + size > data_size)
+ if (off > data_size - size)
off = data_size - size;
if (off <= queued_min && off + size > queued_min)
queued_min = off + size;