From 8eddce6f428cf6fd23afd6b5611f0d67aebe2d6b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 20 Jan 2022 17:21:51 -0500 Subject: [PATCH 1/3] Validate open() mode --- extmod/vfs_fat_file.c | 22 +++++++++++++++++++++- locale/circuitpython.pot | 12 ++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 17b142b95ac6..4da497f5b8b9 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -139,39 +139,59 @@ STATIC const mp_arg_t file_open_args[] = { STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_arg_val_t *args) { int mode = 0; const char *mode_s = mp_obj_str_get_str(args[1].u_obj); - // TODO make sure only one of r, w, x, a, and b, t are specified + uint32_t rwxa_count = 0; + uint32_t bt_count = 0; + uint32_t plus_count = 0; + bool bad_mode = false; while (*mode_s) { switch (*mode_s++) { case 'r': mode |= FA_READ; + rwxa_count++; break; case 'w': mode |= FA_WRITE | FA_CREATE_ALWAYS; + rwxa_count++; break; case 'x': mode |= FA_WRITE | FA_CREATE_NEW; + rwxa_count++; break; case 'a': mode |= FA_WRITE | FA_OPEN_ALWAYS; + rwxa_count++; break; case '+': mode |= FA_READ | FA_WRITE; + plus_count++; break; #if MICROPY_PY_IO_FILEIO case 'b': + bt_count++; type = &mp_type_vfs_fat_fileio; break; #endif case 't': + bt_count++; type = &mp_type_vfs_fat_textio; break; + default: + bad_mode = true; + mp_raise_ValueError(translate("Invalid mode")); + break; } } + + if (rwxa_count != 1 || plus_count > 1 || bt_count > 1 || bad_mode) { + mp_raise_ValueError(translate("Invalid mode")); + } + assert(vfs != NULL); if ((mode & FA_WRITE) != 0 && !filesystem_is_writable_by_python(vfs)) { mp_raise_OSError(MP_EROFS); } + pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t); o->base.type = type; diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 121eac20499f..cfab5d02e25a 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1370,6 +1370,10 @@ msgstr "" msgid "Invalid memory access." msgstr "" +#: extmod/vfs_fat_file.c +msgid "Invalid mode" +msgstr "" + #: ports/espressif/common-hal/wifi/Radio.c msgid "Invalid multicast MAC address" msgstr "" @@ -3992,6 +3996,7 @@ msgstr "" msgid "pow() with 3 arguments requires integers" msgstr "" +#: ports/espressif/boards/adafruit_esp32s2_camera/mpconfigboard.h #: ports/espressif/boards/adafruit_feather_esp32s2/mpconfigboard.h #: ports/espressif/boards/adafruit_feather_esp32s2_tft/mpconfigboard.h #: ports/espressif/boards/adafruit_feather_esp32s2_tftback_nopsram/mpconfigboard.h @@ -3999,15 +4004,18 @@ msgstr "" #: ports/espressif/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h #: ports/espressif/boards/adafruit_metro_esp32s2/mpconfigboard.h #: ports/espressif/boards/adafruit_qtpy_esp32s2/mpconfigboard.h +#: ports/espressif/boards/adafruit_qtpy_esp32s3_nopsram/mpconfigboard.h #: ports/espressif/boards/ai_thinker_esp32-c3s/mpconfigboard.h #: ports/espressif/boards/ai_thinker_esp_12k_nodemcu/mpconfigboard.h #: ports/espressif/boards/artisense_rd00/mpconfigboard.h #: ports/espressif/boards/atmegazero_esp32s2/mpconfigboard.h #: ports/espressif/boards/crumpspace_crumps2/mpconfigboard.h #: ports/espressif/boards/electroniccats_bastwifi/mpconfigboard.h +#: ports/espressif/boards/espressif_esp32c3_devkitm_1_n4/mpconfigboard.h #: ports/espressif/boards/espressif_esp32s3_box/mpconfigboard.h -#: ports/espressif/boards/espressif_esp32s3_devkitc_1/mpconfigboard.h -#: ports/espressif/boards/espressif_esp32s3_devkitc_1_nopsram/mpconfigboard.h +#: ports/espressif/boards/espressif_esp32s3_devkitc_1_n8/mpconfigboard.h +#: ports/espressif/boards/espressif_esp32s3_devkitc_1_n8r2/mpconfigboard.h +#: ports/espressif/boards/espressif_esp32s3_devkitc_1_n8r8/mpconfigboard.h #: ports/espressif/boards/espressif_hmi_devkit_1/mpconfigboard.h #: ports/espressif/boards/espressif_kaluga_1.3/mpconfigboard.h #: ports/espressif/boards/espressif_kaluga_1/mpconfigboard.h From eb08c83ae29b08c86abb0f97a898b8a5c5247f66 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 20 Jan 2022 18:17:38 -0500 Subject: [PATCH 2/3] fix typo --- extmod/vfs_fat_file.c | 1 - 1 file changed, 1 deletion(-) diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 4da497f5b8b9..65ee5f902426 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -177,7 +177,6 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar break; default: bad_mode = true; - mp_raise_ValueError(translate("Invalid mode")); break; } } From fed5227de9af87f7d2aba30c93008f9871a4ab4b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 20 Jan 2022 20:34:04 -0500 Subject: [PATCH 3/3] shrink stringcar_m0_express --- ports/atmel-samd/boards/stringcar_m0_express/mpconfigboard.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/atmel-samd/boards/stringcar_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/stringcar_m0_express/mpconfigboard.mk index 8821f4793ac1..8274c7124317 100644 --- a/ports/atmel-samd/boards/stringcar_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/stringcar_m0_express/mpconfigboard.mk @@ -13,5 +13,6 @@ LONGINT_IMPL = MPZ CIRCUITPY_KEYPAD = 0 CIRCUITPY_ONEWIREIO = 0 +CIRCUITPY_USB_MIDI = 0 CIRCUITPY_BITBANG_APA102 = 1