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 C656A9F67F for ; Fri, 9 Jun 2023 09:51:48 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A91122A860 for ; Fri, 9 Jun 2023 09:51:48 +0200 (CEST) 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 for ; Fri, 9 Jun 2023 09:51:48 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B9D3B428ED for ; Fri, 9 Jun 2023 09:51:47 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pve-devel@lists.proxmox.com Date: Fri, 9 Jun 2023 09:51:41 +0200 Message-Id: <20230609075141.667259-1-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.072 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH container] network permissions: implement checks 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: Fri, 09 Jun 2023 07:51:48 -0000 when creating a new container when restoring a backup when cloning a container and obviously, when changing the nics of an existing container Signed-off-by: Fabian Grünbichler --- src/PVE/API2/LXC.pm | 12 +++++++++++- src/PVE/LXC.pm | 17 ++++++++++++++++- src/PVE/LXC/Create.pm | 5 +++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index 2d67997..ed68390 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -388,6 +388,14 @@ __PACKAGE__->register_method({ print "recovering backed-up configuration from '$archive'\n"; ($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($storage_cfg, $archive, $vmid); + for my $opt (keys %$orig_conf) { + # early check before disks are created + # the "real" check is in later on when actually merging the configs + if ($opt =~ /^net\d+$/ && !$param->{opt}) { + PVE::LXC::check_bridge_access($rpcenv, $authuser, $orig_conf->{$opt}); + } + } + $was_template = delete $orig_conf->{template}; # When we're root call 'restore_configuration' with restricted=0, @@ -1532,7 +1540,7 @@ __PACKAGE__->register_method({ description => "You need 'VM.Clone' permissions on /vms/{vmid}, " . "and 'VM.Allocate' permissions " . "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . - "'Datastore.AllocateSpace' on any used storage.", + "'Datastore.AllocateSpace' on any used storage, and 'SDN.Use' on any bridge.", check => [ 'and', ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], @@ -1724,6 +1732,8 @@ __PACKAGE__->register_method({ my $net = PVE::LXC::Config->parse_lxc_network($value); $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net); + + PVE::LXC::check_bridge_access($rpcenv, $authuser, $newconf->{$opt}); } else { # copy everything else $newconf->{$opt} = $value; diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm index 92c1b60..9642f2e 100644 --- a/src/PVE/LXC.pm +++ b/src/PVE/LXC.pm @@ -18,7 +18,7 @@ use PVE::AccessControl; use PVE::CGroup; use PVE::CpuSet; use PVE::Exception qw(raise_perm_exc); -use PVE::GuestHelpers qw(safe_string_ne safe_num_ne safe_boolean_ne); +use PVE::GuestHelpers qw(check_vnet_access safe_string_ne safe_num_ne safe_boolean_ne); use PVE::INotify; use PVE::JSONSchema qw(get_standard_option); use PVE::Network; @@ -1317,6 +1317,7 @@ sub check_ct_modify_config_perm { } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' || $opt eq 'searchdomain' || $opt eq 'hostname') { $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); + PVE::LXC::check_bridge_access($rpcenv, $authuser, $newconf->{$opt}); } elsif ($opt eq 'features') { raise_perm_exc("changing feature flags for privileged container is only allowed for root\@pam") if !$unprivileged; @@ -1383,6 +1384,20 @@ sub check_ct_modify_config_perm { return 1; } +sub check_bridge_access { + my ($rpcenv, $authuser, $raw) = @_; + + return 1 if $authuser eq 'root@pam'; + + my $net = PVE::LXC::Config->parse_lxc_network($raw); + my $bridge = $net->{bridge}; + my $tag = $net->{tag}; + my $trunks = $net->{trunks}; + check_vnet_access($rpcenv, $authuser, $bridge, $tag, $trunks); + + return 1; +}; + sub umount_all { my ($vmid, $storage_cfg, $conf, $noerr) = @_; diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm index b2e3d00..981f92d 100644 --- a/src/PVE/LXC/Create.pm +++ b/src/PVE/LXC/Create.pm @@ -325,6 +325,7 @@ sub sanitize_and_merge_config { my ($conf, $oldconf, $restricted, $unique) = @_; my $rpcenv = PVE::RPCEnvironment::get(); + my $authuser = $rpcenv->get_user(); foreach my $key (keys %$oldconf) { next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent'; @@ -354,6 +355,10 @@ sub sanitize_and_merge_config { next; } + if ($key =~ /^net\d+$/ && !defined($conf->{$key})) { + PVE::LXC::check_bridge_access($rpcenv, $authuser, $oldconf->{$key}); + } + if ($unique && $key =~ /^net\d+$/) { my $net = PVE::LXC::Config->parse_lxc_network($oldconf->{$key}); my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); -- 2.39.2