Skip to content

Commit 544435f

Browse files
committed
Merge bitcoin#578: Avoid implementation-defined and undefined behavior when dealing with sizes
14c7dbd Simplify control flow in DER parsing (Tim Ruffing) ec8f20b Avoid out-of-bound pointers and integer overflows in size comparisons (Tim Ruffing) 01ee1b3 Parse DER-enconded length into a size_t instead of an int (Tim Ruffing) 3cb057f Fix possible integer overflow in DER parsing (Tim Ruffing) Pull request description: This is a result of auditing the code for overflow issues at random places. None of this is critical but I think all of it should be fixed. I know this touches "red" code. I double-checked and triple-checked this but I can understand if some of the changes are not desirable because they change well-tested code. Best reviewed in individual commits. ACKs for commit 14c7db: Tree-SHA512: 312dd3f961739752e1a861e75bd755920f634f87ee9668793e102c224434e8d21367452e114de729322c71a89f4fa82126aa5d32742f2bbbc091777c99515e10
2 parents 143dc6e + 14c7dbd commit 544435f

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

contrib/lax_der_parsing.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
3232
lenbyte = input[pos++];
3333
if (lenbyte & 0x80) {
3434
lenbyte -= 0x80;
35-
if (pos + lenbyte > inputlen) {
35+
if (lenbyte > inputlen - pos) {
3636
return 0;
3737
}
3838
pos += lenbyte;
@@ -51,7 +51,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
5151
lenbyte = input[pos++];
5252
if (lenbyte & 0x80) {
5353
lenbyte -= 0x80;
54-
if (pos + lenbyte > inputlen) {
54+
if (lenbyte > inputlen - pos) {
5555
return 0;
5656
}
5757
while (lenbyte > 0 && input[pos] == 0) {
@@ -89,7 +89,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
8989
lenbyte = input[pos++];
9090
if (lenbyte & 0x80) {
9191
lenbyte -= 0x80;
92-
if (pos + lenbyte > inputlen) {
92+
if (lenbyte > inputlen - pos) {
9393
return 0;
9494
}
9595
while (lenbyte > 0 && input[pos] == 0) {

src/ecdsa_impl.h

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,68 +46,73 @@ static const secp256k1_fe secp256k1_ecdsa_const_p_minus_order = SECP256K1_FE_CON
4646
0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL
4747
);
4848

49-
static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned char *sigend) {
50-
int lenleft, b1;
51-
size_t ret = 0;
49+
static int secp256k1_der_read_len(size_t *len, const unsigned char **sigp, const unsigned char *sigend) {
50+
size_t lenleft;
51+
unsigned char b1;
52+
VERIFY_CHECK(len != NULL);
53+
*len = 0;
5254
if (*sigp >= sigend) {
53-
return -1;
55+
return 0;
5456
}
5557
b1 = *((*sigp)++);
5658
if (b1 == 0xFF) {
5759
/* X.690-0207 8.1.3.5.c the value 0xFF shall not be used. */
58-
return -1;
60+
return 0;
5961
}
6062
if ((b1 & 0x80) == 0) {
6163
/* X.690-0207 8.1.3.4 short form length octets */
62-
return b1;
64+
*len = b1;
65+
return 1;
6366
}
6467
if (b1 == 0x80) {
6568
/* Indefinite length is not allowed in DER. */
66-
return -1;
69+
return 0;
6770
}
6871
/* X.690-207 8.1.3.5 long form length octets */
69-
lenleft = b1 & 0x7F;
70-
if (lenleft > sigend - *sigp) {
71-
return -1;
72+
lenleft = b1 & 0x7F; /* lenleft is at least 1 */
73+
if (lenleft > (size_t)(sigend - *sigp)) {
74+
return 0;
7275
}
7376
if (**sigp == 0) {
7477
/* Not the shortest possible length encoding. */
75-
return -1;
78+
return 0;
7679
}
77-
if ((size_t)lenleft > sizeof(size_t)) {
80+
if (lenleft > sizeof(size_t)) {
7881
/* The resulting length would exceed the range of a size_t, so
7982
* certainly longer than the passed array size.
8083
*/
81-
return -1;
84+
return 0;
8285
}
8386
while (lenleft > 0) {
84-
ret = (ret << 8) | **sigp;
85-
if (ret + lenleft > (size_t)(sigend - *sigp)) {
86-
/* Result exceeds the length of the passed array. */
87-
return -1;
88-
}
87+
*len = (*len << 8) | **sigp;
8988
(*sigp)++;
9089
lenleft--;
9190
}
92-
if (ret < 128) {
91+
if (*len > (size_t)(sigend - *sigp)) {
92+
/* Result exceeds the length of the passed array. */
93+
return 0;
94+
}
95+
if (*len < 128) {
9396
/* Not the shortest possible length encoding. */
94-
return -1;
97+
return 0;
9598
}
96-
return ret;
99+
return 1;
97100
}
98101

99102
static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char **sig, const unsigned char *sigend) {
100103
int overflow = 0;
101104
unsigned char ra[32] = {0};
102-
int rlen;
105+
size_t rlen;
103106

104107
if (*sig == sigend || **sig != 0x02) {
105108
/* Not a primitive integer (X.690-0207 8.3.1). */
106109
return 0;
107110
}
108111
(*sig)++;
109-
rlen = secp256k1_der_read_len(sig, sigend);
110-
if (rlen <= 0 || (*sig) + rlen > sigend) {
112+
if (secp256k1_der_read_len(&rlen, sig, sigend) == 0) {
113+
return 0;
114+
}
115+
if (rlen == 0 || *sig + rlen > sigend) {
111116
/* Exceeds bounds or not at least length 1 (X.690-0207 8.3.1). */
112117
return 0;
113118
}
@@ -123,8 +128,11 @@ static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char
123128
/* Negative. */
124129
overflow = 1;
125130
}
126-
while (rlen > 0 && **sig == 0) {
127-
/* Skip leading zero bytes */
131+
/* There is at most one leading zero byte:
132+
* if there were two leading zero bytes, we would have failed and returned 0
133+
* because of excessive 0x00 padding already. */
134+
if (rlen > 0 && **sig == 0) {
135+
/* Skip leading zero byte */
128136
rlen--;
129137
(*sig)++;
130138
}
@@ -144,18 +152,16 @@ static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char
144152

145153
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar *rr, secp256k1_scalar *rs, const unsigned char *sig, size_t size) {
146154
const unsigned char *sigend = sig + size;
147-
int rlen;
155+
size_t rlen;
148156
if (sig == sigend || *(sig++) != 0x30) {
149157
/* The encoding doesn't start with a constructed sequence (X.690-0207 8.9.1). */
150158
return 0;
151159
}
152-
rlen = secp256k1_der_read_len(&sig, sigend);
153-
if (rlen < 0 || sig + rlen > sigend) {
154-
/* Tuple exceeds bounds */
160+
if (secp256k1_der_read_len(&rlen, &sig, sigend) == 0) {
155161
return 0;
156162
}
157-
if (sig + rlen != sigend) {
158-
/* Garbage after tuple. */
163+
if (rlen != (size_t)(sigend - sig)) {
164+
/* Tuple exceeds bounds or garage after tuple. */
159165
return 0;
160166
}
161167

src/hash_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ static void secp256k1_sha256_transform(uint32_t* s, const uint32_t* chunk) {
131131
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t len) {
132132
size_t bufsize = hash->bytes & 0x3F;
133133
hash->bytes += len;
134-
while (bufsize + len >= 64) {
134+
VERIFY_CHECK(hash->bytes >= len);
135+
while (len >= 64 - bufsize) {
135136
/* Fill the buffer, and process it. */
136137
size_t chunk_len = 64 - bufsize;
137138
memcpy(((unsigned char*)hash->buf) + bufsize, data, chunk_len);

0 commit comments

Comments
 (0)