Skip to content

Commit a98898d

Browse files
committed
Harden remote debugging integration
Require the flags for turning this on to be set to exactly 1 to avoid accidentally triggering remote debugging in the case of heap corruption. Make a heap copy of the script path before using it to avoid the buffer being overwritten while we're still using it by another debugger.
1 parent 38a4d51 commit a98898d

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

Python/ceval_gil.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,13 +1387,29 @@ _Py_HandlePending(PyThreadState *tstate)
13871387

13881388
#ifdef Py_REMOTE_DEBUG
13891389
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
1390-
if (config->remote_debug) {
1391-
if (tstate->remote_debugger_support.debugger_pending_call) {
1392-
tstate->remote_debugger_support.debugger_pending_call = 0;
1393-
const char *path = tstate->remote_debugger_support.debugger_script_path;
1390+
if (config->remote_debug == 1
1391+
&& tstate->remote_debugger_support.debugger_pending_call == 1)
1392+
{
1393+
tstate->remote_debugger_support.debugger_pending_call = 0;
1394+
1395+
// Immediately make a copy in case of a race with another debugger
1396+
// process that's trying to write to the buffer. At least this way
1397+
// we'll be internally consistent: what we audit is what we run.
1398+
const size_t pathsz
1399+
= sizeof(tstate->remote_debugger_support.debugger_script_path);
1400+
1401+
char *path = PyMem_Malloc(pathsz);
1402+
if (path) {
1403+
// And don't assume the debugger correctly null terminated it.
1404+
memcpy(
1405+
path,
1406+
tstate->remote_debugger_support.debugger_script_path,
1407+
pathsz);
1408+
path[pathsz - 1] = '\0';
13941409
if (*path) {
13951410
run_remote_debugger_script(path);
13961411
}
1412+
PyMem_Free(path);
13971413
}
13981414
}
13991415
#endif

0 commit comments

Comments
 (0)