Skip to content

Commit 6406afb

Browse files
Josverldpgeorge
authored andcommitted
tools/mpremote: Prevent deletion of /remote files via rm -r.
Removes the risk of inadvertently deleting files on the host by preventing the deletion of files via `rm -r` on the `/remote` vfs mount point. Fixes issue micropython#17147. Signed-off-by: Jos Verlinde <[email protected]>
1 parent 37fe3f6 commit 6406afb

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

tools/mpremote/mpremote/commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ def _mkdir(a, *b):
303303

304304
def do_filesystem_recursive_rm(state, path, args):
305305
if state.transport.fs_isdir(path):
306+
if state.transport.mounted:
307+
r_cwd = state.transport.eval("os.getcwd()")
308+
abs_path = os.path.normpath(
309+
os.path.join(r_cwd, path) if not os.path.isabs(path) else path
310+
)
311+
if isinstance(state.transport, SerialTransport) and abs_path.startswith(
312+
f'{SerialTransport.fs_hook_mount}/'
313+
):
314+
raise CommandError(
315+
f"rm -r not permitted on {SerialTransport.fs_hook_mount} directory"
316+
)
306317
for entry in state.transport.fs_listdir(path):
307318
do_filesystem_recursive_rm(state, _remote_path_join(path, entry.name), args)
308319
if path:

tools/mpremote/mpremote/transport_serial.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343

4444
class SerialTransport(Transport):
45+
fs_hook_mount = "/remote" # MUST match the mount point in fs_hook_code
46+
4547
def __init__(self, device, baudrate=115200, wait=0, exclusive=True, timeout=None):
4648
self.in_raw_repl = False
4749
self.use_raw_paste = True
@@ -375,7 +377,11 @@ def write_ctrl_d(self, out_callback):
375377
self.serial = self.serial.orig_serial
376378

377379
# Provide a message about the remount.
378-
out_callback(bytes(f"\r\nRemount local directory {self.cmd.root} at /remote\r\n", "utf8"))
380+
out_callback(
381+
bytes(
382+
f"\r\nRemount local directory {self.cmd.root} at {self.fs_hook_mount}\r\n", "utf8"
383+
)
384+
)
379385

380386
# Enter raw REPL and re-mount the remote filesystem.
381387
self.serial.write(b"\x01")
@@ -392,7 +398,7 @@ def write_ctrl_d(self, out_callback):
392398

393399
def umount_local(self):
394400
if self.mounted:
395-
self.exec('os.umount("/remote")')
401+
self.exec(f'os.umount("{self.fs_hook_mount}")')
396402
self.mounted = False
397403
self.serial = self.serial.orig_serial
398404

@@ -413,7 +419,7 @@ def umount_local(self):
413419
"CMD_RMDIR": 13,
414420
}
415421

416-
fs_hook_code = """\
422+
fs_hook_code = f"""\
417423
import os, io, struct, micropython
418424
419425
SEEK_SET = 0
@@ -746,8 +752,8 @@ def open(self, path, mode):
746752
747753
748754
def __mount():
749-
os.mount(RemoteFS(RemoteCommand()), '/remote')
750-
os.chdir('/remote')
755+
os.mount(RemoteFS(RemoteCommand()), '{SerialTransport.fs_hook_mount}')
756+
os.chdir('{SerialTransport.fs_hook_mount}')
751757
"""
752758

753759
# Apply basic compression on hook code.

0 commit comments

Comments
 (0)