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 #17147. Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
This commit is contained in:
committed by
Damien George
parent
37fe3f66c3
commit
6406afb1f3
@@ -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:
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user