Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal change. #11220

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/sentry/fsimpl/user/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ go_library(
"path.go",
"user.go",
],
visibility = ["//pkg/sentry:internal"],
visibility = [
"//visibility:public",
],
deps = [
"//pkg/abi/linux",
"//pkg/context",
Expand Down
37 changes: 19 additions & 18 deletions pkg/sentry/fsimpl/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,17 @@ func openFile(ctx context.Context, mns *vfs.MountNamespace, path string) (*vfs.F
return fd, nil
}

// findGroupInGroupFile parses a group file and returns the given group's
// FindGroupInGroupFile parses a group file and returns the given group's
// gid. If the gid is a number, we don't need to read the file.
//
// If we don't find the group, we return 0.
func findGroupInGroupFile(ctx context.Context, mns *vfs.MountNamespace, gidString string) auth.KGID {
func FindGroupInGroupFile(group io.Reader, gidString string) auth.KGID {
// gid is a number, we don't need to read the file.
gidInt, err := strconv.Atoi(gidString)
if err == nil {
return auth.KGID(gidInt)
}

fd, err := openFile(ctx, mns, "/etc/group")
if err != nil {
return defaultGID
}
defer fd.DecRef(ctx)

r := &fileReader{
ctx: ctx,
fd: fd,
}

// Group file format:
// group_name:password:gid:<user1,user2,user3>
const (
Expand All @@ -218,7 +207,7 @@ func findGroupInGroupFile(ctx context.Context, mns *vfs.MountNamespace, gidStrin
gidIdx = 2
)

s := bufio.NewScanner(r)
s := bufio.NewScanner(group)
for s.Scan() {
if err := s.Err(); err != nil {
return defaultGID
Expand All @@ -241,7 +230,8 @@ func findGroupInGroupFile(ctx context.Context, mns *vfs.MountNamespace, gidStrin
return defaultGID
}

func findUIDGIDInPasswd(passwd io.Reader, user string) (auth.KUID, auth.KGID) {
// FindUIDGIDInPasswd parses a passwd file and returns the given user's uid and gid.
func FindUIDGIDInPasswd(passwd io.Reader, user string) (auth.KUID, auth.KGID) {
uid := defaultUID
gid := defaultGID

Expand Down Expand Up @@ -357,13 +347,24 @@ func getExecUIDGID(ctx context.Context, mns *vfs.MountNamespace, user string) (a
}
// This return kGid from the passwd file (if we find one). We might have recieved a group id
// string or numeric from the user.
kUID, kGID := findUIDGIDInPasswd(r, user)
kUID, kGID := FindUIDGIDInPasswd(r, user)
usergroup := strings.SplitN(user, ":", 2)

// If we have a group id string, try to resolve it.
if len(usergroup) == 2 {
kGID = findGroupInGroupFile(ctx, mns, usergroup[1])
fdg, err := openFile(ctx, mns, "/etc/group")
if err != nil {
kGID = defaultGID
return kUID, kGID
}
defer fdg.DecRef(ctx)
r = &fileReader{
ctx: ctx,
fd: fdg,
}
kGID = FindGroupInGroupFile(r, usergroup[1])
}
return kUID, kGID

}

// GetExecUIDGIDFromUser retrieves the UID and GID from /etc/passwd file for
Expand Down
Loading