1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
From 40e24ccf3ec191e6f576da967a64630ca2160bfc Mon Sep 17 00:00:00 2001
From: Roman Tsisyk <roman@tsisyk.com>
Date: Fri, 23 Jun 2017 11:34:07 +0300
Subject: [PATCH] Fix possible integer overflow in mp_check()
Malformed MessagePack can cause `int k` counter overflow
inside mp_check()/mp_next().
Closes #16
Patch-Source: https://github.com/rtsisyk/msgpuck/commit/40e24ccf3ec191e6f576da967a64630ca2160bfc
---
msgpuck.h | 59 +++++++++++++++++++++++++++++-----------------------------
test/msgpuck.c | 42 ++++++++++++++++++++++++++++++++++++++++-
2 files changed, 70 insertions(+), 31 deletions(-)
diff --git a/msgpuck.h b/msgpuck.h
index e585a0f3..4ef9f148 100644
--- a/msgpuck.h
+++ b/msgpuck.h
@@ -1980,10 +1980,10 @@ enum {
};
MP_PROTO void
-mp_next_slowpath(const char **data, int k);
+mp_next_slowpath(const char **data, int64_t k);
MP_IMPL void
-mp_next_slowpath(const char **data, int k)
+mp_next_slowpath(const char **data, int64_t k)
{
for (; k > 0; k--) {
uint8_t c = mp_load_u8(data);
@@ -2056,7 +2056,7 @@ mp_next_slowpath(const char **data, int k)
MP_IMPL void
mp_next(const char **data)
{
- int k = 1;
+ int64_t k = 1;
for (; k > 0; k--) {
uint8_t c = mp_load_u8(data);
int l = mp_parser_hint[c];
@@ -2081,14 +2081,17 @@ mp_next(const char **data)
MP_IMPL int
mp_check(const char **data, const char *end)
{
- int k;
- for (k = 1; k > 0; k--) {
- if (mp_unlikely(*data >= end))
- return 1;
+#define MP_CHECK_LEN(_l) \
+ if (mp_unlikely((size_t)(end - *data) < (size_t)(_l))) \
+ return 1;
+ int64_t k;
+ for (k = 1; k > 0; k--) {
+ MP_CHECK_LEN(1);
uint8_t c = mp_load_u8(data);
int l = mp_parser_hint[c];
if (mp_likely(l >= 0)) {
+ MP_CHECK_LEN(l);
*data += l;
continue;
} else if (mp_likely(l > MP_HINT)) {
@@ -2100,71 +2103,68 @@ mp_check(const char **data, const char *end)
switch (l) {
case MP_HINT_STR_8:
/* MP_STR (8) */
- if (mp_unlikely(*data + sizeof(uint8_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint8_t));
len = mp_load_u8(data);
+ MP_CHECK_LEN(len);
*data += len;
break;
case MP_HINT_STR_16:
/* MP_STR (16) */
- if (mp_unlikely(*data + sizeof(uint16_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint16_t));
len = mp_load_u16(data);
+ MP_CHECK_LEN(len);
*data += len;
break;
case MP_HINT_STR_32:
/* MP_STR (32) */
- if (mp_unlikely(*data + sizeof(uint32_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint32_t))
len = mp_load_u32(data);
+ MP_CHECK_LEN(len);
*data += len;
break;
case MP_HINT_ARRAY_16:
/* MP_ARRAY (16) */
- if (mp_unlikely(*data + sizeof(uint16_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint16_t));
k += mp_load_u16(data);
break;
case MP_HINT_ARRAY_32:
/* MP_ARRAY (32) */
- if (mp_unlikely(*data + sizeof(uint32_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint32_t));
k += mp_load_u32(data);
break;
case MP_HINT_MAP_16:
/* MP_MAP (16) */
- if (mp_unlikely(*data + sizeof(uint16_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint16_t));
k += 2 * mp_load_u16(data);
break;
case MP_HINT_MAP_32:
/* MP_MAP (32) */
- if (mp_unlikely(*data + sizeof(uint32_t) > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint32_t));
k += 2 * mp_load_u32(data);
break;
case MP_HINT_EXT_8:
/* MP_EXT (8) */
- if (mp_unlikely(*data + sizeof(uint8_t) + 1 > end))
- return 1;
+ MP_CHECK_LEN(sizeof(uint8_t) + sizeof(uint8_t));
len = mp_load_u8(data);
mp_load_u8(data);
+ MP_CHECK_LEN(len);
*data += len;
break;
case MP_HINT_EXT_16:
/* MP_EXT (16) */
- if (mp_unlikely(*data + sizeof(uint16_t) + 1 > end))
+ MP_CHECK_LEN(sizeof(uint16_t) + sizeof(uint8_t));
return 1;
len = mp_load_u16(data);
mp_load_u8(data);
+ MP_CHECK_LEN(len);
*data += len;
break;
case MP_HINT_EXT_32:
/* MP_EXT (32) */
- if (mp_unlikely(*data + sizeof(uint32_t) + 1 > end))
- return 1;
- len = mp_load_u32(data);
+ MP_CHECK_LEN(sizeof(uint32_t) + sizeof(uint8_t));
+ len = mp_load_u32(data);
mp_load_u8(data);
+ MP_CHECK_LEN(len);
*data += len;
break;
default:
@@ -2172,9 +2172,8 @@ mp_check(const char **data, const char *end)
}
}
- if (mp_unlikely(*data > end))
- return 1;
-
+ assert(*data <= end);
+#undef MP_CHECK_LEN
return 0;
}
diff --git a/test/msgpuck.c b/test/msgpuck.c
index 751b9e11..9265453e 100644
--- a/test/msgpuck.c
+++ b/test/msgpuck.c
@@ -1055,9 +1055,48 @@ test_numbers()
return check_plan();
}
+static int
+test_overflow()
+{
+ plan(4);
+ header();
+
+ const char *chk;
+ char *d;
+ d = data;
+ chk = data;
+ d = mp_encode_array(d, 1);
+ d = mp_encode_array(d, UINT32_MAX);
+ is(mp_check(&chk, d), 1, "mp_check array overflow")
+
+ d = data;
+ chk = data;
+ d = mp_encode_array(d, 1);
+ d = mp_encode_map(d, UINT32_MAX);
+ is(mp_check(&chk, d), 1, "mp_check map overflow")
+
+ d = data;
+ chk = data;
+ d = mp_encode_array(d, 2);
+ d = mp_encode_str(d, "", 0);
+ d = mp_encode_strl(d, UINT32_MAX);
+ is(mp_check(&chk, d), 1, "mp_check str overflow")
+
+ d = data;
+ chk = data;
+ d = mp_encode_array(d, 2);
+ d = mp_encode_bin(d, "", 0);
+ d = mp_encode_binl(d, UINT32_MAX);
+ is(mp_check(&chk, d), 1, "mp_check bin overflow")
+
+ footer();
+ return check_plan();
+}
+
+
int main()
{
- plan(19);
+ plan(20);
test_uints();
test_ints();
test_bools();
@@ -1077,6 +1116,7 @@ int main()
test_mp_print();
test_mp_check();
test_numbers();
+ test_overflow();
return check_plan();
}
|