Skip to content

Commit 62fe2e0

Browse files
Merge pull request #25815 from giuseppe/fix-hostuser
libpod: --user works with --hostuser entries
2 parents 20e1b9d + 85024a9 commit 62fe2e0

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

libpod/container_internal_common.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
195195
cleanupFunc()
196196
}
197197
}()
198+
199+
if err := c.makeBindMounts(); err != nil {
200+
return nil, nil, err
201+
}
202+
198203
overrides := c.getUserOverrides()
199204
execUser, err := lookup.GetUserGroupInfo(c.state.Mountpoint, c.config.User, overrides)
200205
if err != nil {
201-
if slices.Contains(c.config.HostUsers, c.config.User) {
202-
execUser, err = lookupHostUser(c.config.User)
203-
}
204-
if err != nil {
205-
return nil, nil, err
206-
}
206+
return nil, nil, err
207207
}
208208

209209
// NewFromSpec() is deprecated according to its comment
@@ -236,10 +236,6 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
236236
g.SetProcessApparmorProfile(updatedProfile)
237237
}
238238

239-
if err := c.makeBindMounts(); err != nil {
240-
return nil, nil, err
241-
}
242-
243239
if err := c.mountNotifySocket(g); err != nil {
244240
return nil, nil, err
245241
}
@@ -2434,7 +2430,7 @@ func (c *Container) generateGroupEntry() (string, error) {
24342430

24352431
// Things we *can't* handle: adding the user we added in
24362432
// generatePasswdEntry to any *existing* groups.
2437-
addedGID := 0
2433+
addedGID := -1
24382434
if c.config.AddCurrentUserPasswdEntry {
24392435
entry, gid, err := c.generateCurrentUserGroupEntry()
24402436
if err != nil {
@@ -2503,7 +2499,7 @@ func (c *Container) generateUserGroupEntry(addedGID int) (string, error) {
25032499
}
25042500

25052501
splitUser := strings.SplitN(c.config.User, ":", 2)
2506-
group := splitUser[0]
2502+
group := "0"
25072503
if len(splitUser) > 1 {
25082504
group = splitUser[1]
25092505
}
@@ -2513,7 +2509,7 @@ func (c *Container) generateUserGroupEntry(addedGID int) (string, error) {
25132509
return "", nil //nolint: nilerr
25142510
}
25152511

2516-
if addedGID != 0 && addedGID == int(gid) {
2512+
if addedGID != -1 && addedGID == int(gid) {
25172513
return "", nil
25182514
}
25192515

libpod/container_internal_linux_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ func TestGenerateUserGroupEntry(t *testing.T) {
4747
Mountpoint: "/does/not/exist/tmp/",
4848
},
4949
}
50-
group, err := c.generateUserGroupEntry(0)
50+
group, err := c.generateUserGroupEntry(-1)
5151
if err != nil {
5252
t.Fatal(err)
5353
}
5454
assert.Equal(t, group, "456789:x:456789:123456\n")
5555

5656
c.config.User = "567890"
57-
group, err = c.generateUserGroupEntry(0)
57+
group, err = c.generateUserGroupEntry(-1)
5858
if err != nil {
5959
t.Fatal(err)
6060
}
61-
assert.Equal(t, group, "567890:x:567890:567890\n")
61+
assert.Equal(t, group, "0:x:0:567890\n")
6262
}

test/e2e/run_passwd_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ USER 1000`, ALPINE)
9090
Expect(session.OutputToString()).To(ContainSubstring("/etc/group"))
9191
})
9292

93-
It("podman run numeric user not specified in container modifies group", func() {
94-
session := podmanTest.Podman([]string{"run", "--read-only", "-u", "20001", BB, "mount"})
95-
session.WaitWithDefaultTimeout()
96-
Expect(session).Should(ExitCleanly())
97-
Expect(session.OutputToString()).To(ContainSubstring("/etc/group"))
98-
})
99-
10093
It("podman run numeric group from image and no group file", func() {
10194
dockerfile := fmt.Sprintf(`FROM %s
10295
RUN rm -f /etc/passwd /etc/shadow /etc/group

test/system/030-run.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,17 @@ EOF
906906
fi
907907

908908
user=$(id -u)
909+
910+
userspec=$(id -un):$(id -g)
911+
run_podman run --hostuser=$user --user $userspec --rm $IMAGE sh -c 'echo $(id -un):$(id -g)'
912+
is "$output" "$userspec"
913+
914+
run_podman run --hostuser=$user --user $userspec --group-entry="$(id -gn):x:$(id -g):" --rm $IMAGE sh -c 'echo $(id -un):$(id -gn)'
915+
is "$output" "$(id -un):$(id -gn)"
916+
917+
run_podman 126 run --hostuser=$user --user "$(id -un):$(id -gn)" --rm $IMAGE sh -c 'echo $(id -un):$(id -gn)'
918+
is "$output" "Error:.* no matching entries in group file"
919+
909920
run_podman run --hostuser=$user --rm $IMAGE grep $user /etc/passwd
910921
run_podman run --hostuser=$user --user $user --rm $IMAGE grep $user /etc/passwd
911922
user=bogus

0 commit comments

Comments
 (0)