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 745CC1FF0B4 for ; Fri, 27 Nov 2026 14:48:19 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 17C1121735; Fri, 27 Nov 2026 14:47:17 +0100 (CET) From: Fiona Ebner To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 08/10] restore: check permissions up-front Date: Fri, 27 Nov 2026 14:45:32 +0100 Message-ID: <20261127134637.140854-9-f.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261127134637.140854-1-f.ebner@proxmox.com> References: <20261127134637.140854-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795787201001 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.242 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: ZDUX4WVIIMFDAKKAILPB5ASVNKIEPCUI X-Message-ID-Hash: ZDUX4WVIIMFDAKKAILPB5ASVNKIEPCUI X-MailFrom: f.ebner@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: Check if the user has the necessary permissions before destroying the old configuration and restoring all drives, which might take a long time. Moving the checks is made possible by filling in a placeholder value in restore_update_config_line() instead of the final volume ID and replacing it later during volume allocation. For tar, the permissions are not checked up-front and the map is still needed in restore_update_config_line(), since it does not go through allocation like the other restore types. The tests used restore_update_config_line() for simulating failure during 'data' restore. Switch to using Restore::allocate_volumes() instead. The test using hostpci0 as an unprivileged user now behaves quite differently, since the permission check is up-front. Add a second test failing when assigning the VM to the pool to still test that behavior. Signed-off-by: Fiona Ebner --- src/PVE/API2/Qemu.pm | 2 +- src/PVE/QemuServer.pm | 122 ++++++++++++---------- src/PVE/QemuServer/Restore.pm | 28 +++-- src/test/run_qemu_restore_config_tests.pl | 70 ++++++++++--- 4 files changed, 144 insertions(+), 78 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 05ed7997..988d9c45 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -1546,7 +1546,7 @@ __PACKAGE__->register_method({ 'allow-identity-mismatch' => $allow_identity_mismatch, }; if (my $volid = $archive->{volid}) { - # best effort, real check is after restoring! + # best effort, real check is during restore! my $merged = eval { my $old_conf = PVE::Storage::extract_vzdump_config($storecfg, $volid); PVE::QemuServer::restore_merge_config( diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm index cc573cfd..baaa6ef0 100644 --- a/src/PVE/QemuServer.pm +++ b/src/PVE/QemuServer.pm @@ -6495,7 +6495,9 @@ sub restore_file_archive { } sub restore_update_config_line { - my ($cookie, $map, $line, $unique) = @_; + my ($cookie, $virtdev_hash, $line, $unique, $map) = @_; + + die "internal error - need map or virtdev hash\n" if !($virtdev_hash || $map); # vzdump never archives pending changes, and a pending change of a crafted backup would neither # be checked against the privileges of the user nor be visible before the next start applies it @@ -6546,9 +6548,15 @@ sub restore_update_config_line { my $di = parse_drive($virtdev, $value); if (defined($di->{backup}) && !$di->{backup}) { $res .= "#$line"; - } elsif ($map->{$virtdev}) { + } elsif ($map->{$virtdev} || $virtdev_hash->{$virtdev}) { delete $di->{format}; # format can change on restore - $di->{file} = $map->{$virtdev}; + if ($map->{$virtdev}) { + $di->{file} = $map->{$virtdev}; + } else { + # The actual volume ID is set when allocating later. + my $storeid = $virtdev_hash->{$virtdev}->{storeid}; + $di->{file} = "${storeid}:to-be-allocated"; + } $value = print_drive($di); $res .= "$virtdev: $value\n"; } else { @@ -6777,7 +6785,7 @@ sub restore_proxmox_backup_archive { my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid); my $oldconf = PVE::Cluster::cfs_read_file($cfs_path); - my $new_conf_raw = ''; + my $new_conf; my $rpcenv = PVE::RPCEnvironment::get(); my $devinfo = {}; # info about drives included in backup @@ -6843,6 +6851,26 @@ sub restore_proxmox_backup_archive { $rpcenv, $user, $storecfg, $fh, $devinfo, $options, ); + $fh->seek(0, 0) || die "seek failed - $!\n"; + + my $new_conf_raw = ''; + my $cookie = { netcount => 0 }; + while (defined(my $line = <$fh>)) { + $new_conf_raw .= restore_update_config_line( + $cookie, $virtdev_hash, $line, $options->{unique}, + ); + } + + $fh->close(); + + if ($options->{live}) { + # keep lock during live-restore + $new_conf_raw .= "\nlock: create"; + } + + $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); + check_restore_permissions($rpcenv, $user, $new_conf); + # fixme: rate limit? # create empty/temp config @@ -6854,7 +6882,7 @@ sub restore_proxmox_backup_archive { $virtdev_hash); # allocate volumes - my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid); + PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf); foreach my $virtdev (sort keys %$virtdev_hash) { my $d = $virtdev_hash->{$virtdev}; @@ -6899,17 +6927,6 @@ sub restore_proxmox_backup_archive { print "restore proxmox backup image: $dbg_cmdstring\n"; run_command($pbs_restore_cmd); } - - $fh->seek(0, 0) || die "seek failed - $!\n"; - - my $cookie = { netcount => 0 }; - while (defined(my $line = <$fh>)) { - $new_conf_raw .= restore_update_config_line( - $cookie, $map, $line, $options->{unique}, - ); - } - - $fh->close(); }; my $err = $@; @@ -6927,13 +6944,6 @@ sub restore_proxmox_backup_archive { } $options->{progress}->{'volumes-kept'} = 1; - if ($options->{live}) { - # keep lock during live-restore - $new_conf_raw .= "\nlock: create"; - } - - my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); - check_restore_permissions($rpcenv, $user, $new_conf); PVE::QemuServer::Restore::write_config($vmid, $new_conf, $options); eval { rescan($vmid, 1); }; @@ -6986,7 +6996,7 @@ sub restore_external_archive { my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid); my $oldconf = PVE::Cluster::cfs_read_file($cfs_path); - my $new_conf_raw = ''; + my $new_conf; my $rpcenv = PVE::RPCEnvironment::get(); my $devinfo = {}; # info about drives included in backup @@ -7030,6 +7040,21 @@ sub restore_external_archive { $rpcenv, $user, $storecfg, $fh, $devinfo, $options, ); + $fh->seek(0, 0) || die "seek failed - $!\n"; + + my $new_conf_raw = ''; + my $cookie = { netcount => 0 }; + while (defined(my $line = <$fh>)) { + $new_conf_raw .= restore_update_config_line( + $cookie, $virtdev_hash, $line, $options->{unique}, + ); + } + + $fh->close(); + + $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); + check_restore_permissions($rpcenv, $user, $new_conf); + # create empty/temp config PVE::QemuServer::Restore::write_config( $vmid, { memory => 128, lock => 'create' }, $options, @@ -7039,7 +7064,7 @@ sub restore_external_archive { $virtdev_hash); # allocate volumes - my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid); + PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf); for my $virtdev (sort keys $virtdev_hash->%*) { my $d = $virtdev_hash->{$virtdev}; @@ -7079,17 +7104,6 @@ sub restore_external_archive { } die $err if $err; } - - $fh->seek(0, 0) || die "seek failed - $!\n"; - - my $cookie = { netcount => 0 }; - while (defined(my $line = <$fh>)) { - $new_conf_raw .= restore_update_config_line( - $cookie, $map, $line, $options->{unique}, - ); - } - - $fh->close(); }; my $err = $@; @@ -7110,8 +7124,6 @@ sub restore_external_archive { } $options->{progress}->{'volumes-kept'} = 1; - my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); - check_restore_permissions($rpcenv, $user, $new_conf); PVE::QemuServer::Restore::write_config($vmid, $new_conf, $options); eval { rescan($vmid, 1); }; @@ -7426,7 +7438,7 @@ sub restore_vma_archive { my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid); my $oldconf = PVE::Cluster::cfs_read_file($cfs_path); - my $new_conf_raw = ''; + my $new_conf; my %storage_limits; @@ -7451,6 +7463,21 @@ sub restore_vma_archive { $rpcenv, $user, $storecfg, $fh, $devinfo, $opts, ); + $fh->seek(0, 0) || die "seek failed - $!\n"; + + my $new_conf_raw = ''; + my $cookie = { netcount => 0 }; + while (defined(my $line = <$fh>)) { + $new_conf_raw .= restore_update_config_line( + $cookie, $virtdev_hash, $line, $opts->{unique}, + ); + } + + $fh->close(); + + $new_conf = restore_merge_config($conffile, $new_conf_raw, $opts->{override_conf}); + check_restore_permissions($rpcenv, $user, $new_conf); + foreach my $info (values %{$virtdev_hash}) { my $storeid = $info->{storeid}; next if defined($storage_limits{$storeid}); @@ -7473,7 +7500,7 @@ sub restore_vma_archive { $virtdev_hash); # allocate volumes - my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid); + PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf); # print restore information to $fifofh foreach my $virtdev (sort keys %$virtdev_hash) { @@ -7507,17 +7534,6 @@ sub restore_vma_archive { print "map '$d->{devname}' to '$path' (write zeros = ${write_zeros}${cache_info})\n"; } - - $fh->seek(0, 0) || die "seek failed - $!\n"; - - my $cookie = { netcount => 0 }; - while (defined(my $line = <$fh>)) { - $new_conf_raw .= restore_update_config_line( - $cookie, $map, $line, $opts->{unique}, - ); - } - - $fh->close(); }; my $oldtimeout; @@ -7586,8 +7602,6 @@ sub restore_vma_archive { } $opts->{progress}->{'volumes-kept'} = 1; - my $new_conf = restore_merge_config($conffile, $new_conf_raw, $opts->{override_conf}); - check_restore_permissions($rpcenv, $user, $new_conf); PVE::QemuServer::Restore::write_config($vmid, $new_conf, $opts); eval { rescan($vmid, 1); }; @@ -7685,7 +7699,7 @@ sub restore_tar_archive { my $cookie = { netcount => 0 }; while (defined(my $line = <$srcfd>)) { $new_conf_raw .= restore_update_config_line( - $cookie, $map, $line, $opts->{unique}, + $cookie, undef, $line, $opts->{unique}, $map, ); } diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm index 583f7581..a67ac121 100644 --- a/src/PVE/QemuServer/Restore.pm +++ b/src/PVE/QemuServer/Restore.pm @@ -8,17 +8,22 @@ use PVE::Storage::Common; use PVE::QemuConfig; use PVE::QemuServer::Cloudinit; -use PVE::QemuServer::Drive - qw(checked_volume_format drive_has_absolute_path drive_is_cdrom drive_is_cloudinit parse_drive); +use PVE::QemuServer::Drive qw( + checked_volume_format + drive_has_absolute_path + drive_is_cdrom + drive_is_cloudinit + parse_drive + print_drive +); # Helper to allocate and activate all volumes required for a restore # # $storecfg: Storage configuration # $virtdev_hash: as returned by parse_backup_hints() -# -# Returns: { $virtdev => $volid } -sub allocate_volumes($storecfg, $virtdev_hash, $vmid) { - my $map = {}; +# $vmid: the VM ID +# $new_conf: the new configuration for the restored VM, for filling in volid placeholders +sub allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf) { for my $virtdev (sort keys $virtdev_hash->%*) { my $d = $virtdev_hash->{$virtdev}; die "got no size for '$virtdev'\n" if !defined($d->{size}); @@ -46,10 +51,15 @@ sub allocate_volumes($storecfg, $virtdev_hash, $vmid) { PVE::Storage::activate_volumes($storecfg, [$volid]); - $map->{$virtdev} = $volid; + # There can be #qmdump#map lines that are not part of the config and these were restored in + # the past too. + # FIXME: MAJOR VERSION: consider allocating only drives that are referenced in the config. + if ($new_conf->{$virtdev}) { + my $drive = parse_drive($virtdev, $new_conf->{$virtdev}); + $drive->{file} = $volid; + $new_conf->{$virtdev} = print_drive($drive); + } } - - return $map; } # helper to remove disks that will not be used after restore diff --git a/src/test/run_qemu_restore_config_tests.pl b/src/test/run_qemu_restore_config_tests.pl index 3ab81fab..503438e2 100755 --- a/src/test/run_qemu_restore_config_tests.pl +++ b/src/test/run_qemu_restore_config_tests.pl @@ -67,7 +67,7 @@ dir_glob_foreach( while (defined(my $line = <$fh>)) { $got .= PVE::QemuServer::restore_update_config_line( - $cookie, $map, $line, 0, + $cookie, undef, $line, 0, $map, ); } @@ -232,7 +232,12 @@ $qemu_server_module->mock( return $qemu_server_module->original('restore_firewall_config')->(@_); }, restore_update_config_line => sub { - die "restoring the data failed\n" if $fail_at eq 'data'; + my ($cookie, $virtdev_hash, $line, $unique, $map) = @_; + if ($fail_at eq 'data' && defined($map)) { + # NOTE this only triggers for tar restore. Other types fail at + # PVE::QemuServer::Restore::allocate_volumes() + die "restoring the data failed\n"; + } return $qemu_server_module->original('restore_update_config_line')->(@_); }, tar_archive_read_firstfile => sub { return 'qemu-server.conf'; }, @@ -281,6 +286,15 @@ $qemu_server_module->mock( }, ); +# TODO once there are dedicated functions for the actual data part of the restore, mock those. +my $qemu_server_restore_module = Test::MockModule->new('PVE::QemuServer::Restore'); +$qemu_server_restore_module->mock( + allocate_volumes => sub { + die "restoring the data failed\n" if $fail_at eq 'data'; + return $qemu_server_restore_module->original('allocate_volumes')->(@_); + }, +); + my $task_error; my $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment'); $rpcenv_module->mock( @@ -543,11 +557,15 @@ subtest 'failed removal of a restored volume keeps the VMID' => sub { my $run_command = \&PVE::QemuServer::run_command; local *PVE::QemuServer::run_command = sub { my ($cmd) = @_; - return 0 if ref($cmd) && $cmd->[0] eq '/usr/bin/pbs-restore'; + if (ref($cmd) && $cmd->[0] eq '/usr/bin/pbs-restore') { + die "restoring the data failed\n" if $fail_at eq 'pbs-restore'; + return 0; + } return $run_command->(@_); }; - local $fail_at = 'data'; + local $fail_at = 'pbs-restore'; + my $backup = backup_config() . "scsi0: target:vm-100-disk-0,size=1M\n#qmdump#map:scsi0:drive-scsi0:target:raw:\n"; my ($err, $output) = restore_api('pbs', $backup, {}); @@ -1000,27 +1018,51 @@ subtest 'retry in place after a failed live restore' => sub { subtest 'retry in place after a failure once the restored volumes stay' => sub { QemuServerTest::FakeCFS::reset(); - my $backup = $BACKUP_WITH_UID . "hostpci0: 0000:01:00.0\n"; + my $backup = $BACKUP_WITH_UID; - # the permission check needs the restored config, so it runs once the data is restored - $rpcenv->set_user('user@pve'); - my ($err, $output) = restore_api('vma file', $backup, {}); - $rpcenv->set_user('root@pam'); - is( - $err, - "only root can set 'hostpci0' config for non-mapped devices\n", - 'restore refused after restoring the data', - ); + my $fail_assign = 1; + + local *PVE::RPCEnvironment::check_pool_exist = sub { return 1; }; + local *PVE::RPCEnvironment::check_vm_perm = sub { return 1; }; + local *PVE::AccessControl::add_vm_to_pool = sub { + die "failed to assign VM to pool\n" if $fail_assign; + return; + }; + + my ($err, $output) = restore_api('vma file', $backup, { pool => 'somepool' }); + is($err, "failed to assign VM to pool\n", 'restore failed after restoring the data'); ok(defined($QemuServerTest::FakeCFS::configs->{100}), 'VM is left behind'); is(pending_uid_of(100), $UID_BACKUP, 'leftover VM has the uid of the backup as pending uid'); + $fail_assign = 0; + ($err, $output) = restore_api('vma file', $backup, { force => 1 }); is($err, '', 'retry succeeds') or diag($output); is(stored_uid_of(100), $UID_BACKUP, 'retry adopts the uid of the backup'); is(pending_uid_of(100), undef, 'retry: pending uid is removed'); }; +subtest 'permission error leads to early failure' => sub { + QemuServerTest::FakeCFS::reset(); + my $backup = $BACKUP_WITH_UID . "hostpci0: 0000:01:00.0\n"; + + $rpcenv->set_user('user@pve'); + my ($err, $output) = restore_api('vma file', $backup, {}); + is( + $err, + "only root can set 'hostpci0' config for non-mapped devices\n", + 'restore refused because of missing permissions', + ); + is(stored_config(100), undef, 'no config created'); + + $rpcenv->set_user('root@pam'); + ($err, $output) = restore_api('vma file', $backup, {}); + is($err, '', 'privileged user succeeds') or diag($output); + is(stored_uid_of(100), $UID_BACKUP, 'uid matches that of the backup'); + is(pending_uid_of(100), undef, 'there is no pending uid'); +}; + my $FAILURES = { # right after the check, before the restore writes a config itself firewall => "unable to restore the firewall config\n", -- 2.47.3