Skip to content

Fix /proc/<pid>/stat parsing when the command name contains whitespace - #2410

Open
NopeCharon wants to merge 1 commit into
pixie-io:mainfrom
NopeCharon:fix-proc-pid-stat-comm-parsing
Open

NopeCharon wants to merge 1 commit into
pixie-io:mainfrom
NopeCharon:fix-proc-pid-stat-comm-parsing

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

Summary: Fix parsing of /proc/<pid>/stat when the command name contains whitespace

The command name (comm) field of /proc/<pid>/stat is wrapped in parentheses and can contain any character, including runs of spaces, newlines and parentheses. ProcParser::ParseProcPIDStat() and GetPIDStartTimeTicks() read only the first line, split it with absl::SkipWhitespace() and then shifted every field index by the number of spaces in the name. As described in the issue, this goes wrong when:

  • the name has consecutive spaces: they are collapsed by the split but each one is counted in the offset, so later fields are read from the wrong column;
  • the name has a newline: the record is truncated and the field-count check fails;
  • the field-count check itself does not account for spaces in the name.

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 existing kProcStat*Field constants, so the offset arithmetic is gone. Both parsers now use it.

In the ASAN-only path of GetPIDStartTimeTicks(), the file is now read with fread() instead of fgets() so names with newlines work there too, the buffer is trimmed to the bytes actually read, and the FILE* (previously leaked) is closed.

Relevant Issues: Fixes #678

Type of change: /kind bug

Test Plan: Added parameterized tests to //src/common/system:proc_parser_test that 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 both ParseProcPIDStat() and GetPIDStartTimeTicks(). I also ran the new splitting logic in a standalone program on Linux against the real /proc/<pid>/stat of child processes renamed with prctl(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:

Fixed process stats and UPID start time parsing for processes whose name contains consecutive spaces or newlines.

…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
NopeCharon requested a review from a team as a code owner October 25, 2026 09:51

This branch is waiting to be deployed

1 waiting deployment
pr-actions-approval — fc3f5a5c Waiting Oct 25, 2026 by NopeCharon via authorize #3140
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.

/proc/PID/stat mishandles whitespace

1 participant