Repository navigation
Fix /proc/<pid>/stat parsing when the command name contains whitespace - #2410
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
…pace
The command name (comm) field of /proc/<pid>/stat is surrounded by
parentheses and can contain any character, including runs of spaces,
newlines and parentheses. ProcParser::ParseProcPIDStat() and
GetPIDStartTimeTicks() handled this by splitting the first line of the
file on spaces with absl::SkipWhitespace() and then shifting every field
index by the number of spaces in the name. That breaks in several ways:
- Consecutive spaces in the name are collapsed by SkipWhitespace() but
are each counted in the offset, so later fields are read from the
wrong column (or out of bounds of the expected field count).
- Only the first line is read, so a newline in the name truncates the
record and the field-count check fails.
- The field-count check does not account for spaces in the name.
GetPIDStartTimeTicks() is used to build UPIDs, so a process with such a
name could get a wrong start time or be skipped altogether.
Replace the offset arithmetic with a SplitProcPIDStat() helper that
reads the whole file, takes the command name as everything between the
first '(' and the last ')' (no later field can contain a ')'), and splits
only the remainder on whitespace. The returned fields line up with the
existing kProcStat*Field constants, so callers index them directly.
In the ASAN-only code path of GetPIDStartTimeTicks(), read the file with
fread() instead of fgets() so that names containing newlines are
handled there too, trim the buffer to the bytes actually read, and close
the FILE handle, which was previously leaked.
Add parameterized tests covering names with consecutive, leading and
trailing spaces, newlines, unbalanced parentheses and an empty name for
both ParseProcPIDStat() and GetPIDStartTimeTicks().
Fixes pixie-io#678
Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
NopeCharon
requested a deployment
to
pr-actions-approval
October 25, 2026 09:51 — with
GitHub Actions
Waiting
This branch is waiting to be deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary: Fix parsing of
/proc/<pid>/statwhen the command name contains whitespaceThe command name (
comm) field of/proc/<pid>/statis wrapped in parentheses and can contain any character, including runs of spaces, newlines and parentheses.ProcParser::ParseProcPIDStat()andGetPIDStartTimeTicks()read only the first line, split it withabsl::SkipWhitespace()and then shifted every field index by the number of spaces in the name. As described in the issue, this goes wrong when:Since
GetPIDStartTimeTicks()feeds UPID construction, such processes could end up with a wrong start time or be skipped.This PR adds a
SplitProcPIDStat()helper that reads the whole file, takes the command name as everything between the first(and the last)(no later field can contain a)), and only splits the remainder on whitespace. The returned fields line up with the existingkProcStat*Fieldconstants, so the offset arithmetic is gone. Both parsers now use it.In the ASAN-only path of
GetPIDStartTimeTicks(), the file is now read withfread()instead offgets()so names with newlines work there too, the buffer is trimmed to the bytes actually read, and theFILE*(previously leaked) is closed.Relevant Issues: Fixes #678
Type of change: /kind bug
Test Plan: Added parameterized tests to
//src/common/system:proc_parser_testthat write a stat file with names containing consecutive, leading and trailing spaces, newlines, unbalanced parentheses and an empty name, and check every parsed field for bothParseProcPIDStat()andGetPIDStartTimeTicks(). I also ran the new splitting logic in a standalone program on Linux against the real/proc/<pid>/statof child processes renamed withprctl(PR_SET_NAME)(a b,a\nb,x\n\n y,a) (b, empty, etc.). The new logic returned the correct pid, comm, thread count and start time for all of them, while the previous logic returned a wrong start time for names with consecutive spaces or newlines. clang-format and cpplint are clean on the changed files.Changelog Message: