Skip to content

Commit 3d22d52

Browse files
committed
Prevent both host and CircuitPython rw access to mounted filesystems
1 parent bfb80c2 commit 3d22d52

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

py/circuitpy_mpconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ typedef long mp_off_t;
345345
#endif
346346

347347

348+
// For easy debugging printf's.
349+
#define PLAT_PRINTF(...) mp_printf(&mp_plat_print, __VA_ARGS__)
350+
348351
#if MICROPY_PY_ASYNC_AWAIT && !CIRCUITPY_TRACEBACK
349352
#error CIRCUITPY_ASYNCIO requires CIRCUITPY_TRACEBACK
350353
#endif

shared-bindings/storage/__init__.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,23 @@ static mp_obj_t storage_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t
5454
// get the mount point
5555
const char *mnt_str = mp_obj_str_get_str(args[ARG_mount_path].u_obj);
5656

57+
58+
mp_obj_t vfs_obj = args[ARG_filesystem].u_obj;
59+
60+
// Currently, the only supported filesystem is VfsFat.
61+
mp_arg_validate_type(vfs_obj, &mp_fat_vfs_type, MP_QSTR_filesystem);
62+
63+
// Add this back if/when we start supporting other filesystems.
64+
#if 0
5765
// Make sure we're given an object we can mount.
5866
// TODO(tannewt): Make sure we have all the methods we need to operating it
5967
// as a file system.
60-
mp_obj_t vfs_obj = args[ARG_filesystem].u_obj;
6168
mp_obj_t dest[2];
6269
mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
6370
if (dest[0] == MP_OBJ_NULL) {
6471
mp_raise_ValueError(MP_ERROR_TEXT("filesystem must provide mount method"));
6572
}
73+
#endif
6674

6775
common_hal_storage_mount(vfs_obj, mnt_str, args[ARG_readonly].u_bool);
6876

shared-module/storage/__init__.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ void common_hal_storage_mount(mp_obj_t vfs_obj, const char *mount_path, bool rea
126126
// call the underlying object to do any mounting operation
127127
mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t *)&args);
128128

129+
fs_user_mount_t *vfs_fat = MP_OBJ_TO_PTR(vfs_obj);
130+
// Filesystem is read-only to USB if writable by CircuitPython, and vice versa.
131+
filesystem_set_writable_by_usb(vfs_fat, readonly);
132+
filesystem_set_concurrent_write_protection(vfs_fat, true);
133+
129134
// Insert the vfs into the mount table by pushing it onto the front of the
130135
// mount table.
131136
mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);

supervisor/shared/filesystem.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable) {
247247
}
248248

249249
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs) {
250-
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
251-
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) == 0;
250+
return ((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0) ||
251+
((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) == 0);
252252
}
253253

254254
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs) {
255-
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
256-
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) != 0;
255+
return ((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0) ||
256+
((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) != 0);
257257
}
258258

259259
void filesystem_set_internal_concurrent_write_protection(bool concurrent_write_protection) {

0 commit comments

Comments
 (0)