Skip to content

libpcp: fix stack buffer overflow in __pmLogLoadMeta pmDesc name read - #2682

Open
xiejing-dev wants to merge 1 commit into
performancecopilot:mainfrom
xiejing-dev:fix-logmeta-desc-name-overflow
Open

libpcp: fix stack buffer overflow in __pmLogLoadMeta pmDesc name read#2682
xiejing-dev wants to merge 1 commit into
performancecopilot:mainfrom
xiejing-dev:fix-logmeta-desc-name-overflow

Conversation

@xiejing-dev

Copy link
Copy Markdown
Contributor

Pull Request Description

A TYPE_DESC metadata record in a PCP archive stores each metric name as a length-prefixed string. __pmLogLoadMeta() read the 4-byte length, byte-swapped it with ntohl(), and used it directly as the count for

__pmFread(name, 1, len, f);
name[len] = '\0';

where name is a fixed char name[MAXPATHLEN] (4096-byte) stack buffer, with no upper-bound check. A crafted archive setting the name length > 4096 overflows the buffer and smashes the stack (observed: SIGABRT from __stack_chk_fail).

Add a bounds check that rejects len < 0 or len >= MAXPATHLEN with PM_ERR_LOGREC ("Corrupted record in a PCP archive").

QA: add test 2110 (crafts a malformed archive with an oversized pmDesc name length and asserts pmlogcheck/pminfo reject it gracefully instead of crashing).

Verify

logmeta_overflow_check.sh
See the testing script above. It is similar to the QA/2110 change added in this commit.

Before the fix, run it :

[root@localhost pcp-master-notfixed]# sh logmeta_overflow_check.sh
pmlogcheck = src/pmlogcheck/pmlogcheck
pminfo     = src/pminfo/pminfo
libpcp     = src/libpcp/src/libpcp.so.4
archive    = (crafted) oversized pmDesc name length = 8192

=== pmlogcheck (archive) ===
logmeta_overflow_check.sh: line 69: 1589057 Segmentation fault      (core dumped) $PMC $T/evil > "$T/out.txt" 2>&1
result: CRASHED (killed by signal 11: SEGV)
output:

=== pminfo -a (archive) ===
logmeta_overflow_check.sh: line 76: 1589074 Segmentation fault      (core dumped) $PMI -a $T/evil > "$T/out.txt" 2>&1
result: CRASHED (killed by signal 11: SEGV)
output:

verdict:
  CRASHED            -> unpatched build (overflow not guarded)
  exited cleanly (1) -> patched build (graceful 'Corrupted record in a PCP archive')

After this fix,run it:

[root@localhost pcp]# sh logmeta_overflow_check.sh
pmlogcheck = src/pmlogcheck/pmlogcheck
pminfo     = src/pminfo/pminfo
libpcp     = src/libpcp/src/libpcp.so.4
archive    = (crafted) oversized pmDesc name length = 8192

=== pmlogcheck (archive) ===
result: exited cleanly (code 1)
output:
    pmlogcheck: cannot open archive "TMP/evil": Corrupted record in a PCP archive
    Checking abandoned.

=== pminfo -a (archive) ===
result: exited cleanly (code 1)
output:
    pminfo: Cannot open archive "TMP/evil": Corrupted record in a PCP archive

verdict:
  CRASHED            -> unpatched build (overflow not guarded)
  exited cleanly (1) -> patched build (graceful 'Corrupted record in a PCP archive')
[root@localhost pcp]#

Result: the overflow is now caught -- the tools exit cleanly (code 1) with Corrupted record in a PCP archive instead of crashing.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Repository UI (inherited), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: b09be9de-006a-4eed-9da2-fb0c002afe8c

📥 Commits

Reviewing files that changed from the base of the PR and between 918d12b and 794a244.

⛔ Files ignored due to path filters (1)
  • qa/2110.out is excluded by !**/*.out
📒 Files selected for processing (3)
  • qa/2110
  • qa/group
  • src/libpcp/src/logmeta.c
🚧 Files skipped from review as they are similar to previous changes (3)
  • qa/group
  • src/libpcp/src/logmeta.c
  • qa/2110

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation of metric metadata in PCP archives.
    • Malformed archives with invalid or oversized metric names are now rejected gracefully instead of risking a crash.
    • Added coverage to verify safe behavior when inspecting corrupted archives.

Walkthrough

The metadata loader now rejects negative or oversized metric name lengths with PM_ERR_LOGREC. QA 2110 creates a malformed archive and verifies that pmlogcheck and pminfo -a exit without crashing.

Changes

Malformed PCP metadata handling

Layer / File(s) Summary
Metadata name length validation
src/libpcp/src/logmeta.c
__pmLogLoadMeta validates metric name lengths before reading the fixed-size buffer. Invalid lengths produce PM_ERR_LOGREC.
Malformed archive regression test
qa/2110, qa/group
QA 2110 creates an archive with an 8192-byte metadata name, runs pmlogcheck and pminfo -a, and associates the test with the relevant groups.

Possibly related PRs

Poem

A rabbit found a name too wide,
The loader checked the record inside.
PM_ERR_LOGREC closed the door,
QA confirmed the tools ran more.
No crash disturbed the archive trail—
Just safe rejection, clean and hale.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the stack buffer overflow fix in __pmLogLoadMeta.
Description check ✅ Passed The description accurately explains the overflow, the bounds check, and the added QA coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
qa/2110 (1)

45-66: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover the negative-length input.

The fixture writes only EVIL = 8192. It exercises the len >= MAXPATHLEN branch but not the len < 0 branch added in src/libpcp/src/logmeta.c at Line 889. Add a second malformed record with a signed length such as -1 and run both commands against it.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@qa/2110` around lines 45 - 66, Extend the malformed metadata fixture
generation around the existing EVIL record to also create a record with a signed
name length of -1, then write its corresponding archive files and run both
validation commands against that fixture. Preserve the existing 8192-length case
so both the len >= MAXPATHLEN and len < 0 branches in logmeta handling are
exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@qa/2110`:
- Around line 68-76: Update the pmlogcheck and pminfo checks in qa/2110 to
capture each command’s exit status while still sending their stdout and stderr
through _filter. Validate both statuses against the expected results, propagate
failure for signals or unexpected success, and only assign status=0 after both
checks pass.

---

Nitpick comments:
In `@qa/2110`:
- Around line 45-66: Extend the malformed metadata fixture generation around the
existing EVIL record to also create a record with a signed name length of -1,
then write its corresponding archive files and run both validation commands
against that fixture. Preserve the existing 8192-length case so both the len >=
MAXPATHLEN and len < 0 branches in logmeta handling are exercised.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Repository UI (inherited), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 8049d0ea-6cc3-4e1b-a2ca-3376305d8bff

📥 Commits

Reviewing files that changed from the base of the PR and between aecf52c and 654bbf1.

⛔ Files ignored due to path filters (1)
  • qa/2110.out is excluded by !**/*.out
📒 Files selected for processing (3)
  • qa/2110
  • qa/group
  • src/libpcp/src/logmeta.c

Comment thread qa/2110
A TYPE_DESC metadata record in a PCP archive stores each metric name as a
length-prefixed string.  __pmLogLoadMeta() read the 4-byte length, byte-swapped
it with ntohl(), and used it directly as the count for

    __pmFread(name, 1, len, f);
    name[len] = '\0';

where name is a fixed char name[MAXPATHLEN] (4096-byte) stack buffer, with no
upper-bound check.  A crafted archive setting the name length > 4096 overflows
the buffer and smashes the stack (observed: SIGABRT from __stack_chk_fail).

Add a bounds check that rejects len < 0 or len >= MAXPATHLEN with PM_ERR_LOGREC
("Corrupted record in a PCP archive").

QA: add test 2110 (crafts a malformed archive with an oversized pmDesc name
length and asserts pmlogcheck/pminfo reject it gracefully instead of crashing).
@xiejing-dev
xiejing-dev force-pushed the fix-logmeta-desc-name-overflow branch from 654bbf1 to 794a244 Compare August 7, 2026 07:58
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@xiejing-dev

Copy link
Copy Markdown
Contributor Author

The CI failure appears unrelated to this PR: QA 338 crossed UTC midnight, causing inconsistent TODAY/YESTERDAY archive names. It looks like the failed job needs to be rerun.

@natoscott

Copy link
Copy Markdown
Member

@xiejing-dev agreed - I've kicked it off again. The test has an existing 10 minute proximity-to-midnight guard, perhaps that's still too close somehow.

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.

2 participants