all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH container 2/2] oci create: honor `User` from OCI image config
Date: Mon, 15 Dec 2025 18:41:59 +0100	[thread overview]
Message-ID: <20251215174203.267293-3-f.schauer@proxmox.com> (raw)
In-Reply-To: <20251215174203.267293-1-f.schauer@proxmox.com>

Honor a custom user and group specified for the entrypoint via the OCI
image config `User` field instead of ignoring it.

User and group name lookups, including supplementary groups, are
resolved from /etc/passwd and /etc/group in the container file system.
This behaviour matches the OCI image spec. [0]

[0] https://specs.opencontainers.org/image-spec/config/?v=v1.1.1#properties

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
This requires the following patch for LXC in order to work properly:
https://github.com/lxc/lxc/pull/4626

 src/PVE/LXC/Create.pm | 59 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index 9956cf9..08e0386 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -768,6 +768,65 @@ sub restore_oci_archive {
         }
     }
 
+    if (my $usercfg = $oci_config_get_checked_scalar->('User')) {
+        my ($user, $group) = $usercfg =~ /^([^:]+)(?::([^:]+))?$/
+            or die "OCI config value for 'User' has an invalid format\n";
+
+        my $etc_passwd = "$rootdir/etc/passwd";
+        my $etc_group = "$rootdir/etc/group";
+
+        # Scan file, match column $match_index against $match_val, return value at $ret_index
+        my $lookup_field = sub {
+            my ($file, $match_index, $match_val, $ret_index) = @_;
+
+            open(my $fh, '<', $file) or return undef;
+            while (my $line = <$fh>) {
+                my @fields = split(/:/, $line);
+                if (defined($fields[$match_index]) && $fields[$match_index] eq $match_val) {
+                    return $fields[$ret_index];
+                }
+            }
+            return undef;
+        };
+
+        my $get_supplementary_groups = sub {
+            my ($username) = @_;
+
+            my @groups;
+            open(my $fh, '<', $etc_group) or return undef;
+            while (defined(my $line = <$fh>)) {
+                push @groups, $1
+                    if ($line =~ m/^[^:]*:[^:]*:([^:]*):(?:[^,]*,)*$username(?:,|$)/);
+            }
+            return join(',', @groups);
+        };
+
+        my ($uid, $username, $gid, $groups);
+
+        if ($user =~ /^\d+$/) {
+            $uid = $user;
+            $username = $lookup_field->($etc_passwd, 2, $uid, 0);
+        } else {
+            $username = $user;
+            $uid = $lookup_field->($etc_passwd, 0, $username, 2);
+        }
+
+        if (defined($group)) {
+            if ($group =~ /^\d+$/) {
+                $gid = $group;
+            } else {
+                $gid = $lookup_field->($etc_group, 0, $group, 2);
+            }
+        } else {
+            $gid = $lookup_field->($etc_passwd, 2, $uid, 3) if defined($uid);
+            $groups = $get_supplementary_groups->($username) if defined($username);
+        }
+
+        push $conf->{lxc}->@*, ['lxc.init.uid', $uid] if defined($uid);
+        push $conf->{lxc}->@*, ['lxc.init.gid', $gid] if defined($gid);
+        push $conf->{lxc}->@*, ['lxc.init.groups', $groups] if defined($groups);
+    }
+
     if (my $working_dir = $oci_config_get_checked_scalar->('WorkingDir')) {
         push $conf->{lxc}->@*, ['lxc.init.cwd', $working_dir];
     }
-- 
2.47.3



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-12-15 17:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-15 17:41 [pve-devel] [PATCH container 0/2] " Filip Schauer
2025-12-15 17:41 ` [pve-devel] [PATCH container 1/2] config: add `lxc.init.uid`/`gid`/`groups` keys Filip Schauer
2025-12-15 17:41 ` Filip Schauer [this message]
2025-12-16 12:03 ` [pve-devel] [PATCH container 0/2] oci create: honor `User` from OCI image config Maximiliano Sandoval
2025-12-16 18:13   ` Filip Schauer

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=20251215174203.267293-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal