Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add nullptr checks for new_pyobject_id [backport 2.9] #10135

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions ddtrace/appsec/_iast/_taint_tracking/Utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,65 @@ PyObjectToString(PyObject* obj)
PyObject*
new_pyobject_id(PyObject* tainted_object)
{
if (!tainted_object)
return nullptr;

if (PyUnicode_Check(tainted_object)) {
PyObject* empty_unicode = PyUnicode_New(0, 127);
if (!empty_unicode)
return tainted_object;
PyObject* val = Py_BuildValue("(OO)", tainted_object, empty_unicode);
if (!val) {
Py_XDECREF(empty_unicode);
return tainted_object;
}
PyObject* result = PyUnicode_Join(empty_unicode, val);
Py_DecRef(empty_unicode);
Py_DecRef(val);
if (!result) {
result = tainted_object;
}
Py_XDECREF(empty_unicode);
Py_XDECREF(val);
return result;
}
if (PyBytes_Check(tainted_object)) {
PyObject* empty_bytes = PyBytes_FromString("");
auto bytes_join_ptr = py::reinterpret_borrow<py::bytes>(empty_bytes).attr("join");
auto val = Py_BuildValue("(OO)", tainted_object, empty_bytes);
auto res = PyObject_CallFunctionObjArgs(bytes_join_ptr.ptr(), val, NULL);
Py_DecRef(val);
Py_DecRef(empty_bytes);
if (!empty_bytes)
return tainted_object;

const auto bytes_join_ptr = py::reinterpret_borrow<py::bytes>(empty_bytes).attr("join");
const auto val = Py_BuildValue("(OO)", tainted_object, empty_bytes);
if (!val or !bytes_join_ptr.ptr()) {
Py_XDECREF(empty_bytes);
return tainted_object;
}

const auto res = PyObject_CallFunctionObjArgs(bytes_join_ptr.ptr(), val, NULL);
Py_XDECREF(val);
Py_XDECREF(empty_bytes);
return res;
} else if (PyByteArray_Check(tainted_object)) {
PyObject* empty_bytes = PyBytes_FromString("");
if (!empty_bytes)
return tainted_object;

PyObject* empty_bytearray = PyByteArray_FromObject(empty_bytes);
auto bytearray_join_ptr = py::reinterpret_borrow<py::bytes>(empty_bytearray).attr("join");
auto val = Py_BuildValue("(OO)", tainted_object, empty_bytearray);
auto res = PyObject_CallFunctionObjArgs(bytearray_join_ptr.ptr(), val, NULL);
Py_DecRef(val);
Py_DecRef(empty_bytes);
Py_DecRef(empty_bytearray);
if (!empty_bytearray) {
Py_XDECREF(empty_bytes);
return tainted_object;
}

const auto bytearray_join_ptr = py::reinterpret_borrow<py::bytes>(empty_bytearray).attr("join");
const auto val = Py_BuildValue("(OO)", tainted_object, empty_bytearray);
if (!val or !bytearray_join_ptr.ptr()) {
Py_XDECREF(empty_bytes);
Py_XDECREF(empty_bytearray);
return tainted_object;
}

const auto res = PyObject_CallFunctionObjArgs(bytearray_join_ptr.ptr(), val, NULL);
Py_XDECREF(val);
Py_XDECREF(empty_bytes);
Py_XDECREF(empty_bytearray);
return res;
}
return tainted_object;
Expand All @@ -121,4 +155,4 @@ get_pyobject_size(PyObject* obj)
len_candidate_text = PyByteArray_Size(obj);
}
return len_candidate_text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Code Security: add null pointer checks when creating new objects ids.
Loading