Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Give the re-signed launchd.plist.sig Apple's constraint category

Maximilian Paß78b86df74c7f
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift
index 3c5894aa..bbf02398 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift
@@ -64,8 +64,18 @@ extension CryptexFilesystemPatcher {
/// `launchd_cache_loader` takes it as the secure cache. `createTrustcache`
/// adds the new cdhashes to the static trust cache, which is what the loader
/// asks AMFI about. See `CustomFirmwareDetachedSignature`.
+ ///
+ /// The new cdhashes take the launch-constraint category of Apple's
+ /// signature, as a re-signed binary does (`resignedCDHashes`): Apple's
+ /// static trust cache gives both cdhashes of its `launchd.plist.sig`
+ /// category 2 (iOS 18.6 22G86).
func resignLaunchdCache(targetMount: String) throws {
let target = try VPhoneConfinedDirectory.pin(absolutePath: targetMount)
+ let original = try target.exists(CustomFirmwareDetachedSignature.launchdCacheSignaturePath)
+ ? CustomFirmwareDetachedSignature.codeDirectoryHashes(
+ in: target.readData(CustomFirmwareDetachedSignature.launchdCacheSignaturePath),
+ ).map(\.cdhash)
+ : []
let cache = try target.readData(CustomFirmwareDetachedSignature.launchdCachePath)
let signature = CustomFirmwareDetachedSignature.sign(cache, identifierStem: "launchd")
let file = try createTmpDir().appending(path: "launchd.plist.sig")
@@ -73,7 +83,13 @@ extension CryptexFilesystemPatcher {
// root:admin 0644, as Apple installs it.
try target.replaceFile(CustomFirmwareDetachedSignature.launchdCacheSignaturePath,
fromFileAt: file, mode: 0o644, owner: (0, 80))
+ for cdHash in original {
+ print(" [+] Apple's launchd.plist.sig: cdhash \(cdHash.map { String(format: "%02x", $0) }.joined())")
+ }
for hash in try CustomFirmwareDetachedSignature.codeDirectoryHashes(in: signature) {
+ if !original.isEmpty, !original.contains(hash.cdhash) {
+ resignedCDHashes[hash.cdhash] = original
+ }
print(" [+] Signed launchd.plist: cdhash \(hash.cdhash.map { String(format: "%02x", $0) }.joined()) (hash type \(hash.hashType))")
}
}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift
index 21d5f950..47c911ae 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift
@@ -258,5 +258,9 @@ extension CryptexFilesystemPatcher {
.data.write(to: trustCache)
let categorized = cache.entries.filter { $0.constraintCategory != 0 }.count
print(" [+] Constraint categories for \(categorized) of \(cache.entries.count) trust cache entries")
+ for entry in cache.entries where resignedCDHashes[entry.cdHash] != nil {
+ let hex = entry.cdHash.map { String(format: "%02x", $0) }.joined()
+ print(" [+] Re-signed cdhash \(hex) (hash type \(entry.hashType)): category \(entry.constraintCategory)")
+ }
}
}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/TrustCacheTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/TrustCacheTests.swift
index f35ed215..feb8706f 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/TrustCacheTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/TrustCacheTests.swift
@@ -81,6 +81,32 @@ struct TrustCacheTests {
#expect(cache.entries.map(\.constraintCategory) == [0])
}
+ /// The detached launchd.plist signature's cdhashes reach the static cache
+ /// through a raw version 1 base trust cache. Read back, they take the
+ /// category Apple's signature had (2 in 22G86's static trust cache), as
+ /// `resignLaunchdCache` maps them.
+ @Test
+ func `a re-signed launchd.plist signature keeps the category of Apple's`() throws {
+ let apple = try CustomFirmwareDetachedSignature.codeDirectoryHashes(
+ in: CustomFirmwareDetachedSignature.sign(Data("Apple's launchd.plist".utf8), identifierStem: "launchd"),
+ ).map(\.cdhash)
+ let resigned = try CustomFirmwareDetachedSignature.codeDirectoryHashes(
+ in: CustomFirmwareDetachedSignature.sign(Data("launchd.plist with vphoned".utf8), identifierStem: "launchd"),
+ )
+ let detached = try TrustCache(payload: CustomFirmwareDetachedSignature.trustCache(resigned))
+ #expect(detached.version == 1)
+ #expect(Set(detached.entries.map(\.cdHash)) == Set(resigned.map(\.cdhash)))
+
+ let os = TrustCache(version: 2, uuid: Data(count: 16), entries: apple.map {
+ .init(cdHash: $0, hashType: 2, flags: 0, constraintCategory: 2)
+ })
+ let inherited = Dictionary(uniqueKeysWithValues: resigned.map { ($0.cdhash, apple) })
+ let cache = detached.withConstraintCategories(from: [os], inherited: inherited)
+ #expect(cache.version == 2)
+ #expect(cache.entries.map(\.constraintCategory) == [2, 2])
+ #expect(detached.withConstraintCategories(from: [os]).entries.map(\.constraintCategory) == [0, 0])
+ }
+
/// Checked against codesign, which prints the CDHash of the slice it
/// is asked for. /bin/ls is universal, so both slices are reached.
@Test(.enabled(if: FileManager.default.isExecutableFile(atPath: "/usr/bin/codesign")))