Skip to content

[packetbeat] MongoDB parser fix bounds checks#47925

Merged
stanek-michal merged 6 commits intomainfrom
mongodb-parser-fixes
Dec 15, 2025
Merged

[packetbeat] MongoDB parser fix bounds checks#47925
stanek-michal merged 6 commits intomainfrom
mongodb-parser-fixes

Conversation

@stanek-michal
Copy link
Contributor

@stanek-michal stanek-michal commented Dec 4, 2025

Proposed commit message

packetbeat: fix bounds checking in mongodb protocol parser

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works. Where relevant, I have used the stresstest.sh script to run them under stress conditions and race detector to verify their stability.
  • I have added an entry in ./changelog/fragments using the changelog tool.

How to test this PR locally

cd packetbeat/protos/mongodb && go test -v ./...

@stanek-michal stanek-michal requested a review from a team as a code owner December 4, 2025 16:43
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Dec 4, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2025

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
@mergify
Copy link
Contributor

mergify bot commented Dec 4, 2025

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @stanek-michal? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.
@stanek-michal stanek-michal added bug Team:Security-Linux Platform Linux Platform Team in Security Solution labels Dec 4, 2025
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Dec 4, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/sec-linux-platform (Team:Security-Linux Platform)

@stanek-michal stanek-michal added backport-8.19 Automated backport to the 8.19 branch backport-9.1 Automated backport to the 9.1 branch backport-9.2 Automated backport to the 9.2 branch labels Dec 4, 2025
Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost
Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength
Copy link
Contributor

@nicholasberlin nicholasberlin left a comment

Choose a reason for hiding this comment

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

Assuming my comments about int( are bogus, LGTM

Copy link
Contributor

@nfritts nfritts left a comment

Choose a reason for hiding this comment

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

Any logging that can be triggered by "Every" packet should be Debug at the highest.

@stanek-michal
Copy link
Contributor Author

Any logging that can be triggered by "Every" packet should be Debug at the highest.

good point, fixed.

@stanek-michal stanek-michal merged commit 306d2f4 into main Dec 15, 2025
47 checks passed
@stanek-michal stanek-michal deleted the mongodb-parser-fixes branch December 15, 2025 16:50
mergify bot pushed a commit that referenced this pull request Dec 15, 2025
* [Packetbeat] Fix bounds checking in MongoDB protocol parser

Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost

* [Packetbeat] Add more MongoDB protocol parser tests

Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength

* update changelog with packetbeat mongodb changes

* Switch logging to debug on invalid packets

* formatting

* nolint for binary parsing

(cherry picked from commit 306d2f4)
mergify bot pushed a commit that referenced this pull request Dec 15, 2025
* [Packetbeat] Fix bounds checking in MongoDB protocol parser

Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost

* [Packetbeat] Add more MongoDB protocol parser tests

Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength

* update changelog with packetbeat mongodb changes

* Switch logging to debug on invalid packets

* formatting

* nolint for binary parsing

(cherry picked from commit 306d2f4)
mergify bot pushed a commit that referenced this pull request Dec 15, 2025
* [Packetbeat] Fix bounds checking in MongoDB protocol parser

Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost

* [Packetbeat] Add more MongoDB protocol parser tests

Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength

* update changelog with packetbeat mongodb changes

* Switch logging to debug on invalid packets

* formatting

* nolint for binary parsing

(cherry picked from commit 306d2f4)
pierrehilbert pushed a commit that referenced this pull request Dec 22, 2025
* [Packetbeat] Fix bounds checking in MongoDB protocol parser

Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost

* [Packetbeat] Add more MongoDB protocol parser tests

Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength

* update changelog with packetbeat mongodb changes

* Switch logging to debug on invalid packets

* formatting

* nolint for binary parsing

(cherry picked from commit 306d2f4)

Co-authored-by: Michal Stanek <75310947+stanek-michal@users.noreply.github.com>
pierrehilbert pushed a commit that referenced this pull request Dec 22, 2025
* [Packetbeat] Fix bounds checking in MongoDB protocol parser

Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost

* [Packetbeat] Add more MongoDB protocol parser tests

Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength

* update changelog with packetbeat mongodb changes

* Switch logging to debug on invalid packets

* formatting

* nolint for binary parsing

(cherry picked from commit 306d2f4)

Co-authored-by: Michal Stanek <75310947+stanek-michal@users.noreply.github.com>
pierrehilbert pushed a commit that referenced this pull request Dec 22, 2025
* [Packetbeat] Fix bounds checking in MongoDB protocol parser

Issues addressed:
- Negative message length causing panic in truncate()
- Negative BSON document length causing slice bounds panic
- Negative or excessive numberReturned in OP_REPLY causing allocation panic
- Negative document sequence size in OP_MSG causing integer overflow
- Error handling gap in OP_QUERY where first readDocument error was lost

* [Packetbeat] Add more MongoDB protocol parser tests

Add tests verifying that malformed packets are properly
rejected.

Test coverage added:
- Negative message length (0x80000000)
- Message length smaller than header (< 16 bytes)
- Negative BSON document length (-1)
- Zero BSON document length
- Negative numberReturned in OP_REPLY
- Excessively large numberReturned (0x3FFFFFFF)
- Negative OP_MSG document sequence size
- OP_MSG sequence size exceeding buffer
- Valid OP_REPLY parsing (regression test)

All tests use deferred recover() to catch any panics that would
indicate the fixes are not working properly.

---
How to Test

cd packetbeat/protos/mongodb && go test -v ./...

cd packetbeat/protos/mongodb && go test -race -v ./...

cd packetbeat/protos/mongodb && go test -v -run TestMongodbParser_negativeMessageLength

* update changelog with packetbeat mongodb changes

* Switch logging to debug on invalid packets

* formatting

* nolint for binary parsing

(cherry picked from commit 306d2f4)

Co-authored-by: Michal Stanek <75310947+stanek-michal@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-8.19 Automated backport to the 8.19 branch backport-9.1 Automated backport to the 9.1 branch backport-9.2 Automated backport to the 9.2 branch bug Team:Security-Linux Platform Linux Platform Team in Security Solution

4 participants