From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 9980B1FF0DE for ; Thu, 19 Nov 2026 11:37:40 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 126D12180C; Thu, 19 Nov 2026 11:36:50 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH container v4 20/41] restore: decide by the reservation whether the container is new Date: Thu, 19 Nov 2026 11:34:06 +0100 Message-ID: <20261119103551.1666964-21-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119103551.1666964-1-t.lamprecht@proxmox.com> References: <20261119103551.1666964-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795084556461 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.161 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: QIOKAJIDB6I2W6BRQENFKNLDVQTJKZ2P X-Message-ID-Hash: QIOKAJIDB6I2W6BRQENFKNLDVQTJKZ2P X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: A container can get removed after the checks of the API call, but before the VMID is reserved. The reservation then creates a new config. The restore still treated the container as an existing one, because it decided that with a check before the reservation. This had two effects. A restore that failed early only removed the lock and left the new, empty config behind, which kept the VMID taken. And the 'Sys.Modify' check for creating a privileged container was skipped. Use what the reservation found instead. A failed restore then removes the config that it created, and the 'Sys.Modify' check also applies to a privileged container that gets created this way. Signed-off-by: Thomas Lamprecht --- changes v3 -> v4: * new in v4: split out of 'restore: take over the guest uid from the backup where possible', which did this along the way, with a test of its own src/PVE/API2/LXC.pm | 8 ++++---- src/test/lifecycle_tests.pl | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index a02fb2c..9997ca8 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -493,7 +493,7 @@ __PACKAGE__->register_method({ # Only overwrite a container that the checks above were done for. The permission to only # overwrite a container does not cover creating it anew, should it be gone by now. - eval { + my $reservation = eval { PVE::LXC::Config->create_and_lock_config( $vmid, $overwrite_only ? 'require' : $force && $same_container_exists, @@ -501,7 +501,7 @@ __PACKAGE__->register_method({ }; die "$emsg $@" if $@; - my $destroy_config_on_error = !$same_container_exists; + my $destroy_config_on_error = !$reservation->{existed}; # set once the worker cleans up after a failure on its own my $started = 0; @@ -553,7 +553,7 @@ __PACKAGE__->register_method({ # implicit privileged change, or creating a new privileged container is checked here if ( - (!$same_container_exists || $old_conf->{unprivileged}) + (!$reservation->{existed} || $old_conf->{unprivileged}) && !$conf->{unprivileged} ) { $rpcenv->check($authuser, '/', ['Sys.Modify']); @@ -718,7 +718,7 @@ __PACKAGE__->register_method({ # container and nothing of it is left, its firewall config gets removed like # its config. In all other cases, the previous firewall config is put back. my $firewall_config = - $same_container_exists && !@left ? undef : $old_firewall_config; + $reservation->{existed} && !@left ? undef : $old_firewall_config; eval { PVE::GuestHelpers::set_firewall_config_raw($vmid, $firewall_config) }; if (my $firewall_err = $@) { warn "unable to reset the firewall config - $firewall_err"; diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl index b1d185a..0f5aad2 100755 --- a/src/test/lifecycle_tests.pl +++ b/src/test/lifecycle_tests.pl @@ -1142,6 +1142,33 @@ subtest 'migration tunnel waits for a volume transfer before it cleans up' => su ok(defined(read_raw_config(300)), 'reserved config is kept while a transfer still runs'); }; +subtest 'restore treats a container that got removed before the reservation as new' => sub { + my $removed_in_between = sub($raw) { + reset_state(); + write_raw_config(200, $raw); + $in_between = sub { PVE::LXC::Config->destroy_config(200) }; + }; + + $removed_in_between->(existing_config()); + $backup_read_error = "unable to read backup\n"; + like(restore_ct(200, { force => 1 }), qr/unable to read backup/, 'early failure: fails'); + ok(!defined(read_raw_config(200)), 'early failure: the VMID is released'); + + # overwriting a privileged container with a privileged one needs no 'Sys.Modify', creating + # one does + my $privileged = sub($raw) { return $raw =~ s/^unprivileged: 1\n//mr }; + $removed_in_between->($privileged->(existing_config())); + $backup_raw_config = $privileged->(backup_config()); + my $deny = { '/' => { 'Sys.Modify' => 1 } }; + my $err = as_user('user@pve', $deny, sub { restore_ct(200, { force => 1 }) }); + like( + $err, + qr/permission check failed \(\/, Sys\.Modify\)/, + 'privileged: restore is refused', + ); + ok(!defined(read_raw_config(200)), 'privileged: the VMID is released'); +}; + remove_tree($WORKDIR); done_testing(); -- 2.47.3