NokiaTube | New address | Mode Phone | On high quality
Skip to content

Interceptor: attach()/replace() crash when the target is unmapped (e.g. re-hooking after a module unload) #1218

Open

Description

@ab3lson

Summary

gum_interceptor_attach() and gum_interceptor_replace() disassemble and patch the target function's prologue without first checking that it is still mapped. If the target's code has been freed since the address was obtained — a shared library that has been dlclose()d, or JIT-compiled code that has been freed — gum reads (and would then patch) freed memory and crashes with SIGSEGV.

Note this is distinct from the existing, working behavior where a live hook is discarded when its module unloads (/Core/ModuleRegistry/hooks_should_be_discarded_when_module_unloads). The gap here is a fresh attach()/replace() onto an address that is already unmapped at the time of the call.

Reproducer

Minimal, self-contained (links against gum only):

#include <gum/gum.h>
#include <stdio.h>

static void on_enter (GumInvocationContext * ic, gpointer user_data) {}

int
main (void)
{
  GumInterceptor * interceptor;
  GumInvocationListener * listener;
  gsize page_size;
  gpointer target;
  GumAttachReturn result;

  gum_init_embedded ();

  interceptor = gum_interceptor_obtain ();
  listener = gum_make_call_listener (on_enter, NULL, NULL, NULL);

  /* Obtain a valid address, then unmap it so it points at freed memory. */
  page_size = gum_query_page_size ();
  target = gum_memory_allocate (NULL, page_size, page_size, GUM_PAGE_RW);
  gum_memory_free (target, page_size);

  result = gum_interceptor_attach (interceptor, target, listener, NULL);
  printf ("attach returned %d\n", result);   /* SIGSEGV before this is fixed */

  return 0;
}

Backtrace

#0  reader ()                                      capstone X86Disassembler.c:866  (*byte = info->code[...])
#5  cs_disasm (buffer=0x..e119 <Cannot access memory at address 0x..e119>)
#6  gum_x86_reader_disassemble_instruction_at (address=0x..e119)
#9  _gum_interceptor_backend_resolve_redirect      guminterceptor-x86.c:461
#10 gum_interceptor_resolve                         guminterceptor.c
#11 gum_interceptor_attach (target=0x..e119)        guminterceptor.c

When this happens

Any caller that attaches/replaces an address that may have been freed between obtaining the address and calling attach()/replace(). Concretely: re-applying a hook to a function whose library was dlclose()d, or to JIT-compiled code that was freed, during the window the address is stale. gum has no cheap way to return an error today — it just faults.

Why gum_ensure_code_readable() doesn't catch it

gum_interceptor_resolve() already calls gum_ensure_code_readable(), but that helper is #if defined (HAVE_ANDROID) only and, even on Android, merely softens execute-only pages — for an unmapped page gum_memory_query_protection() fails and it silently continues, so the disassembly still faults.

Fix

A small guard with gum_memory_is_readable() in gum_interceptor_resolve() and gum_interceptor_instrument(), reporting the condition as a new GUM_ATTACH_NOT_MAPPED / GUM_REPLACE_NOT_MAPPED instead of crashing. PR with a regression test follows.

Reproduced on frida-gum 17.23.3 (Linux/x86_64 and Android/arm64).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions