'normal' tags require 'VM.Config.Options' on '/vm/<vmid>', but not
allowed tags (either limited with 'user-tag-access' or 'privileged-tags'
in the datacenter config) require 'Sys.Modify' on '/'

this patch implements the proper checks on adding/editing/deleting
these permissions

Signed-off-by: Dominik Csapak <d.csa...@proxmox.com>
---
changes from v8:
* adapt to 'get_allowed_tags'
* cache privilege checks (so we don't have to do it when many tags change)
 PVE/API2/Qemu.pm | 53 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 30348e6..269451c 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -539,7 +539,6 @@ my $generaloptions = {
     'startup' => 1,
     'tdf' => 1,
     'template' => 1,
-    'tags' => 1,
 };
 
 my $vmpoweroptions = {
@@ -609,6 +608,7 @@ my $check_vm_modify_config_perm = sub {
        next if PVE::QemuServer::is_valid_drivename($opt);
        next if $opt eq 'cdrom';
        next if $opt =~ m/^(?:unused|serial|usb)\d+$/;
+       next if $opt eq 'tags';
 
 
        if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) {
@@ -1631,6 +1631,31 @@ my $update_vm_api  = sub {
                }
            };
 
+           my $allowed_tags;
+           my $privileged_tags;
+           my $freeform;
+           my $privileged_user = $rpcenv->check($authuser, '/', 
['Sys.Modify'], 1);
+           my $has_config_options =
+               $rpcenv->check_vm_perm($authuser, $vmid, undef, 
['VM.Config.Options'], 1);
+
+           my $check_tag_perms = sub {
+               my ($tag) = @_;
+
+               return if $privileged_user;
+
+               $rpcenv->check_vm_perm($authuser, $vmid, undef, 
['VM.Config.Options'])
+                   if !$has_config_options;
+
+               if (!defined($allowed_tags) && !defined($privileged_tags) && 
!defined($freeform)) {
+                   ($allowed_tags, $privileged_tags, $freeform)
+                       = PVE::DataCenterConfig::get_allowed_tags($rpcenv, 
$authuser);
+               }
+
+               if ((!$allowed_tags->{$tag} && !$freeform) || 
$privileged_tags->{$tag}) {
+                   die "'Sys.Modify' required on '/' for modifying tag $tag\n" 
if !$privileged_user;
+               }
+           };
+
            foreach my $opt (@delete) {
                $modified->{$opt} = 1;
                $conf = PVE::QemuConfig->load_config($vmid); # update/reload
@@ -1689,6 +1714,13 @@ my $update_vm_api  = sub {
                    }
                    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
+               } elsif ($opt eq 'tags') {
+                   foreach my $tag (PVE::Tools::split_list($val)) {
+                       check_tag_perms->($tag);
+                   }
+
+                   delete $conf->{$opt};
+                   PVE::QemuConfig->write_config($vmid, $conf);
                } else {
                    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
@@ -1748,6 +1780,25 @@ my $update_vm_api  = sub {
                    } elsif ($authuser ne 'root@pam') {
                        die "only root can modify '$opt' config for real 
devices\n";
                    }
+                   $conf->{pending}->{$opt} = $param->{$opt};
+               } elsif ($opt eq 'tags') {
+                   my $old_tags = {};
+                   my $new_tags = {};
+
+                   map { $old_tags->{$_} += 1 } 
PVE::Tools::split_list($conf->{$opt} // '');
+                   map { $new_tags->{$_} += 1 } 
PVE::Tools::split_list($param->{$opt});
+
+                   my $check_tags = sub {
+                       my ($a, $b) = @_;
+                       foreach my $tag (keys %$a) {
+                           next if ($b->{$tag} // 0) == ($a->{$tag} // 0);
+                           $check_tag_perms->($tag);
+                       }
+                   };
+
+                   $check_tags->($old_tags, $new_tags);
+                   $check_tags->($new_tags, $old_tags);
+
                    $conf->{pending}->{$opt} = $param->{$opt};
                } else {
                    $conf->{pending}->{$opt} = $param->{$opt};
-- 
2.30.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to