Skip to content

user: stricter parsing - #276

Merged
kolyshkin merged 8 commits into
moby:mainfrom
kolyshkin:user-parse-fixes
Nov 17, 2026
Merged

kolyshkin merged 8 commits into
moby:mainfrom
kolyshkin:user-parse-fixes

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

This is a carry of #230 fixing a few more issues on top of it.


user: fix typo in test-name

user: split numeric ID parsing from name detection

Split parseNumeric into a strict numeric parser and a parseMaybeNumeric
wrapper that allows non-numeric values to be interpreted as user or group
names.

This allows passwd and group file parsing to reject invalid numeric IDs
without changing the behavior of callers that accept names.

user: extract accepted range validation to a helper

user: skip empty and malformed records

Skip empty and malformed records when parsing passwd and group files,
instead of accepting incomplete records, with missing or invalid
numeric fields silently parsed as 0.

Treat missing fields as empty, allowing empty string fields while
rejecting missing, non-numeric, or out-of-range numeric fields. This
aligns with glibc, which skips malformed records when reading passwd
and group files.

For UIDs and GIDs, the accepted range is the one checked by validateID.
This also rejects negative IDs (which setresuid and setresgid treat as
"do not change"), and makes the result the same on 32-bit and 64-bit
platforms. As parsed GIDs are now always in range, remove the redundant
validateID check from GetAdditionalGroups.

Since any parsing error makes the record malformed, parseLine and
parseParts now only report whether the record is valid.

Add tests for empty, incomplete, and malformed records, and document
the behavior based on the corresponding glibc implementations.

For reference:

user: ParsePasswdFilter: skip comments

ParseGroupFilter skips lines starting with '#', following glibc,
but ParsePasswdFilter does not. As a result, a commented out entry
such as "#olduser:x:0:0::/root:/bin/sh" is returned as a user named
"#olduser" with UID 0, which GetExecUser can then match by UID.

Skip comments in ParsePasswdFilter, too.

user: GetAdditionalGroups: fix panic on empty group name

For an empty string, parseGroupArg returns nil, nil, which is then
dereferenced, resulting in a nil pointer dereference panic.

Return an error instead.

user: skip leading whitespace and empty group members

glibc skips whitespace before group members and drops empty ones, so
a group line like "grp:x:1234:root, adm,,user" has three members:
"root", "adm", and "user". We kept " adm" and "" as is, so user adm
was not considered a member of grp, and GetExecUser silently omitted
the group from the supplementary groups.

Do the same as glibc: skip leading whitespace in group members, and
drop empty ones. Note that, like in glibc, trailing whitespace is kept,
so "root ,adm" has members "root " and "adm".

user: ParseSubIDFilter: validate records like shadow-utils

ParseSubIDFilter accepts records which shadow-utils rejects, such as
ones with negative or too large IDs or counts ("user:-1:65536",
"user:4294967296:1"), with an empty name, or with an extra field.

Validate records the same way as shadow-utils' subordinate_parse
does: there must be exactly three fields, the name must not be empty,
and the range must fit into 32-bit IDs. Skip records that fail the
validation.

Rename TestParseSubIDFilterOverflow to TestParseSubIDFilter, and add
more test cases.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin

Copy link
Copy Markdown
Collaborator Author

@thaJeztah @vvoland PTAL (this one supersedes #278 and #230)

@kolyshkin
kolyshkin marked this pull request as ready for review November 13, 2026 02:09
@kolyshkin
kolyshkin requested a review from vvoland November 13, 2026 07:27
Comment thread user/user.go Outdated
Comment on lines +105 to +106
// Comma-separated lists. Like glibc, skip whitespace around
// the elements, and drop empty ones.

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.

nit: glibc's parse_list only skips *eading whitespace. So a , b gives ["a ", "b"] in glibc but ["a", "b"] here.

https://sources.debian.org/src/glibc/2.42-13/nss/nss_files/files-parse.c/#L278-L291

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! I checked glibc git history and it looks like this logic is there since 1997. Changed the code to match that, PTAL.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

(leaving this unresolved so the comments are visible)

@vvoland vvoland 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.

LGTM

@thaJeztah thaJeztah 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.

changes LGTM

If that's acceptable I can rework the series to be more logical (currently it's more like fixup upon fixup)

@kolyshkin any re-organising you wanted to do before merging this, or OK to keep it as-is?

Split parseNumeric into a strict numeric parser and a parseMaybeNumeric
wrapper that allows non-numeric values to be interpreted as user or group
names.

This allows passwd and group file parsing to reject invalid numeric IDs
without changing the behavior of callers that accept names.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin

kolyshkin commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

If that's acceptable I can rework the series to be more logical (currently it's more like fixup upon fixup)

@kolyshkin any re-organising you wanted to do before merging this, or OK to keep it as-is?

I already refactored it, forgot to update the description.

I'll do some final cleanups and merge this.

thaJeztah and others added 6 commits November 16, 2026 11:25
While at it, reword the comment explaining the MaxInt32 limit.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
Skip empty and malformed records when parsing passwd and group files,
instead of accepting incomplete records, with missing or invalid
numeric fields silently parsed as 0.

Treat missing fields as empty, allowing empty string fields while
rejecting missing, non-numeric, or out-of-range numeric fields. This
aligns with glibc, which skips malformed records when reading passwd
and group files.

For UIDs and GIDs, the accepted range is the one checked by validateID.
This also rejects negative IDs (which setresuid and setresgid treat as
"do not change"), and makes the result the same on 32-bit and 64-bit
platforms. As parsed GIDs are now always in range, remove the redundant
range check from GetAdditionalGroups.

Since any parsing error makes the record malformed, parseLine and
parseParts now only report whether the record is valid.

Add tests for empty, incomplete, and malformed records, and document
the behavior based on the corresponding glibc implementations.

For reference:
- https://sources.debian.org/src/glibc/2.42-13/nss/nss_fgetent_r.c/
- https://sources.debian.org/src/glibc/2.42-13/nss/fgetpwent_r.c/

Co-authored-by: Mickael Emirkanian <[email protected]>
Co-authored-by: Kir Kolyshkin <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
ParseGroupFilter skips lines starting with '#', following glibc,
but ParsePasswdFilter does not. As a result, a commented out entry
such as "#olduser:x:0:0::/root:/bin/sh" is returned as a user named
"#olduser" with UID 0, which GetExecUser can then match by UID.

Skip comments in ParsePasswdFilter, too.

Signed-off-by: Kir Kolyshkin <[email protected]>
For an empty string, parseGroupArg returns nil, nil, which is then
dereferenced, resulting in a nil pointer dereference panic.

Return an error instead.

Signed-off-by: Kir Kolyshkin <[email protected]>
glibc skips whitespace before group members and drops empty ones, so
a group line like "grp:x:1234:root, adm,,user" has three members:
"root", "adm", and "user". We kept " adm" and "" as is, so user adm
was not considered a member of grp, and GetExecUser silently omitted
the group from the supplementary groups.

Do the same as glibc: skip leading whitespace in group members, and
drop empty ones. Note that, like in glibc, trailing whitespace is kept,
so "root ,adm" has members "root " and "adm".

Signed-off-by: Kir Kolyshkin <[email protected]>
ParseSubIDFilter accepts records which shadow-utils rejects, such as
ones with negative or too large IDs or counts ("user:-1:65536",
"user:4294967296:1"), with an empty name, or with an extra field.

Validate records the same way as shadow-utils' subordinate_parse
does: there must be exactly three fields, the name must not be empty,
and the range must fit into 32-bit IDs. Skip records that fail the
validation.

Add more test cases.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
kolyshkin merged commit 9586d2f into moby:main Nov 17, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

user: parseLine silently ignores uid/gid conversion errors, malformed ids parse as 0

3 participants