Validate open() mode

This commit is contained in:
Dan Halbert 2022-01-20 17:21:51 -05:00
parent 3762f4e0f0
commit 8eddce6f42
2 changed files with 31 additions and 3 deletions

View File

@ -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;

View File

@ -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