-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
gh-157227: _remote_debugging: Validate lists and tuples in write_sample
#157228
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,22 @@ | |
| } \ | ||
| } while (0) | ||
|
|
||
| #define CHECK_TUPLE_ITEMS(obj, n) do { \ | ||
| if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) < (n)) { \ | ||
| PyErr_Format(PyExc_TypeError, \ | ||
| #obj " must be a tuple of at least %zd items", \ | ||
| (Py_ssize_t)(n)); \ | ||
| return -1; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #define CHECK_LIST(obj) do { \ | ||
| if (!PyList_Check(obj)) { \ | ||
| PyErr_SetString(PyExc_TypeError, #obj " must be a list"); \ | ||
| return -1; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| /* ============================================================================ | ||
| * WRITER-SPECIFIC UTILITY HELPERS | ||
| * ============================================================================ */ | ||
|
|
@@ -838,8 +854,8 @@ build_frame_stack(BinaryWriter *writer, PyObject *frame_list, | |
| *curr_depth = (stack_depth < MAX_STACK_DEPTH) ? stack_depth : MAX_STACK_DEPTH; | ||
|
|
||
| for (Py_ssize_t k = 0; k < (Py_ssize_t)*curr_depth; k++) { | ||
| /* Use unchecked accessors since we control the data structures */ | ||
| PyObject *frame_info = PyList_GET_ITEM(frame_list, k); | ||
| CHECK_TUPLE_ITEMS(frame_info, 4); | ||
|
|
||
| /* Get filename, location, funcname, opcode from FrameInfo using unchecked access */ | ||
| PyObject *filename = PyStructSequence_GET_ITEM(frame_info, 0); | ||
|
|
@@ -854,20 +870,13 @@ build_frame_stack(BinaryWriter *writer, PyObject *frame_list, | |
| int32_t end_column = LOCATION_NOT_AVAILABLE; | ||
|
|
||
| if (location != Py_None) { | ||
| CHECK_TUPLE_ITEMS(location, 4); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| /* LocationInfo is a struct sequence or tuple with: | ||
| * (lineno, end_lineno, column, end_column) */ | ||
| PyObject *lineno_obj = PyTuple_Check(location) ? | ||
| PyTuple_GET_ITEM(location, 0) : | ||
| PyStructSequence_GET_ITEM(location, 0); | ||
| PyObject *end_lineno_obj = PyTuple_Check(location) ? | ||
| PyTuple_GET_ITEM(location, 1) : | ||
| PyStructSequence_GET_ITEM(location, 1); | ||
| PyObject *column_obj = PyTuple_Check(location) ? | ||
| PyTuple_GET_ITEM(location, 2) : | ||
| PyStructSequence_GET_ITEM(location, 2); | ||
| PyObject *end_column_obj = PyTuple_Check(location) ? | ||
| PyTuple_GET_ITEM(location, 3) : | ||
| PyStructSequence_GET_ITEM(location, 3); | ||
| PyObject *lineno_obj = PyTuple_GET_ITEM(location, 0); | ||
| PyObject *end_lineno_obj = PyTuple_GET_ITEM(location, 1); | ||
| PyObject *column_obj = PyTuple_GET_ITEM(location, 2); | ||
| PyObject *end_column_obj = PyTuple_GET_ITEM(location, 3); | ||
|
|
||
| PYLONG_TO_INT32_OR_DEFAULT(lineno_obj, lineno, LOCATION_NOT_AVAILABLE); | ||
| PYLONG_TO_INT32_OR_DEFAULT(end_lineno_obj, end_lineno, LOCATION_NOT_AVAILABLE); | ||
|
|
@@ -925,9 +934,11 @@ static int | |
| process_thread_sample(BinaryWriter *writer, PyObject *thread_info, | ||
| uint32_t interpreter_id, uint64_t timestamp_us) | ||
| { | ||
| CHECK_TUPLE_ITEMS(thread_info, 3); | ||
| PyObject *thread_id_obj = PyStructSequence_GET_ITEM(thread_info, 0); | ||
| PyObject *status_obj = PyStructSequence_GET_ITEM(thread_info, 1); | ||
| PyObject *frame_list = PyStructSequence_GET_ITEM(thread_info, 2); | ||
| CHECK_LIST(frame_list); | ||
|
|
||
| uint64_t thread_id = PyLong_AsUnsignedLongLong(thread_id_obj); | ||
| if (thread_id == (uint64_t)-1 && PyErr_Occurred()) { | ||
|
|
@@ -1010,17 +1021,16 @@ process_thread_sample(BinaryWriter *writer, PyObject *thread_info, | |
| int | ||
| binary_writer_write_sample(BinaryWriter *writer, PyObject *stack_frames, uint64_t timestamp_us) | ||
| { | ||
| if (!PyList_Check(stack_frames)) { | ||
| PyErr_SetString(PyExc_TypeError, "stack_frames must be a list"); | ||
| return -1; | ||
| } | ||
| CHECK_LIST(stack_frames); | ||
|
|
||
| Py_ssize_t num_interpreters = PyList_GET_SIZE(stack_frames); | ||
| for (Py_ssize_t i = 0; i < num_interpreters; i++) { | ||
| PyObject *interp_info = PyList_GET_ITEM(stack_frames, i); | ||
| CHECK_TUPLE_ITEMS(interp_info, 2); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We validate as we go, so a bad thread in the middle leaves the earlier threads already written and |
||
|
|
||
| PyObject *interp_id_obj = PyStructSequence_GET_ITEM(interp_info, 0); | ||
| PyObject *threads = PyStructSequence_GET_ITEM(interp_info, 1); | ||
| CHECK_LIST(threads); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This narrows the hole but does not close it. threads = []
class Evil:
def __index__(self):
del threads[:]
return 0
threads.extend([(1, Evil(), []), (2, 0, [])])
_remote_debugging.BinaryWriter("/tmp/o.bin", 1000, 0, compression=0).write_sample([(0, threads)], 2000)This still segfaults on |
||
|
|
||
| unsigned long interp_id_long = PyLong_AsUnsignedLong(interp_id_obj); | ||
| if (interp_id_long == (unsigned long)-1 && PyErr_Occurred()) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Truth be told, we reinvent similar check over and over:
cpython/Objects/memoryobject.c
Lines 217 to 222 in 9a75080
cpython/Modules/_testbuffer.c
Lines 26 to 33 in 9a75080
Sometimes without a macro:
cpython/Modules/_io/stringio.c
Lines 922 to 930 in 9a75080
cpython/Python/_warnings.c
Lines 450 to 465 in 9a75080