Skip to content

pkg/oci: reject out-of-range uid/gid from image user database - #13797

Open
aysha-afrah26 wants to merge 1 commit into
containerd:mainfrom
aysha-afrah26:oci-user-id-range
Open

aysha-afrah26 wants to merge 1 commit into
containerd:mainfrom
aysha-afrah26:oci-user-id-range

Conversation

@aysha-afrah26

@aysha-afrah26 aysha-afrah26 commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

When the container's USER is given as a name rather than a numeric id, containerd resolves it against the image's /etc/passwd and /etc/group and copies the resulting uid and gid into the runtime spec's uint32 User fields with no range check. An image whose passwd entry maps a name to something like 4294967296 (1<<32) therefore has the value wrap to 0, so a container that should run as an unprivileged account runs as root. I ran into it after adding a passwd entry with a uid just past the 32-bit boundary and seeing the spec come back with UID 0.

The lookup helpers (UserFromFS, GIDFromFS, getSupplementalGroupsFromFS, and the groupname branch of WithAppendAdditionalGroups) now validate the resolved ids against the range OCI accepts, [0, MaxUint32-1], and return an error for anything outside it. Per review, the numeric form of WithUser also moves from its old MaxInt32 cap, which rested on a stale comment claiming runc requires ids to fit in int32, to the same MaxUint32-1 bound, so both paths accept exactly what OCI accepts. Regression tests cover the boundary in both directions: 4294967294 is accepted, 4294967295 and 1<<32 are rejected.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens OCI spec generation against malicious or malformed /etc/passwd and /etc/group entries in an image by rejecting UID/GID values outside runc’s supported <= MaxInt32 range, preventing silent uint32 narrowing/wraparound (e.g., 1<<32 → 0).

Changes:

  • Add a shared validUserID bound check and apply it to UID/GID values resolved from image user/group databases.
  • Reject out-of-range GIDs when resolving additional groups by name from /etc/group.
  • Add regression tests ensuring out-of-range IDs from FS-backed passwd/group data are rejected.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/oci/spec_opts.go Adds validUserID and enforces MaxInt32 bounds for UID/GID values obtained via passwd/group lookups to avoid truncation/wrap.
pkg/oci/spec_opts_user_bounds_test.go Adds regression tests for rejecting out-of-range UID/GID values read from /etc/passwd and /etc/group.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/oci/spec_opts.go Resolved
@AkihiroSuda

Copy link
Copy Markdown
Member

/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0
/cherry-pick release/1.7

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@AkihiroSuda: once the present PR merges, I will cherry-pick it on top of release/1.7, release/2.0, release/2.2, release/2.3 in new PRs and assign them to you.

Details

In response to this:

/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0
/cherry-pick release/1.7

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Comment thread pkg/oci/spec_opts.go Outdated

// validUserID reports whether id, read from a user database file such as
// /etc/passwd or /etc/group, fits the range containerd accepts for a runtime
// spec UID/GID. runc requires these to be <= MaxInt32, and the numeric form of

@AkihiroSuda AkihiroSuda Jul 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runc requires these to be <= MaxInt32

No, runc accepts UID up to 4294967294 (MaxUint32 - 1).

runc spec
jq '.process.user.uid=4294967294 | .process.args=["id"]' config.json | sponge config.json
mkdir rootfs
docker run -d --name busybox busybox
docker export busybox | tar Cx rootfs
runc run foo
# Output: uid=4294967294 gid=0(root)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I lifted that from the existing comment in WithUser and didn't verify it against current runc. Reworded the comment so it no longer makes that claim. I kept the bound at MaxInt32 so the name lookup path stays consistent with what the numeric USER path already enforces; the actual wrap only happens past MaxUint32. If you'd rather relax both paths to MaxUint32-1, I can do that here or as a follow-up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/oci should accept whatever OCI accepts, i.e., MaxUint32-1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Both the numeric WithUser path and the lookup helpers now use MaxUint32-1, and I dropped the stale comment about runc's int32 limit. Boundary tests updated: 4294967294 is accepted, 4294967295 rejected. One leftover: the numeric branch of WithAppendAdditionalGroups still parses gids with ParseUint(..., 32), so it accepts 4294967295; can align that here too if you'd like.

@AkihiroSuda AkihiroSuda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-project-automation github-project-automation Bot moved this from Needs Triage to Needs Update in Pull Request Review Jul 15, 2026
@samuelkarp samuelkarp added the status/needs-update Awaiting contributor update label Jul 15, 2026
Copilot AI review requested due to automatic review settings July 16, 2026 06:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts.go Outdated
Comment on lines +1024 to +1026
if !validUserID(g.Gid) {
return fmt.Errorf("group %q has gid %d out of range", g.Name, g.Gid)
}
Copilot AI review requested due to automatic review settings July 17, 2026 09:31
@kubernetes-prow kubernetes-prow Bot added size/L and removed size/M labels Jul 17, 2026
@AkihiroSuda

Copy link
Copy Markdown
Member

Please squash the commits

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts.go Outdated
// read from an image cannot wrap when narrowed to the spec's uint32 field
// (for example 1<<32 becomes 0, i.e. root).
func validUserID(id int) bool {
return id >= 0 && int64(id) <= math.MaxUint32-1
Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts_user_bounds_test.go Resolved
@aysha-afrah26

Copy link
Copy Markdown
Contributor Author

Done, squashed into a single commit. The two CI failures on the last run were the flaky TestCRIImagePullTimeout, unrelated to this change.

@AkihiroSuda

Copy link
Copy Markdown
Member

Please check copilot comments

Copilot AI review requested due to automatic review settings July 21, 2026 06:48
@aysha-afrah26

Copy link
Copy Markdown
Contributor Author

Went through the remaining comments and pushed an update, still as one commit:

  • numeric USER values now parse via ParseInt(..., 10, 64), so ids above MaxInt32 behave the same on 32-bit hosts instead of hitting ErrRange
  • the numeric branch of WithAppendAdditionalGroups now rejects 4294967295 like the name branch does, and TestWithAppendAdditionalGroups covers both the numeric and group-name out-of-range paths
  • fixed the untyped 4294967294 constant in the test, which would not have compiled on 32-bit, and the fs-based bounds tests skip on 32-bit since the parser saturates ids to MaxInt32 before the check can see them (details in the inline thread)
  • the gofmt one is a false positive: gofmt keeps math.MaxUint32-1 unspaced inside a comparison, and the tree is gofmt clean

Verified with go test ./pkg/oci/ plus build and vet for linux/arm and linux/386.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread pkg/oci/spec_opts.go Resolved
@github-project-automation github-project-automation Bot moved this from Needs Update to Review In Progress in Pull Request Review Aug 4, 2026
@aysha-afrah26

Copy link
Copy Markdown
Contributor Author

gentle ping

@AkihiroSuda
AkihiroSuda requested a review from a team August 26, 2026 20:32

@fuweid fuweid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should fix the issue in moby as well.

// /etc/passwd or /etc/group is rejected when it falls outside the range
// OCI accepts ([0, MaxUint32-1]), instead of wrapping when narrowed to the
// spec's uint32 fields (for example 1<<32 -> 0, i.e. root).
func TestUserIDBoundsFromFS(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestMalformedUserIDFromFSIsRejected(t *testing.T) {
	t.Parallel()

	fsys := fstest.MapFS{
		"etc/passwd": &fstest.MapFile{
			Data: []byte("evil:x:not-a-uid:10::/:/bin/sh\n"),
			Mode: 0o644,
		},
	}

	usr, err := UserFromFS(fsys, func(u user.User) bool { return u.Name == "evil" })
	if err == nil {
		t.Fatalf("expected malformed uid to be rejected, got uid=%d gid=%d", usr.Uid, usr.Gid)
	}
}

https://github.com/moby/sys/blob/d6b812df97ee7e0c1e16437fd4d1fa61738427db/user/user.go#L82

This package is not correct to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and your test shows the nastier variant: a non-numeric uid parses as 0 even on 64-bit, so a malformed entry quietly becomes root, and the bounds check here can't catch it since 0 is a valid uid. By the time ParsePasswdFilter returns, the value is already collapsed, so it has to be fixed at the parser. Filed moby/sys#248 for the swallowed conversion error and offered to send the patch there. Once the parser surfaces it, the 32-bit saturation case this PR skips becomes detectable too; I'd keep this PR to the range check since it's already approved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked this so pkg/oci no longer trusts the ints moby/sys/user hands back. Each passwd/group line's id fields are now checked as raw text before the entry is used. A non-numeric field like not-a-uid skips the line instead of resolving to 0, and an id past MaxUint32-1 is rejected, including on 32-bit where the parser would otherwise saturate it. Your case is covered in TestMalformedUserIDFromFS, along with empty, -1 and 0x10, plus a check that a broken line can't shadow the real root entry. moby/sys#248 is still open for fixing it at the parser.

samuelkarp
samuelkarp previously approved these changes Aug 26, 2026
@samuelkarp
samuelkarp dismissed their stale review August 26, 2026 23:03

Dismiss based on fuweid@ comments

@samuelkarp samuelkarp removed the cherry-pick/1.7.x Change to be cherry picked to release/1.7 branch label Oct 1, 2026
Copilot AI balanced review requested due to automatic review settings October 8, 2026 15:45
Comment thread pkg/oci/spec_opts.go Fixed
Comment thread pkg/oci/spec_opts.go Fixed
Comment thread pkg/oci/spec_opts.go Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts_user_test.go Outdated
Comment on lines +411 to +413
if testCase.skipOn32Bit && strconv.IntSize == 32 {
t.Skip("gids parsed from /etc/group saturate to MaxInt32 on 32-bit platforms")
}
Copilot AI balanced review requested due to automatic review settings October 8, 2026 15:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Numeric additional groups can incorrectly fail due to matching /etc/group entries, and two regression-test gaps should be corrected.

5 open findings

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread pkg/oci/spec_opts_user_test.go Resolved
Copilot AI balanced review requested due to automatic review settings October 15, 2026 13:02
@aysha-afrah26

Copy link
Copy Markdown
Contributor Author

Addressed the latest Copilot findings in the same commit. Numeric entries passed to WithAppendAdditionalGroups are no longer included in the /etc/group name lookup, so a group literally named 4242 with a bad gid can't fail a request for gid 4242. The 32-bit skip on the overgrp case is gone since the raw-field check catches it there too, and the test harness now requires the expected error instead of only comparing it when one happens. @fuweid @samuelkarp the malformed-id concern is handled in pkg/oci now (details in the thread above), so this should be ready for another look.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts.go Resolved
Comment thread pkg/oci/spec_opts_user_test.go Resolved
When the container USER is a name, the uid/gid resolved from the
image's /etc/passwd and /etc/group was copied into the runtime spec's
uint32 User fields with no range check, so an id such as 1<<32 wrapped
to 0 and a container meant to run unprivileged ran as root. Validate
the ids resolved by the lookup helpers and return an error for values
OCI cannot represent.

The bound is MaxUint32-1 for both the lookup helpers and the numeric
WithUser path, replacing the old MaxInt32 cap that rested on a stale
comment claiming runc requires ids to fit in int32. The numeric branch
of WithAppendAdditionalGroups enforces the same bound, and numeric
USER values, including the uid WithAdditionalGIDs looks up, are parsed
as int64 so ids above MaxInt32 behave the same on 32-bit platforms.

The id fields are checked as raw text rather than as the ints
github.com/moby/sys/user parses them into, because that parser discards
conversion errors: a field that is not a number at all becomes 0, i.e.
root, and one too wide for an int saturates, so neither can be told from
a genuine id once the line has been parsed. An entry whose id field is
not a number is skipped like a blank line, so it can neither resolve to
root nor shadow a well-formed entry for the same user, while an id that
parses but exceeds the bound is reported as out of range. The lookup
helpers fill in the user.User and user.Group entries themselves, the
same way that parser does, so each file is still read with a single
scanner.

Signed-off-by: Aysha Afrah Ziya <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 07:43
@aysha-afrah26

Copy link
Copy Markdown
Contributor Author

Handled the latest Copilot round, still one commit.

WithAdditionalGIDs now parses the uid as int64 the way WithUser does, so on 32-bit a uid like 4294967294 is looked up as a uid instead of being matched as a member name. The new TestWithAdditionalGIDs case fails on linux/386 without the change.

usersFromFS and groupsFromFS no longer run the moby/sys/user parser once per line. They build the entries themselves after the raw id check, field for field the way that parser does, so each file is read with a single scanner again. For a 10k-entry passwd that cuts allocations per lookup from about 48 MB to 2.2 MB.

TestWithUser now requires the expected error instead of only comparing it when one is returned.

go test ./pkg/oci/ passes natively and on linux/386, golangci-lint is clean, and vet passes for linux/386 and linux/arm.

Comment thread pkg/oci/spec_opts.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread pkg/oci/spec_opts.go
Comment on lines +1264 to +1265
if errors.Is(uidErr, errNotAnID) || errors.Is(gidErr, errNotAnID) {
continue
Comment thread pkg/oci/spec_opts.go
Comment on lines +1276 to +1279
u.Uid, _ = strconv.Atoi(string(fields[passwdUIDField]))
u.Gid, _ = strconv.Atoi(string(fields[passwdGIDField]))
if filter != nil && !filter(u) {
continue

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-pick/2.0.x Change to be cherry picked to release/2.0 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch cherry-pick/2.3.x Change to be cherry picked to release/2.3 size/L

Projects

Status: Review In Progress

Development

Successfully merging this pull request may close these issues.

8 participants