Frida 17.31.0 Released ∞
release
This one is mostly about Stalker. A follow can now bring exclusions of its own, excluding a range also covers the calls that reach it through a stub, and a transform can force a branch either way. Underneath, invalidation and unfollow were reworked to fix crashes and a hang on unload. @kkuehl and @as0ler contributed a fix each as well.
Exclusions per follow
Ranges added with Stalker.exclude() apply to every followed thread, and can’t be removed again, as they are baked into the code compiled for each of them. Stalker.follow() now takes an exclude option, with a range or an array of ranges that apply to that follow only, on top of the global ones:
Stalker.follow(threadId, {
events: { call: true },
onCallSummary(summary) {
send(summary);
},
exclude: Process.getModuleByName('libsystem_malloc.dylib')
});They last for as long as the thread is followed, so a later follow of the same thread may use different ones, and so may threads followed at the same time. From C, a transformer provides them through the new get_exclusions() vfunc, and gum_stalker_transformer_make_excluding() wraps any transformer with a set of ranges.
Exclusions and stubs
A call into another module almost always goes through a stub in the calling module. The stub isn’t in the excluded range, so Stalker didn’t treat the call as excluded, and went on to follow the stub’s jump into the range. Stalker now looks at where the stub leads, and treats a call to a stub that points into an excluded range as an excluded call. On x86 this covers direct calls, on arm ARM PLT entries, also when reached from Thumb code, and on arm64 both direct and indirect calls. The check is only made when there are exclusions, so a follow without any doesn’t pay for it.
Interceptor benefits from the same groundwork. On arm64, attaching to a stub or PLT entry now instruments the function it jumps to, as on x86 and arm. Before, only the auth stub was seen through, and only on Apple platforms. And on x86, PLT entries built for IBT, which start with ENDBR64, are now resolved like plain ones.
New iterator methods
A transform can now replace the current instruction with a jump to any target, through putChainingJump(). Stalker keeps following from there, and chains it like one of the application’s own jumps. This makes it possible to force a conditional branch either way without touching the flags or registers that it tests, by passing the branch target or the address of the next instruction:
Stalker.follow(threadId, {
transform(iterator) {
let insn;
while ((insn = iterator.next()) !== null) {
if (insn.address.equals(check)) {
iterator.putChainingJump(insn.next);
continue;
}
iterator.keep();
}
}
});Here check is the address of a conditional branch that should never be taken. The jump ends the block, so next() returns null afterwards.
The other one is releaseUserDataReg(). It puts the application’s value back in the register returned by getUserDataReg(), for code that must see every register as the application left it, such as a hand-written gate that saves them all. Calling getUserDataReg() again takes it back.
The typings in @types/frida-gum 19.14.0 cover all of the above.
Unfollow and invalidation
A thread unfollowed by another thread only noticed at its next block switch. One that kept looping through blocks that were all chained to each other never got there, so it stayed followed, and garbage collection, and with it script unload, waited forever. Unfollowing now invalidates the thread’s blocks, so the next one it enters takes it out.
Invalidating a block used to overwrite its head with a trampoline. A thread returning into that head, such as from a call made by a transform, ran the trampoline from the middle, and executed the block twice or crashed. On x86, a block smaller than the trampoline also lost the head of the block compiled after it. The trampoline now lives in front of the block, and invalidation only replaces the one instruction that every entry passes through.
Also, a block that outgrew its space when recompiled kept the callouts of a discarded compile, which were never released and could lead the next invalidation into stale code.
Kirby tracked down a bug on 32-bit x86, where getUserDataReg() only has ESI and EDI to choose from. An instruction touching both, such as mov [esi], edi, ran with EDI holding the user data instead of the application’s value. It showed up in the test suite of frida-rust, where followed code returned the wrong result. Stalker now hands the register back before relocating any instruction that touches it. Thanks!
GumJS
Several fixes to how long things stay alive:
- After unfollow, a thread keeps running a follow’s compiled code until its next block. The CModule that a native transform or onEvent callback belongs to, and the data passed along with it, could be freed in the meantime. They are now held until Stalker is done with them.
- A CModule’s finalize() may use memory passed in through its symbols, but that memory could be released first. The CModule now holds on to each native resource among its symbols.
- On V8, a NativeCallback reachable from its own function was leaked, and so was a CModule that was part of a reference cycle.
Also on V8, a Stalker callout or call probe written in C that called a NativeCallback aborted the process. Such code runs on Stalker’s own stack, which V8 mistook for a stack overflow.
CModule also gained gum_memory_read_into(). It is like gum_memory_read(), but copies into a buffer you provide instead of allocating one, and leaves errno and the last error alone, so it can be used from a Stalker callout on the followed thread.
macOS and iOS
@as0ler ran into a process we were unable to attach to. dyld’s list of loaded images had a cleared record in it, with a load address or file path of zero. Reading it failed with “Unable to read from process memory”, and that one record made us give up on the whole list. We now skip such records and collect the rest. Thanks!
Swift
An RPC call made on a destroyed script never completed, as frida-core drops messages posted to such a script. It now fails right away.
Enjoy!
Changelog
- stalker: Support exclusions per follow, through a new get_exclusions() vfunc and gum_stalker_transformer_make_excluding().
- stalker: See through stubs when excluding calls.
- stalker: Add put_chaining_jump() to the iterator.
- stalker: Let transforms release the user data register.
- stalker: Make unfollow reach chained code.
- stalker: Patch only the entry of an invalid block.
- stalker: Release callouts of an outgrown block.
- stalker: Look up the stack range without a Stalker.
- stalker: Fix x86 instructions touching all user data registers. Thanks @kkuehl!
- interceptor: Resolve arm64 stubs to their targets.
- x86-reader: Look past ENDBR for indirect jumps.
- arm-reader: Decode PLT entries without Capstone.
- arm64-reader: Add try_get_indirect_jump_target().
- arm64-reader: Read only B as a relative jump.
- memory: Add gum_memory_read_into(), also exposed to CModule.
- darwin: Skip cleared dyld image info records. Thanks @as0ler!
- gumjs: Add exclude option to Stalker.follow().
- gumjs: Add putChainingJump() and releaseUserDataReg() to StalkerIterator.
- gumjs: Keep what a follow calls into alive.
- gumjs: Make CModule entries reference-counted.
- gumjs: Keep CModule symbols alive through finalize(), also when a NativeResource is wrapped in an object’s handle property.
- gumjs: Fix V8 NativeCallback leak in cycles.
- gumjs: Set V8’s stack limit in NativeCallback too.
- compiler: Bump @types/frida-gum to 19.14.0.
- barebone: Bump @types/frida-gum to 19.14.0.
- swift: Fail RPC calls on a destroyed script.
oleavr