Skip to content

Commit

Permalink
PYTHON-4305 Fix bson size check (#1564)
Browse files Browse the repository at this point in the history
(cherry picked from commit 372b5d6)
  • Loading branch information
ShaneHarvey authored and blink1073 committed Mar 27, 2024
1 parent 449d0f3 commit 56b6b6d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions bson/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,7 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
uint32_t c_w_s_size;
uint32_t code_size;
uint32_t scope_size;
uint32_t len;
PyObject* code;
PyObject* scope;
PyObject* code_type;
Expand All @@ -2424,7 +2425,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
memcpy(&code_size, buffer + *position, 4);
code_size = BSON_UINT32_FROM_LE(code_size);
/* code_w_scope length + code length + code + scope length */
if (!code_size || max < code_size || max < 4 + 4 + code_size + 4) {
len = 4 + 4 + code_size + 4;
if (!code_size || max < code_size || max < len || len < code_size) {
goto invalid;
}
*position += 4;
Expand All @@ -2442,12 +2444,9 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,

memcpy(&scope_size, buffer + *position, 4);
scope_size = BSON_UINT32_FROM_LE(scope_size);
if (scope_size < BSON_MIN_SIZE) {
Py_DECREF(code);
goto invalid;
}
/* code length + code + scope length + scope */
if ((4 + code_size + 4 + scope_size) != c_w_s_size) {
len = 4 + 4 + code_size + scope_size;
if (scope_size < BSON_MIN_SIZE || len != c_w_s_size || len < scope_size) {
Py_DECREF(code);
goto invalid;
}
Expand Down

0 comments on commit 56b6b6d

Please sign in to comment.