Fix: prevent signed integer overflow in OP_MSG message sizes#2693
Open
HyperPS wants to merge 6 commits intomongodb:masterfrom
Open
Fix: prevent signed integer overflow in OP_MSG message sizes#2693HyperPS wants to merge 6 commits intomongodb:masterfrom
HyperPS wants to merge 6 commits intomongodb:masterfrom
Conversation
Jibola
requested changes
Jan 31, 2026
Contributor
Jibola
left a comment
There was a problem hiding this comment.
Thank you for identifying this code improvement.
Overall, Logic looks good, but please fix formatting to match _cmessagemodule.c and confirm all length writes now use _check_int32_size.
Author
|
This fixes signed int32 overflow issues in OP_MSG and buffer size calculations by validating message and section lengths before casting to int32, preventing integer truncation and potential memory corruption. |
Co-authored-by: Jib <Jibzade@gmail.com>
Co-authored-by: Jib <Jibzade@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes in this PR
This change fixes multiple signed integer overflow risks when computing MongoDB wire protocol message lengths in the C extension (
pymongo/_cmessagemodule.c).Previously, several message size calculations were performed using
intand written directly into int32 fields without validating bounds. In edge cases involving large buffers or payloads, this could lead to signed integer truncation, incorrect message lengths, and potential memory corruption.This patch:
_check_int32_size()helper to validate all computedmessage and section sizes before downcasting to
int32_tsize_tSecurity Impact
This issue was reported via Huntr as a potential integer overflow leading to malformed MongoDB wire protocol messages. While exploitation requires crafted inputs, validating message sizes defensively prevents undefined behavior and improves robustness of the PyMongo C extension.
Huntr report: