You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Executes a block of Python code in a given remote Python process.
248
260
249
261
Args:
250
262
pid (int): The process ID of the target Python process.
251
263
code (str): A string containing the Python code to be executed.
264
+
timeout (int): An optional timeout for waiting for the remote
265
+
process to execute the code. If the timeout is exceeded a
266
+
``TimeoutError`` will be raised.
252
267
"""
253
268
254
269
An example usage of the API would look like:
@@ -258,7 +273,9 @@ An example usage of the API would look like:
258
273
import sys
259
274
# Execute a print statement in a remote Python process with PID 12345
260
275
try:
261
-
sys.remote_exec(12345, "print('Hello from remote execution!')")
276
+
sys.remote_exec(12345, "print('Hello from remote execution!')", timeout=3)
277
+
exceptTimeoutError:
278
+
print(f"The remote process took too long to execute the code")
262
279
exceptExceptionas e:
263
280
print(f"Failed to execute code: {e}")
264
281
@@ -270,7 +287,6 @@ This change has no impact on existing Python code or interpreter performance.
270
287
The added fields are only accessed during debugger attachment, and the checking
271
288
mechanism piggybacks on existing interpreter safe points.
272
289
273
-
274
290
Security Implications
275
291
=====================
276
292
@@ -280,23 +296,26 @@ the PEP doesn't specify how memory should be written to the target process, in p
280
296
this will be done using standard system calls that are already being used by other
281
297
debuggers and tools. Some examples are:
282
298
283
-
* On Linux, the ``process_vm_readv()`` and ``process_vm_writev()`` system calls
299
+
* On Linux, the `process_vm_readv() <https://man7.org/linux/man-pages/man2/process_vm_readv.2.html>`__
300
+
and `process_vm_writev() <https://man7.org/linux/man-pages/man2/process_vm_writev.2.html>`__ system calls
284
301
are used to read and write memory from another process. These operations are
285
-
controlled by ptrace access mode checks - the same ones that govern debugger
286
-
attachment. A process can only read from or write to another process's memory
287
-
if it has the appropriate permissions (typically requiring either root or the
288
-
``CAP_SYS_PTRACE`` capability, though less security minded distributions may
289
-
allow any process running as the same uid to attach).
290
-
291
-
* On macOS, the interface would leverage ``mach_vm_read_overwrite()`` and
292
-
``mach_vm_write()`` through the Mach task system. These operations require
302
+
controlled by `ptrace <https://man7.org/linux/man-pages/man2/ptrace.2.html>`__ access mode
303
+
checks - the same ones that govern debugger attachment. A process can only read from
304
+
or write to another process's memory if it has the appropriate permissions (typically
305
+
requiring either root or the `CAP_SYS_PTRACE <https://man7.org/linux/man-pages/man7/capabilities.7.html>`__
306
+
capability, though less security minded distributions may allow any process running as the same uid to attach).
307
+
308
+
* On macOS, the interface would leverage `mach_vm_read_overwrite()<https://developer.apple.com/documentation/kernel/1402127-mach_vm_read_overwrite>`__ and
309
+
`mach_vm_write()<https://developer.apple.com/documentation/kernel/1402070-mach_vm_write>`__ through the Mach task system. These operations require
293
310
``task_for_pid()`` access, which is strictly controlled by the operating
294
311
system. By default, access is limited to processes running as root or those
295
312
with specific entitlements granted by Apple's security framework.
296
313
297
-
* On Windows, the ``ReadProcessMemory()`` and ``WriteProcessMemory()`` functions
314
+
* On Windows, the `ReadProcessMemory() <https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-readprocessmemory>`__
315
+
and `WriteProcessMemory() <https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-writeprocessmemory>`__ functions
298
316
provide similar functionality. Access is controlled through the Windows
299
-
security model - a process needs ``PROCESS_VM_READ`` and ``PROCESS_VM_WRITE``
317
+
security model - a process needs `PROCESS_VM_READ <https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights>`__
318
+
and `PROCESS_VM_WRITE <https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights>`__
300
319
permissions, which typically require the same user context or appropriate
301
320
privileges. These are the same permissions required by debuggers, ensuring
302
321
consistent security semantics across platforms.
@@ -310,7 +329,7 @@ All mechanisms ensure that:
310
329
The memory operations themselves are well-established and have been used safely
311
330
for decades in tools like GDB, LLDB, and various system profilers.
312
331
313
-
It’s important to note that any attempt to attach to a Python process via this
332
+
It's important to note that any attempt to attach to a Python process via this
314
333
mechanism would be detectable by system-level monitoring tools. This
315
334
transparency provides an additional layer of accountability, allowing
316
335
administrators to audit debugging operations in sensitive environments.
@@ -319,12 +338,12 @@ Further, the strict reliance on OS-level security controls ensures that existing
319
338
system policies remain effective. For enterprise environments, this means
320
339
administrators can continue to enforce debugging restrictions using standard
321
340
tools and policies without requiring additional configuration. For instance,
322
-
leveraging Linux’s ``ptrace_scope`` or macOS’s ``taskgated`` to restrict
323
-
debugger access will equally govern the proposed interface.
0 commit comments