Skip to content

fix: add bounds check for /proc/[pid]/stat fields in fillFromTIDStat#2076

Merged
shirou merged 1 commit intoshirou:masterfrom
Yanhu007:fix/process-stat-bounds-check
Apr 18, 2026
Merged

fix: add bounds check for /proc/[pid]/stat fields in fillFromTIDStat#2076
shirou merged 1 commit intoshirou:masterfrom
Yanhu007:fix/process-stat-bounds-check

Conversation

@Yanhu007
Copy link
Copy Markdown
Contributor

Fixes #2070

Problem

fillFromTIDStatWithContext accesses fields[4], fields[7], fields[14], fields[15], fields[18], and fields[22] without bounds checking. This causes an index-out-of-range panic when /proc/[pid]/stat has fewer than 23 fields.

The function already guards fields[42] at line 1073:

if len(fields) > 42 {
    iotime, err = strconv.ParseFloat(fields[42], 64)
} else {
    iotime = 0 // e.g. SmartOS containers
}

But the same protection is missing for the lower-indexed fields.

Fix

Add an early bounds check:

if len(fields) < 23 {
    return 0, 0, nil, 0, 0, 0, nil, fmt.Errorf("malformed stat file: expected at least 23 fields, got %d", len(fields))
}

This complements the fix in #1995 which added a similar check in a different code path.

fillFromTIDStatWithContext accesses fields up to index 22 without
bounds checking. This causes an index-out-of-range panic when
/proc/[pid]/stat has fewer than 23 fields, which can happen in
container environments or with non-standard kernels.

The function already guards fields[42] with a length check but
is missing the same protection for lower-indexed fields.

Fixes shirou#2070
Copy link
Copy Markdown
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

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

LGTM. Minimal and correct fix — the < 23 threshold matches the highest low-index access (fields[22]), and the early return prevents the panic cleanly.

Thanks for the fix!

@shirou shirou merged commit d6a4111 into shirou:master Apr 18, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic in process stat parsing on short /proc/[pid]/stat (missing bounds check)

2 participants