8000 linux: Refuse to attach to processes frozen by the cgroup freezer by kkuehl · Pull Request #1317 · frida/frida-core · GitHub
[go: up one dir, main page]

Skip to content

linux: Refuse to attach to processes frozen by the cgroup freezer - #1317

Merged
oleavr merged 2 commits into
frida:mainfrom
kkuehl:fix/frozen-attach
Dec 3, 2026
Merged

oleavr merged 2 commits into
frida:mainfrom
kkuehl:fix/frozen-attach

Conversation

@kkuehl
@kkuehl kkuehl commented Dec 2, 2026 •
Copy link
Copy Markdown
Contributor

Problem

Attaching to a process that is suspended by the cgroup v2 freezer — as used
by Android's cached-apps freezer for backgrounded apps — fails and, worse,
corrupts the target: Device.attach() times out after ~5 s, and the
process then crashes with SIGSEGV the moment it is thawed.

Reproduction (rooted Android 15 arm64 emulator, frida-server 17.30.0)

  1. Launch an app, send it to the background, and freeze it:
    am freeze --sticky <pid> (the framework's cached-apps freezer does the
    same automatically after ~70 s). The task is now in do_freezer_trap
    (/proc/<pid>/cgroup → 0::/…, cgroup.events: frozen 1).
  2. device.attach(pid) → after ~5 s:
    TimedOutError: unexpectedly timed out while waiting for stop from process with PID <pid>.
  3. The process is still alive and still frozen.
  4. am unfreeze --sticky <pid> → within a second the process dies:
    signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x320,
    backtrace #00 pc 0000000000000320 <unknown>.

The same sequence also reproduced 4/4 on a physical Pixel running Android 17, with the framework's
freezer freezing the app on its own (no am freeze).

Spawning (-f + resume) and attaching to a non-frozen process both work; so
does unfreezing first and then attaching.

Cause

PTRACE_SEIZE + PTRACE_INTERRUPT do place a frozen task in a ptrace-stop,
so SeizeSession.init_async() succeeds and the injector proceeds to set up its
bootstrap mmap() remote call in RemoteCall.execute()
(src/linux/frida-helper-backend.vala): it saves the original registers, writes
the call's registers (on arm64 lr = DUMMY_RETURN_ADDRESS = 0x320, pc = mmap,
x8 = __NR_mmap, args in x0..x5) and issues PTRACE_CONT.

But a cgroup-frozen task does not actually run on PTRACE_CONT — it goes
straight back to sleep in the freezer — so the call never reaches the dummy
return address, and wait_for_signals({SEGV, STOP}) times out. The restore in
the failure path then silently fails: PTRACE_SETREGS only works while the
thread is in a ptrace-stop, and after the PTRACE_CONT it is not, so the
catch (Error e) {} swallows the ESRCH and the target is left with the remote
call's registers in place. When the app is later thawed it runs the abandoned
call and faults at the 0x320 return sentinel — exactly the observed crash
(the crash register dump shows x8=0xde (mmap), x2=0x7 (RWX), x3=0x22
(MAP_PRIVATE|ANON), x0=a freshly-mapped address, pc=lr=0x320).

Fix

Two parts, one concern — a frozen process cannot be driven through ptrace
remote calls, so never leave it mutated:

  1. Fail fast, untouched. init_async() now detects the cgroup v2 freezer up
    front (via /proc/<pid>/cgroup → cgroup.events: frozen 1), before
    PTRACE_SEIZE, and throws a clear Error.INVALID_OPERATION naming the
    freezer. No ptrace call is made, so the process is left exactly as it was.
  2. Always restore. RemoteCall.execute() now re-stops the thread before
    restoring the saved registers if the plain SETREGS fails, so a target that
    gets frozen mid-injection (a race) is restored in every case instead of being
    left with a half-applied call frame.

Detection is read-only (/proc + /sys/fs/cgroup), needs no elevated
permission, and is a no-op on systems without the cgroup v2 freezer (the reads
fail and it returns false, so normal attaches are unaffected).

Testing

Rooted google_apis Android 15 (API 35) arm64 emulator; stock frida-python
17.30.0 client. Server built from this branch for android-arm64.

Case Before (stock 17.30.0) After (this branch)
Attach to frozen process TimedOutError after 5.49 s InvalidOperationError naming the freezer, 0.02 s
Process after am unfreeze dies — SIGSEGV … fault addr 0x320 survives, no tombstone
Attach to non-frozen process (control) OK, 0.13 s, agent script runs OK, 0.05 s, agent script runs

New error message:

unable to attach to process <pid> because it is suspended by the cgroup
freezer (e.g. Android's cached-apps freezer); resume it before attaching
(on Android: `am unfreeze <pid>`), or spawn it instead

(The cgroup-freezer scenario needs a rooted device plus cgroup delegation, so
it is not expressible as a portable unit test in the suite; verified with a
scripted emulator reproduction that fails before and passes after.)

A second commit fixes the MinGW builds that main has been failing since
86f584e: frida_create_loader_token read the parent pid as
PROCESS_BASIC_INFORMATION.Reserved3, a name only MSVC's winternl.h has.
It now reads a local struct with the documented layout, checked against
MinGW's header on i686 and x86_64. Happy to split it out into its own PR.

@kkuehl
kkuehl commented Dec 2, 2026
Copy link
Copy Markdown
Contributor Author

The remaining red job, windows-shared (arm64), fails at configure in idun, not in this change: idun takes the from-source shared GLib for static and asks for gioopenssl (the x64 job passes because it uses the SDK's GLib). Fix: frida/idun#16. This PR's second commit fixes the MinGW builds, which are green now.

A process frozen by the cgroup v2 freezer, e.g. by Android's cached-apps
freezer, still enters ptrace-stop, but PTRACE_CONT does not run it. Our
first remote call thus timed out, and since SETREGS fails on a running
thread the target kept the call's registers, crashing once thawed.

Fail early when the process is frozen, and re-stop the thread before
restoring its registers after a failed remote call.
MinGW's winternl.h lacks PROCESS_BASIC_INFORMATION.Reserved3, so define
our own struct for the parent PID lookup.
@oleavr
oleavr force-pushed the fix/frozen-attach branch from 905c9a4 to 1ffe97b Compare December 3, 2026 09:53
@oleavr
oleavr merged commit 333ffbf into frida:main Dec 3, 2026
2 of 36 checks passed
@oleavr
oleavr commented Dec 3, 2026
Copy link
Copy Markdown
Member

Thanks! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0