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 AC7D26043E for ; Mon, 10 Jan 2022 14:45:51 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9FFB919C1 for ; Mon, 10 Jan 2022 14:45:21 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 3F3BB19AF for ; Mon, 10 Jan 2022 14:45:19 +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 177DF45FBD for ; Mon, 10 Jan 2022 14:45:19 +0100 (CET) Date: Mon, 10 Jan 2022 14:45:08 +0100 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox VE development discussion References: <20220105152215.1307583-1-o.bektas@proxmox.com> <20220105152215.1307583-5-o.bektas@proxmox.com> In-Reply-To: <20220105152215.1307583-5-o.bektas@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.15.0 (https://github.com/astroidmail/astroid) Message-Id: <1641820993.tq6zsgq6uk.astroid@nora.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.221 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] [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: Mon, 10 Jan 2022 13:45:51 -0000 this patch mixes a lot of "check for 'root@pam' as short cut" with=20 "check for 'root@pam' for stuff limited to 'root@pam'". as per the=20 discussion in the cover letter - if we make Sys.Root imply all other=20 privileges like 'root@pam' currently does, then all the short cut=20 instances can just remain as is.. On January 5, 2022 4:22 pm, Oguz Bektas wrote: > to allow API users with the 'Sys.Root' permission to call these > endpoints. >=20 > Signed-off-by: Oguz Bektas > --- > PVE/API2/Qemu.pm | 74 ++++++++++++++++++++++++++++++++---------------- > 1 file changed, 49 insertions(+), 25 deletions(-) >=20 > 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 =3D { > my $check_vm_create_serial_perm =3D sub { > my ($rpcenv, $authuser, $vmid, $pool, $param) =3D @_; > =20 > - return 1 if $authuser eq 'root@pam'; > + return 1 if PVE::Tools::check_for_root("/vms/$vmid"); > =20 > foreach my $opt (keys %{$param}) { > next if $opt !~ m/^serial\d+$/; > @@ -370,7 +370,7 @@ my $check_vm_create_serial_perm =3D sub { > my $check_vm_create_usb_perm =3D sub { > my ($rpcenv, $authuser, $vmid, $pool, $param) =3D @_; > =20 > - return 1 if $authuser eq 'root@pam'; > + return 1 if PVE::Tools::check_for_root("/vms/$vmid"); > =20 > foreach my $opt (keys %{$param}) { > next if $opt !~ m/^usb\d+$/; > @@ -388,7 +388,7 @@ my $check_vm_create_usb_perm =3D sub { > my $check_vm_modify_config_perm =3D sub { > my ($rpcenv, $authuser, $vmid, $pool, $key_list) =3D @_; > =20 > - return 1 if $authuser eq 'root@pam'; > + return 1 if PVE::Tools::check_for_root("/vms/$vmid"); > =20 > 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 =3D sub { > =20 > my $background_delay =3D extract_param($param, 'background_delay'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > if (defined(my $cipassword =3D $param->{cipassword})) { > # Same logic as in cloud-init (but with the regex fixed...) > $param->{cipassword} =3D PVE::Tools::encrypt_pw($cipassword) > @@ -1119,7 +1121,7 @@ my $update_vm_api =3D sub { > =20 > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > my $delete_str =3D extract_param($param, 'delete'); > =20 > @@ -1340,7 +1342,7 @@ my $update_vm_api =3D sub { > } elsif ($opt =3D~ 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 =3D sub { > } elsif ($opt =3D~ m/^usb\d+$/) { > if ($val =3D~ 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 =3D sub { > } elsif ($opt =3D~ m/^serial\d+/) { > if ((!defined($conf->{$opt}) || $conf->{$opt} eq 'socket') && $par= am->{$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} =3D $param->{$opt}; > } elsif ($opt =3D~ m/^usb\d+/) { > if ((!defined($conf->{$opt}) || $conf->{$opt} =3D~ m/spice/) && $p= aram->{$opt} =3D~ 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} =3D $param->{$opt}; > @@ -1644,9 +1646,11 @@ __PACKAGE__->register_method({ > my $authuser =3D $rpcenv->get_user(); > my $vmid =3D $param->{vmid}; > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $skiplock =3D $param->{skiplock}; > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > my $early_checks =3D sub { > # test if VM exists > @@ -2291,10 +2295,12 @@ __PACKAGE__->register_method({ > my $machine =3D extract_param($param, 'machine'); > my $force_cpu =3D extract_param($param, 'force-cpu'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $get_root_param =3D sub { > my $value =3D extract_param($param, $_[0]); > raise_param_exc({ "$_[0]" =3D> "Only root may use this option." }) > - if $value && $authuser ne 'root@pam'; > + if $value && !$is_root; > return $value; > }; > =20 > @@ -2436,17 +2442,19 @@ __PACKAGE__->register_method({ > my $node =3D extract_param($param, 'node'); > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > my $keepActive =3D extract_param($param, 'keepActive'); > raise_param_exc({ keepActive =3D> "Only root may use this option." }) > - if $keepActive && $authuser ne 'root@pam'; > + if $keepActive && !$is_root; > =20 > my $migratedfrom =3D extract_param($param, 'migratedfrom'); > raise_param_exc({ migratedfrom =3D> "Only root may use this option." }) > - if $migratedfrom && $authuser ne 'root@pam'; > + if $migratedfrom && !$is_root; > =20 > =20 > my $storecfg =3D PVE::Storage::config(); > @@ -2513,9 +2521,11 @@ __PACKAGE__->register_method({ > =20 > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); > =20 > @@ -2580,13 +2590,15 @@ __PACKAGE__->register_method({ > my $node =3D extract_param($param, 'node'); > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > my $keepActive =3D extract_param($param, 'keepActive'); > raise_param_exc({ keepActive =3D> "Only root may use this option." }) > - if $keepActive && $authuser ne 'root@pam'; > + if $keepActive && !$is_root; > =20 > my $storecfg =3D PVE::Storage::config(); > =20 > @@ -2735,13 +2747,15 @@ __PACKAGE__->register_method({ > my $node =3D extract_param($param, 'node'); > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $todisk =3D extract_param($param, 'todisk') // 0; > =20 > my $statestorage =3D extract_param($param, 'statestorage'); > =20 > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); > =20 > @@ -2811,13 +2825,15 @@ __PACKAGE__->register_method({ > =20 > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > my $nocheck =3D extract_param($param, 'nocheck'); > raise_param_exc({ nocheck =3D> "Only root may use this option." }) > - if $nocheck && $authuser ne 'root@pam'; > + if $nocheck && !$is_root; > =20 > my $to_disk_suspended; > eval { > @@ -2883,9 +2899,11 @@ __PACKAGE__->register_method({ > =20 > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $skiplock =3D extract_param($param, 'skiplock'); > raise_param_exc({ skiplock =3D> "Only root may use this option." }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); > =20 > @@ -3392,6 +3410,8 @@ __PACKAGE__->register_method({ > =20 > my $storecfg =3D PVE::Storage::config(); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > my $move_updatefn =3D sub { > my $conf =3D PVE::QemuConfig->load_config($vmid); > PVE::QemuConfig->check_lock($conf); > @@ -3671,7 +3691,7 @@ __PACKAGE__->register_method({ > raise_param_exc({ 'target-vmid' =3D> $msg, 'storage' =3D> $msg }); > } elsif ($target_vmid) { > $rpcenv->check_vm_perm($authuser, $target_vmid, undef, ['VM.Config.= Disk']) > - if $authuser ne 'root@pam'; > + if !$is_root; > =20 > raise_param_exc({ 'target-vmid' =3D> "must be different than source= VMID to reassign disk" }) > if $vmid eq $target_vmid; > @@ -3910,15 +3930,17 @@ __PACKAGE__->register_method({ > =20 > my $vmid =3D extract_param($param, 'vmid'); > =20 > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > raise_param_exc({ force =3D> "Only root may use this option." }) > - if $param->{force} && $authuser ne 'root@pam'; > + if $param->{force} && !$is_root; > =20 > raise_param_exc({ migration_type =3D> "Only root may use this option." = }) > - if $param->{migration_type} && $authuser ne 'root@pam'; > + if $param->{migration_type} && !$is_root; > =20 > # allow root only until better network permissions are available > raise_param_exc({ migration_network =3D> "Only root may use this option= ." }) > - if $param->{migration_network} && $authuser ne 'root@pam'; > + if $param->{migration_network} && !$is_root; > =20 > # test if VM exists > my $conf =3D PVE::QemuConfig->load_config($vmid); > @@ -4099,8 +4121,10 @@ __PACKAGE__->register_method({ > my $sizestr =3D extract_param($param, 'size'); > =20 > my $skiplock =3D extract_param($param, 'skiplock'); > + my $is_root =3D PVE::Tools::check_for_root("/vms/$vmid"); > + > raise_param_exc({ skiplock =3D> "Only root may use this option."= }) > - if $skiplock && $authuser ne 'root@pam'; > + if $skiplock && !$is_root; > =20 > my $storecfg =3D PVE::Storage::config(); > =20 > --=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