Allow non-root users with the VM.Config.HWType privilege to configure /dev/urandom & /dev/random as an entropy source for a VirtIO RNG device. /dev/hwrng remains restricted to the root user.
Signed-off-by: Filip Schauer <f.scha...@proxmox.com> --- PVE/API2/Qemu.pm | 42 ++++++++++++++++++++++++++++++++++++++++++ PVE/QemuServer.pm | 13 +++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index db356b7e..8262c9d4 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -670,6 +670,7 @@ my $hwtypeoptions = { 'vga' => 1, 'watchdog' => 1, 'audio0' => 1, + 'rng0' => 1, }; my $generaloptions = { @@ -798,6 +799,36 @@ my sub check_vm_create_hostpci_perm { return 1; }; +my sub check_rng_perm { + my ($rpcenv, $authuser, $vmid, $pool, $opt, $value) = @_; + + return 1 if $authuser eq 'root@pam'; + + $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); + + my $device = PVE::JSONSchema::parse_property_string('pve-qm-rng', $value); + if ($device->{source}) { + if ($device->{source} eq '/dev/hwrng') { + die "only root can set '$opt' config for a non-mapped Hardware RNG device\n"; + } + } + + return 1; +} + +my sub check_vm_create_rng_perm { + my ($rpcenv, $authuser, $vmid, $pool, $param) = @_; + + return 1 if $authuser eq 'root@pam'; + + foreach my $opt (keys %{$param}) { + next if $opt !~ m/^rng\d+$/; + check_rng_perm($rpcenv, $authuser, $vmid, $pool, $opt, $param->{$opt}); + } + + return 1; +}; + my $check_vm_modify_config_perm = sub { my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; @@ -1111,6 +1142,7 @@ __PACKAGE__->register_method({ &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $param); check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param); check_vm_create_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $param); + check_vm_create_rng_perm($rpcenv, $authuser, $vmid, $pool, $param); PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $param); &$check_cpu_model_access($rpcenv, $authuser, $param); @@ -2008,6 +2040,10 @@ my $update_vm_api = sub { check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $val); PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); PVE::QemuConfig->write_config($vmid, $conf); + } elsif ($opt eq m/^rng\d+$/) { + check_rng_perm($rpcenv, $authuser, $vmid, undef, $opt, $val); + PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); + PVE::QemuConfig->write_config($vmid, $conf); } elsif ($opt eq 'tags') { assert_tag_permissions($vmid, $val, '', $rpcenv, $authuser); delete $conf->{$opt}; @@ -2098,6 +2134,12 @@ my $update_vm_api = sub { } check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $param->{$opt}); $conf->{pending}->{$opt} = $param->{$opt}; + } elsif ($opt =~ m/^rng\d+$/) { + if (my $oldvalue = $conf->{$opt}) { + check_rng_perm($rpcenv, $authuser, $vmid, undef, $opt, $oldvalue); + } + check_rng_perm($rpcenv, $authuser, $vmid, undef, $opt, $param->{$opt}); + $conf->{pending}->{$opt} = $param->{$opt}; } elsif ($opt eq 'tags') { assert_tag_permissions($vmid, $conf->{$opt}, $param->{$opt}, $rpcenv, $authuser); $conf->{pending}->{$opt} = PVE::GuestHelpers::get_unique_tags($param->{$opt}); diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index 93e65825..606f51fa 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -6602,8 +6602,17 @@ sub check_mapping_access { } else { die "either 'host' or 'mapping' must be set.\n"; } - } - } + } elsif ($opt =~ m/^rng\d+$/) { + my $device = PVE::JSONSchema::parse_property_string('pve-qm-rng', $conf->{$opt}); + + if ($device->{source}) { + if ($device->{source} eq '/dev/hwrng') { + die "only root can set '$opt' config for a non-mapped Hardware RNG device\n" + if $user ne 'root@pam'; + } + } + } + } }; sub check_restore_permissions { -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel