tools/mpremote: Prevent deletion of /remote files via rm -r.
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled

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 #17147.

Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
This commit is contained in:
Jos Verlinde
2025-04-17 16:52:23 +02:00
committed by Damien George
parent 37fe3f66c3
commit 6406afb1f3
2 changed files with 22 additions and 5 deletions

View File

@@ -303,6 +303,17 @@ def do_filesystem_recursive_cp(state, src, dest, multiple, check_hash):
def do_filesystem_recursive_rm(state, path, args):
if state.transport.fs_isdir(path):
if state.transport.mounted:
r_cwd = state.transport.eval("os.getcwd()")
abs_path = os.path.normpath(
os.path.join(r_cwd, path) if not os.path.isabs(path) else path
)
if isinstance(state.transport, SerialTransport) and abs_path.startswith(
f'{SerialTransport.fs_hook_mount}/'
):
raise CommandError(
f"rm -r not permitted on {SerialTransport.fs_hook_mount} directory"
)
for entry in state.transport.fs_listdir(path):
do_filesystem_recursive_rm(state, _remote_path_join(path, entry.name), args)
if path:

View File

@@ -42,6 +42,8 @@ from .transport import TransportError, TransportExecError, Transport
class SerialTransport(Transport):
fs_hook_mount = "/remote" # MUST match the mount point in fs_hook_code
def __init__(self, device, baudrate=115200, wait=0, exclusive=True, timeout=None):
self.in_raw_repl = False
self.use_raw_paste = True
@@ -375,7 +377,11 @@ class SerialTransport(Transport):
self.serial = self.serial.orig_serial
# Provide a message about the remount.
out_callback(bytes(f"\r\nRemount local directory {self.cmd.root} at /remote\r\n", "utf8"))
out_callback(
bytes(
f"\r\nRemount local directory {self.cmd.root} at {self.fs_hook_mount}\r\n", "utf8"
)
)
# Enter raw REPL and re-mount the remote filesystem.
self.serial.write(b"\x01")
@@ -392,7 +398,7 @@ class SerialTransport(Transport):
def umount_local(self):
if self.mounted:
self.exec('os.umount("/remote")')
self.exec(f'os.umount("{self.fs_hook_mount}")')
self.mounted = False
self.serial = self.serial.orig_serial
@@ -413,7 +419,7 @@ fs_hook_cmds = {
"CMD_RMDIR": 13,
}
fs_hook_code = """\
fs_hook_code = f"""\
import os, io, struct, micropython
SEEK_SET = 0
@@ -746,8 +752,8 @@ class RemoteFS:
def __mount():
os.mount(RemoteFS(RemoteCommand()), '/remote')
os.chdir('/remote')
os.mount(RemoteFS(RemoteCommand()), '{SerialTransport.fs_hook_mount}')
os.chdir('{SerialTransport.fs_hook_mount}')
"""
# Apply basic compression on hook code.