From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 562661FF0DA for ; Thu, 19 Nov 2026 20:24:07 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8FA4521AB1; Thu, 19 Nov 2026 20:20:20 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH container v5 25/41] restore: refuse overwriting a container with another guest's backup Date: Thu, 19 Nov 2026 20:18:57 +0100 Message-ID: <20261119191914.2336934-26-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119191914.2336934-1-t.lamprecht@proxmox.com> References: <20261119191914.2336934-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: 1795115960634 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.156 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: 67ILLU32YTM7P6E6SEQM2TAK5YZE54M5 X-Message-ID-Hash: 67ILLU32YTM7P6E6SEQM2TAK5YZE54M5 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: Restoring the backup of another guest over an existing container results in the data of the backup under the identity of the target. Tools that follow that identity would take the result for the original guest. Refuse a restore if the uids differ, unless 'allow-identity-mismatch' is set through the create API or 'pct restore'. The existing 'force' option permits overwriting a container but does not override this check. With the override, a container that already had an identity keeps it. A container that has the uid of the backup as pending uid was left behind by an unfinished restore of that backup, so a retry may proceed. Without 'unique', the retry adopts that uid if no other guest uses it. For standard input, the config of the backup is only known after the old volumes were destroyed and the archive got extracted. So require the override whenever the target already has an identity. A target without identity can still be overwritten without it. Compare the uids before the restore destroys anything. The option is a standard option of pve-guest-common, shared with the restore of VMs. Signed-off-by: Thomas Lamprecht Reviewed-by: Dominik Csapak Tested-by: Dominik Csapak --- no changes since v4 changes v3 -> v4: * applying the network options only once the old container is removed moved to an independent fix before the series, which does so for create as well * 'allow-identity-mismatch' is the standard option of pve-guest-common, with a sentence about standard input added for containers * use prepare_restore of pve-guest-common for the check * tests: the cases for a container left behind by a restore are one subtest * a comment: plainer wording changes v2 -> v3: * apply the network options after the old container is removed, so that removing it does not drop the IPAM mapping of a restored interface with the same MAC address * description of 'allow-identity-mismatch': any backup with the pending uid counts changes v1 -> v2: * use the shared check of pve-guest-common * 'Allow overwriting' instead of 'Allow to overwrite' and a default for 'allow-identity-mismatch' (Dominik) src/PVE/API2/LXC.pm | 30 +++++++++- src/test/lifecycle_tests.pl | 109 ++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 3 deletions(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index 0e4d462..3f7d427 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -121,6 +121,11 @@ my sub assert_not_restore_from_external { die "refusing to restore privileged container backup from external source\n"; } +my $allow_identity_mismatch_option = + get_standard_option('pve-guest-allow-identity-mismatch', { requires => 'restore' }); +$allow_identity_mismatch_option->{description} .= " A restore from standard input over a container" + . " that has a uid always needs this option, as the backup cannot be checked beforehand."; + my $check_storage_access_migrate = sub { my ($rpcenv, $authuser, $storecfg, $storage, $node) = @_; @@ -261,6 +266,7 @@ __PACKAGE__->register_method({ . " the guest uid of the backup.", requires => 'restore', }, + 'allow-identity-mismatch' => $allow_identity_mismatch_option, pool => { optional => 1, type => 'string', @@ -323,6 +329,7 @@ __PACKAGE__->register_method({ my $unprivileged = extract_param($param, 'unprivileged'); my $restore = extract_param($param, 'restore'); my $unique = extract_param($param, 'unique'); + my $allow_identity_mismatch = extract_param($param, 'allow-identity-mismatch'); $param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits}) if defined($param->{cpuunits}); # clamp value depending on cgroup version @@ -533,9 +540,14 @@ __PACKAGE__->register_method({ ($orig_conf, $orig_mp_param, $archive_fh) = PVE::LXC::Create::recover_config($storage_cfg, $archive, $vmid); $backup_uid = (PVE::LXC::Config->get_identity($orig_conf) // {})->{uid}; - # a container with another uid keeps it, which only gets logged + # Restoring over an existing container must not turn it into another + # guest, so this check comes before the restore changes anything. $pending_uid = PVE::GuestIdentity::prepare_restore( - 'PVE::LXC::Config', $vmid, $reservation, $backup_uid, 1, + 'PVE::LXC::Config', + $vmid, + $reservation, + $backup_uid, + $allow_identity_mismatch, ); for my $opt (keys %$orig_conf) { @@ -569,6 +581,12 @@ __PACKAGE__->register_method({ ) { $rpcenv->check($authuser, '/', ['Sys.Modify']); } + } elsif ($reservation->{previous} && !$allow_identity_mismatch) { + # the uid of such a backup is only known once the container got overwritten + my $uid = $reservation->{previous}->{uid}; + die "cannot verify the guest identity of a backup from standard input" + . " before overwriting CT $vmid (uid '$uid') - use" + . " 'allow-identity-mismatch' to overwrite the CT anyway\n"; } } if ($storage_only_mode) { @@ -690,8 +708,14 @@ __PACKAGE__->register_method({ my $restored_uid = (PVE::LXC::Config->get_identity($conf) // {})->{uid}; if ($archive eq '-') { $backup_uid = $restored_uid; + # A mismatch only gets logged here, as a restore from standard input + # over a container with an identity required the override already. $pending_uid = PVE::GuestIdentity::prepare_restore( - 'PVE::LXC::Config', $vmid, $reservation, $backup_uid, 1, + 'PVE::LXC::Config', + $vmid, + $reservation, + $backup_uid, + $allow_identity_mismatch, ); } else { # the merge read the config of the backup again, it must have the uid diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl index d713bf6..226941b 100755 --- a/src/test/lifecycle_tests.pl +++ b/src/test/lifecycle_tests.pl @@ -28,6 +28,7 @@ use PVE::INotify; use PVE::JSONSchema; use PVE::Tools qw(file_get_contents file_set_contents); +use PVE::CLI::pct; use PVE::LXC; use PVE::LXC::Config; use PVE::LXC::Create; @@ -340,6 +341,7 @@ my $backup_raw_config; # the config in the backup, undef for none my $backup_read_error; # set to let reading the config of the backup fail my $backup_fw_config; # the firewall config in the backup, if any my $hook_error; # set to let the setup hooks at the end of a create or restore fail +my $volumes_touched = 0; my $storage_module = Test::MockModule->new('PVE::Storage'); $storage_module->mock( @@ -378,6 +380,7 @@ $lxc_module->mock( create_disks => sub { my ($storecfg, $vmid, $settings, $conf, $pending, $allocated) = @_; my $vollist = $allocated // []; + $volumes_touched++; for my $ms (sort keys %$settings) { my $volid = "local:$vmid/vm-$vmid-disk-$ms.raw"; $conf->{$ms} = "$volid,size=4G"; @@ -388,6 +391,7 @@ $lxc_module->mock( destroy_disks => sub { return [] }, destroy_lxc_container => sub { my ($storecfg, $vmid, $conf, $replacement_conf) = @_; + $volumes_touched++; if (defined($replacement_conf)) { PVE::LXC::Config->write_config($vmid, $replacement_conf); } else { @@ -529,6 +533,7 @@ my sub reset_state { $backup_fw_config = undef; $now_ms = 1_700_000_000_000; $domain_lock_error = undef; + $volumes_touched = 0; } # API call helpers @@ -2036,6 +2041,9 @@ subtest 'using the container left behind drops its pending uid' => sub { . " restore - a retry now needs 'allow-identity-mismatch'"; like($task_log, qr/^WARN: \Q$dropped\E$/m, 'dropped'); is(pending_uid_of(200), undef, 'pending uid is gone'); + + $err = restore_ct(200, { force => 1 }); + like($err, qr/backup belongs to another guest/, 'container stays a guest of its own'); }; subtest 'restore with unique keeps a pending uid, but never adopts it' => sub { @@ -2219,6 +2227,107 @@ subtest 'restore fails if the backup changed after its uid was read' => sub { } }; +subtest 'restore over a container of another guest needs an override' => sub { + my $existing_raw = existing_config($EXISTING_META); + my $existing_conf = PVE::LXC::Config::parse_pct_config('/lxc/200.conf', $existing_raw); + delete $existing_conf->{digest}; + + my $setup = sub { + reset_state(); + $backup_raw_config = backup_config($BACKUP_META); + write_raw_config(200, $existing_raw); + }; + my $refused = sub($name, $param, $expected_err) { + $setup->(); + my $err = restore_ct(200, { force => 1, $param->%* }); + is($err, "unable to restore CT 200 - $expected_err\n", "$name: restore is refused"); + is($volumes_touched, 0, "$name: volumes are not touched"); + my $conf = PVE::LXC::Config->load_config(200); + delete $conf->{digest}; + is_deeply($conf, $existing_conf, "$name: config is unchanged"); + }; + + my $mismatch_err = + "backup belongs to another guest: its uid 'backupbackupbackupba' differs from uid" + . " 'existingexistingexis' of the existing CT 200 - use 'allow-identity-mismatch' to" + . " overwrite the CT anyway"; + my $stdin_err = + "cannot verify the guest identity of a backup from standard input before overwriting" + . " CT 200 (uid 'existingexistingexis') - use 'allow-identity-mismatch' to overwrite the" + . " CT anyway"; + my $ignored = "guest identity: ignoring backup uid backupbackupbackupba, CT 200 keeps uid" + . " existingexistingexis"; + my $stdin = { ostemplate => '-', rootfs => 'local:4' }; + $refused->('different uid', {}, $mismatch_err); + $refused->('different uid, unique', { unique => 1 }, $mismatch_err); + $refused->('from STDIN', $stdin, $stdin_err); + + for my $param ({}, $stdin) { + my $name = $param->{ostemplate} ? 'from STDIN' : 'from file'; + $setup->(); + my $err = restore_ct(200, { force => 1, 'allow-identity-mismatch' => 1, $param->%* }); + is($err, '', "$name: restore with override succeeds") or diag($task_log); + is( + PVE::LXC::Config->load_config(200)->{hostname}, + 'backup', + "$name: backup is restored", + ); + is_deeply( + identity_of(200), + { uid => 'existingexistingexis', rtime => 1500000000000 }, + "$name: identity of the container is kept", + ); + like($task_log, qr/^\Q$ignored\E$/m, "$name: override is logged"); + like( + $task_log, + qr/^guest identity: kept uid existingexistingexis$/m, + "$name: result is logged", + ); + } + + reset_state(); + $backup_raw_config = backup_config($BACKUP_META); + write_raw_config(200, existing_config('ctime=1500000000')); + my $err = restore_ct(200, { force => 1, $stdin->%* }); + is($err, '', 'restore from STDIN over a container without identity succeeds'); + is(identity_of(200)->{uid}, 'backupbackupbackupba', 'uid of the backup is adopted'); + + $err = create_ct(201, { 'allow-identity-mismatch' => 1 }); + like($err, qr/allow-identity-mismatch/, 'override is only allowed for restores'); + + my ($class, $method, undef, $fixed) = $PVE::CLI::pct::cmddef->{restore}->@*; + is("$class->$method", 'PVE::API2::LXC->create_vm', 'pct restore uses the create API call'); + ok($fixed->{restore}, 'pct restore sets restore, so it accepts the override'); +}; + +subtest 'restore over the container left behind by a restore of a backup' => sub { + failed_restore_in_place(); + $backup_raw_config = backup_config($BACKUP_META =~ s/uid=.*/uid=anotheranotheranothe/r); + my $err = restore_ct(200, { force => 1 }); + like( + $err, + qr/backup belongs to another guest: its uid .anotheranotheranothe. differs/, + 'another backup is refused', + ); + is(pending_uid_of(200), 'backupbackupbackupba', 'pending uid is unchanged'); + + $backup_raw_config = backup_config($BACKUP_META); + my $param = { force => 1, ostemplate => '-', rootfs => 'local:4' }; + $err = restore_ct(200, $param); + like( + $err, + qr/cannot verify the guest identity of a backup from standard input/, + 'the same backup from STDIN is refused', + ); + + $err = restore_ct(200, { $param->%*, 'allow-identity-mismatch' => 1 }); + is($err, '', 'from STDIN with override succeeds') or diag($task_log); + is( + identity_of(200)->{uid}, 'backupbackupbackupba', 'its uid is the pending uid: adopted', + ); + is(pending_uid_of(200), undef, 'pending uid is removed'); +}; + remove_tree($WORKDIR); done_testing(); -- 2.47.3