From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 6F3FCA1BAA for ; Fri, 16 Jun 2023 09:50:13 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4FC632F316 for ; Fri, 16 Jun 2023 09:50:13 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Fri, 16 Jun 2023 09:50:12 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 02EB345A72 for ; Fri, 16 Jun 2023 09:50:12 +0200 (CEST) Date: Fri, 16 Jun 2023 09:50:05 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox VE development discussion References: <20230614084622.1446211-1-d.csapak@proxmox.com> <20230614084622.1446211-2-d.csapak@proxmox.com> In-Reply-To: <20230614084622.1446211-2-d.csapak@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.16.0 (https://github.com/astroidmail/astroid) Message-Id: <1686900692.sfzqrewa6n.astroid@yuna.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.072 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: Re: [pve-devel] [PATCH qemu-server v6 1/6] enable cluster mapped USB devices for guests X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jun 2023 07:50:13 -0000 On June 14, 2023 10:46 am, Dominik Csapak wrote: > this patch allows configuring usb devices that are mapped via > cluster resource mapping when the user has 'Resource.Use' on the ACL > path '/resource/usb/{ID}' (in addition to the usual required vm config > privileges) >=20 > for now, this is only valid if there is exactly one mapping for the > host, since we don't track passed through usb devices yet >=20 > also checks the permission for usb devices on clone/restore >=20 > Note that this now fails for 'raw' devices when the user is not root, so > this is a breaking change! >=20 > Signed-off-by: Dominik Csapak > --- > changes from v5: > * move the permission check into QemuServer.pm > * add a 'check_restore_permissions' functions and refactor the checks > into there > PVE/API2/Qemu.pm | 41 ++++++++++++++++++++++++++++++++++++++--- > PVE/QemuServer.pm | 36 +++++++++++++++++++++++++++++++++--- > PVE/QemuServer/USB.pm | 27 ++++++++++++++++++++++++--- > 3 files changed, 95 insertions(+), 9 deletions(-) >=20 > diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm > index c92734a6..865edf7f 100644 > --- a/PVE/API2/Qemu.pm > +++ b/PVE/API2/Qemu.pm > @@ -32,6 +32,7 @@ use PVE::QemuServer::Drive; > use PVE::QemuServer::ImportDisk; > use PVE::QemuServer::Monitor qw(mon_cmd); > use PVE::QemuServer::Machine; > +use PVE::QemuServer::USB qw(parse_usb_device); > use PVE::QemuMigrate; > use PVE::RPCEnvironment; > use PVE::AccessControl; > @@ -590,8 +591,13 @@ my $check_vm_create_usb_perm =3D sub { > =20 > foreach my $opt (keys %{$param}) { > next if $opt !~ m/^usb\d+$/; > + my $entry =3D PVE::JSONSchema::parse_property_string('pve-qm-usb', $par= am->{$opt}); > + my $device =3D parse_usb_device($entry->{host}); > =20 > - if ($param->{$opt} =3D~ m/spice/) { > + if ($device->{spice}) { > + $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType'= ]); > + } elsif ($device->{mapped}) { > + $rpcenv->check_full($authuser, "/mapping/usb/$entry->{host}", ['Map= ping.Use']); > $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType'= ]); > } else { > die "only root can set '$opt' config for real devices\n"; this part here > @@ -1719,7 +1725,12 @@ my $update_vm_api =3D sub { > PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); > PVE::QemuConfig->write_config($vmid, $conf); > } elsif ($opt =3D~ m/^usb\d+$/) { > - if ($val =3D~ m/spice/) { > + my $device =3D PVE::JSONSchema::parse_property_string('pve-qm-usb'= , $val); > + my $host =3D parse_usb_device($device->{host}); > + if ($host->{spice}) { > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > + } elsif ($host->{mapped}) { > + $rpcenv->check_full($authuser, "/mapping/usb/$device->{host}", ['Mapp= ing.Use']); > $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > } elsif ($authuser ne 'root@pam') { > die "only root can delete '$opt' config for real devices\n"; this part here (modulo variable names, especially confusing since $device is used in both!) > @@ -1784,7 +1795,30 @@ my $update_vm_api =3D sub { > } > $conf->{pending}->{$opt} =3D $param->{$opt}; > } elsif ($opt =3D~ m/^usb\d+/) { > - if ((!defined($conf->{$opt}) || $conf->{$opt} =3D~ m/spice/) && $p= aram->{$opt} =3D~ m/spice/) { > + my $olddevice; > + my $oldhost; > + if (defined($conf->{$opt})) { > + $olddevice =3D PVE::JSONSchema::parse_property_string('pve-qm-usb', $= conf->{$opt}); > + $oldhost =3D parse_usb_device($olddevice->{host}); > + } > + if (defined($oldhost)) { > + if ($oldhost->{spice}) { > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWTyp= e']); > + } elsif ($oldhost->{mapped}) { > + $rpcenv->check_full($authuser, "/mapping/usb/$olddevice->{host}",= ['Mapping.Use']); > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWTyp= e']); > + } elsif ($authuser ne 'root@pam') { > + die "only root can modify '$opt' config for real devices\n"; > + } this part here > + } > + > + my $newdevice =3D PVE::JSONSchema::parse_property_string('pve-qm-u= sb', $param->{$opt}); > + my $newhost =3D parse_usb_device($newdevice->{host}); > + > + if ($newhost->{spice}) { > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > + } elsif ($newhost->{mapped}) { > + $rpcenv->check_full($authuser, "/mapping/usb/$newdevice->{host}", ['M= apping.Use']); > $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > } elsif ($authuser ne 'root@pam') { > die "only root can modify '$opt' config for real devices\n"; and this part here are all identical except for some variations in variable names. this screams "single helper" to me.. it also might make sense to structure it differently? 1. always check VM.Config.HWType (explicitly or implicitly done in all bran= ches atm) 2. if mapped, check mapping 3. if not, die if not root I know you discussed with Thomas to split the config format a bit more here to make it less overloaded, which might make this part easier to understand as well :) > @@ -3511,6 +3545,7 @@ __PACKAGE__->register_method({ > my $oldconf =3D $snapname ? $conf->{snapshots}->{$snapname} : $conf= ; > =20 > my $sharedvm =3D &$check_storage_access_clone($rpcenv, $authuser, $= storecfg, $oldconf, $storage); > + PVE::QemuServer::check_mapping_access($rpcenv, $authuser, $oldconf)= ; > =20 > PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $oldconf); > =20 > diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm > index b978ab54..ab5458c3 100644 > --- a/PVE/QemuServer.pm > +++ b/PVE/QemuServer.pm > @@ -1095,6 +1095,8 @@ The Host USB device or port or the value 'spice'. H= OSTUSBDEVICE syntax is: > =20 > You can use the 'lsusb -t' command to list existing usb devices. > =20 > +Alternatively, you can used an ID of a mapped usb device. > + > NOTE: This option allows direct access to host hardware. So it is no lon= ger possible to migrate such > machines - use with special care. > =20 > @@ -1111,6 +1113,8 @@ EODESCR > }, > }; > =20 > +PVE::JSONSchema::register_format('pve-qm-usb', $usb_fmt); > + > my $usbdesc =3D { > optional =3D> 1, > type =3D> 'string', format =3D> $usb_fmt, > @@ -2248,7 +2252,12 @@ PVE::JSONSchema::register_format('pve-qm-usb-devic= e', \&verify_usb_device); > sub verify_usb_device { > my ($value, $noerr) =3D @_; > =20 > - return $value if parse_usb_device($value); > + my $parsed =3D eval { parse_usb_device($value) }; > + if (my $err =3D $@) { > + die $err if !$noerr; > + return; > + } > + return $value if defined($parsed); > =20 > return if $noerr; > =20 > @@ -6527,6 +6536,27 @@ sub check_bridge_access { > return 1; > }; > =20 > +sub check_mapping_access { > + my ($rpcenv, $user, $conf) =3D @_; > + > + for my $opt (keys $conf->%*) { > + if ($opt =3D~ m/^usb\d+$/) { > + my $entry =3D PVE::JSONSchema::parse_property_string('pve-qm-usb', $= conf->{$opt}); > + my $device =3D parse_usb_device($entry->{host}); > + if ($device->{mapped}) { > + $rpcenv->check_full($user, "/mapping/usb/$entry->{host}", ['Mapp= ing.Use']); > + } elsif (!$device->{spice}) { > + die "only root can set '$opt' config for real devices\n"; > + } > + } > + } > +}; > + > +sub check_restore_permissions { > + my ($rpcenv, $user, $conf) =3D @_; > + check_bridge_access($rpcenv, $user, $conf); > + check_mapping_access($rpcenv, $user, $conf); > +} might want a FIXME comment there to improve this w.r.t. checking before disk allocation if possible? e.g., the helper could take old and optionally new config (not possible for STDIN, but then we are root anyway so the checks should also never fail ;)), and do the checks on the "overlay". then we could call it twice: - with old config and optionally extracted config before starting the restore (early abort!) - with the merged config after the restore (final check with the real config we write out) > # vzdump restore implementaion > =20 > sub tar_archive_read_firstfile { > @@ -7171,7 +7201,7 @@ sub restore_proxmox_backup_archive { > } > =20 > my $new_conf =3D $restore_merge_config->($conffile, $new_conf_raw, $= options->{override_conf}); > - check_bridge_access($rpcenv, $user, $new_conf); > + check_restore_permissions($rpcenv, $user, $new_conf); > PVE::QemuConfig->write_config($vmid, $new_conf); > =20 > eval { rescan($vmid, 1); }; > @@ -7485,7 +7515,7 @@ sub restore_vma_archive { > } > =20 > my $new_conf =3D $restore_merge_config->($conffile, $new_conf_raw, $= opts->{override_conf}); > - check_bridge_access($rpcenv, $user, $new_conf); > + check_restore_permissions($rpcenv, $user, $new_conf); > PVE::QemuConfig->write_config($vmid, $new_conf); > =20 > eval { rescan($vmid, 1); }; > diff --git a/PVE/QemuServer/USB.pm b/PVE/QemuServer/USB.pm > index 686461cc..d6b4c531 100644 > --- a/PVE/QemuServer/USB.pm > +++ b/PVE/QemuServer/USB.pm > @@ -6,6 +6,7 @@ use PVE::QemuServer::PCI qw(print_pci_addr); > use PVE::QemuServer::Machine; > use PVE::QemuServer::Helpers qw(min_version windows_version); > use PVE::JSONSchema; > +use PVE::Mapping::USB; > use base 'Exporter'; > =20 > our @EXPORT_OK =3D qw( > @@ -17,7 +18,7 @@ get_usb_devices > my $OLD_MAX_USB =3D 5; > =20 > sub parse_usb_device { > - my ($value) =3D @_; > + my ($value, $checkMap) =3D @_; > =20 > return if !$value; > =20 > @@ -31,7 +32,27 @@ sub parse_usb_device { > } elsif ($value =3D~ m/^spice$/i) { > $res->{spice} =3D 1; > } else { > - return; > + # we have no ordinary usb device, must be a mapping > + my $devices =3D PVE::Mapping::USB::find_on_current_node($value); > + if ($checkMap) { > + die "USB device mapping not found for '$value'\n" if !$devices || != scalar($devices->@*); > + die "More than one USB mapping per host not supported\n" if scalar(= $devices->@*) > 1; > + eval { > + PVE::Mapping::USB::assert_valid($value, $devices->[0]); > + }; > + if (my $err =3D $@) { > + die "USB Mapping invalid (hardware probably changed): $err\n"; > + } > + my $device =3D $devices->[0]; > + > + if ($device->{path}) { > + $res =3D parse_usb_device($device->{path}); > + } else { > + $res =3D parse_usb_device($device->{id}); > + } > + } > + > + $res->{mapped} =3D 1; > } > =20 > return $res; > @@ -111,7 +132,7 @@ sub get_usb_devices { > my $port =3D $use_qemu_xhci ? $i + 1 : undef; > =20 > if (defined($d->{host})) { > - my $hostdevice =3D parse_usb_device($d->{host}); > + my $hostdevice =3D parse_usb_device($d->{host}, 1); > $hostdevice->{usb3} =3D $d->{usb3}; > if ($hostdevice->{spice}) { > # usb redir support for spice > --=20 > 2.30.2 >=20 >=20 >=20 > _______________________________________________ > pve-devel mailing list > pve-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel >=20 >=20 >=20