diff options
author | guenther <> | 2016-10-17 03:30:14 +0000 |
---|---|---|
committer | guenther <> | 2016-10-17 03:30:14 +0000 |
commit | e136d75872f02f48d5744f8e936b2ed07c641e9d (patch) | |
tree | df5203a55de10a8b341c4646f077322546b5c9c2 | |
parent | 0293a237b08ee4da5eacd1c96495dc1b9831d520 (diff) | |
download | openbsd-e136d75872f02f48d5744f8e936b2ed07c641e9d.tar.gz openbsd-e136d75872f02f48d5744f8e936b2ed07c641e9d.tar.bz2 openbsd-e136d75872f02f48d5744f8e936b2ed07c641e9d.zip |
If BN_div_word() fails (by returning (BN_ULONG)-1) or if the division
fails to reduce the input in the expected space then fail out instead
of overflowing the allocated buffer.
combines openssl commits 28a89639da50b1caed4ff3015508f23173bf3e49 and
3612ff6fcec0e3d1f2a598135fe12177c0419582
ok doug@ beck@
-rw-r--r-- | src/lib/libcrypto/bn/bn_print.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c index 2c1681a2c0..f526065592 100644 --- a/src/lib/libcrypto/bn/bn_print.c +++ b/src/lib/libcrypto/bn/bn_print.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_print.c,v 1.29 2016/03/02 06:16:11 doug Exp $ */ | 1 | /* $OpenBSD: bn_print.c,v 1.30 2016/10/17 03:30:14 guenther 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,7 +109,7 @@ err: | |||
109 | char * | 109 | char * |
110 | BN_bn2dec(const BIGNUM *a) | 110 | BN_bn2dec(const BIGNUM *a) |
111 | { | 111 | { |
112 | int i = 0, num, ok = 0; | 112 | int i = 0, num, bn_data_num, ok = 0; |
113 | char *buf = NULL; | 113 | char *buf = NULL; |
114 | char *p; | 114 | char *p; |
115 | BIGNUM *t = NULL; | 115 | BIGNUM *t = NULL; |
@@ -136,7 +136,8 @@ BN_bn2dec(const BIGNUM *a) | |||
136 | */ | 136 | */ |
137 | i = BN_num_bits(a) * 3; | 137 | i = BN_num_bits(a) * 3; |
138 | num = (i / 10 + i / 1000 + 1) + 1; | 138 | num = (i / 10 + i / 1000 + 1) + 1; |
139 | bn_data = reallocarray(NULL, num / BN_DEC_NUM + 1, sizeof(BN_ULONG)); | 139 | bn_data_num = num / BN_DEC_NUM + 1; |
140 | bn_data = reallocarray(NULL, bn_data_num, sizeof(BN_ULONG)); | ||
140 | buf = malloc(num + 3); | 141 | buf = malloc(num + 3); |
141 | if ((buf == NULL) || (bn_data == NULL)) { | 142 | if ((buf == NULL) || (bn_data == NULL)) { |
142 | BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); | 143 | BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); |
@@ -151,9 +152,12 @@ BN_bn2dec(const BIGNUM *a) | |||
151 | if (BN_is_negative(t)) | 152 | if (BN_is_negative(t)) |
152 | *p++ = '-'; | 153 | *p++ = '-'; |
153 | 154 | ||
154 | i = 0; | ||
155 | while (!BN_is_zero(t)) { | 155 | while (!BN_is_zero(t)) { |
156 | if (lp - bn_data >= bn_data_num) | ||
157 | goto err; | ||
156 | *lp = BN_div_word(t, BN_DEC_CONV); | 158 | *lp = BN_div_word(t, BN_DEC_CONV); |
159 | if (*lp == (BN_ULONG)-1) | ||
160 | goto err; | ||
157 | lp++; | 161 | lp++; |
158 | } | 162 | } |
159 | lp--; | 163 | lp--; |