From 6c01aba05b12c1f078197a45a711bcc5092b2a1c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 13 Mar 2019 15:17:07 -0700 Subject: [PATCH] Prevent other filesystem changes besides file writes Such as moving files, adding directories and changing the label. --- extmod/vfs_fat.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index c7e8a81e81..20d3e3e5ce 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -39,7 +39,7 @@ #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" #include "lib/timeutils/timeutils.h" - +#include "supervisor/filesystem.h" #include "supervisor/shared/translate.h" #if _MAX_SS == _MIN_SS @@ -99,6 +99,12 @@ STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, const return MP_OBJ_FROM_PTR(vfs); } +STATIC void verify_fs_writable(fs_user_mount_t *vfs) { + if (!filesystem_is_writable_by_python(vfs)) { + mp_raise_OSError(MP_EROFS); + } +} + #if _FS_REENTRANT STATIC mp_obj_t fat_vfs_del(mp_obj_t self_in) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(self_in); @@ -201,6 +207,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_i STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); + verify_fs_writable(self); const char *path = mp_obj_str_get_str(path_in); FILINFO fno; @@ -235,6 +242,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir); STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); + verify_fs_writable(self); const char *old_path = mp_obj_str_get_str(path_in); const char *new_path = mp_obj_str_get_str(path_out); @@ -271,6 +279,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename); STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); + verify_fs_writable(self); const char *path = mp_obj_str_get_str(path_o); FRESULT res = f_mkdir(&self->fatfs, path); if (res == FR_OK) { @@ -435,6 +444,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getlabel_obj, vfs_fat_getlabel); STATIC mp_obj_t vfs_fat_setlabel(mp_obj_t self_in, mp_obj_t label_in) { fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in); + verify_fs_writable(self); const char *label_str = mp_obj_str_get_str(label_in); FRESULT res = f_setlabel(&self->fatfs, label_str); if (res != FR_OK) {