Skip to content

Commit 6d0d22c

Browse files
committed
reduce the chance of parsing /etc/passwd & /etc/group
Signed-off-by: lifubang <[email protected]>
1 parent 4d948b1 commit 6d0d22c

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

libcontainer/init_linux.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -440,35 +440,59 @@ func syncParentSeccomp(pipe *os.File, seccompFd *os.File) error {
440440
return readSync(pipe, procSeccompDone)
441441
}
442442

443-
// setupUser changes the groups, gid, and uid for the user inside the container
444-
func setupUser(config *initConfig) error {
443+
func getExecUser(userAndGroup string) (*user.ExecUser, error) {
445444
// Set up defaults.
446445
defaultExecUser := user.ExecUser{
447446
Uid: 0,
448447
Gid: 0,
449448
Home: "/",
450449
}
451450

452-
passwdPath, err := user.GetPasswdPath()
451+
u, g, ok := strings.Cut(userAndGroup, ":")
452+
453+
if !ok || os.Getenv("HOME") == "" {
454+
passwdPath, err := user.GetPasswdPath()
455+
if err != nil {
456+
return nil, err
457+
}
458+
459+
groupPath, err := user.GetGroupPath()
460+
if err != nil {
461+
return nil, err
462+
}
463+
464+
return user.GetExecUserPath(userAndGroup, &defaultExecUser, passwdPath, groupPath)
465+
}
466+
uid, err := strconv.Atoi(u)
453467
if err != nil {
454-
return err
468+
return nil, err
455469
}
456-
457-
groupPath, err := user.GetGroupPath()
470+
gid, err := strconv.Atoi(g)
458471
if err != nil {
459-
return err
472+
return nil, err
460473
}
474+
return &user.ExecUser{
475+
Uid: uid,
476+
Gid: gid,
477+
Home: os.Getenv("HOME"),
478+
}, nil
479+
}
461480

462-
execUser, err := user.GetExecUserPath(config.User, &defaultExecUser, passwdPath, groupPath)
481+
// setupUser changes the groups, gid, and uid for the user inside the container.
482+
func setupUser(config *initConfig) error {
483+
execUser, err := getExecUser(config.User)
463484
if err != nil {
464485
return err
465486
}
466487

467488
var addGroups []int
468489
if len(config.AdditionalGroups) > 0 {
469-
addGroups, err = user.GetAdditionalGroupsPath(config.AdditionalGroups, groupPath)
470-
if err != nil {
471-
return err
490+
for _, group := range config.AdditionalGroups {
491+
gid, err := strconv.Atoi(group)
492+
if err != nil {
493+
return err
494+
}
495+
addGroups = append(addGroups, gid)
472496
}
473497
}
474498

libcontainer/integration/exec_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func TestAdditionalGroups(t *testing.T) {
395395
Env: standardEnvironment,
396396
Stdin: nil,
397397
Stdout: &stdout,
398-
AdditionalGroups: []string{"plugdev", "audio"},
398+
AdditionalGroups: []string{"1", "2"},
399399
Init: true,
400400
}
401401
err = container.Run(&pconfig)
@@ -407,12 +407,12 @@ func TestAdditionalGroups(t *testing.T) {
407407
outputGroups := stdout.String()
408408

409409
// Check that the groups output has the groups that we specified
410-
if !strings.Contains(outputGroups, "audio") {
411-
t.Fatalf("Listed groups do not contain the audio group as expected: %v", outputGroups)
410+
if !strings.Contains(outputGroups, "1") {
411+
t.Fatalf("Listed groups do not contain the group as expected: %v", outputGroups)
412412
}
413413

414-
if !strings.Contains(outputGroups, "plugdev") {
415-
t.Fatalf("Listed groups do not contain the plugdev group as expected: %v", outputGroups)
414+
if !strings.Contains(outputGroups, "2") {
415+
t.Fatalf("Listed groups do not contain the group as expected: %v", outputGroups)
416416
}
417417
}
418418

libcontainer/integration/execin_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestExecInAdditionalGroups(t *testing.T) {
162162
Env: standardEnvironment,
163163
Stdin: nil,
164164
Stdout: &stdout,
165-
AdditionalGroups: []string{"plugdev", "audio"},
165+
AdditionalGroups: []string{"1", "2"},
166166
}
167167
err = container.Run(&pconfig)
168168
ok(t, err)
@@ -176,12 +176,12 @@ func TestExecInAdditionalGroups(t *testing.T) {
176176
outputGroups := stdout.String()
177177

178178
// Check that the groups output has the groups that we specified
179-
if !strings.Contains(outputGroups, "audio") {
180-
t.Fatalf("Listed groups do not contain the audio group as expected: %v", outputGroups)
179+
if !strings.Contains(outputGroups, "1") {
180+
t.Fatalf("Listed groups do not contain the group as expected: %v", outputGroups)
181181
}
182182

183-
if !strings.Contains(outputGroups, "plugdev") {
184-
t.Fatalf("Listed groups do not contain the plugdev group as expected: %v", outputGroups)
183+
if !strings.Contains(outputGroups, "2") {
184+
t.Fatalf("Listed groups do not contain the group as expected: %v", outputGroups)
185185
}
186186
}
187187

0 commit comments

Comments
 (0)