Skip to content

Commit 3c603f7

Browse files
jeplerdpgeorge
authored andcommitted
extmod/vfs_posix: Add additional readonly checks.
Otherwise operations such as unlink can be performed on a nominally read-only VfsPosix. Signed-off-by: Jeff Epler <jepler@gmail.com>
1 parent d79000d commit 3c603f7

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

extmod/vfs_posix.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,21 @@ static mp_obj_t vfs_posix_umount(mp_obj_t self_in) {
160160
}
161161
static MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_umount_obj, vfs_posix_umount);
162162

163+
static inline bool vfs_posix_is_readonly(mp_obj_vfs_posix_t *self) {
164+
return self->readonly;
165+
}
166+
167+
static void vfs_posix_require_writable(mp_obj_t self_in) {
168+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
169+
if (vfs_posix_is_readonly(self)) {
170+
mp_raise_OSError(MP_EROFS);
171+
}
172+
}
173+
163174
static mp_obj_t vfs_posix_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) {
164175
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
165176
const char *mode = mp_obj_str_get_str(mode_in);
166-
if (self->readonly
177+
if (vfs_posix_is_readonly(self)
167178
&& (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL || strchr(mode, '+') != NULL)) {
168179
mp_raise_OSError(MP_EROFS);
169180
}
@@ -303,6 +314,7 @@ typedef struct _mp_obj_listdir_t {
303314
} mp_obj_listdir_t;
304315

305316
static mp_obj_t vfs_posix_mkdir(mp_obj_t self_in, mp_obj_t path_in) {
317+
vfs_posix_require_writable(self_in);
306318
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
307319
const char *path = vfs_posix_get_path_str(self, path_in);
308320
MP_THREAD_GIL_EXIT();
@@ -320,11 +332,13 @@ static mp_obj_t vfs_posix_mkdir(mp_obj_t self_in, mp_obj_t path_in) {
320332
static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_mkdir_obj, vfs_posix_mkdir);
321333

322334
static mp_obj_t vfs_posix_remove(mp_obj_t self_in, mp_obj_t path_in) {
335+
vfs_posix_require_writable(self_in);
323336
return vfs_posix_fun1_helper(self_in, path_in, unlink);
324337
}
325338
static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_remove_obj, vfs_posix_remove);
326339

327340
static mp_obj_t vfs_posix_rename(mp_obj_t self_in, mp_obj_t old_path_in, mp_obj_t new_path_in) {
341+
vfs_posix_require_writable(self_in);
328342
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
329343
const char *old_path = vfs_posix_get_path_str(self, old_path_in);
330344
const char *new_path = vfs_posix_get_path_str(self, new_path_in);
@@ -339,6 +353,7 @@ static mp_obj_t vfs_posix_rename(mp_obj_t self_in, mp_obj_t old_path_in, mp_obj_
339353
static MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_rename_obj, vfs_posix_rename);
340354

341355
static mp_obj_t vfs_posix_rmdir(mp_obj_t self_in, mp_obj_t path_in) {
356+
vfs_posix_require_writable(self_in);
342357
return vfs_posix_fun1_helper(self_in, path_in, rmdir);
343358
}
344359
static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_rmdir_obj, vfs_posix_rmdir);

0 commit comments

Comments
 (0)