Run a virtual iPhone on an Apple Silicon Mac.
Swift
cfw: jb: Fix iOS 27 random resprings (FileProvider-scoped vnode_check_open)
zqxwcefac586a95dc5
diff --git a/research/0_binary_patch_comparison.md b/research/0_binary_patch_comparison.mdindex 75569c9f..8f1075c3 100644--- a/research/0_binary_patch_comparison.md+++ b/research/0_binary_patch_comparison.md@@ -122,7 +122,7 @@### JB-Only Kernel Methods (Reference List)-> **⚠ iOS-27 hard-gate (2026-07-20).** Every patch below whose Purpose cites "iOS 27" / "27.0 on the 26.4 kernel" is **hard-gated to a 27.x base** via `KernelJBPatcher.applyIOS27` — set by `FirmwarePipeline` from the iPhone base `ProductVersion` (mirroring the `applyExcGuard`/iOS-18 mechanism at patch 27 above; standalone `patch-component` defaults it true, override with `--target-os`). Gated set: **JB-02b, JB-02d, JB-09 (the `ops[124]` entry only — the 201..316 hooks stay), JB-10b, JB-26, JB-27, JB-28**. On an 18.x/26.x base **none** of them run. This supersedes the per-row "No-op-in-effect for version-matched userlands" notes: those describe the effect *if applied*, but **JB-27 is NOT a no-op on 26.x** — its `cmp 0x588→0x6e0` retarget makes the 26.x userclient reject its own native 0x588 SwapEnd → dead display (the 26.5 regression that motivated the hard-gate). **Verified 2026-07-20:** a 26.5 base produces a **byte-identical** kernelcache to pre-branch `main` (`patch-component --component kernel-jb --target-os 26.5` vs main's output — `cmp` clean, 83 records each), and a 27.0 base additionally emits exactly the 11 gated records.+> **⚠ iOS-27 hard-gate (2026-07-20).** Every patch below whose Purpose cites "iOS 27" / "27.0 on the 26.4 kernel" is **hard-gated to a 27.x base** via `KernelJBPatcher.applyIOS27` — set by `FirmwarePipeline` from the iPhone base `ProductVersion` (mirroring the `applyExcGuard`/iOS-18 mechanism at patch 27 above; standalone `patch-component` defaults it true, override with `--target-os`). Gated set: **JB-02b, JB-02d, JB-09 (the `ops[124]` add, plus on 27 the `ops[267]` removal — handed to JB-29; the other 201..316 hooks stay), JB-10b, JB-26, JB-27, JB-28, JB-29**. On an 18.x/26.x base **none** of them run. This supersedes the per-row "No-op-in-effect for version-matched userlands" notes: those describe the effect *if applied*, but **JB-27 is NOT a no-op on 26.x** — its `cmp 0x588→0x6e0` retarget makes the 26.x userclient reject its own native 0x588 SwapEnd → dead display (the 26.5 regression that motivated the hard-gate). **Verified 2026-07-20 (JB-29 addendum 2026-07-23):** a 26.5 base produces a **byte-identical** kernelcache to pre-branch `main` (`patch-component --component kernel-jb --target-os 26.5` vs main's output — `cmp` clean, 83 records each; re-confirmed unchanged after JB-29 by `sha256`, since `ops[267]` stays blanket-neutered on 26.5), and a 27.0 base emits **95** records on the `c0ecdb4b` deployment kernel: the 11 gated records above **plus JB-29's 2** (`jb.fpfs_scoped_open.{ops_retarget,cave}`), with `sandbox_ext_267` suppressed (JB-29 owns `ops[267]` on 27 → the FileProvider-scoped trampoline instead of the blanket allow-stub).| # | Group | Method | Function | Purpose | JB Enabled || ----- | ----- | ------------------------------------- | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :--------: |@@ -157,6 +157,7 @@| JB-26 | B | `patch_iomfb_swapend_variable_size` | IOMFB userclient method-5 (SwapEnd) `__DATA_CONST` dispatch entry (`checkStructureInputSize`) | **iOS-27 VZ-view fix, kernel half — paired with DSC force-kern (DSC-patch item 11).** The 26.4 userclient's method-5 dispatch entry hard-checks `checkStructureInputSize == 0x588`; forced-kern iOS 27 sends its native `0x6e0`. Rewrite the size field to `kIOUCVariableStructureSize (0xFFFFFFFF)` so `IOUserClient::externalMethod` accepts 27's struct and reaches the handler. Anchor (structural): the sole `__DATA_CONST` entry `{ptr(ptrauth, top-byte≥0x80), scalarIn=0, structIn=0x588, scalarOut=0, structOut=0}` (verified unique; decompressed file-off 0x9c7228). No-op-in-effect for version-matched 26.x (sends 0x588). Re-enabled 2026-07-15 (was disabled when 27 present-path was still unknown). | Y || JB-27 | B | `patch_iomfb_swapend_handler_size` | method-5 handler internal size gate (`cmp w2,#0x588 ; b.ne <err>`) | Companion to JB-26: beyond the dispatch-table check the handler re-checks the struct size (`cmp w2,#0x588 ; b.ne <kIOReturnBadArgument>`; verified unique at decompressed file-off 0x16ae22c; success path forwards the raw struct ptr to a `vtable+0x590` paravirt swap method). Retarget the `cmp` immediate to `0x6e0` so forced-kern iOS 27's native SwapEnd reaches real swap processing (27's IOMFBSwapRec prefix matches 26.x → handler reads valid fields). Semantic anchor (`cmp w2,#imm` word + following `b.ne` decode). Enabled together with JB-26 + DSC force-kern for iOS 27. | Y || JB-28 | A | `patch_disk_images2_client_abi` | `com.apple.driver.AppleDiskImages2` kext: `DIDeviceCreatorUserClient::CreateDevice` + `DIDeviceIOUserClient::Connect` ABI-version gates, and the `AllocPortsArray`/`RegisterNotificationPort` notif-port sizing | **iOS-27 DDI (`/System/Developer`) auto-mount — attach layer.** `pymobiledevice3 mounter auto-mount` on the 26.4-kernel / 27-userland hybrid fails at attach: the kernel DiskImages2 driver is ABI v9, the 27 userland's DiskImages2 controller/daemon is ABI v11 (`DIDeviceCreatorUserClient::CreateDevice: Incompatible client: expected ABI version 9 actual 11`). Three layers: **GATE1** NOPs the CreateDevice controller-ABI `cmp #9 ; b.ne` reject; **GATE2b** NOPs the Connect daemon-ABI `cmp #9 ; b.ne` reject (both anchored on the C++ signature cstring → unique `cmp #9`/`b.ne`; version-robust; no-op-in-effect on version-matched userlands where ABI 9==9). **GATE2** (array + 2 bound checks) fixes a RegisterNotificationPort off-by-one (userland registers at index==maxPorts, one past the array) by widening the AllocPortsArray allocation (`lsl x1,xN,#3` → `mov x1,#0x4000`) AND both bound-check field loads (`ldrh [.,#0xd8]`/`ldr [.,#0xe8]` → `mov wD,#0x800`); applied **all-or-nothing** (widening the bound checks without the backing array would let RegisterNotificationPort write past it → corruption) and skipped/logged on builds whose notif-port codegen differs (the off-by-one is 26.4-hybrid-specific). Pairs with the sandbox ops[124] allow (JB-09) and the diskimagesiod userland patch (CFW binary-patch #13). Anchors structural (Capstone decode of the pinned function's instructions); replacement bytes from the Keystone-backed `ARM64Encoder`. **GATE2a anchor note:** the AllocPortsArray size-shift is matched on `lsl` mnemonic + destination `x1` (the unique size-writing lsl in the function) — NOT a 3-operand `lsl xd,xn,#imm` shape, because Capstone on this toolchain decodes the lsl-immediate (a UBFM alias) as **2 operands**; requiring 3 operands makes GATE2 silently skip (the all-or-nothing returns a harmless no-op). Verified on the `c0ecdb4b` 26.4 deployment kernel via `patch-component --component kernel-jb --records-out`: all five di2 records emit (`di2_createdevice_abi`, `di2_connect_abi`, `di2_allocports_size`, `di2_notif_boundcheck_d8`, `di2_notif_boundcheck_e8`) — always verify the di2 records EMIT, not merely that the JB suite reports "no failures". No-op-in-effect for version-matched userlands. | Y |+| JB-29 | C | `patch_fpfs_scoped_vnode_open` | Sandbox MACF `mpo_vnode_check_open` (`ops[267]`) → per-process trampoline code cave | **iOS-27 random-respring fix (fpfs balloon).** JB-09 blanket-neuters `mpo_vnode_check_open` (`ops[267]` → allow) so processes can read `/var/jb`. But FileProvider's fpfs parent-walk (`fpfs_pkg_fd_lookup` → `openbyid` climbing via `getattrlistat(ATTR_CMN_PAROBJID)`) uses the stock check's EACCES at the domain-container boundary as its terminus; neutered, the walk climbs unbounded → `ResolverService` (FileProviderResolver) balloons to ~12 GB → `vm-compressor-space-shortage` jetsam → backboardd killed → **respring** (seen on 27b4/24A5390f). Fix keeps the global bypass and enforces the real check for the FileProvider daemons only: on 27, `ops[267]` is left un-neutered (removed from JB-09's blanket list — conditional `if !applyIOS27` in `patchSandboxHooksExtended`), then retargeted to a 20-insn code-cave trampoline that inlines `current_proc` (`mrs tpidr_el1` → `ldr [+0x3F0]` uthread → `ldr [+0x18]` proc), loads `p_comm` (`+0x56C`), compares the first 8 bytes to `"Resolver"`/`"fileprov"`, and on match `b`s to the real `vnode_check_open` (restores the terminus) else returns allow (`mov x0,#0 ; ret` → `/var/jb` reads + Sileo/TrollStore icons unaffected). Register-only (x8–x10), no frame/call (~18 insns/open). Struct offsets recovered via the VZ gdb stub on vphone600; cave bytes from the Keystone-backed `ARM64Encoder`, verified by Capstone round-trip; the `ops[267]` retarget preserves the auth-rebase high bits (same encoder family as JB-09). **VALIDATED on-device (2026-07-23, `17,3_27.0_24A5390f` + cloudOS 26.4, JB, `setup_machine` deploy): resprings stop; Sileo/TrollStoreLite icons render.** Backward-compat: a 26.5 base is unaffected (`ops[267]` still blanket-neutered there) — verified byte-identical kernelcache pre-vs-post (`cmp` clean, `sha256` unchanged). See `KernelJBPatchFpfsScopedOpen.swift`. | Y |### EXP-Only Kernel Methods (Reference List)diff --git a/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchFpfsScopedOpen.swift b/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchFpfsScopedOpen.swiftnew file mode 100644index 00000000..15934567--- /dev/null+++ b/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchFpfsScopedOpen.swift@@ -0,0 +1,110 @@+// KernelJBPatchFpfsScopedOpen.swift — iOS 27 only.+//+// The JB neuters Sandbox vnode_check_open globally so processes can reach /var/jb. But+// FileProvider's fpfs parent-walk needs the stock check to deny at the domain-container+// boundary; without it the walk climbs unbounded and ResolverService balloons → jetsam →+// respring. KernelJBPatchSandboxExtended therefore leaves ops[267] un-neutered on 27, and+// this patch retargets it to a trampoline that runs the real check only for the FileProvider+// daemons (matched by p_comm) and returns allow for everyone else — preserving the bypass.++import Foundation++extension KernelJBPatcher {+ private static let vnodeCheckOpenIndex = 267++ @discardableResult+ func patchFpfsScopedVnodeOpen() -> Bool {+ log("\n[JB] fpfs: scope vnode_check_open to FileProvider daemons")++ guard let opsTable = findSandboxOpsTableFpfs() else {+ log(" [-] sandbox ops table not found"); return false+ }+ let entryOff = opsTable + Self.vnodeCheckOpenIndex * 8+ guard entryOff + 8 <= buffer.count else { return false }+ let entryRaw = buffer.readU64(at: entryOff)+ guard (entryRaw & (1 << 63)) != 0 else {+ log(" [-] ops[267] not the real hook (neutered?): 0x\(String(format: "%016X", entryRaw))"); return false+ }+ let realHookOff = decodeChainedPtr(entryRaw)+ guard realHookOff >= 0, codeRanges.contains(where: { realHookOff >= $0.start && realHookOff < $0.end }) else {+ log(" [-] ops[267] target not in code"); return false+ }++ guard let caveOff = findCodeCave(size: 20 * 4) else { log(" [-] no code cave"); return false }+ guard let caveBytes = buildScopedOpenCave(caveOff: caveOff, realHookOff: realHookOff) else { return false }+ guard let newEntry = encodeAuthRebaseTarget(origVal: entryRaw, targetFoff: caveOff) else { return false }++ emit(entryOff, newEntry, patchID: "jb.fpfs_scoped_open.ops_retarget",+ description: "ops[267] -> FileProvider-scoped vnode_check_open trampoline")+ emit(caveOff, caveBytes, patchID: "jb.fpfs_scoped_open.cave",+ description: "trampoline: FileProvider daemons -> real check, else allow")+ return true+ }++ // vphone600 struct offsets recovered via the kernel gdb stub; cave bytes verified by+ // capstone round-trip. p_comm[0:8] little-endian: "Resolver" = ResolverService,+ // "fileprov" = fileproviderd.+ private func buildScopedOpenCave(caveOff: Int, realHookOff: Int) -> Data? {+ func movkX10(_ hw: [UInt16]) -> [UInt32] {+ var out: [UInt32] = [0xD280_0000 | (UInt32(hw[0]) << 5) | 10] // movz x10, #hw0+ for i in 1 ..< 4 { out.append(0xF280_0000 | (UInt32(i) << 21) | (UInt32(hw[i]) << 5) | 10) } // movk lsl #16*i+ return out+ }+ var w: [UInt32] = [+ 0xD538_D088, // mrs x8, tpidr_el1+ 0xF941_F908, // ldr x8, [x8, #0x3F0] ; uthread+ 0xF940_0D08, // ldr x8, [x8, #0x18] ; proc+ 0x9115_B108, // add x8, x8, #0x56C ; &p_comm+ 0xF940_0109, // ldr x9, [x8] ; p_comm[0:8]+ ]+ w += movkX10([0x6552, 0x6f73, 0x766c, 0x7265]) // x10 = "Resolver"+ w.append(0xEB0A_013F) // cmp x9, x10+ let beqA = w.count; w.append(0)+ w += movkX10([0x6966, 0x656c, 0x7270, 0x766f]) // x10 = "fileprov"+ w.append(0xEB0A_013F) // cmp x9, x10+ let beqB = w.count; w.append(0)+ w.append(0xD280_0000) // mov x0, #0+ w.append(0xD65F_03C0) // ret+ let enforce = w.count; w.append(0) // b <realHook>++ func beq(from: Int) -> UInt32 {+ let imm19 = UInt32(bitPattern: Int32(((enforce - from) * 4) >> 2)) & 0x7FFFF+ return 0x5400_0000 | (imm19 << 5) // b.eq (cond EQ)+ }+ w[beqA] = beq(from: beqA)+ w[beqB] = beq(from: beqB)+ guard let bData = encodeB(from: caveOff + enforce * 4, to: realHookOff) else { return nil }+ w[enforce] = bData.withUnsafeBytes { $0.load(as: UInt32.self) }.littleEndian++ guard w.count == 20 else { log(" [-] cave length drifted: \(w.count)"); return nil }+ var data = Data(capacity: 80)+ for x in w { withUnsafeBytes(of: x.littleEndian) { data.append(contentsOf: $0) } }+ return data+ }++ private func encodeAuthRebaseTarget(origVal: UInt64, targetFoff: Int) -> Data? {+ guard (origVal & (1 << 63)) != 0 else { return nil }+ let v = (origVal & ~UInt64(0xFFFF_FFFF)) | (UInt64(targetFoff) & 0xFFFF_FFFF)+ return withUnsafeBytes(of: v.littleEndian) { Data($0) }+ }++ private func findSandboxOpsTableFpfs() -> Int? {+ guard let seatbeltOff = buffer.findString("Seatbelt sandbox policy"),+ let pattern = "\u{0}Sandbox\u{0}".data(using: .utf8),+ let range = buffer.data.range(of: pattern) else { return nil }+ let sandboxOff = range.lowerBound + 1+ for seg in segments where (seg.name == "__DATA_CONST" || seg.name == "__DATA") && seg.fileSize > 40 {+ var i = Int(seg.fileOffset); let end = i + Int(seg.fileSize)+ while i <= end - 40 {+ defer { i += 8 }+ let v0 = buffer.readU64(at: i)+ guard v0 != 0, (v0 & (1 << 63)) == 0, (v0 & 0x7FF_FFFF_FFFF) == UInt64(sandboxOff) else { continue }+ let v1 = buffer.readU64(at: i + 8)+ guard (v1 & (1 << 63)) == 0, (v1 & 0x7FF_FFFF_FFFF) == UInt64(seatbeltOff) else { continue }+ let vOps = buffer.readU64(at: i + 32)+ if (vOps & (1 << 63)) == 0 { return Int(vOps & 0x7FF_FFFF_FFFF) }+ }+ }+ return nil+ }+}diff --git a/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchSandboxExtended.swift b/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchSandboxExtended.swiftindex ee33dfa8..8b08c9ba 100644--- a/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchSandboxExtended.swift+++ b/sources/FirmwarePatcher/Kernel/JBPatches/KernelJBPatchSandboxExtended.swift@@ -57,7 +57,7 @@ extension KernelJBPatcher {("vnode_check_ioctl", 261),("vnode_check_link", 264),("vnode_check_listextattr", 265),- ("vnode_check_open", 267),+ // vnode_check_open (267) is appended conditionally below.("vnode_check_readlink", 270),("vnode_check_setattrlist", 275),("vnode_check_setextattr", 276),@@ -82,6 +82,13 @@ extension KernelJBPatcher {hookIndices.append(("proc_check_syscall_unix", 124))}+ // On iOS 27, leave ops[267] real so KernelJBPatchFpfsScopedOpen can retarget it to a+ // FileProvider-scoped trampoline (the fpfs walk needs the stock check as its terminus,+ // else ResolverService balloons → respring). Neuter it as usual on every other base.+ if !applyIOS27 {+ hookIndices.append(("vnode_check_open", 267))+ }+var patched = 0for (hookName, idx) in hookIndices {let entryOff = opsTable + idx * 8diff --git a/sources/FirmwarePatcher/Kernel/KernelJBPatcher.swift b/sources/FirmwarePatcher/Kernel/KernelJBPatcher.swiftindex 9b864ed9..05a5e832 100644--- a/sources/FirmwarePatcher/Kernel/KernelJBPatcher.swift+++ b/sources/FirmwarePatcher/Kernel/KernelJBPatcher.swift@@ -93,6 +93,7 @@ public final class KernelJBPatcher: KernelJBPatcherBase, Patcher {patchContainerManagerUpcall()patchIomfbSwapEndVariableSize() // dispatch checkStructureInputSize → variablepatchIomfbSwapEndHandlerSize() // handler cmp w2,#0x588 → 0x6e0+ patchFpfsScopedVnodeOpen() // ops[267] → FileProvider-scoped trampoline (fpfs respring fix)}return patches