public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH container] fix #7267: do not inherit host attributes for rootfs
Date: Wed, 28 Jan 2026 17:54:05 +0100	[thread overview]
Message-ID: <20260128165406.308592-1-f.schauer@proxmox.com> (raw)

With the introduction of 0db559517ac6 (mountpoint_insert_staged: inherit
attributes if directory already exists) mountpoints started preserving
attributes of the underlying directory they were mounted over. This also
unintentionally applied to the rootfs, causing "/" inside the container
to be owned by "nobody".

This commit exempts the rootfs from the attribute preservation, to avoid
inheriting attributes from the host.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/PVE/LXC.pm            | 18 ++++++++++--------
 src/lxc-pve-prestart-hook |  6 ++++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index bab27b8..2df0e6c 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -2121,7 +2121,7 @@ sub mountpoint_stage {
 }
 
 sub mountpoint_insert_staged {
-    my ($mount_fd, $rootdir_fd, $mp_dir, $opt, $root_uid, $root_gid) = @_;
+    my ($mount_fd, $rootdir_fd, $mp_dir, $opt, $root_uid, $root_gid, $keep_attrs) = @_;
 
     if (!defined($rootdir_fd)) {
         sysopen($rootdir_fd, '.', O_PATH | O_DIRECTORY)
@@ -2130,12 +2130,14 @@ sub mountpoint_insert_staged {
 
     my $dest_fd = walk_tree_nofollow_fd('/', $rootdir_fd, $mp_dir, 1, $root_uid, $root_gid);
 
-    # Preserve attributes of destination directory
-    my ($mode, $uid, $gid) = (stat($dest_fd))[2, 4, 5];
-    PVE::Tools::fchownat(fileno($mount_fd), '', $uid, $gid, PVE::Tools::AT_EMPTY_PATH)
-        or die "failed to propagate uid and gid to mountpoint: $!\n";
-    PVE::Tools::fchmodat(fileno($mount_fd), '.', $mode, 0)
-        or die "failed to propagate access mode to mountpoint: $!\n";
+    if ($keep_attrs) {
+        # Preserve attributes of destination directory
+        my ($mode, $uid, $gid) = (stat($dest_fd))[2, 4, 5];
+        PVE::Tools::fchownat(fileno($mount_fd), '', $uid, $gid, PVE::Tools::AT_EMPTY_PATH)
+            or die "failed to propagate uid and gid to mountpoint: $!\n";
+        PVE::Tools::fchmodat(fileno($mount_fd), '.', $mode, 0)
+            or die "failed to propagate access mode to mountpoint: $!\n";
+    }
 
     PVE::Tools::move_mount(
         fileno($mount_fd),
@@ -2476,7 +2478,7 @@ sub mountpoint_hotplug : prototype($$$$$) {
         chdir('/')
             or die "failed to change root directory within the container's mount namespace: $!\n";
 
-        mountpoint_insert_staged($mount_fd, undef, $mp->{mp}, $opt, $root_uid, $root_gid);
+        mountpoint_insert_staged($mount_fd, undef, $mp->{mp}, $opt, $root_uid, $root_gid, 1);
     });
 }
 
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index 41af242..d264ab9 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -95,11 +95,12 @@ PVE::LXC::Tools::lxc_hook(
                 $mountpoint, $dir, $storage_cfg, undef, $root_uid, $root_gid,
             );
 
-            my ($dest_dir, $dest_base_fd);
+            my ($dest_dir, $dest_base_fd, $keep_attrs);
             if ($rootdir_fd) {
                 # Mount relative to the rootdir fd.
                 $dest_base_fd = $rootdir_fd;
                 $dest_dir = './' . $mountpoint->{mp};
+                $keep_attrs = 1;
             } else {
                 # Assert that 'rootfs' is the first one:
                 die "foreach_mount() error\n" if $opt ne 'rootfs';
@@ -109,10 +110,11 @@ PVE::LXC::Tools::lxc_hook(
                 sysopen($dest_base_fd, '/', O_PATH | O_DIRECTORY)
                     or die "failed to open '.': $!\n";
                 $dest_dir = $rootdir;
+                $keep_attrs = 0;
             }
 
             PVE::LXC::mountpoint_insert_staged(
-                $mount_fd, $dest_base_fd, $dest_dir, $opt, $root_uid, $root_gid,
+                $mount_fd, $dest_base_fd, $dest_dir, $opt, $root_uid, $root_gid, $keep_attrs,
             );
 
             # From now on we mount inside our rootfs:
-- 
2.47.3





             reply	other threads:[~2026-01-28 17:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-28 16:54 Filip Schauer [this message]
2026-01-29  7:18 ` applied: " Fabian Grünbichler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260128165406.308592-1-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal