Repository navigation
pkg/oci: reject out-of-range uid/gid from image user database - #13797
aysha-afrah26 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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
validUserIDbound 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.
|
/cherry-pick release/2.3 |
|
@AkihiroSuda: once the present PR merges, I will cherry-pick it on top of DetailsIn response to this:
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. |
|
|
||
| // 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 |
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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.
There was a problem hiding this comment.
pkg/oci should accept whatever OCI accepts, i.e., MaxUint32-1
There was a problem hiding this comment.
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.
1807f56 to
745b9a7
Compare
| if !validUserID(g.Gid) { | ||
| return fmt.Errorf("group %q has gid %d out of range", g.Name, g.Gid) | ||
| } |
|
Please squash the commits |
| // 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 |
f6dad69 to
ba35c39
Compare
|
Done, squashed into a single commit. The two CI failures on the last run were the flaky TestCRIImagePullTimeout, unrelated to this change. |
|
Please check copilot comments |
ba35c39 to
339d966
Compare
|
Went through the remaining comments and pushed an update, still as one commit:
Verified with go test ./pkg/oci/ plus build and vet for linux/arm and linux/386. |
|
gentle ping |
fuweid
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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)
}
}This package is not correct to me.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
2c3c9e9 to
fd4f24e
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Numeric GIDs can incorrectly trigger name-lookup errors, and valid 32-bit regression coverage is skipped.
4 open findings
Exclude numeric arguments from name lookup · New Remove obsolete 32-bit skip to preserve range-check coverage · New Minor: gofmt would normally insert spaces around the binary operator here ("math.MaxUint32 - 1").… The new out-of-range check for group names in WithAppendAdditionalGroups (reading /etc/group and…
8 resolved since last review
This range check can be bypassed on 32-bit builds because github.com/moby/sys/user’s parseLine…WithAppendAdditionalGroupstreats a numeric GID that overflowsParseUint(..., 32)(e.g.… This assertion uses the untyped constant 4294967294 as an interface{} argument, which will default… With this new validation you’re effectively treating 4294967295 (math.MaxUint32) as out-of-range… This numeric-UID parsing uses strconv.Atoi (int-sized), so on 32-bit builds valid OCI-range IDs… This branch also uses strconv.Atoi (int-sized), so valid OCI-range IDs above MaxInt32 will be… WithAppendAdditionalGroups now rejects out-of-range GIDs when a group is resolved via /etc/group,… WithAppendAdditionalGroups now rejects out-of-range GIDs when resolving a group name from…
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
| if testCase.skipOn32Bit && strconv.IntSize == 32 { | ||
| t.Skip("gids parsed from /etc/group saturate to MaxInt32 on 32-bit platforms") | ||
| } |
fd4f24e to
4113d50
Compare
There was a problem hiding this comment.
🟡 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
Regression test fails to enforce expected error · New Exclude numeric arguments from name lookup Remove obsolete 32-bit skip to preserve range-check coverage Minor: gofmt would normally insert spaces around the binary operator here ("math.MaxUint32 - 1").… The new out-of-range check for group names in WithAppendAdditionalGroups (reading /etc/group and…
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
4113d50 to
07e3175
Compare
|
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 |
There was a problem hiding this comment.
🟡 Changes recommended
Supplementary-group parsing remains inconsistent on 32-bit platforms, per-row scanners increase allocations, and rejection assertions need strengthening.
6 open findings
Fix large UID parsing on 32-bit platforms · New Reuse scanner buffers during passwd and group parsing · New Strengthen tests to require expected rejection errors · New Remove obsolete 32-bit skip to preserve range-check coverage Minor: gofmt would normally insert spaces around the binary operator here ("math.MaxUint32 - 1").… The new out-of-range check for group names in WithAppendAdditionalGroups (reading /etc/group and…
2 resolved since last review
🧠 Review effort: Balanced
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]>
07e3175 to
0088a86
Compare
|
Handled the latest Copilot round, still one commit.
|
There was a problem hiding this comment.
🟡 Changes recommended
A matching passwd entry with an invalid GID can silently fall back to GID 0, while 32-bit ID narrowing causes missed matches and false matches in user and group lookups.
5 open issues
Reject matching users with invalid GIDs instead of falling back · New Prevent UID narrowing from causing lookup misses and collisions · New Remove obsolete 32-bit skip to preserve range-check coverage Minor: gofmt would normally insert spaces around the binary operator here ("math.MaxUint32 - 1").… The new out-of-range check for group names in WithAppendAdditionalGroups (reading /etc/group and…
3 resolved since last review
Review effort: Balanced
| if errors.Is(uidErr, errNotAnID) || errors.Is(gidErr, errNotAnID) { | ||
| continue |
| u.Uid, _ = strconv.Atoi(string(fields[passwdUIDField])) | ||
| u.Gid, _ = strconv.Atoi(string(fields[passwdGIDField])) | ||
| if filter != nil && !filter(u) { | ||
| continue |



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.