summaryrefslogtreecommitdiffstats
path: root/crypto/bn/bn_rand.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2015-05-19 16:03:02 +0100
committerMatt Caswell <matt@openssl.org>2015-05-22 23:40:38 +0100
commitefee575ad464bfb60bf72dcb73f9b51768f4b1a1 (patch)
tree2350f1a20e2752afd68de0aebb656f0591974e91 /crypto/bn/bn_rand.c
parent7cc18d8158b5fc2676393d99b51c30c135502107 (diff)
Fix off-by-one in BN_rand
If BN_rand is called with |bits| set to 1 and |top| set to 1 then a 1 byte buffer overflow can occur. There are no such instances within the OpenSSL at the moment. Thanks to Mateusz Kocielski (LogicalTrust), Marek Kroemeke, Filip Palian for discovering and reporting this issue. Reviewed-by: Kurt Roeckx <kurt@openssl.org>
Diffstat (limited to 'crypto/bn/bn_rand.c')
-rw-r--r--crypto/bn/bn_rand.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/crypto/bn/bn_rand.c b/crypto/bn/bn_rand.c
index 4dd3f924a7..2764c8a307 100644
--- a/crypto/bn/bn_rand.c
+++ b/crypto/bn/bn_rand.c
@@ -122,6 +122,11 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
int ret = 0, bit, bytes, mask;
time_t tim;
+ if (bits < 0 || (bits == 1 && top > 0)) {
+ BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
+ return 0;
+ }
+
if (bits == 0) {
BN_zero(rnd);
return 1;
@@ -168,7 +173,7 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
}
}
- if (top != -1) {
+ if (top >= 0) {
if (top) {
if (bit == 0) {
buf[0] = 1;