@@ -6,7 +6,8 @@ Remote debugging attachment protocol
66This protocol enables external tools to attach to a running CPython process and
77execute Python code remotely.
88
9- Most platforms require elevated privileges to attach to another Python process.
9+ Attaching to another Python process may require additional permissions or
10+ configuration, depending on the platform.
1011
1112Disabling remote debugging
1213--------------------------
@@ -23,44 +24,91 @@ To disable remote debugging support, use any of the following:
2324Permission requirements
2425=======================
2526
26- Attaching to a running Python process for remote debugging requires elevated
27- privileges on most platforms. The specific requirements and troubleshooting
27+ Attaching to a running Python process for remote debugging requires special
28+ configuration on most platforms. The specific requirements and troubleshooting
2829steps depend on your operating system:
2930
3031.. rubric :: Linux
3132
32- The tracer process must have the `` CAP_SYS_PTRACE `` capability or equivalent
33- privileges. You can only trace processes you own and can signal. Tracing may
34- fail if the process is already being traced, or if it is running with
35- set-user-ID or set-group-ID. Security modules like Yama may further restrict
36- tracing.
33+ In general, you can debug your own processes, but there are several common
34+ configurations that may disable this. Some Linux distributions enable ** ptrace
35+ restrictions **, aka "Yama," as a form of system hardening. Recent versions of
36+ the `` setpriv `` command (util-linux 2.41, released June 2025) let you loosen
37+ ptrace restrictions on a per-process basis:
3738
38- To temporarily relax ptrace restrictions (until reboot), run:
39+ ``setpriv --ptracer any python3 ``
40+
41+ (This is configured on the process *being debugged *.) You can also turn off
42+ ptrace restrictions for all processes until reboot with:
3943
4044 ``echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope ``
4145
46+ This can also be configured persistently, usually in ``/etc/sysctl.d ``.
47+
4248.. note ::
4349
4450 Disabling ``ptrace_scope `` reduces system hardening and should only be done
45- in trusted environments.
46-
47- If running inside a container, use ``--cap-add=SYS_PTRACE `` or
48- ``--privileged ``, and run as root if needed.
51+ in low-security environments.
52+
53+ It is also possible that the ``ptrace `` system call is disabled because of a
54+ security filter. In particular, this was common with older versions of some
55+ container software. Docker 19.03 or newer (released 2019) and containerd 1.6.7
56+ or newer (released 2022) will automatically allow usage of the ``ptrace ``
57+ system call inside containers, when running on Linux kernel 4.8 or higher. If
58+ you cannot upgrade to these versions, you can create your container with an
59+ option like ``--security-opt seccomp=unconfined `` to disable the system call
60+ security filter for that container. This weakens the container's isolation and
61+ should only be done in low-security environments.
62+
63+ If you need to trace a process that you *do not * own, you will need superuser
64+ access or equivalent. This also applies to processes that have changed their
65+ security credentials, e.g., set-user-ID or set-group-ID processes (though this
66+ is unusual for Python). Try running the debugging command with ``sudo -E ``.
4967
50- Try re-running the command with elevated privileges :
68+ .. note : :
5169
52- ``sudo -E !! ``
70+ The ``CAP_SYS_PTRACE `` capability is equivalent to superuser access, in
71+ that it allows debugging *any * process, not just your own. You may see
72+ advice on the internet suggesting using it to work around ptrace
73+ restrictions or system call filters. This may work in practice, as would
74+ ``sudo ``, but this gives the debugging process much more access than it
75+ needs and should only be done in low-security environments.
5376
77+ Finally, note that a process can only have one tracer at a time. If you have
78+ already attached to a Python process under ``strace ``, ``gdb ``, etc., you
79+ won't be able to simultaneously use remote debugging. (Superuser access cannot
80+ get around this restriction.)
5481
5582.. rubric :: macOS
5683
57- To attach to another process, you typically need to run your debugging tool
58- with elevated privileges. This can be done by using ``sudo `` or running as
59- root.
84+ By default, macOS disables the ability to debug other processes.
85+
86+ You can modify your Python binary to opt in to being debugged by giving it an
87+ **ad-hoc code signature ** with an **entitlement ** enabling it to be debugged.
88+ (An ad-hoc "signature" is just a configuration without any actual cryptographic
89+ signature or a need for a certificate or anything else such as an Apple
90+ developer program membership.)
91+
92+ The following commands will create a file ``get-task-allow.plist `` with the
93+ necessary entitlement and add it to the Python binary:
94+
95+ .. code-block :: sh
96+
97+ echo ' {"com.apple.security.get-task-allow": true}' | plutil -convert xml1 -o get-task-allow.plist -
98+ codesign --sign - --entitlements get-task-allow.plist path/to/bin/python3
99+
100+ where ``path/to/bin/python3 `` is the path to your Python binary, which you can
101+ find by e.g. running ``which python3 `` or evaluating ``sys.base_executable `` at
102+ the Python REPL. (These instructions are for a non-framework build of Python.
103+ Framework builds may need to be configured differently.)
60104
61- Even when attaching to processes you own, macOS may block debugging unless
62- the debugger is run with root privileges due to system security restrictions .
105+ You should then be able to debug your own Python processes started with that
106+ binary .
63107
108+ Alternatively, much as with Linux, processes with superuser privileges e.g. ``sudo ``
109+ are not subject to this check and can debug any user's process on the system
110+ (though there are additional checks on specific binaries, such as OS-provided
111+ commands, due to System Integrity Protection).
64112
65113.. rubric :: Windows
66114
0 commit comments