From a5da23005860f7dcc397a63fe349f217189120be Mon Sep 17 00:00:00 2001 From: Piotr Bartman-Szwarc Date: Mon, 30 Sep 2024 16:06:35 +0200 Subject: [PATCH] q-dev: filter out not allowed arguments during detach --- qubesadmin/tools/qvm_device.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/qubesadmin/tools/qvm_device.py b/qubesadmin/tools/qvm_device.py index 5e5d98db..6c3d92e3 100644 --- a/qubesadmin/tools/qvm_device.py +++ b/qubesadmin/tools/qvm_device.py @@ -232,12 +232,27 @@ def detach_device(args): subcommand. """ vm = args.domains[0] - if args.device: - device = args.device - # ignore device id, detach any device - device.device_id = '*' + device = args.device + if device and device.port_id != '*': assignment = DeviceAssignment(device) + + try: + actual_dev = assignment.device + except ProtocolError as exc: + raise qubesadmin.exc.QubesException(str(exc)) + + if not assignment.matches(actual_dev): + raise qubesadmin.exc.QubesException( + f"{device} is not attached.") + vm.devices[args.devclass].detach(assignment) + elif args.device: + assignment = DeviceAssignment(device) + for ass in (vm.devices[args.devclass].get_attached_devices()): + if assignment.matches(ass.device): + vm.devices[args.devclass].detach(ass) + else: + raise qubesadmin.exc.QubesException(f"{device} is not attached.") else: for ass in (vm.devices[args.devclass].get_attached_devices()): vm.devices[args.devclass].detach(ass)