diff options
author | jsing <> | 2015-02-19 06:10:29 +0000 |
---|---|---|
committer | jsing <> | 2015-02-19 06:10:29 +0000 |
commit | b48cf8cc3375610db39211d1da5904ff311d75d8 (patch) | |
tree | a2bee07c521bf3d450b8d9880388795bf6bb56c2 | |
parent | 8ad720213befab5b57cf61e74b30f2150bee6153 (diff) | |
download | openbsd-b48cf8cc3375610db39211d1da5904ff311d75d8.tar.gz openbsd-b48cf8cc3375610db39211d1da5904ff311d75d8.tar.bz2 openbsd-b48cf8cc3375610db39211d1da5904ff311d75d8.zip |
If BN_rand() or BN_pseudo_rand() are called with a NULL rnd argument,
BN_bin2bn() will helpfully allocate a BN which is then leaked. Avoid this
by explicitly checking for NULL at the start of the bnrand() function.
Fixes Coverity ID 78831.
ok miod@
-rw-r--r-- | src/lib/libcrypto/bn/bn_rand.c | 11 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/bn/bn_rand.c | 11 |
2 files changed, 16 insertions, 6 deletions
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c index 334c65dd57..ac5c5eb308 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.16 2014/10/22 13:02:04 jsing Exp $ */ | 1 | /* $OpenBSD: bn_rand.c,v 1.17 2015/02/19 06:10:29 jsing 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 | * |
@@ -123,9 +123,14 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) | |||
123 | unsigned char *buf = NULL; | 123 | unsigned char *buf = NULL; |
124 | int ret = 0, bit, bytes, mask; | 124 | int ret = 0, bit, bytes, mask; |
125 | 125 | ||
126 | if (rnd == NULL) { | ||
127 | BNerr(BN_F_BNRAND, ERR_R_PASSED_NULL_PARAMETER); | ||
128 | return (0); | ||
129 | } | ||
130 | |||
126 | if (bits == 0) { | 131 | if (bits == 0) { |
127 | BN_zero(rnd); | 132 | BN_zero(rnd); |
128 | return 1; | 133 | return (1); |
129 | } | 134 | } |
130 | 135 | ||
131 | bytes = (bits + 7) / 8; | 136 | bytes = (bits + 7) / 8; |
@@ -175,7 +180,7 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) | |||
175 | buf[0] &= ~mask; | 180 | buf[0] &= ~mask; |
176 | if (bottom) /* set bottom bit if requested */ | 181 | if (bottom) /* set bottom bit if requested */ |
177 | buf[bytes - 1] |= 1; | 182 | buf[bytes - 1] |= 1; |
178 | if (!BN_bin2bn(buf, bytes, rnd)) | 183 | if (BN_bin2bn(buf, bytes, rnd) == NULL) |
179 | goto err; | 184 | goto err; |
180 | ret = 1; | 185 | ret = 1; |
181 | 186 | ||
diff --git a/src/lib/libssl/src/crypto/bn/bn_rand.c b/src/lib/libssl/src/crypto/bn/bn_rand.c index 334c65dd57..ac5c5eb308 100644 --- a/src/lib/libssl/src/crypto/bn/bn_rand.c +++ b/src/lib/libssl/src/crypto/bn/bn_rand.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_rand.c,v 1.16 2014/10/22 13:02:04 jsing Exp $ */ | 1 | /* $OpenBSD: bn_rand.c,v 1.17 2015/02/19 06:10:29 jsing 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 | * |
@@ -123,9 +123,14 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) | |||
123 | unsigned char *buf = NULL; | 123 | unsigned char *buf = NULL; |
124 | int ret = 0, bit, bytes, mask; | 124 | int ret = 0, bit, bytes, mask; |
125 | 125 | ||
126 | if (rnd == NULL) { | ||
127 | BNerr(BN_F_BNRAND, ERR_R_PASSED_NULL_PARAMETER); | ||
128 | return (0); | ||
129 | } | ||
130 | |||
126 | if (bits == 0) { | 131 | if (bits == 0) { |
127 | BN_zero(rnd); | 132 | BN_zero(rnd); |
128 | return 1; | 133 | return (1); |
129 | } | 134 | } |
130 | 135 | ||
131 | bytes = (bits + 7) / 8; | 136 | bytes = (bits + 7) / 8; |
@@ -175,7 +180,7 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) | |||
175 | buf[0] &= ~mask; | 180 | buf[0] &= ~mask; |
176 | if (bottom) /* set bottom bit if requested */ | 181 | if (bottom) /* set bottom bit if requested */ |
177 | buf[bytes - 1] |= 1; | 182 | buf[bytes - 1] |= 1; |
178 | if (!BN_bin2bn(buf, bytes, rnd)) | 183 | if (BN_bin2bn(buf, bytes, rnd) == NULL) |
179 | goto err; | 184 | goto err; |
180 | ret = 1; | 185 | ret = 1; |
181 | 186 | ||