repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
linux-ptrace-exit-race-pidfd-getfd-fd-theft.mdMarkdown102 lines6.1 KiBsource raw download

title: "Linux ptrace exit-race pidfdgetfd() FD theft" section: "Linux" sectionSlug: "linux-hardening" sourcePath: "src/linux-hardening/main-system-information/kernel-lpe-cves/linux-ptrace-exit-race-pidfd_getfd-fd-theft.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/linux-hardening/main-system-information/kernel-lpe-cves/linux-ptrace-exit-race-pidfd_getfd-fd-theft.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Linux ptrace exit-race pidfd_getfd() FD theft#

A useful Linux kernel privesc pattern is to turn a ptrace authorization bug into file descriptor theft from a privileged process.

In the Qualys __ptrace_may_access() case study (CVE-2026-46333), the attacker races a privileged process that is exiting or dropping credentials and uses pidfd_getfd() to duplicate an FD into the attacker process.[1][2]

Core idea#

pidfd_getfd() duplicates a file descriptor from another process, but first checks ptrace-style permissions against the target.[3] If that authorization is incorrectly granted during a teardown window, an unprivileged attacker can copy:

This transforms a kernel-side authorization bug into a very practical userspace primitive.[1]

Why the primitive is dangerous#

The attack does not need a bug in the privileged helper itself. The helper only needs to temporarily hold something valuable:

Once duplicated into the attacker process, the duplicate refers to the same open file description, so subsequent reads or IPC requests use the already-open FD rather than reopening the original pathname or starting a fresh authentication flow.[2][3]

Exploitation pattern#

  1. Identify a setuid / setgid / file-capability binary or root daemon that opens sensitive files or keeps useful IPC connections.[2]
  2. Gain a relationship that satisfies the relevant ptrace policy checks for the target path (for example, being the parent of a spawned privileged child under permissive YAMA settings).[2][4]
  3. Race the process while it is exiting, dropping credentials, or otherwise entering a state where ptrace access should have become unavailable.[2]
  4. Use pidfd_open() + pidfd_getfd() to duplicate the target FD during the narrow authorization window.[2][3][5]
  5. Reuse the stolen FD from the unprivileged context.[2]
    • read() secrets from a privileged file descriptor
    • send requests over a stolen authenticated IPC channel to get root-side actions

Minimal primitive shape.[1][3][5]

int p = pidfd_open(victim_pid, 0);
int stolen = pidfd_getfd(p, victim_fd, 0);
/* use stolen with read()/write()/sendmsg()/ioctl() depending on target */

Practical targets to audit#

Prioritize binaries and daemons that, even briefly, do one of these:[1][2]

Good hunting candidates:[1]

YAMA as an exploit gate#

kernel.yama.ptrace_scope is a major practical gate for ptrace-family abuse:[3][4]

For this technique, ptrace_scope=2 is a strong temporary mitigation because it breaks the public pidfd_getfd() exploitation path with -EPERM for unprivileged users.[1][2][3]

Detection / review ideas#

When auditing privileged Linux software, look for these combinations:

When auditing the kernel, treat any path that does ptrace-equivalent authorization during task teardown as high risk, especially if success yields direct access to task->files or other already-authorized process resources.[2]

References#