summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2024-03-16 20:42:33 +0000
committertb <>2024-03-16 20:42:33 +0000
commit68bc09a557a7892bbe063a0dca7bf5b29969d17b (patch)
treeafec26b3eec5a25b6b736b2fa67ee5af9d9d75b6
parent171db56525fb141b1eec2ce5711800d195aa1d8f (diff)
downloadopenbsd-68bc09a557a7892bbe063a0dca7bf5b29969d17b.tar.gz
openbsd-68bc09a557a7892bbe063a0dca7bf5b29969d17b.tar.bz2
openbsd-68bc09a557a7892bbe063a0dca7bf5b29969d17b.zip
Fix signed integer overflow in bnrand()
If more bits than INT_MAX - 7 are requested, the calculation of number of bytes required to store the bignum triggers undefined behavior due to signed integer overflow. This will typically result in bytes becoming negative which will then make malloc() fail. If the ulimit should be high enough to make malloc() succeed, there is a bad out of bounds write in case bottom is set (an odd number was requested). On jsing's request this does not deal with another bug which we could catch with a similar check due to BN_bn2bin() failing later on as the number of words in a BIGNUM is some fraction of INT_MAX. ok jsing
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
index a5b163c820..9cfcd8e2c0 100644
--- a/src/lib/libcrypto/bn/bn_rand.c
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_rand.c,v 1.29 2023/08/03 18:53:55 tb Exp $ */ 1/* $OpenBSD: bn_rand.c,v 1.30 2024/03/16 20:42:33 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -109,6 +109,7 @@
109 * 109 *
110 */ 110 */
111 111
112#include <limits.h>
112#include <stdio.h> 113#include <stdio.h>
113#include <stdlib.h> 114#include <stdlib.h>
114#include <string.h> 115#include <string.h>
@@ -133,6 +134,10 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
133 BNerror(BN_R_BITS_TOO_SMALL); 134 BNerror(BN_R_BITS_TOO_SMALL);
134 return (0); 135 return (0);
135 } 136 }
137 if (bits > INT_MAX - 7) {
138 BNerror(BN_R_BIGNUM_TOO_LONG);
139 return (0);
140 }
136 141
137 if (bits == 0) { 142 if (bits == 0) {
138 BN_zero(rnd); 143 BN_zero(rnd);