Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Find the LLB rootfs and TXM CFW patches on cloudOS 18

Maximilian Paß39061e615ea3
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/IBoot/BasePatches/IBootPatchRootfsBypass.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/IBoot/BasePatches/IBootPatchRootfsBypass.swift
index 08995b90..c97545c1 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/IBoot/BasePatches/IBootPatchRootfsBypass.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/IBoot/BasePatches/IBootPatchRootfsBypass.swift
@@ -12,21 +12,167 @@ extension IBootPatcher {
/// Python: `patch_rootfs_bypass()`
func patchRootfssBypass() {
// 4a: cbz/cbnz before error code 0x3B7 → unconditional b
- patchCbzBeforeError(errorCode: 0x3B7, description: "rootfs: skip sig check (0x3B7)")
+ let sigCheck = patchCbzBeforeError(errorCode: 0x3B7, description: "rootfs: skip sig check (0x3B7)")
// 4b: NOP b.hs after cmp x8, #0x400
patchBhsAfterCmp0x400()
// 4c: cbz/cbnz before error code 0x3C2 → unconditional b
- patchCbzBeforeError(errorCode: 0x3C2, description: "rootfs: skip sig verify (0x3C2)")
+ let sigVerify = patchCbzBeforeError(errorCode: 0x3C2, description: "rootfs: skip sig verify (0x3C2)")
// 4d: NOP cbz x8 null check (ldr x8, [xN, #0x78])
- patchNullCheck0x78()
+ let nullCheck = patchNullCheck0x78()
// 4e: cbz/cbnz before error code 0x110 → unconditional b
- patchCbzBeforeError(errorCode: 0x110, description: "rootfs: skip size verify (0x110)")
+ let sizeVerify = patchCbzBeforeError(errorCode: 0x110, description: "rootfs: skip size verify (0x110)")
+
+ // The error codes are iBoot's __LINE__ numbers, so another release
+ // moves them (iOS 18.6: 0x3AB, 0x3B6 and 0x10D). Whatever the literals
+ // missed is found again by the shape of the two functions.
+ if !sigCheck || !sigVerify {
+ patchRootfsGatesByShape(sigCheck: !sigCheck, sigVerify: !sigVerify)
+ }
+ if !nullCheck || !sizeVerify {
+ patchSizeVerifyByShape(nullCheck: !nullCheck, sizeVerify: !sizeVerify)
+ }
+ }
+
+ // MARK: - Shape fallback
+
+ /// The function holding the unique `cmp x8, #0x400 ; b.hs` size gate, as a
+ /// file-offset range from its `pacibsp` to the next one.
+ private func rootfsFunction() -> (start: Int, end: Int)? {
+ let sites = bhsAfterCmp0x400Sites()
+ guard sites.count == 1 else { return nil }
+ return functionRange(containing: sites[0])
+ }
+
+ /// From `pacibsp` back from `offset` to the next `pacibsp` after it.
+ private func functionRange(containing offset: Int) -> (start: Int, end: Int)? {
+ let raw = buffer.original
+ var start = offset & ~3
+ while start >= 0, raw.loadLE(UInt32.self, at: start) != ARM64.pacibspU32 {
+ start -= 4
+ }
+ guard start >= 0 else { return nil }
+ var end = start + 4
+ while end + 4 <= raw.count, raw.loadLE(UInt32.self, at: end) != ARM64.pacibspU32 {
+ end += 4
+ }
+ return (start, end)
+ }
+
+ /// `cbz w0, <ok> ; mov w8, #<line>` gates in a range: a verification
+ /// call's result tested, and the line number of its failure set.
+ private func verificationGates(in range: (start: Int, end: Int), thenBranch: Bool = false) -> [(off: Int, target: Int)] {
+ let insns = disasm.disassemble(in: buffer.original, at: range.start, count: (range.end - range.start) / 4)
+ var gates: [(off: Int, target: Int)] = []
+ for (index, insn) in insns.enumerated() where index + 2 < insns.count {
+ let next = insns[index + 1]
+ guard insn.mnemonic == "cbz",
+ let ops = insn.detail?.operands, ops.count == 2,
+ ops[0].type == .register, ops[0].reg == .w(0),
+ ops[1].type == .immediate,
+ next.mnemonic == "mov",
+ let movOps = next.detail?.operands, movOps.count == 2,
+ movOps[0].type == .register, movOps[0].reg == .w(8),
+ movOps[1].type == .immediate
+ else { continue }
+ if thenBranch, insns[index + 2].mnemonic != "b" { continue }
+ gates.append((Int(insn.address), Int(ops[1].imm)))
+ }
+ return gates
+ }
+
+ private func emitGateBranch(_ gate: (off: Int, target: Int), id: String, description: String) {
+ guard let bInsn = ARM64Encoder.encodeB(from: gate.off, to: gate.target) else {
+ if verbose { print(" [-] \(description): B encoding out of range") }
+ return
+ }
+ emit(gate.off, bInsn, id: id, description: description)
+ }
+
+ /// The signature check and verification are the last two gates of the
+ /// rootfs function: 26.x `0x3B7` and `0x3C2`, 18.6 `0x3AB` and `0x3B6`.
+ private func patchRootfsGatesByShape(sigCheck: Bool, sigVerify: Bool) {
+ guard let range = rootfsFunction() else {
+ if verbose { print(" [-] rootfs gates by shape: rootfs function not found") }
+ return
+ }
+ let gates = verificationGates(in: range)
+ guard gates.count >= 2 else {
+ if verbose { print(" [-] rootfs gates by shape: \(gates.count) cbz w0 ; mov w8 gates in the rootfs function") }
+ return
+ }
+ if sigCheck {
+ emitGateBranch(gates[gates.count - 2], id: "\(component)-boot-rootfs.cbz_0x3b7",
+ description: "rootfs: skip sig check (by shape)")
+ }
+ if sigVerify {
+ emitGateBranch(gates[gates.count - 1], id: "\(component)-boot-rootfs.cbz_0x3c2",
+ description: "rootfs: skip sig verify (by shape)")
+ }
+ }
+
+ /// The size verification is in the caller of the function laid out right
+ /// after the rootfs function that also loads `[xN, #0x78]` and tests it
+ /// for null: the null check is the first such pair, the size check the
+ /// last `cbz w0 ; mov w8, #<line> ; b` gate (26.x `0x110`, 18.6 `0x10D`).
+ private func patchSizeVerifyByShape(nullCheck: Bool, sizeVerify: Bool) {
+ guard let rootfs = rootfsFunction() else {
+ if verbose { print(" [-] size verify by shape: rootfs function not found") }
+ return
+ }
+ let raw = buffer.original
+ var callers = Set<Int>()
+ var off = 0
+ while off + 4 <= raw.count {
+ let word = raw.loadLE(UInt32.self, at: off)
+ if word >> 26 == 0x25 {
+ let imm26 = Int32(bitPattern: (word & 0x03FF_FFFF) << 6) >> 6
+ if off + Int(imm26) * 4 == rootfs.end, let range = functionRange(containing: off) {
+ callers.insert(range.start)
+ }
+ }
+ off += 4
+ }
+
+ var found: [(nullCheck: Int, gate: (off: Int, target: Int))] = []
+ for start in callers.sorted() {
+ guard let range = functionRange(containing: start) else { continue }
+ let insns = disasm.disassemble(in: raw, at: range.start, count: (range.end - range.start) / 4)
+ var nullSite: Int?
+ for (index, insn) in insns.enumerated() where index + 1 < insns.count {
+ let next = insns[index + 1]
+ guard insn.mnemonic == "ldr",
+ let ops = insn.detail?.operands, ops.count == 2,
+ ops[0].type == .register, ops[1].type == .memory, ops[1].mem.disp == 0x78,
+ next.mnemonic == "cbz",
+ let nextOps = next.detail?.operands, nextOps.count == 2,
+ nextOps[0].type == .register, nextOps[0].reg == ops[0].reg
+ else { continue }
+ nullSite = Int(next.address)
+ break
+ }
+ if let nullSite, let gate = verificationGates(in: range, thenBranch: true).last {
+ found.append((nullSite, gate))
+ }
+ }
+ guard found.count == 1 else {
+ if verbose { print(" [-] size verify by shape: \(found.count) candidate functions") }
+ return
+ }
+ if nullCheck {
+ emit(found[0].nullCheck, ARM64.nop, id: "\(component)-boot-rootfs.null_check_0x78",
+ description: "rootfs: NOP cbz null check (#0x78, by shape)")
+ }
+ if sizeVerify {
+ emitGateBranch(found[0].gate, id: "\(component)-boot-rootfs.cbz_0x110",
+ description: "rootfs: skip size verify (by shape)")
+ }
}
/// Find unique `mov w8, #<errorCode>` and convert the cbz/cbnz 4 bytes before
/// it into an unconditional branch to the same target.
/// Python: `_patch_cbz_before_error()`
- private func patchCbzBeforeError(errorCode: UInt32, description: String) {
+ @discardableResult
+ private func patchCbzBeforeError(errorCode: UInt32, description: String) -> Bool {
let pattern = encodedMovW8(errorCode)
let locs = buffer.findAll(pattern)
@@ -34,7 +180,7 @@ extension IBootPatcher {
if verbose {
print(" [-] \(description): expected 1 'mov w8, #0x\(String(errorCode, radix: 16))', found \(locs.count)")
}
- return
+ return false
}
let errOff = locs[0]
@@ -44,27 +190,28 @@ extension IBootPatcher {
if verbose {
print(" [-] \(description): no instruction at 0x\(String(format: "%X", cbzOff))")
}
- return
+ return false
}
guard insn.mnemonic == "cbz" || insn.mnemonic == "cbnz" else {
if verbose {
print(" [-] \(description): expected cbz/cbnz at 0x\(String(format: "%X", cbzOff)), got \(insn.mnemonic)")
}
- return
+ return false
}
// Extract branch target from the operand string (last operand is the immediate)
- guard let detail = insn.detail, detail.operands.count >= 2 else { return }
+ guard let detail = insn.detail, detail.operands.count >= 2 else { return false }
let target = Int(detail.operands[1].imm)
guard let bInsn = ARM64Encoder.encodeB(from: cbzOff, to: target) else {
if verbose {
print(" [-] \(description): B encoding out of range")
}
- return
+ return false
}
emit(cbzOff, bInsn, id: "\(component)-boot-rootfs.cbz_0x\(String(errorCode, radix: 16))", description: description)
+ return true
}
/// NOP the `b.hs` of the unique `cmp x8,#0x400 ; b.hs` rootfs size gate.
@@ -72,17 +219,7 @@ extension IBootPatcher {
/// (the other two are followed by `b.hi`); 26.1/26.3 have just the one.
/// Python: `_patch_bhs_after_cmp_0x400()`
private func patchBhsAfterCmp0x400() {
- var bhsSites: [Int] = []
- for insns in chunkedDisasm() {
- for insn in insns where insn.mnemonic == "cmp" && insn.operandString == "x8, #0x400" {
- let bhsOff = Int(insn.address) + 4
- guard let next = disasm.disassembleOne(in: buffer.original, at: bhsOff),
- next.mnemonic == "b.hs" else { continue }
- if !bhsSites.contains(bhsOff) {
- bhsSites.append(bhsOff)
- }
- }
- }
+ let bhsSites = bhsAfterCmp0x400Sites()
guard bhsSites.count == 1 else {
if verbose {
@@ -99,10 +236,28 @@ extension IBootPatcher {
)
}
+ /// Every `b.hs` that follows a `cmp x8, #0x400`.
+ private func bhsAfterCmp0x400Sites() -> [Int] {
+ var bhsSites: [Int] = []
+ for insns in chunkedDisasm() {
+ for insn in insns where insn.mnemonic == "cmp" && insn.operandString == "x8, #0x400" {
+ let bhsOff = Int(insn.address) + 4
+ guard let next = disasm.disassembleOne(in: buffer.original, at: bhsOff),
+ next.mnemonic == "b.hs" else { continue }
+ if !bhsSites.contains(bhsOff) {
+ bhsSites.append(bhsOff)
+ }
+ }
+ }
+
+ return bhsSites
+ }
+
/// Find `ldr xR, [xN, #0x78]; cbz xR` preceding the unique `mov w8, #0x110`
/// and NOP the cbz.
/// Python: `_patch_null_check_0x78()`
- private func patchNullCheck0x78() {
+ @discardableResult
+ private func patchNullCheck0x78() -> Bool {
let pattern = encodedMovW8(0x110)
let locs = buffer.findAll(pattern)
@@ -110,7 +265,7 @@ extension IBootPatcher {
if verbose {
print(" [-] rootfs null check: expected 1 'mov w8, #0x110', found \(locs.count)")
}
- return
+ return false
}
let errOff = locs[0]
@@ -137,7 +292,7 @@ extension IBootPatcher {
id: "\(component)-boot-rootfs.null_check_0x78",
description: "rootfs: NOP cbz x8 null check (#0x78)",
)
- return
+ return true
}
scan -= 4
}
@@ -145,5 +300,6 @@ extension IBootPatcher {
if verbose {
print(" [-] rootfs null check: ldr+cbz #0x78 pattern not found")
}
+ return false
}
}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/TXM/TXMDevPatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/TXM/TXMDevPatcher.swift
index fbba6951..f046ed19 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/TXM/TXMDevPatcher.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/TXM/TXMDevPatcher.swift
@@ -69,6 +69,24 @@ public final class TXMDevPatcher: TXMPatcher {
}
off += 4
}
+ guard refs.isEmpty else { return refs }
+
+ // The iOS 18 TXMs reach their strings with a lone `adr` (the linker
+ // relaxed `adrp` + `add` into `adr` + `nop`). Such a reference is its
+ // own ADD: both offsets are the `adr`.
+ off = 0
+ while off + 4 <= size {
+ let raw = buffer.readU32(at: off)
+ // ADR: bit[31]=0, bits[28:24]=10000
+ if raw & 0x9F00_0000 == 0x1000_0000 {
+ let imm21 = (((raw >> 5) & 0x7FFFF) << 2) | ((raw >> 29) & 0x3)
+ let signedImm21 = Int(Int32(bitPattern: imm21 << 11) >> 11)
+ if off + signedImm21 == targetOff {
+ refs.append((off, off))
+ }
+ }
+ off += 4
+ }
return refs
}
@@ -203,6 +221,35 @@ public final class TXMDevPatcher: TXMPatcher {
return starts.first
}
+ /// The `strb w0, [x20, #flag]` for the flag byte the debugger gate at `fn`
+ /// tests with `add xT, xObj, #flag ; ldarb wF, [xT]`.
+ private func debuggerGateFlagStore(_ fn: Int) -> (bytes: Data, label: String)? {
+ var flags = Set<Int64>()
+ var scan = fn
+ let end = min(fn + 0x400, buffer.count - 8)
+ while scan < end {
+ if scan > fn, buffer.readU32(at: scan) == ARM64.pacibspU32 { break }
+ if let add = disasm.disassembleOne(in: buffer.data, at: scan),
+ add.mnemonic == "add",
+ let ops = add.detail?.operands, ops.count == 3,
+ ops[0].type == .register, ops[1].type == .register, ops[2].type == .immediate,
+ let load = disasm.disassembleOne(in: buffer.data, at: scan + 4),
+ load.mnemonic == "ldarb",
+ let loadOps = load.detail?.operands, loadOps.count == 2,
+ loadOps[1].type == .memory, loadOps[1].mem.base == ops[0].reg
+ {
+ flags.insert(ops[2].imm)
+ }
+ scan += 4
+ }
+ guard flags.count == 1 else { return nil }
+ switch flags.first! {
+ case 0x30: return (ARM64.strbW0X20_30, "0x30")
+ case 0x20: return (ARM64.strbW0X20_20, "0x20")
+ default: return nil
+ }
+ }
+
// MARK: - Dev Patches
/// Force the selector-24 pre-check policy walk to admit every manifest.
@@ -545,6 +592,14 @@ public final class TXMDevPatcher: TXMPatcher {
}
let stubOff = stubs[0]
+ // The flag the shellcode raises is the byte the gate itself tests
+ // (`add xT, xObj, #flag ; ldarb wF, [xT] ; tbnz wF, #0`): +0x30 in the
+ // 26.x TXMs, +0x20 in the iOS 18 ones, where +0x30 holds a pointer.
+ guard let flagStore = debuggerGateFlagStore(fn) else {
+ log(" [-] TXM: selector42|29 flag offset not recognised in the debugger gate")
+ return
+ }
+
guard let cave = findUdfCave(minInsns: 6, nearOff: stubOff) else {
log(" [-] TXM: no UDF cave found for selector42|29 shellcode")
return
@@ -577,9 +632,9 @@ public final class TXMDevPatcher: TXMPatcher {
)
emit(
cave + 8,
- ARM64.strbW0X20_30,
+ flagStore.bytes,
patchID: "txm-boot-sel42_29.shell_strb",
- description: "selector42|29 shellcode: strb w0,[x20,#0x30]",
+ description: "selector42|29 shellcode: strb w0,[x20,#\(flagStore.label)]",
)
emit(
cave + 12,
@@ -682,16 +737,46 @@ public final class TXMDevPatcher: TXMPatcher {
}
}
- guard cands.count == 1 else {
- log(" [-] TXM: expected 1 developer mode guard, found \(cands.count)")
+ if cands.count == 1 {
+ emit(
+ cands[0],
+ ARM64.nop,
+ patchID: "txm-boot-developer_mode_bypass",
+ description: "developer mode bypass",
+ )
return
}
- emit(
- cands[0],
- ARM64.nop,
- patchID: "txm-boot-developer_mode_bypass",
- description: "developer mode bypass",
- )
+ // iOS 18 compiles the same decision the other way round: the block
+ // that enables developer mode by system policy (`mov w, #1` then the
+ // string) sits out of line, and `cbz/tbz w9` earlier in the function
+ // jumps to it. Making that jump unconditional enables it the same way.
+ if cands.isEmpty, let ref = refs.first.map({ $0.addOff }), let fn = findFuncStart(ref) {
+ var jumps: [(off: Int, target: Int)] = []
+ var scan = fn
+ while scan < ref {
+ if let ins = disasm.disassembleOne(in: buffer.data, at: scan),
+ ins.mnemonic == "cbz" || ins.mnemonic == "tbz",
+ ins.operandString.hasPrefix("w9,"),
+ let last = ins.detail?.operands.last, last.type == .immediate,
+ case let target = Int(last.imm),
+ target >= ref - 8, target <= ref
+ {
+ jumps.append((scan, target))
+ }
+ scan += 4
+ }
+ if jumps.count == 1, let b = ARM64Encoder.encodeB(from: jumps[0].off, to: jumps[0].target) {
+ emit(
+ jumps[0].off,
+ b,
+ patchID: "txm-boot-developer_mode_bypass",
+ description: "developer mode bypass: always take the system-policy enable",
+ )
+ return
+ }
+ }
+
+ log(" [-] TXM: expected 1 developer mode guard, found \(cands.count)")
}
}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift
index 5f66962f..41d51397 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift
@@ -664,6 +664,7 @@ struct ARM64EncoderKeystoneParityTests {
("cmp w0, w0", ARM64.cmpW0W0, 0x6B00_001F),
("cmp x0, x0", ARM64.cmpX0X0, 0xEB00_001F),
("strb w0, [x20, #0x30]", ARM64.strbW0X20_30, 0x3900_C280),
+ ("strb w0, [x20, #0x20]", ARM64.strbW0X20_20, 0x3900_8280),
("cbz x2, #8", ARM64.cbzX2_8, 0xB400_0042),
("str x0, [x2]", ARM64.strX0X2, 0xF900_0040),
("cmp xzr, xzr", ARM64.cmpXzrXzr, 0xEB1F_03FF),
diff --git a/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/ARM64/ARM64Constants.swift b/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/ARM64/ARM64Constants.swift
index 1c757702..68d72e0d 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/ARM64/ARM64Constants.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/ARM64/ARM64Constants.swift
@@ -73,6 +73,9 @@ public enum ARM64 {
/// STRB W0, [X20, #0x30]
public static let strbW0X20_30 = encodeU32(0x3900_C280)
+ /// STRB W0, [X20, #0x20]
+ public static let strbW0X20_20 = encodeU32(0x3900_8280)
+
/// STRB W8, [X19]
public static let strbW8X19 = encodeU32(0x3900_0268)