[pve-devel] [PATCH qemu-server v8] api: update: improve tag privilege check

Dominik Csapak d.csapak at proxmox.com
Tue Oct 18 16:02:11 CEST 2022


'normal' tags require 'VM.Config.Options' on '/vm/<vmid>', but not
allowed tags (either limited with 'user-tag-privileges' or 'admin-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.csapak at proxmox.com>
---
 PVE/API2/Qemu.pm | 51 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 3ec31c2..27fbdcc 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -538,7 +538,6 @@ my $generaloptions = {
     'startup' => 1,
     'tdf' => 1,
     'template' => 1,
-    'tags' => 1,
 };
 
 my $vmpoweroptions = {
@@ -608,6 +607,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+$/) {
@@ -1520,6 +1520,29 @@ my $update_vm_api  = sub {
 		}
 	    };
 
+	    my $user_tags;
+	    my $admin_tags;
+
+	    my $check_tag_perms = sub {
+		my ($tag) = @_;
+
+		if ($rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
+		    return;
+		}
+
+		$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Options']);
+
+		if (!defined($user_tags) && !defined($admin_tags)) {
+		    ($user_tags, $admin_tags) = PVE::DataCenterConfig::get_user_admin_tags();
+		}
+
+		if ((defined($user_tags) && !$user_tags->{$tag}) ||
+		    (defined($admin_tags) && $admin_tags->{$tag})
+		) {
+		    $rpcenv->check($authuser, '/', ['Sys.Modify']);
+		}
+	    };
+
 	    foreach my $opt (@delete) {
 		$modified->{$opt} = 1;
 		$conf = PVE::QemuConfig->load_config($vmid); # update/reload
@@ -1578,6 +1601,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);
@@ -1637,6 +1667,25 @@ my $update_vm_api  = sub {
 		    } elsif ($authuser ne 'root at 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






More information about the pve-devel mailing list