Run a virtual iPhone on an Apple Silicon Mac.
Swift
Fix C23 (WIP): switch from ops table pointer rewrite to inline trampoline
Lakr916c9c216827
diff --git a/scripts/patchers/kernel_jb_base.py b/scripts/patchers/kernel_jb_base.pyindex 4f9b207d..bb17665d 100644--- a/scripts/patchers/kernel_jb_base.py+++ b/scripts/patchers/kernel_jb_base.py@@ -128,9 +128,21 @@ class KernelJBPatcherBase(KernelPatcher):"""Find a region of zeros/0xFF/UDF in executable memory for shellcode.Returns file offset of the cave start, or -1 if not found.Reads from self.data (mutable) so previously allocated caves are skipped.++ Only searches __TEXT_EXEC and __TEXT_BOOT_EXEC segments.+ __PRELINK_TEXT is excluded because KTRR makes it non-executable at+ runtime on ARM64e, even though the Mach-O marks it R-X."""+ EXEC_SEGS = ("__TEXT_EXEC", "__TEXT_BOOT_EXEC")+ exec_ranges = [+ (foff, foff + fsz)+ for name, _, foff, fsz, _ in self.all_segments+ if name in EXEC_SEGS and fsz > 0+ ]+ exec_ranges.sort()+needed = (size + align - 1) // align * align- for rng_start, rng_end in self.code_ranges:+ for rng_start, rng_end in exec_ranges:run_start = -1run_len = 0for off in range(rng_start, rng_end, 4):diff --git a/scripts/patchers/kernel_jb_patch_hook_cred_label.py b/scripts/patchers/kernel_jb_patch_hook_cred_label.pyindex 02312b1a..87fc319c 100644--- a/scripts/patchers/kernel_jb_patch_hook_cred_label.py+++ b/scripts/patchers/kernel_jb_patch_hook_cred_label.py@@ -2,19 +2,26 @@from .kernel_jb_base import asm, _rd32, _rd64, RET, NOP, struct+PACIBSP = bytes([0x7F, 0x23, 0x03, 0xD5]) # 0xD503237F+class KernelJBPatchHookCredLabelMixin:def patch_hook_cred_label_update_execve(self):- """Redirect _hook_cred_label_update_execve ops table entry to shellcode.-- Patches the sandbox MAC ops table entry for cred_label_update_execve- to point to custom shellcode that performs vnode_getattr ownership- propagation. Instead of calling vfs_context_current (which may not- exist as a BL-callable function), we construct a vfs_context on the- stack using current_thread (mrs tpidr_el1) and the caller's- credential (x0 = old_cred).+ """Inline-trampoline the sandbox cred_label_update_execve hook.++ Injects ownership-propagation shellcode by replacing the first+ instruction (PACIBSP) of the original hook with ``B cave``.+ The cave runs PACIBSP, performs vnode_getattr ownership propagation,+ then ``B hook+4`` to resume the original function.++ Previous approach (ops table pointer rewrite) broke the chained+ fixup integrity check, causing PAC failures in unrelated kexts.+ Inline trampoline avoids PAC entirely — B is PC-relative."""- self._log("\n[JB] _hook_cred_label_update_execve: ops table + shellcode")+ self._log(+ "\n[JB] _hook_cred_label_update_execve: "+ "inline trampoline + shellcode"+ )# ── 1. Find vnode_getattr via string anchor ──────────────vnode_getattr_off = self._resolve_symbol("_vnode_getattr")@@ -67,88 +74,108 @@ class KernelJBPatchHookCredLabelMixin:return Falseself._log(- f" [+] hook at ops[{hook_index}] = 0x{orig_hook:X} ({best_size} bytes)"+ f" [+] hook at ops[{hook_index}] = 0x{orig_hook:X} "+ f"({best_size} bytes)")+ # Verify first instruction is PACIBSP+ first_insn = self.raw[orig_hook : orig_hook + 4]+ if first_insn != PACIBSP:+ self._log(+ f" [-] first insn not PACIBSP "+ f"(got 0x{_rd32(self.raw, orig_hook):08X})"+ )+ return False+# ── 4. Find code cave ────────────────────────────────────- cave = self._find_code_cave(180)+ cave = self._find_code_cave(200)if cave < 0:self._log(" [-] no code cave found")return Falseself._log(f" [+] code cave at 0x{cave:X}")- # ── 5. Encode BL to vnode_getattr ────────────────────────- vnode_bl_off = cave + 17 * 4+ # ── 5. Encode branches ─────────────────────────────────+ # BL cave→vnode_getattr (slot 18)+ vnode_bl_off = cave + 18 * 4vnode_bl = self._encode_bl(vnode_bl_off, vnode_getattr_off)if not vnode_bl:self._log(" [-] BL to vnode_getattr out of range")return False- # ── 6. Encode B to original hook ─────────────────────────- b_back_off = cave + 44 * 4- b_back = self._encode_b(b_back_off, orig_hook)- if not b_back:- self._log(" [-] B to original hook out of range")+ # B cave→hook+4 (back to STP after PACIBSP, last slot)+ b_resume_slot = 45+ b_resume_off = cave + b_resume_slot * 4+ b_resume = self._encode_b(b_resume_off, orig_hook + 4)+ if not b_resume:+ self._log(" [-] B to hook+4 out of range")+ return False++ # B hook→cave (replaces PACIBSP at function entry)+ b_to_cave = self._encode_b(orig_hook, cave)+ if not b_to_cave:+ self._log(" [-] B to cave out of range")return False- # ── 7. Build shellcode ───────────────────────────────────+ # ── 6. Build shellcode ───────────────────────────────────# MAC hook args: x0=old_cred, x1=new_cred, x2=proc, x3=vp#- # Parts [8-10] construct a vfs_context on the stack instead- # of calling vfs_context_current, which may not exist as a- # direct BL target in stripped ARM64e kernels.+ # The cave starts with PACIBSP (relocated from hook entry),+ # then performs ownership propagation, then resumes the+ # original function at hook+4 (the STP instruction).## struct vfs_context { thread_t vc_thread; kauth_cred_t vc_ucred; }- # We place it at [sp, #0x70] (between saved regs and vattr buffer).+ # Built on the stack at [sp, #0x70].parts = []- parts.append(NOP) # 0- parts.append(asm("cbz x3, #0xa8")) # 1- parts.append(asm("sub sp, sp, #0x400")) # 2- parts.append(asm("stp x29, x30, [sp]")) # 3- parts.append(asm("stp x0, x1, [sp, #16]")) # 4- parts.append(asm("stp x2, x3, [sp, #32]")) # 5- parts.append(asm("stp x4, x5, [sp, #48]")) # 6- parts.append(asm("stp x6, x7, [sp, #64]")) # 7- # Construct vfs_context inline (replaces BL vfs_context_current)- parts.append(asm("mrs x8, tpidr_el1")) # 8: current_thread- parts.append(asm("stp x8, x0, [sp, #0x70]")) # 9: {thread, cred}- parts.append(asm("add x2, sp, #0x70")) # 10: ctx = &vfs_ctx+ parts.append(PACIBSP) # 0: relocated from hook+ parts.append(asm("cbz x3, #0xb0")) # 1: if vp==NULL → slot 45+ parts.append(asm("sub sp, sp, #0x400")) # 2+ parts.append(asm("stp x29, x30, [sp]")) # 3+ parts.append(asm("stp x0, x1, [sp, #16]")) # 4+ parts.append(asm("stp x2, x3, [sp, #32]")) # 5+ parts.append(asm("stp x4, x5, [sp, #48]")) # 6+ parts.append(asm("stp x6, x7, [sp, #64]")) # 7+ # Construct vfs_context inline+ parts.append(asm("mrs x8, tpidr_el1")) # 8: current_thread+ parts.append(asm("stp x8, x0, [sp, #0x70]")) # 9: {thread, cred}+ parts.append(asm("add x2, sp, #0x70")) # 10: ctx = &vfs_ctx# Setup vnode_getattr(vp, &vattr, ctx)- parts.append(asm("ldr x0, [sp, #0x28]")) # 11: x0 = vp- parts.append(asm("add x1, sp, #0x80")) # 12: x1 = &vattr- parts.append(asm("mov w8, #0x380")) # 13: vattr size- parts.append(asm("stp xzr, x8, [x1]")) # 14: init vattr- parts.append(asm("stp xzr, xzr, [x1, #0x10]")) # 15: init vattr- parts.append(NOP) # 16- parts.append(vnode_bl) # 17: BL vnode_getattr+ parts.append(asm("ldr x0, [sp, #0x28]")) # 11: x0 = vp (saved x3)+ parts.append(asm("add x1, sp, #0x80")) # 12: x1 = &vattr+ parts.append(asm("mov w8, #0x380")) # 13: vattr size+ parts.append(asm("stp xzr, x8, [x1]")) # 14: init vattr+ parts.append(asm("stp xzr, xzr, [x1, #0x10]")) # 15: init vattr+16+ parts.append(NOP) # 16+ parts.append(NOP) # 17+ parts.append(vnode_bl) # 18: BL vnode_getattr# Check result + propagate ownership- parts.append(asm("cbnz x0, #0x50")) # 18: error → skip- parts.append(asm("mov w2, #0")) # 19: changed = 0- parts.append(asm("ldr w8, [sp, #0xCC]")) # 20: va_mode- parts.append(bytes([0xA8, 0x00, 0x58, 0x36])) # 21: tbz w8,#11- parts.append(asm("ldr w8, [sp, #0xC4]")) # 22: va_uid- parts.append(asm("ldr x0, [sp, #0x18]")) # 23: new_cred- parts.append(asm("str w8, [x0, #0x18]")) # 24: cred->uid- parts.append(asm("mov w2, #1")) # 25: changed = 1- parts.append(asm("ldr w8, [sp, #0xCC]")) # 26: va_mode- parts.append(bytes([0xA8, 0x00, 0x50, 0x36])) # 27: tbz w8,#10- parts.append(asm("mov w2, #1")) # 28: changed = 1- parts.append(asm("ldr w8, [sp, #0xC8]")) # 29: va_gid- parts.append(asm("ldr x0, [sp, #0x18]")) # 30: new_cred- parts.append(asm("str w8, [x0, #0x28]")) # 31: cred->gid- parts.append(asm("cbz w2, #0x1c")) # 32: if !changed- parts.append(asm("ldr x0, [sp, #0x20]")) # 33: proc- parts.append(asm("ldr w8, [x0, #0x454]")) # 34: p_csflags- parts.append(asm("orr w8, w8, #0x100")) # 35: CS_VALID- parts.append(asm("str w8, [x0, #0x454]")) # 36: store- parts.append(asm("ldp x0, x1, [sp, #16]")) # 37: restore- parts.append(asm("ldp x2, x3, [sp, #32]")) # 38- parts.append(asm("ldp x4, x5, [sp, #48]")) # 39- parts.append(asm("ldp x6, x7, [sp, #64]")) # 40- parts.append(asm("ldp x29, x30, [sp]")) # 41- parts.append(asm("add sp, sp, #0x400")) # 42- parts.append(NOP) # 43- parts.append(b_back) # 44: B orig_hook+ parts.append(asm("cbnz x0, #0x4c")) # 19: error → slot 38+ parts.append(asm("mov w2, #0")) # 20: changed = 0+ parts.append(asm("ldr w8, [sp, #0xCC]")) # 21: va_mode+ parts.append(bytes([0xA8, 0x00, 0x58, 0x36])) # 22: tbz w8,#11+ parts.append(asm("ldr w8, [sp, #0xC4]")) # 23: va_uid+ parts.append(asm("ldr x0, [sp, #0x18]")) # 24: new_cred+ parts.append(asm("str w8, [x0, #0x18]")) # 25: cred->uid+ parts.append(asm("mov w2, #1")) # 26: changed = 1+ parts.append(asm("ldr w8, [sp, #0xCC]")) # 27: va_mode+ parts.append(bytes([0xA8, 0x00, 0x50, 0x36])) # 28: tbz w8,#10+ parts.append(asm("mov w2, #1")) # 29: changed = 1+ parts.append(asm("ldr w8, [sp, #0xC8]")) # 30: va_gid+ parts.append(asm("ldr x0, [sp, #0x18]")) # 31: new_cred+ parts.append(asm("str w8, [x0, #0x28]")) # 32: cred->gid+ parts.append(asm("cbz w2, #0x14")) # 33: if !changed → slot 38+ parts.append(asm("ldr x0, [sp, #0x20]")) # 34: proc+ parts.append(asm("ldr w8, [x0, #0x454]")) # 35: p_csflags+ parts.append(asm("orr w8, w8, #0x100")) # 36: CS_VALID+ parts.append(asm("str w8, [x0, #0x454]")) # 37: store+ # Restore and resume+ parts.append(asm("ldp x0, x1, [sp, #16]")) # 38+ parts.append(asm("ldp x2, x3, [sp, #32]")) # 39+ parts.append(asm("ldp x4, x5, [sp, #48]")) # 40+ parts.append(asm("ldp x6, x7, [sp, #64]")) # 41+ parts.append(asm("ldp x29, x30, [sp]")) # 42+ parts.append(asm("add sp, sp, #0x400")) # 43+ parts.append(NOP) # 44+ parts.append(b_resume) # 45: B hook+4for i, part in enumerate(parts):self.emit(@@ -157,17 +184,13 @@ class KernelJBPatchHookCredLabelMixin:f"shellcode+{i * 4} [_hook_cred_label_update_execve]",)- # ── 8. Rewrite ops table entry ───────────────────────────- # Preserve auth rebase upper 32 bits (PAC key, diversity,- # chain next) and replace lower 32 bits with cave foff.- entry_off = ops_table + hook_index * 8- orig_raw = _rd64(self.raw, entry_off)- new_raw = (orig_raw & 0xFFFFFFFF00000000) | (cave & 0xFFFFFFFF)+ # ── 7. Patch function entry ─────────────────────────────+ # Replace PACIBSP with B cave (inline trampoline).+ # No ops table modification — avoids chained fixup integrity issues.self.emit(- entry_off,- struct.pack("<Q", new_raw),- f"ops_table[{hook_index}] = cave 0x{cave:X} "- f"[_hook_cred_label_update_execve]",+ orig_hook,+ b_to_cave,+ "B cave [_hook_cred_label_update_execve trampoline]",)return True