[pve-devel] [PATCH qemu-server v3 07/13] PVE/API2/Qemu: add permission checks for mapped usb devices
Dominik Csapak
d.csapak at proxmox.com
Tue Sep 20 14:50:22 CEST 2022
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
PVE/API2/Qemu.pm | 40 +++++++++++++++++++++++++++++++++++++---
PVE/QemuServer.pm | 2 ++
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 3ec31c2..7afd7a4 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::USB qw(parse_usb_device);
use PVE::QemuMigrate;
use PVE::RPCEnvironment;
use PVE::AccessControl;
@@ -586,8 +587,13 @@ my $check_vm_create_usb_perm = sub {
foreach my $opt (keys %{$param}) {
next if $opt !~ m/^usb\d+$/;
+ my $entry = PVE::JSONSchema::parse_property_string('pve-qm-usb', $param->{$opt});
+ my $device = parse_usb_device($entry->{host});
- if ($param->{$opt} =~ m/spice/) {
+ if ($device->{spice}) {
+ $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
+ } elsif ($device->{mapped}) {
+ $rpcenv->check_full($authuser, "/hardware/$entry->{host}", ['Hardware.Use']);
$rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
} else {
die "only root can set '$opt' config for real devices\n";
@@ -1571,7 +1577,12 @@ my $update_vm_api = sub {
PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
PVE::QemuConfig->write_config($vmid, $conf);
} elsif ($opt =~ m/^usb\d+$/) {
- if ($val =~ m/spice/) {
+ my $device = PVE::JSONSchema::parse_property_string('pve-qm-usb', $val);
+ my $host = 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, "/hardware/$device->{host}", ['Hardware.Use']);
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
} elsif ($authuser ne 'root at pam') {
die "only root can delete '$opt' config for real devices\n";
@@ -1632,7 +1643,30 @@ my $update_vm_api = sub {
}
$conf->{pending}->{$opt} = $param->{$opt};
} elsif ($opt =~ m/^usb\d+/) {
- if ((!defined($conf->{$opt}) || $conf->{$opt} =~ m/spice/) && $param->{$opt} =~ m/spice/) {
+ my $olddevice;
+ my $oldhost;
+ if (defined($conf->{$opt})) {
+ $olddevice = PVE::JSONSchema::parse_property_string('pve-qm-usb', $conf->{$opt});
+ $oldhost = parse_usb_device($olddevice->{host});
+ }
+ if (defined($oldhost)) {
+ if ($oldhost->{spice}) {
+ $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
+ } elsif ($oldhost->{mapped}) {
+ $rpcenv->check_full($authuser, "/hardware/$olddevice->{host}", ['Hardware.Use']);
+ $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
+ } elsif ($authuser ne 'root at pam') {
+ die "only root can modify '$opt' config for real devices\n";
+ }
+ }
+
+ my $newdevice = PVE::JSONSchema::parse_property_string('pve-qm-usb', $param->{$opt});
+ my $newhost = 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, "/hardware/$newdevice->{host}", ['Hardware.Use']);
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
} elsif ($authuser ne 'root at pam') {
die "only root can modify '$opt' config for real devices\n";
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index be38e10..6a2ad8d 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1085,6 +1085,8 @@ EODESCR
},
};
+PVE::JSONSchema::register_format('pve-qm-usb', $usb_fmt);
+
my $usbdesc = {
optional => 1,
type => 'string', format => $usb_fmt,
--
2.30.2
More information about the pve-devel
mailing list