py/builtinimport: Fix crash handling "weak link" module names.

There are two calls to mp_builtin___import__():
1. ports/unix/main.c:main_() which provides a str in args[0]
2. py/runtime.c:mp_import_name() which provides a qstr in args[0]

The default implementation of mp_builtin___import__() is
mp_builtin___import___default() which has a different implementation based
on MICROPY_ENABLE_EXTERNAL_IMPORT.

If MICROPY_ENABLE_EXTERNAL_IMPORT is disabled then the handling of weak
links assumes that args[0] is a `const char *`, when it is either a str or
qstr object.

Use the existing qstr of the module name instead, and also use a vstr
instead of strcpy() to ensure no overflow occurs.
This commit is contained in:
Simon Arlott 2022-11-01 18:44:23 +00:00 committed by Damien George
parent 43dd3ea74d
commit f8b0ae32d3
1 changed files with 12 additions and 8 deletions

View File

@ -45,6 +45,15 @@
#define DEBUG_printf(...) (void)0
#endif
#if MICROPY_MODULE_WEAK_LINKS
STATIC qstr make_weak_link_name(vstr_t *buffer, qstr name) {
vstr_reset(buffer);
vstr_add_char(buffer, 'u');
vstr_add_str(buffer, qstr_str(name));
return qstr_from_strn(buffer->buf, buffer->len);
}
#endif
#if MICROPY_ENABLE_EXTERNAL_IMPORT
// Must be a string of one byte.
@ -367,10 +376,7 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
// formerly known as "weak links").
#if MICROPY_MODULE_WEAK_LINKS
if (stat == MP_IMPORT_STAT_NO_EXIST && module_obj == MP_OBJ_NULL) {
char *umodule_buf = vstr_str(path);
umodule_buf[0] = 'u';
strcpy(umodule_buf + 1, qstr_str(level_mod_name));
qstr umodule_name = qstr_from_str(umodule_buf);
qstr umodule_name = make_weak_link_name(path, level_mod_name);
module_obj = mp_module_get_builtin(umodule_name);
}
#elif MICROPY_PY_SYS
@ -581,10 +587,8 @@ mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
#if MICROPY_MODULE_WEAK_LINKS
// Check if there is a weak link to this module
char umodule_buf[MICROPY_ALLOC_PATH_MAX];
umodule_buf[0] = 'u';
strcpy(umodule_buf + 1, args[0]);
qstr umodule_name_qstr = qstr_from_str(umodule_buf);
VSTR_FIXED(umodule_path, MICROPY_ALLOC_PATH_MAX);
qstr umodule_name_qstr = make_weak_link_name(&umodule_path, module_name_qstr);
module_obj = mp_module_get_loaded_or_builtin(umodule_name_qstr);
if (module_obj != MP_OBJ_NULL) {
return module_obj;