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 DD96488312 for ; Wed, 5 Jan 2022 16:22:57 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D2CF5A996 for ; Wed, 5 Jan 2022 16:22:27 +0100 (CET) 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 id 49004A98C for ; Wed, 5 Jan 2022 16:22:26 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 212B346B73 for ; Wed, 5 Jan 2022 16:22:26 +0100 (CET) From: Oguz Bektas To: pve-devel@lists.proxmox.com Date: Wed, 5 Jan 2022 16:22:15 +0100 Message-Id: <20220105152215.1307583-5-o.bektas@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220105152215.1307583-1-o.bektas@proxmox.com> References: <20220105152215.1307583-1-o.bektas@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.684 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [qemu.pm] Subject: [pve-devel] [RFC qemu-server 4/4] api: use common helper for checking root privileges 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: Wed, 05 Jan 2022 15:22:57 -0000 to allow API users with the 'Sys.Root' permission to call these endpoints. Signed-off-by: Oguz Bektas --- PVE/API2/Qemu.pm | 74 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 6992f6f..e846a1e 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -352,7 +352,7 @@ my $cloudinitoptions = { my $check_vm_create_serial_perm = sub { my ($rpcenv, $authuser, $vmid, $pool, $param) = @_; - return 1 if $authuser eq 'root@pam'; + return 1 if PVE::Tools::check_for_root("/vms/$vmid"); foreach my $opt (keys %{$param}) { next if $opt !~ m/^serial\d+$/; @@ -370,7 +370,7 @@ my $check_vm_create_serial_perm = sub { my $check_vm_create_usb_perm = sub { my ($rpcenv, $authuser, $vmid, $pool, $param) = @_; - return 1 if $authuser eq 'root@pam'; + return 1 if PVE::Tools::check_for_root("/vms/$vmid"); foreach my $opt (keys %{$param}) { next if $opt !~ m/^usb\d+$/; @@ -388,7 +388,7 @@ my $check_vm_create_usb_perm = sub { my $check_vm_modify_config_perm = sub { my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; - return 1 if $authuser eq 'root@pam'; + return 1 if PVE::Tools::check_for_root("/vms/$vmid"); foreach my $opt (@$key_list) { # some checks (e.g., disk, serial port, usb) need to be done somewhere @@ -1105,6 +1105,8 @@ my $update_vm_api = sub { my $background_delay = extract_param($param, 'background_delay'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + if (defined(my $cipassword = $param->{cipassword})) { # Same logic as in cloud-init (but with the regex fixed...) $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) @@ -1119,7 +1121,7 @@ my $update_vm_api = sub { my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; my $delete_str = extract_param($param, 'delete'); @@ -1340,7 +1342,7 @@ my $update_vm_api = sub { } elsif ($opt =~ m/^serial\d+$/) { if ($val eq 'socket') { $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); - } elsif ($authuser ne 'root@pam') { + } elsif (!$is_root) { die "only root can delete '$opt' config for real devices\n"; } PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); @@ -1348,7 +1350,7 @@ my $update_vm_api = sub { } elsif ($opt =~ m/^usb\d+$/) { if ($val =~ m/spice/) { $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); - } elsif ($authuser ne 'root@pam') { + } elsif (!$is_root) { die "only root can delete '$opt' config for real devices\n"; } PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); @@ -1392,14 +1394,14 @@ my $update_vm_api = sub { } elsif ($opt =~ m/^serial\d+/) { if ((!defined($conf->{$opt}) || $conf->{$opt} eq 'socket') && $param->{$opt} eq 'socket') { $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); - } elsif ($authuser ne 'root@pam') { + } elsif (!$is_root) { die "only root can modify '$opt' config for real devices\n"; } $conf->{pending}->{$opt} = $param->{$opt}; } elsif ($opt =~ m/^usb\d+/) { if ((!defined($conf->{$opt}) || $conf->{$opt} =~ m/spice/) && $param->{$opt} =~ m/spice/) { $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); - } elsif ($authuser ne 'root@pam') { + } elsif (!$is_root) { die "only root can modify '$opt' config for real devices\n"; } $conf->{pending}->{$opt} = $param->{$opt}; @@ -1644,9 +1646,11 @@ __PACKAGE__->register_method({ my $authuser = $rpcenv->get_user(); my $vmid = $param->{vmid}; + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $skiplock = $param->{skiplock}; raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; my $early_checks = sub { # test if VM exists @@ -2291,10 +2295,12 @@ __PACKAGE__->register_method({ my $machine = extract_param($param, 'machine'); my $force_cpu = extract_param($param, 'force-cpu'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $get_root_param = sub { my $value = extract_param($param, $_[0]); raise_param_exc({ "$_[0]" => "Only root may use this option." }) - if $value && $authuser ne 'root@pam'; + if $value && !$is_root; return $value; }; @@ -2436,17 +2442,19 @@ __PACKAGE__->register_method({ my $node = extract_param($param, 'node'); my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; my $keepActive = extract_param($param, 'keepActive'); raise_param_exc({ keepActive => "Only root may use this option." }) - if $keepActive && $authuser ne 'root@pam'; + if $keepActive && !$is_root; my $migratedfrom = extract_param($param, 'migratedfrom'); raise_param_exc({ migratedfrom => "Only root may use this option." }) - if $migratedfrom && $authuser ne 'root@pam'; + if $migratedfrom && !$is_root; my $storecfg = PVE::Storage::config(); @@ -2513,9 +2521,11 @@ __PACKAGE__->register_method({ my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); @@ -2580,13 +2590,15 @@ __PACKAGE__->register_method({ my $node = extract_param($param, 'node'); my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; my $keepActive = extract_param($param, 'keepActive'); raise_param_exc({ keepActive => "Only root may use this option." }) - if $keepActive && $authuser ne 'root@pam'; + if $keepActive && !$is_root; my $storecfg = PVE::Storage::config(); @@ -2735,13 +2747,15 @@ __PACKAGE__->register_method({ my $node = extract_param($param, 'node'); my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $todisk = extract_param($param, 'todisk') // 0; my $statestorage = extract_param($param, 'statestorage'); my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); @@ -2811,13 +2825,15 @@ __PACKAGE__->register_method({ my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; my $nocheck = extract_param($param, 'nocheck'); raise_param_exc({ nocheck => "Only root may use this option." }) - if $nocheck && $authuser ne 'root@pam'; + if $nocheck && !$is_root; my $to_disk_suspended; eval { @@ -2883,9 +2899,11 @@ __PACKAGE__->register_method({ my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $skiplock = extract_param($param, 'skiplock'); raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); @@ -3392,6 +3410,8 @@ __PACKAGE__->register_method({ my $storecfg = PVE::Storage::config(); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + my $move_updatefn = sub { my $conf = PVE::QemuConfig->load_config($vmid); PVE::QemuConfig->check_lock($conf); @@ -3671,7 +3691,7 @@ __PACKAGE__->register_method({ raise_param_exc({ 'target-vmid' => $msg, 'storage' => $msg }); } elsif ($target_vmid) { $rpcenv->check_vm_perm($authuser, $target_vmid, undef, ['VM.Config.Disk']) - if $authuser ne 'root@pam'; + if !$is_root; raise_param_exc({ 'target-vmid' => "must be different than source VMID to reassign disk" }) if $vmid eq $target_vmid; @@ -3910,15 +3930,17 @@ __PACKAGE__->register_method({ my $vmid = extract_param($param, 'vmid'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + raise_param_exc({ force => "Only root may use this option." }) - if $param->{force} && $authuser ne 'root@pam'; + if $param->{force} && !$is_root; raise_param_exc({ migration_type => "Only root may use this option." }) - if $param->{migration_type} && $authuser ne 'root@pam'; + if $param->{migration_type} && !$is_root; # allow root only until better network permissions are available raise_param_exc({ migration_network => "Only root may use this option." }) - if $param->{migration_network} && $authuser ne 'root@pam'; + if $param->{migration_network} && !$is_root; # test if VM exists my $conf = PVE::QemuConfig->load_config($vmid); @@ -4099,8 +4121,10 @@ __PACKAGE__->register_method({ my $sizestr = extract_param($param, 'size'); my $skiplock = extract_param($param, 'skiplock'); + my $is_root = PVE::Tools::check_for_root("/vms/$vmid"); + raise_param_exc({ skiplock => "Only root may use this option." }) - if $skiplock && $authuser ne 'root@pam'; + if $skiplock && !$is_root; my $storecfg = PVE::Storage::config(); -- 2.30.2