Skip to content

Commit 330517b

Browse files
committed
Validate portions of mpy load to detect corruption
Fixes adafruit#1917
1 parent ecf2442 commit 330517b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

py/persistentcode.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ STATIC qstr load_qstr(mp_reader_t *reader) {
128128
size_t len = read_uint(reader);
129129
char str[len];
130130
read_bytes(reader, (byte*)str, len);
131+
// Validate the QSTRs by ensuring they do not contain any null terminations. They are length encoded instead.
132+
for (size_t i = 0; i < len; i++) {
133+
if (str[i] == '\0') {
134+
mp_raise_RuntimeError(translate("Corrupt .mpy file"));
135+
}
136+
}
131137
qstr qst = qstr_from_strn(str, len);
132138
return qst;
133139
}
@@ -145,11 +151,12 @@ STATIC mp_obj_t load_obj(mp_reader_t *reader) {
145151
return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
146152
} else if (obj_type == 'i') {
147153
return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
148-
} else {
149-
assert(obj_type == 'f' || obj_type == 'c');
154+
} else if (obj_type == 'f' || obj_type == 'c') {
150155
return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
151156
}
152157
}
158+
mp_raise_RuntimeError(translate("Corrupt .mpy file"));
159+
return MP_OBJ_FROM_PTR(&mp_const_none_obj);
153160
}
154161

155162
STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {

0 commit comments

Comments
 (0)