Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

kernel: apply EXC_GUARD (Mach port guard) disable for iOS 18 bases

zqxwce911152881e07
diff --git a/research/0_binary_patch_comparison.md b/research/0_binary_patch_comparison.md
index 0ae720e4..b76a0377 100644
--- a/research/0_binary_patch_comparison.md
+++ b/research/0_binary_patch_comparison.md
@@ -116,7 +116,9 @@
| 15 | `mov w0,#0` | `_handle_fsioc_graft` | Allow fsioc graft | Y | Y | Y |
| 16 | NOP (3x) | `handle_get_dev_by_role` | Bypass APFS role-lookup deny gates for boot mounts | Y | Y | Y |
| 17-26 | `mov x0,#0; ret` (5 hooks) | Sandbox MACF ops table | Stub 5 sandbox hooks | Y | Y | Y |
-| 27 | `PACIBSP→RET` | `_thread_guard_violation` | Disable EXC_GUARD delivery (match production behavior) | - | Y | - |
+| 27 | `PACIBSP→RET` | `_thread_guard_violation` | Disable fatal EXC_GUARD (Mach port guard) delivery. Dev variant always; regular/jb/exp gain it automatically on **iOS 18 bases** (18.6.2's runningboardd/SpringBoard trip `GUARD_TYPE_MACH_PORT` "flavor 10", a guard the 26.1 kernel enforces fatally, crash-looping the UI). 26.x bases boot without it and are unaffected. | 18† | Y | 18† |
+
+† iOS 18 bases only — auto-detected in `FirmwarePipeline` from `iPhone-BuildManifest.plist`'s `ProductVersion` (the pre-hybrid manifest fw_prepare preserves; the live BuildManifest reads the cloudOS 26.1 version). Passed to `KernelPatcher` as `applyExcGuard`, which gates patch 27.
### JB-Only Kernel Methods (Reference List)
diff --git a/sources/FirmwarePatcher/Kernel/KernelPatcher.swift b/sources/FirmwarePatcher/Kernel/KernelPatcher.swift
index 699452ce..aeffb14b 100644
--- a/sources/FirmwarePatcher/Kernel/KernelPatcher.swift
+++ b/sources/FirmwarePatcher/Kernel/KernelPatcher.swift
@@ -15,9 +15,17 @@ public final class KernelPatcher: KernelPatcherBase, Patcher {
/// When true, includes dev-only kernel patches (e.g. EXC_GUARD disable).
public var isDev: Bool = false
- public convenience init(data: Data, verbose: Bool = true, isDev: Bool) {
+ /// When true, apply the EXC_GUARD (Mach port guard) disable even on
+ /// non-dev variants. Set for iOS 18 bases: their older userland
+ /// (runningboardd/SpringBoard) trips a Mach port guard the 26.1 kernel
+ /// enforces fatally (EXC_GUARD, GUARD_TYPE_MACH_PORT "flavor 10"),
+ /// which crash-loops the UI. Scoped to iOS 18 bases so 26.x is unaffected.
+ public var applyExcGuard: Bool = false
+
+ public convenience init(data: Data, verbose: Bool = true, isDev: Bool, applyExcGuard: Bool = false) {
self.init(data: data, verbose: verbose)
self.isDev = isDev
+ self.applyExcGuard = applyExcGuard
}
// MARK: - Find All
@@ -44,9 +52,11 @@ public final class KernelPatcher: KernelPatcherBase, Patcher {
patchApfsMount() // 13-15
patchSandbox() // 16-25
- // Dev-only patches (not applied to regular or JB variants)
- if isDev {
- patchExcGuardBehavior() // 26 (dev only)
+ // EXC_GUARD (Mach port guard) disable — applied on the dev variant
+ // always, and on any variant with an iOS 18 base (see applyExcGuard).
+ // Not applied to 26.x bases, which boot without it.
+ if isDev || applyExcGuard {
+ patchExcGuardBehavior() // 26
}
return patches
diff --git a/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift b/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
index 268300f0..d6a9decb 100644
--- a/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
+++ b/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
@@ -82,6 +82,11 @@ public final class FirmwarePipeline {
let noVphoned: Bool
let loader: any FirmwareLoader
+ /// Set when the iPhone base is iOS 18.x (read from iPhone-BuildManifest.plist).
+ /// Gates the EXC_GUARD kernel patch, which iOS 18 bases need but 26.x don't.
+ /// Computed in `patchAll()` before `buildComponentList()` runs.
+ private var iosBaseIs18 = false
+
// MARK: - Init
public init(
@@ -112,6 +117,13 @@ public final class FirmwarePipeline {
log("[*] VM directory: \(vmDirectory.path)")
log("[*] Restore directory: \(restoreDir.path)")
+ // Detect the iPhone base iOS version (from the pre-hybrid manifest that
+ // fw_prepare preserves — the live BuildManifest.plist reads the cloudOS
+ // version, not the base). iOS 18 bases need the EXC_GUARD patch.
+ let baseVersion = Self.readBaseProductVersion(restoreDir)
+ iosBaseIs18 = baseVersion?.hasPrefix("18.") ?? false
+ log("[*] iPhone base iOS: \(baseVersion ?? "unknown")\(iosBaseIs18 ? " (enabling iOS-18 EXC_GUARD kernel patch)" : "")")
+
let components = buildComponentList()
log("[*] Patching \(components.count) boot-chain components ...")
@@ -180,6 +192,10 @@ public final class FirmwarePipeline {
func buildComponentList() -> [ComponentDescriptor] {
var components: [ComponentDescriptor] = []
+ // Captured by value into the patcher factory closures below (avoids
+ // capturing self). True only for iOS 18 bases; gates the EXC_GUARD patch.
+ let applyExcGuard = iosBaseIs18
+
// 1. AVPBooter — always present, lives in VM root.
// Patched for every non-less variant (regular/dev/jb/exp).
components.append(ComponentDescriptor(
@@ -277,7 +293,7 @@ public final class FirmwarePipeline {
[]
case .regular:
[{ data, verbose in
- KernelPatcher(data: data, verbose: verbose, isDev: false)
+ KernelPatcher(data: data, verbose: verbose, isDev: false, applyExcGuard: applyExcGuard)
}]
case .dev:
[{ data, verbose in
@@ -286,7 +302,7 @@ public final class FirmwarePipeline {
case .jb:
[
{ data, verbose in
- KernelPatcher(data: data, verbose: verbose, isDev: false)
+ KernelPatcher(data: data, verbose: verbose, isDev: false, applyExcGuard: applyExcGuard)
},
{ data, verbose in
KernelJBPatcher(data: data, verbose: verbose)
@@ -295,7 +311,7 @@ public final class FirmwarePipeline {
case .exp:
[
{ data, verbose in
- KernelPatcher(data: data, verbose: verbose, isDev: false)
+ KernelPatcher(data: data, verbose: verbose, isDev: false, applyExcGuard: applyExcGuard)
},
{ data, verbose in
KernelJBPatcher(data: data, verbose: verbose)
@@ -382,6 +398,20 @@ public final class FirmwarePipeline {
return restoreDir
}
+ /// Read the iPhone base `ProductVersion` from `iPhone-BuildManifest.plist`
+ /// (preserved by fw_prepare before the hybrid manifest overwrites
+ /// BuildManifest.plist). Returns nil if absent/unreadable — callers then
+ /// treat the base as non-iOS-18 (conservative).
+ static func readBaseProductVersion(_ restoreDir: URL) -> String? {
+ let url = restoreDir.appendingPathComponent("iPhone-BuildManifest.plist")
+ guard let data = try? Data(contentsOf: url),
+ let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
+ let dict = plist as? [String: Any],
+ let version = dict["ProductVersion"] as? String
+ else { return nil }
+ return version
+ }
+
private func compareRestoreDirectories(_ lhs: URL, _ rhs: URL) -> Bool {
let leftName = lhs.lastPathComponent
let rightName = rhs.lastPathComponent