Repository navigation
user: stricter parsing - #276
Conversation
8254db7 to
5991029
Compare
Signed-off-by: Sebastiaan van Stijn <[email protected]> Signed-off-by: Kir Kolyshkin <[email protected]>
5991029 to
b93e973
Compare
|
@thaJeztah @vvoland PTAL (this one supersedes #278 and #230) |
| // Comma-separated lists. Like glibc, skip whitespace around | ||
| // the elements, and drop empty ones. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Good catch! I checked glibc git history and it looks like this logic is there since 1997. Changed the code to match that, PTAL.
There was a problem hiding this comment.
(leaving this unresolved so the comments are visible)
b93e973 to
335ef47
Compare
thaJeztah
left a comment
There was a problem hiding this comment.
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]>
I already refactored it, forgot to update the description. I'll do some final cleanups and merge this. |
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]>
335ef47 to
08a7aa2
Compare
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.