요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
PR_SET_PTRACER와 sysctl
Yama.rst:37-75Application-specific debugger 관계와 ptrace_scope 네 mode를 정리합니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
====
Yama
====
Yama is a Linux Security Module that collects system-wide DAC security
protections that are not handled by the core kernel itself. This is
selectable at build-time with ``CONFIG_SECURITY_YAMA``, and can be controlled
at run-time through sysctls in ``/proc/sys/kernel/yama``:
ptrace_scope
============
As Linux grows in popularity, it will become a larger target for
malware. One particularly troubling weakness of the Linux process
interfaces is that a single user is able to examine the memory and
running state of any of their processes. For example, if one application
(e.g. Pidgin) was compromised, it would be possible for an attacker to
attach to other running processes (e.g. Firefox, SSH sessions, GPG agent,
etc) to extract additional credentials and continue to expand the scope
of their attack without resorting to user-assisted phishing.
This is not a theoretical problem. `SSH session hijacking
<https://www.blackhat.com/presentations/bh-usa-05/bh-us-05-boileau.pdf>`_
and `arbitrary code injection
<https://c-skills.blogspot.com/2007/05/injectso.html>`_ attacks already
exist and remain possible if ptrace is allowed to operate as before.
Since ptrace is not commonly used by non-developers and non-admins, system
builders should be allowed the option to disable this debugging system.
For a solution, some applications use ``prctl(PR_SET_DUMPABLE, ...)`` to
specifically disallow such ptrace attachment (e.g. ssh-agent), but many
do not. A more general solution is to only allow ptrace directly from a
parent to a child process (i.e. direct "gdb EXE" and "strace EXE" still
work), or with ``CAP_SYS_PTRACE`` (i.e. "gdb --pid=PID", and "strace -p PID"
still work as root).
In mode 1, software that has defined application-specific relationships
between a debugging process and its inferior (crash handlers, etc),
``prctl(PR_SET_PTRACER, pid, ...)`` can be used. An inferior can declare which
other process (and its descendants) are allowed to call ``PTRACE_ATTACH``
against it. Only one such declared debugging process can exists for
each inferior at a time. For example, this is used by KDE, Chromium, and
Firefox's crash handlers, and by Wine for allowing only Wine processes
to ptrace each other. If a process wishes to entirely disable these ptrace
restrictions, it can call ``prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, ...)``
so that any otherwise allowed process (even those in external pid namespaces)
may attach.
The sysctl settings (writable only with ``CAP_SYS_PTRACE``) are:
0 - classic ptrace permissions:
a process can ``PTRACE_ATTACH`` to any other
process running under the same uid, as long as it is dumpable (i.e.
did not transition uids, start privileged, or have called
``prctl(PR_SET_DUMPABLE...)`` already). Similarly, ``PTRACE_TRACEME`` is
unchanged.
1 - restricted ptrace:
a process must have a predefined relationship
with the inferior it wants to call ``PTRACE_ATTACH`` on. By default,
this relationship is that of only its descendants when the above
classic criteria is also met. To change the relationship, an
inferior can call ``prctl(PR_SET_PTRACER, debugger, ...)`` to declare
an allowed debugger PID to call ``PTRACE_ATTACH`` on the inferior.
Using ``PTRACE_TRACEME`` is unchanged.
2 - admin-only attach:
only processes with ``CAP_SYS_PTRACE`` may use ptrace, either with
``PTRACE_ATTACH`` or through children calling ``PTRACE_TRACEME``.
3 - no attach:
no processes may use ptrace with ``PTRACE_ATTACH`` nor via
``PTRACE_TRACEME``. Once set, this sysctl value cannot be changed.
The original children-only logic was based on the restrictions in grsecurity.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
Yama
1-9Yama는 core kernel 자체가 처리하지 않는 system-wide DAC 보안 보호를 모은 Linux Security Module입니다.
Build 시 `CONFIG_SECURITY_YAMA`로 선택하며 runtime에는 `/proc/sys/kernel/yama`의 sysctl로 제어합니다.
ptrace_scope의 위협 모델
10-29Linux가 널리 쓰일수록 malware의 큰 표적이 됩니다. 특히 Linux process interface에서는 한 user가 자신의 어느 process든 memory와 실행 상태를 조사할 수 있습니다.
예를 들어 Pidgin 같은 application이 침해되면 공격자가 Firefox, SSH session, GPG agent 같은 다른 실행 process에 attach해 추가 credential을 추출하고 user-assisted phishing 없이 공격 범위를 넓힐 수 있습니다.
이는 이론적 문제가 아닙니다. SSH session hijacking과 arbitrary code injection 공격이 이미 존재하며 ptrace를 기존 방식으로 허용하면 계속 가능합니다.
- SSH session hijacking
https://www.blackhat.com/presentations/bh-usa-05/bh-us-05-boileau.pdf - Arbitrary code injection
https://c-skills.blogspot.com/2007/05/injectso.html
Non-developer와 non-admin은 ptrace를 흔히 사용하지 않으므로 system builder가 이 debugging system을 비활성화할 수 있어야 합니다.
Parent-child 제한과 capability
30-36일부 application은 `prctl(PR_SET_DUMPABLE, ...)`로 ptrace attach를 명시적으로 금지하지만, 예를 들어 `ssh-agent`와 달리 많은 application은 그렇게 하지 않습니다.
더 일반적인 해결책은 parent가 direct child를 ptrace하는 것만 허용해 `gdb EXE`와 `strace EXE`는 계속 동작하게 하는 것입니다. 또는 `CAP_SYS_PTRACE`를 가진 root가 `gdb --pid=PID`와 `strace -p PID`를 사용할 수 있습니다.
Mode 1의 PR_SET_PTRACER
37-48Mode 1에서 crash handler처럼 debugger process와 inferior 사이의 application-specific 관계를 정의한 software는 `prctl(PR_SET_PTRACER, pid, ...)`를 사용할 수 있습니다.
Inferior는 자신에게 `PTRACE_ATTACH`를 호출할 수 있는 다른 process와 그 descendant를 선언할 수 있습니다. Inferior마다 한 번에 하나의 debugger process만 선언할 수 있습니다.
KDE, Chromium, Firefox crash handler가 이를 사용하며 Wine도 Wine process끼리만 ptrace하도록 사용합니다.
Process가 이 제한을 완전히 끄려면 `prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, ...)`를 호출합니다. 그러면 external PID namespace의 process를 포함해 원래 허용되는 어떤 process든 attach할 수 있습니다.
ptrace_scope mode
49-75Sysctl 설정은 `CAP_SYS_PTRACE`가 있어야 쓸 수 있습니다.
| 값 | Mode | 동작 |
|---|---|---|
| 0 | classic ptrace permissions | 같은 UID로 실행되고 dumpable인 다른 process에 `PTRACE_ATTACH`할 수 있습니다. UID가 바뀌었거나 privileged로 시작했거나 `prctl(PR_SET_DUMPABLE...)`을 호출한 process는 제외됩니다. `PTRACE_TRACEME`는 그대로입니다. |
| 1 | restricted ptrace | Inferior와 사전 정의 관계가 있어야 합니다. 기본 관계는 classic 조건도 만족하는 descendant입니다. Inferior가 `prctl(PR_SET_PTRACER, debugger, ...)`로 허용할 debugger PID를 선언할 수 있고 `PTRACE_TRACEME`는 그대로입니다. |
| 2 | admin-only attach | `CAP_SYS_PTRACE`가 있는 process만 `PTRACE_ATTACH` 또는 child의 `PTRACE_TRACEME`를 통해 ptrace를 사용할 수 있습니다. |
| 3 | no attach | 어떤 process도 `PTRACE_ATTACH`나 `PTRACE_TRACEME`로 ptrace를 사용할 수 없습니다. 이 값으로 설정하면 다시 바꿀 수 없습니다. |
원래의 children-only logic은 grsecurity의 제한을 기반으로 했습니다.
ptrace 위협과 제한
Yama.rst:1-36동일 UID process를 통한 credential 탈취 위험과 parent-child 제한을 설명합니다.