Skip to content

fix(logger): validate ring-buffer sample bounds before decoding - #1972

Open
vikash2006-dot wants to merge 1 commit into
kmesh-net:mainfrom
vikash2006-dot:fix/ringbuf-log-decode-bounds
Open

vikash2006-dot wants to merge 1 commit into
kmesh-net:mainfrom
vikash2006-dot:fix/ringbuf-log-decode-bounds

Conversation

@vikash2006-dot

@vikash2006-dot vikash2006-dot commented Oct 20, 2026 •

Copy link
Copy Markdown

What type of PR is this?
/kind bug

What this PR does / why we need it:
pkg/logger.decodeRecord previously performed unchecked slice operations on raw ring-buffer event samples (km_log_event). A truncated sample with fewer than 4 bytes, a sample specifying a zero message length, or a sample advertising a length exceeding the buffer bounds caused decodeRecord to panic with a slice bounds out of range error.

Additionally, handleLogEvents did not skip processing when decodeRecord returned an error, immediately falling through to dereference le.Msg on a nil le pointer, crashing the daemon's log reader goroutine.

This PR fixes these issues by:

  • Validating that the sample buffer has at least 4 bytes (recordLenSize) before reading the length prefix.
  • Validating that the message length is at least 1 byte (recordNullSize for the null terminator), rejecting zero-length payloads.
  • Using 64-bit comparisons (uint64(len(data)) < uint64(recordLenSize)+uint64(lenOfMsg)) to prevent 32-bit integer overflows when checking that the advertised payload fits within the sample.
  • Adding a continue guard on decode failure in handleLogEvents so malformed events are logged and skipped, allowing the log reader to continue processing subsequent events.
  • Adding comprehensive table-driven unit tests in pkg/logger/logger_test.go covering empty slices, short samples (<4 bytes), zero message length, oversized lengths, math.MaxUint32 boundary handling, empty messages (\x00), valid messages, and padded ring-buffer records.

Which issue(s) this PR fixes:
Fixes #1777

Special notes for your reviewer:
Please pay particular attention to the bounds checks in decodeRecord() and the error handling in handleLogEvents(). The added tests cover truncated samples, zero-length messages, oversized lengths, integer boundary cases, and valid records. Feedback on any edge cases or potential regressions is welcome.

Does this PR introduce a user-facing change?:

NONE

@kmesh-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign lizhencheng9527 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot

Copy link
Copy Markdown
Collaborator

Welcome @vikash2006-dot! It looks like this is your first PR to kmesh-net/kmesh 🎉

pkg/logger.decodeRecord previously performed unchecked slice operations on
raw ring-buffer event samples. If a sample was shorter than 4 bytes,
advertised a zero message length, or specified a message length exceeding
the sample buffer, decodeRecord panicked with a slice bounds out of range error.

Additionally, handleLogEvents did not skip further processing when decodeRecord
failed, causing a nil pointer dereference when accessing le.Msg and killing
the daemon log reader goroutine.

This commit:
- Validates the sample has at least 4 bytes to read the message length prefix.
- Validates that the message length is at least 1 byte (including '\0').
- Uses 64-bit comparisons to prevent integer overflow when verifying that the
  advertised message fits within the sample slice.
- Adds a continue guard on decode failure in handleLogEvents so later events
  can still be read.
- Adds comprehensive table-driven unit tests for decodeRecord covering empty,
  short, zero-length, oversized, max uint32, and valid samples.

Signed-off-by: Vikash Kumar <vk2542883@gmail.com>
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 40.11%. Comparing base (9d3eb5c) to head (9b7280f).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
pkg/logger/logger.go 90.90% 1 Missing ⚠️
Files with missing lines Coverage Δ
pkg/logger/logger.go 40.54% <90.90%> (+40.54%) ⬆️

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ab3deff...9b7280f. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed ring-buffer samples can panic the daemon log reader

2 participants