Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Fix B8 convert_port_to_map: was patching PAC check instead of kernel_map guard

Lakr5655fd78d76f
diff --git a/research/kernel_patch_jb/patch_convert_port_to_map.md b/research/kernel_patch_jb/patch_convert_port_to_map.md
index 9c4e028c..28bea7cf 100644
--- a/research/kernel_patch_jb/patch_convert_port_to_map.md
+++ b/research/kernel_patch_jb/patch_convert_port_to_map.md
@@ -1,34 +1,48 @@
# B8 `patch_convert_port_to_map`
-## How the patch works
-- Source: `scripts/patchers/kernel_jb_patch_port_to_map.py`.
-- Locator strategy:
- 1. Anchor panic string: `"userspace has control access to a kernel map"`.
- 2. Find xref to that string and nearby `BL _panic`.
- 3. Walk backward to locate the conditional branch entering the panic block.
-- Patch action:
- - Replace that conditional branch with unconditional `B` to `resume_off` (`bl_panic + 4`), so execution skips the panic call.
+## Status: FIXED (was PANIC)
-## Expected outcome
-- Kernel no longer panics on this convert-port-to-map control-access check path.
+## Root cause of failure
+The patcher's backward branch search found the **wrong branch** to redirect:
+- Walked backward from panic string ADRP looking for a branch targeting the "error region"
+- Found PAC validation `B.EQ` at `0xFFFFFE0007B06E80` (checks PAC auth succeeded)
+- Its target (`0xB06E88`, the ADRL for kernel_map) happened to fall within the error
+ region range `[adrp-0x40, bl_panic+4]` — a false positive
+- Patching this B.EQ to `B resume_off` caused ALL port-to-map conversions to skip the
+ map lookup entirely, returning NULL for every task's vm_map
+- Result: "initproc failed to start -- exit reason namespace 2 subcode 0x6"
-## Target
-- The panic edge in `_convert_port_to_map_with_flavor` (or equivalent stripped function), specifically the branch feeding the panic block.
+## Actual code flow in `_convert_port_to_map_with_flavor`
+```
+0xB06E7C: CMP X16, X17 ; PAC validation check
+0xB06E80: B.EQ 0xB06E88 ; if PAC valid → continue ← patcher incorrectly patched this
+0xB06E84: BRK #0xC472 ; PAC failure trap
+0xB06E88: ADRL X8, kernel_map ; load kernel_map address
+0xB06E90: CMP X16, X8 ; compare map ptr with kernel_map
+0xB06E94: B.NE 0xB06EE8 ; if NOT kernel_map → normal path ← correct target
+0xB06E98: ... set up panic args ...
+0xB06EAC: ADRL X0, "userspace has control access..."
+0xB06EB4: BL _panic ; noreturn
+```
-## IDA MCP evidence
-- Panic string: `0xfffffe0007040701`
-- String xref site: `0xfffffe0007b06eac`
-- Containing function start: `0xfffffe0007b06db8`
+## Fix applied
+Completely new approach — instead of backward branch search:
+1. Walk backward from string ADRP to find `CMP + B.cond` pattern
+2. The `CMP Xn, Xm` followed by `B.NE target` (where target > adrp_off) is the guard
+3. Replace `B.NE` with unconditional `B` to same target
+4. This makes the kernel_map case take the normal path instead of panicking
+
+The fixed patch changes `B.NE 0xB06EE8` at `0xB06E94` to `B 0xB06EE8`:
+- If map == kernel_map: now takes normal path (was: panic)
+- If map != kernel_map: unchanged (takes normal path)
-## Full call-stack sample (static, from IDA xrefs)
-- This is one complete observed caller chain down to the patched function:
- 1. data dispatch ref: `0xfffffe00078ee8c8` -> function `sub_FFFFFE00089B17E0` (`0xfffffe00089b17e0`)
- 2. call at `0xfffffe00089b1c50`: `BL sub_FFFFFE0008A1F0A0`
- 3. call at `0xfffffe0008a1f31c`: `BL sub_FFFFFE0008A19DC8`
- 4. call at `0xfffffe0008a19e34`: `BL sub_FFFFFE0008A2D948`
- 5. call at `0xfffffe0008a2d9a4`: `BL sub_FFFFFE0007BD2544`
- 6. call at `0xfffffe0007bd2694`: `B sub_FFFFFE0007B06DB8`
- 7. panic-log reference inside target function: `0xfffffe0007b06eac`
+## IDA MCP evidence
+- Panic string: `0xfffffe0007040701` "userspace has control access to a kernel map %p through task %p @%s:%d"
+- String xref: `0xfffffe0007b06eac`
+- Function: `sub_FFFFFE0007B06DB8` (size 0x154)
+- `sub_FFFFFE00082FA814` at BL target is `_panic` (calls itself with "Assertion failed", never returns)
+- Code after BL _panic (0xB06EB8) is dead code containing a TBNZ→BRK trap
## Risk
-- This converts a hard fail (panic) into continue-execution behavior; if downstream invariants were relying on panic-stop, memory safety assumptions may no longer hold.
+- Allows userspace to obtain a reference to the kernel vm_map through IPC
+- Required for JB: enables kernel memory access from userspace
diff --git a/research/patch_comparison_all_variants.md b/research/patch_comparison_all_variants.md
index 649e7eb8..59299c99 100644
--- a/research/patch_comparison_all_variants.md
+++ b/research/patch_comparison_all_variants.md
@@ -232,10 +232,14 @@ with capstone semantic matching and keystone-generated patch bytes only:
1. `AMFIIsCDHashInTrustCache` function rewrite
- Locator: semantic function-body matcher in AMFI text.
- Patch: `mov x0,#1 ; cbz x2,+8 ; str x0,[x2] ; ret`.
-2. AMFI execve kill path bypass (2 BL sites)
+2. AMFI execve kill path bypass (shared return value)
- Locator: string xref to `"AMFI: hook..execve() killing"` (fallback `"execve() killing"`),
- then function-local early `bl` + `cbz/cbnz w0` pair matcher.
- - Patch: `bl -> mov x0,#0` at two helper callsites.
+ then backward scan from function end for `MOV W0, #1` + `LDP x29, x30` epilogue.
+ - Patch: `MOV W0, #1 -> MOV W0, #0` at the shared kill-return instruction.
+ - All kill paths (unsigned code, restricted exec mode, VPN plugin, dyld sig, etc.)
+ converge on this single return value.
+ - Previous approach (BL→MOV X0,#0 at two early sites) was patching vnode-type
+ precondition assertions, not the actual kill checks — caused CBZ→panic.
3. `task_conversion_eval_internal` guard bypass
- Locator: unique cmp/branch motif:
`ldr xN,[xN,#imm] ; cmp xN,x0 ; b.eq ; cmp xN,x1 ; b.eq`.
@@ -248,9 +252,9 @@ with capstone semantic matching and keystone-generated patch bytes only:
**Group B: Simple patches (string-anchored / pattern-matched)**
5. `_postValidation` additional CMP bypass
-6. `_proc_security_policy` stub (mov x0,#0; ret)
+6. `_proc_security_policy` stub (mov x0,#0; ret) — FIXED: was patching copyio instead
7. `_proc_pidinfo` pid-0 guard NOP (2 sites)
-8. `_convert_port_to_map_with_flavor` panic skip
+8. `_convert_port_to_map_with_flavor` panic skip — FIXED: was patching PAC check instead
9. `_vm_fault_enter_prepare` PMAP check NOP
10. `_vm_map_protect` permission check skip
11. `___mac_mount` MAC check bypass (NOP + mov x8,xzr)
@@ -269,6 +273,7 @@ with capstone semantic matching and keystone-generated patch bytes only:
21. `_cred_label_update_execve` cs_flags shellcode
22. `_syscallmask_apply_to_proc` filter mask shellcode
23. `_hook_cred_label_update_execve` ops table + vnode_getattr shellcode
+ - Code cave restricted to __TEXT_EXEC only (__PRELINK_TEXT excluded due to KTRR)
24. `kcall10` syscall 439 replacement shellcode
## Cross-Version Dynamic Snapshot
diff --git a/scripts/patchers/kernel_jb.py b/scripts/patchers/kernel_jb.py
index ef4b6fa2..f967eeea 100644
--- a/scripts/patchers/kernel_jb.py
+++ b/scripts/patchers/kernel_jb.py
@@ -67,9 +67,9 @@ class KernelJBPatcher(
# Group B: Simple patches (string-anchored / pattern-matched)
self.patch_post_validation_additional() # B5
- # self.patch_proc_security_policy() # B6 (PANIC)
+ self.patch_proc_security_policy() # B6 (fixed: was patching copyio)
self.patch_proc_pidinfo() # B7
- # self.patch_convert_port_to_map() # B8 (PANIC)
+ self.patch_convert_port_to_map() # B8 (fixed: was patching PAC check)
self.patch_vm_fault_enter_prepare() # B9
self.patch_vm_map_protect() # B10
self.patch_mac_mount() # B11
diff --git a/scripts/patchers/kernel_jb_patch_port_to_map.py b/scripts/patchers/kernel_jb_patch_port_to_map.py
index 3b24c82d..a67755c8 100644
--- a/scripts/patchers/kernel_jb_patch_port_to_map.py
+++ b/scripts/patchers/kernel_jb_patch_port_to_map.py
@@ -1,12 +1,23 @@
"""Mixin: KernelJBPatchPortToMapMixin."""
-from .kernel_jb_base import ARM64_OP_IMM
+from .kernel_jb_base import ARM64_OP_IMM, NOP, struct, _rd32
class KernelJBPatchPortToMapMixin:
def patch_convert_port_to_map(self):
"""Skip panic in _convert_port_to_map_with_flavor.
+
Anchor: 'userspace has control access to a kernel map' panic string.
+
+ The function flow around the kernel_map check is:
+ CMP X16, X8 ; compare map ptr with kernel_map
+ B.NE normal_path ; if NOT kernel_map, continue normally
+ ; fall through: set up panic args and call _panic (noreturn)
+
+ Fix: walk backward from the string ref to find the B.cond that
+ guards the panic fall-through, then make it unconditional.
+ This causes the kernel_map case to take the normal path instead
+ of panicking, allowing userspace to access the kernel map.
"""
self._log("\n[JB] _convert_port_to_map_with_flavor: skip panic")
@@ -21,25 +32,40 @@ class KernelJBPatchPortToMapMixin:
return False
for adrp_off, add_off, _ in refs:
- bl_panic = self._find_bl_to_panic_in_range(
- add_off, min(add_off + 0x40, self.size)
- )
- if bl_panic < 0:
- continue
- resume_off = bl_panic + 4
- err_lo = adrp_off - 0x40
- for back in range(adrp_off - 4, max(adrp_off - 0x200, 0), -4):
- target, kind = self._decode_branch_target(back)
- if target is not None and err_lo <= target <= bl_panic + 4:
- b_bytes = self._encode_b(back, resume_off)
- if b_bytes:
- self.emit(
- back,
- b_bytes,
- f"b #0x{resume_off - back:X} "
- f"[_convert_port_to_map skip panic]",
- )
- return True
+ # Walk backward from the string ADRP to find CMP + B.cond
+ # The pattern is: CMP Xn, Xm; B.NE target
+ # We want to change B.NE to unconditional B (always skip panic).
+ for back in range(adrp_off - 4, max(adrp_off - 0x60, 0), -4):
+ d = self._disas_at(back, 2)
+ if not d or len(d) < 2:
+ continue
+ i0, i1 = d[0], d[1]
+ # Look for CMP + B.NE/B.CS/B.HI (conditional branch away from
+ # the panic path). The branch target should be AFTER the panic
+ # call (i.e., forward past the string ref region).
+ if i0.mnemonic != "cmp":
+ continue
+ if not i1.mnemonic.startswith("b."):
+ continue
+ # Decode the branch target
+ target, kind = self._decode_branch_target(back + 4)
+ if target is None:
+ continue
+ # The branch should go FORWARD past the panic (beyond adrp_off)
+ if target <= adrp_off:
+ continue
+
+ # Found the conditional branch that skips the panic path.
+ # Replace it with unconditional B to same target.
+ b_bytes = self._encode_b(back + 4, target)
+ if b_bytes:
+ self.emit(
+ back + 4,
+ b_bytes,
+ f"b 0x{target:X} "
+ f"[_convert_port_to_map skip panic]",
+ )
+ return True
self._log(" [-] branch site not found")
return False