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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 73C3D8A0C5 for ; Mon, 1 Aug 2022 15:02:08 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 655CD30092 for ; Mon, 1 Aug 2022 15:01:38 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 1 Aug 2022 15:01:37 +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 3C84640F21 for ; Mon, 1 Aug 2022 15:01:37 +0200 (CEST) Date: Mon, 01 Aug 2022 15:01:30 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox VE development discussion References: <20220719114639.3035048-1-d.csapak@proxmox.com> <20220719114639.3035048-9-d.csapak@proxmox.com> In-Reply-To: <<20220719114639.3035048-9-d.csapak@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.15.0 (https://github.com/astroidmail/astroid) Message-Id: <1659357443.58xyuoq5i7.astroid@nora.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.160 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 Subject: Re: [pve-devel] [PATCH qemu-server 4/7] PVE/API2/Qemu: add permission checks for mapped pci devices 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: Mon, 01 Aug 2022 13:02:08 -0000 On July 19, 2022 1:46 pm, Dominik Csapak wrote: > Signed-off-by: Dominik Csapak > --- > PVE/API2/Qemu.pm | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 52 insertions(+), 2 deletions(-) >=20 > diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm > index aa7ddea..a8029c2 100644 > --- a/PVE/API2/Qemu.pm > +++ b/PVE/API2/Qemu.pm > @@ -26,6 +26,7 @@ use PVE::QemuServer::Drive; > use PVE::QemuServer::ImportDisk; > use PVE::QemuServer::Monitor qw(mon_cmd); > use PVE::QemuServer::Machine; > +use PVE::QemuServer::PCI; > use PVE::QemuServer::USB qw(parse_usb_device); > use PVE::QemuMigrate; > use PVE::RPCEnvironment; > @@ -583,6 +584,26 @@ my $check_vm_create_usb_perm =3D sub { > return 1; > }; > =20 > +my $check_vm_create_hostpci_perm =3D sub { > + my ($rpcenv, $authuser, $vmid, $pool, $param) =3D @_; > + > + return 1 if $authuser eq 'root@pam'; > + > + foreach my $opt (keys %{$param}) { > + next if $opt !~ m/^hostpci\d+$/; > + > + my $device =3D PVE::JSONSchema::parse_property_string('pve-qm-hostpci',= $param->{$opt}); > + if ($device->{host} !~ m/:/) { > + $rpcenv->check_hw_perm($authuser, $device->{host}, ['Hardware.Use']= ); > + $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType'= ]); > + } else { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } > + } > + > + return 1; > +}; > + > my $check_vm_modify_config_perm =3D sub { > my ($rpcenv, $authuser, $vmid, $pool, $key_list) =3D @_; > =20 > @@ -593,7 +614,7 @@ my $check_vm_modify_config_perm =3D sub { > # else, as there the permission can be value dependend > next if PVE::QemuServer::is_valid_drivename($opt); > next if $opt eq 'cdrom'; > - next if $opt =3D~ m/^(?:unused|serial|usb)\d+$/; > + next if $opt =3D~ m/^(?:unused|serial|usb|hostpci)\d+$/; > =20 > =20 > if ($cpuoptions->{$opt} || $opt =3D~ m/^numa\d+$/) { > @@ -621,7 +642,7 @@ my $check_vm_modify_config_perm =3D sub { > # also needs privileges on the storage, that will be checked later > $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk', = 'VM.PowerMgmt' ]); > } else { > - # catches hostpci\d+, args, lock, etc. > + # catches args, lock, etc. > # new options will be checked here > die "only root can set '$opt' config\n"; > } > @@ -856,6 +877,7 @@ __PACKAGE__->register_method({ > =20 > &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $pa= ram); > &$check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param= ); > + &$check_vm_create_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $p= aram); > =20 > &$check_cpu_model_access($rpcenv, $authuser, $param); > =20 > @@ -1569,6 +1591,16 @@ 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/^hostpci\d+$/) { > + my $olddevice =3D PVE::JSONSchema::parse_property_string('pve-qm-h= ostpci', $val); > + if ($olddevice->{host} !~ m/:/) { > + $rpcenv->check_hw_perm($authuser, $olddevice->{host}, ['Hardware.Use'= ]); > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > + } elsif ($authuser ne 'root@pam') { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } > + PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); > + PVE::QemuConfig->write_config($vmid, $conf); > } else { > PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); > PVE::QemuConfig->write_config($vmid, $conf); > @@ -1651,6 +1683,24 @@ my $update_vm_api =3D sub { > } elsif ($authuser ne 'root@pam') { > die "only root can modify '$opt' config for real devices\n"; > } > + > + $conf->{pending}->{$opt} =3D $param->{$opt}; > + } elsif ($opt =3D~ m/^hostpci\d+$/) { > + my $olddevice; > + if (defined($conf->{$opt})) { > + $olddevice =3D PVE::JSONSchema::parse_property_string('pve-qm-hostpci= ', $conf->{$opt}); > + } > + my $newdevice =3D PVE::JSONSchema::parse_property_string('pve-qm-h= ostpci', $param->{$opt}); > + if ((!defined($olddevice) || $olddevice->{host} !~ m/:/) && $newde= vice->{host} !~ m/:/) { > + if (defined($olddevice)) { > + $rpcenv->check_hw_perm($authuser, $olddevice->{host}, ['Hardware.= Use']); > + } > + $rpcenv->check_hw_perm($authuser, $newdevice->{host}, ['Hardware.Use'= ]); > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > + } elsif ($authuser ne 'root@pam') { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } nit: this is a bit confusing to read, and might benefit from splitting the=20 check for old and for new, like so if (defined($old)) { parse old check old || check root } parse new check new || check root check VM.Config.HWType > + PVE::QemuServer::PCI::parse_hostpci($param->{$opt}); > $conf->{pending}->{$opt} =3D $param->{$opt}; > } else { > $conf->{pending}->{$opt} =3D $param->{$opt}; > --=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