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 EE4DD1FF0C0 for ; Wed, 18 Nov 2026 05:43:11 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A96A621A9B; Wed, 18 Nov 2026 05:39:35 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH container v3 25/38] restore: refuse overwriting a container with another guest's backup Date: Wed, 18 Nov 2026 05:37:37 +0100 Message-ID: <20261118043817.1659212-26-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118043817.1659212-1-t.lamprecht@proxmox.com> References: <20261118043817.1659212-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: 1794976703135 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.186 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: A7JZPZBLHF2SAPMI64SGBEZVGEXVXNGW X-Message-ID-Hash: A7JZPZBLHF2SAPMI64SGBEZVGEXVXNGW 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 takes that uid over 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. Apply the network options only once the old container is removed. Applying them can allocate addresses in the IPAM of SDN for their MAC addresses. The cleanup after an earlier failure would remove such a mapping even if the existing container still uses that MAC address, and removing the old container removes the mappings of its MAC addresses, also of one that an option keeps. Validate and queue the options before the worker starts, without changing the IPAM state. Signed-off-by: Thomas Lamprecht --- 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 | 56 +++++++++- src/test/lifecycle_tests.pl | 198 ++++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+), 1 deletion(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index d231d24..a5e45c2 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -223,6 +223,18 @@ __PACKAGE__->register_method({ . " the guest uid of the backup.", requires => 'restore', }, + 'allow-identity-mismatch' => { + optional => 1, + type => 'boolean', + default => 0, + description => "Allow overwriting an existing container with a backup of" + . " another guest, as detected by a different 'uid' in 'meta'. A restore from" + . " standard input over a container that has a uid always needs this option," + . " as the backup cannot be checked beforehand. Unless the container is left" + . " over from an unfinished restore of a backup with the same uid, it keeps its" + . " own uid.", + requires => 'restore', + }, pool => { optional => 1, type => 'string', @@ -285,6 +297,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 @@ -446,7 +459,13 @@ __PACKAGE__->register_method({ # check/activate default storage &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs}); - PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param); + if ($restore) { + # applying network options can allocate addresses in the IPAM of SDN, so only check and + # queue the options here, and apply them once the restore may overwrite the container + PVE::LXC::Config->queue_pct_config_changes($vmid, $conf, $no_disk_param, [], []); + } else { + PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param); + } $conf->{unprivileged} = 1 if $unprivileged; $conf->{meta} = PVE::LXC::Config->new_meta_info_string() if !$restore; @@ -463,6 +482,32 @@ __PACKAGE__->register_method({ # the cleanup of a failure removes the firewall config, also one that existed before my $vmfw_conf_existed = !$reservation->{existed} && __vmfw_conf_exists($vmid); + # Restoring over an existing container must not turn it into another guest. The identity of + # a backup from STDIN is only known once the container got overwritten. A container with the + # uid of the backup as pending uid is a leftover of an earlier restore of it. + my $assert_identity_matches = sub { + my ($backup_identity, $from_stdin) = @_; + + my $target = $reservation->{previous} or return; + + if ($from_stdin) { + if (!$allow_identity_mismatch) { + die "cannot verify the guest identity of a backup from standard input" + . " before overwriting CT $vmid (uid '$target->{uid}') - use" + . " 'allow-identity-mismatch' to overwrite the CT anyway\n"; + } + return; + } + + PVE::GuestIdentity::assert_backup_matches_target( + 'PVE::LXC::Config', + $vmid, + $reservation, + $backup_identity ? $backup_identity->{uid} : undef, + $allow_identity_mismatch, + ); + }; + my $code = sub { my $old_conf = PVE::LXC::Config->load_config($vmid); # the container may have changed since the protection check above @@ -482,6 +527,7 @@ __PACKAGE__->register_method({ ($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($storage_cfg, $archive, $vmid); $backup_identity = PVE::LXC::Config->get_identity($orig_conf); + $assert_identity_matches->($backup_identity); $pending_uid = restore_pending_uid($reservation, $backup_identity); PVE::LXC::Config->record_pending_uid($vmid, $pending_uid) if defined($pending_uid); @@ -517,6 +563,8 @@ __PACKAGE__->register_method({ ) { $rpcenv->check($authuser, '/', ['Sys.Modify']); } + } else { + $assert_identity_matches->(undef, 1); } } if ($storage_only_mode) { @@ -592,6 +640,10 @@ __PACKAGE__->register_method({ # up until here the old container is intact, besides its lock and pending uid $destroy_config_on_error = 1; + # registers addresses in the IPAM of SDN, so only once the old container is gone + PVE::LXC::Config->vmconfig_apply_pending($vmid, $conf, $storage_cfg) + if $restore; + eval { my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1); $bwlimit = PVE::Storage::get_bandwidth_limit( @@ -633,6 +685,8 @@ __PACKAGE__->register_method({ # the API parameters cannot carry 'meta', so it comes from the backup if ($archive eq '-') { $backup_identity = PVE::LXC::Config->get_identity($conf); + # only logs a mismatch, which needed the override already + $assert_identity_matches->($backup_identity); $pending_uid = restore_pending_uid($reservation, $backup_identity); } else { # the extraction opened the archive again, it must be the checked one diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl index 93d69b1..7606d3e 100755 --- a/src/test/lifecycle_tests.pl +++ b/src/test/lifecycle_tests.pl @@ -29,6 +29,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; @@ -298,6 +299,7 @@ my sub run_task($code) { my $backup_raw_config; my $backup_read_error; +my $volumes_touched = 0; my $storage_module = Test::MockModule->new('PVE::Storage'); $storage_module->mock( @@ -325,6 +327,7 @@ $lxc_module->mock( check_running => sub { return 0 }, create_disks => sub { my ($storecfg, $vmid, $settings, $conf) = @_; + $volumes_touched++; my $vollist = []; for my $ms (sort keys %$settings) { my $volid = "local:$vmid/vm-$vmid-disk-$ms.raw"; @@ -336,6 +339,7 @@ $lxc_module->mock( destroy_disks => sub { }, 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 { @@ -1438,6 +1442,17 @@ subtest 'restore only changes the container that the checks were done for' => su is_deeply($current_conf->(), $conf_of->($other_raw), 'early failure: config is unchanged'); ok(!$vmfw_removed_reserved->{200}, 'early failure: firewall config is kept'); + $cluster_module->mock(check_vmid_unused => sub { return 1 }); + reset_configs(); + $volumes_touched = 0; + $in_between = sub { write_raw_config(200, $other_raw) }; + $err = restore_ct(200); + $cluster_module->mock(check_vmid_unused => $mocked_unused); + isnt($err, '', 'created in between, other uid: restore fails'); + is_deeply($current_conf->(), $conf_of->($other_raw), 'other uid: config is unchanged'); + ok(!$vmfw_removed_reserved->{200}, 'other uid: firewall config is kept'); + is($volumes_touched, 0, 'other uid: volumes are not touched'); + reset_configs(); write_raw_config(200, $existing_raw); $in_between = sub { PVE::LXC::Config->destroy_config(200) }; @@ -1529,6 +1544,19 @@ subtest 'restore fails if the backup changed after its uid was read' => sub { like($err, qr/^\Q$changed\E$/, 'free VMID: restore fails'); ok(!defined(read_raw_config(200)), 'free VMID: nothing is left behind'); + # the checked backup has the uid of the container, so the restore needs no override + reset_configs(); + write_raw_config( + 200, + existing_config('ctime=1500000000,rtime=1500000000000,uid=backupbackupbackupba'), + ); + $err = restore_ct(200, { force => 1 }); + like($err, qr/^\Q$changed\E$/, 'in place: restore fails'); + ok( + !defined(read_raw_config(200)), + 'in place: the other backup is not left behind with the uid of the container', + ); + $create_module->mock(restore_archive => $mocked_restore_archive); }; @@ -1575,6 +1603,44 @@ 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'); + + $backup_raw_config =~ s/^arch: amd64$/arch: amd64\nunprivileged: 1/m; + $err = restore_ct(200, { force => 1 }); + like($err, qr/backup belongs to another guest/, 'container stays a guest of its own'); +}; + +subtest 'retry over the container left behind by a restore of another backup' => sub { + failed_restore_in_place(); + + $backup_raw_config = + backup_config('ctime=1400000000,rtime=1400000000000,uid=anotheranotheranothe'); + my $err = restore_ct(200, { force => 1 }); + like( + $err, + qr/backup belongs to another guest: its uid .anotheranotheranothe. differs/, + 'refused', + ); + is(pending_uid_of(200), 'backupbackupbackupba', 'pending uid is unchanged'); +}; + +subtest 'restore from STDIN over the container left behind' => sub { + failed_restore_in_place(); + my $leftover = identity_of(200)->{uid}; + + $backup_raw_config = + backup_config('ctime=1400000000,rtime=1400000000000,uid=backupbackupbackupba'); + my $param = { force => 1, ostemplate => '-', rootfs => 'local:4' }; + my $err = restore_ct(200, $param); + like($err, qr/cannot verify the guest identity of a backup from standard input/, 'refused'); + + $err = restore_ct(200, { $param->%*, 'allow-identity-mismatch' => 1 }); + is($err, '', 'restore with override succeeds') or diag($task_log); + is( + identity_of(200)->{uid}, + 'backupbackupbackupba', + 'pending uid matches the backup: taken over', + ); + is(pending_uid_of(200), undef, 'pending uid is removed'); }; subtest 'restore with unique keeps a pending uid, but never takes it over' => sub { @@ -1692,6 +1758,138 @@ subtest 'every write of a restore keeps the pending uid' => sub { $storage_module->unmock('volume_has_feature'); }; +subtest 'restore over a container of another guest needs an override' => sub { + my $existing_meta = 'ctime=1500000000,rtime=1500000000000,uid=existingexistingexis'; + 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}; + $backup_raw_config = + backup_config('ctime=1400000000,rtime=1400000000000,uid=backupbackupbackupba'); + + # network options can change the IPAM of SDN, with the same MAC address as the existing one + my $net = 'name=eth0,bridge=vnet0,hwaddr=BC:24:11:00:00:01,ip=dhcp'; + $existing_raw .= "net0: $net,type=veth\n"; + $existing_conf = PVE::LXC::Config::parse_pct_config('/lxc/200.conf', $existing_raw); + delete $existing_conf->{digest}; + my @sdn_calls; + my $ipam = {}; # bridge => MAC => hostname + my $vnets_module = Test::MockModule->new('PVE::Network::SDN::Vnets'); + $vnets_module->mock( + add_next_free_cidr => sub { + my ($bridge, $hostname, $mac) = @_; + push @sdn_calls, 'add_next_free_cidr'; + $ipam->{$bridge}->{$mac} = $hostname; + }, + del_ips_from_mac => sub { + my ($bridge, $mac) = @_; + push @sdn_calls, 'del_ips_from_mac'; + delete $ipam->{$bridge}->{$mac}; + }, + ); + $lxc_module->unmock('delete_ifaces_ipams_ips'); + my $mocked_destroy = \&PVE::LXC::destroy_lxc_container; + $lxc_module->mock( + destroy_lxc_container => sub { + my ($storecfg, $vmid, $conf) = @_; + # like the real function, which removes the addresses of the old container + PVE::LXC::delete_ifaces_ipams_ips($conf, $vmid); + return $mocked_destroy->(@_); + }, + ); + + my $refused = sub { + my ($name, $param, $expected_err) = @_; + + reset_configs(); + write_raw_config(200, $existing_raw); + $volumes_touched = 0; + @sdn_calls = (); + my $err = restore_ct(200, { force => 1, $param->%* }); + like($err, $expected_err, "$name: restore is refused"); + is($volumes_touched, 0, "$name: volumes are not touched"); + is_deeply(\@sdn_calls, [], "$name: IPAM is 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 = + "unable to restore CT 200 - 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 = + "unable to restore CT 200 - 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', {}, qr/^\Q$mismatch_err\E$/); + $refused->('different uid, unique', { unique => 1 }, qr/^\Q$mismatch_err\E$/); + $refused->('different uid, network option', { net0 => $net }, qr/^\Q$mismatch_err\E$/); + $refused->('from STDIN', $stdin, qr/^\Q$stdin_err\E$/); + $refused->('from STDIN, network option', { $stdin->%*, net0 => $net }, + qr/^\Q$stdin_err\E$/); + + for my $param ({}, $stdin) { + my $name = $param->{ostemplate} ? 'from STDIN' : 'from file'; + reset_configs(); + write_raw_config(200, $existing_raw); + @sdn_calls = (); + $ipam = { vnet0 => { 'BC:24:11:00:00:01' => 'existing' } }; + my $err = restore_ct( + 200, + { force => 1, 'allow-identity-mismatch' => 1, net0 => $net, $param->%* }, + ); + is($err, '', "$name: restore with override succeeds") or diag($task_log); + is_deeply( + \@sdn_calls, + ['del_ips_from_mac', 'add_next_free_cidr'], + "$name: network option is applied once the old container is removed", + ); + ok( + exists($ipam->{vnet0}->{'BC:24:11:00:00:01'}), + "$name: restored interface has an IPAM mapping", + ); + 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 target 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_configs(); + write_raw_config(200, existing_config('ctime=1500000000')); + my $err = restore_ct(200, { force => 1, ostemplate => '-', rootfs => 'local:4' }); + is($err, '', 'restore from STDIN over a container without identity succeeds'); + is(identity_of(200)->{uid}, 'backupbackupbackupba', 'uid of the backup is taken over'); + + $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'); + + $lxc_module->mock( + delete_ifaces_ipams_ips => sub { }, + destroy_lxc_container => $mocked_destroy, + ); +}; + remove_tree($WORKDIR); done_testing(); -- 2.47.3