Skip to content

Commit 093d0c0

Browse files
stinosdpgeorge
authored andcommitted
py/objtype: Validate super() arguments.
This fixes various null dereferencing and out-of-bounds access because super_attr assumes the held obj is effectively an object of the held type, which is now verified. Fixes issue micropython#12830. Signed-off-by: stijn <[email protected]>
1 parent d1bf0ee commit 093d0c0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

py/objtype.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#define ENABLE_SPECIAL_ACCESSORS \
4545
(MICROPY_PY_DESCRIPTORS || MICROPY_PY_DELATTR_SETATTR || MICROPY_PY_BUILTINS_PROPERTY)
4646

47+
static mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo);
4748
static mp_obj_t static_class_method_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
4849

4950
/******************************************************************************/
@@ -1260,9 +1261,15 @@ static mp_obj_t super_make_new(const mp_obj_type_t *type_in, size_t n_args, size
12601261
// 0 arguments are turned into 2 in the compiler
12611262
// 1 argument is not yet implemented
12621263
mp_arg_check_num(n_args, n_kw, 2, 2, false);
1263-
if (!mp_obj_is_type(args[0], &mp_type_type)) {
1264+
1265+
// Per CPython: "If the second argument is an object, isinstance(obj, type) must be true.
1266+
// If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods)."
1267+
const mp_obj_type_t *second_arg_type = mp_obj_get_type(args[1]);
1268+
mp_obj_t second_arg_obj = second_arg_type == &mp_type_type ? args[1] : MP_OBJ_FROM_PTR(second_arg_type);
1269+
if (mp_obj_is_subclass(second_arg_obj, args[0]) == mp_const_false) {
12641270
mp_raise_TypeError(NULL);
12651271
}
1272+
12661273
mp_obj_super_t *o = m_new_obj(mp_obj_super_t);
12671274
*o = (mp_obj_super_t) {{type_in}, args[0], args[1]};
12681275
return MP_OBJ_FROM_PTR(o);

tests/basics/builtin_super.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Check that super rejects invalid arguments.
2+
try:
3+
super(str, 0)
4+
except TypeError:
5+
print("TypeError")
6+
7+
try:
8+
super(str, int)
9+
except TypeError:
10+
print("TypeError")
11+
12+
try:
13+
super(0, int)
14+
except TypeError:
15+
print("TypeError")

0 commit comments

Comments
 (0)