Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

VPhoneSign: drop LC_SOURCE_VERSION when the command area has no room

刘珂ee8b70ca8a6a
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneSign/MachO/VPhoneMachOImage.swift b/VPhoneExecutable/VPhoneCommand/VPhoneSign/MachO/VPhoneMachOImage.swift
index fb09ba01..0e65c0e6 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneSign/MachO/VPhoneMachOImage.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneSign/MachO/VPhoneMachOImage.swift
@@ -328,9 +328,9 @@ struct VPhoneMachOImage {
// every other command, so the command area is laid out again rather
// than patched in place. For a linker-produced file the command was
// last anyway and nothing moves.
- let carried = commands.filter { $0.command != UInt32(LC_CODE_SIGNATURE) }
+ var carried = commands.filter { $0.command != UInt32(LC_CODE_SIGNATURE) }
let commandSize = MemoryLayout<linkedit_data_command>.size
- let rebuilt = carried.reduce(0) { $0 + $1.size } + commandSize
+ var rebuilt = carried.reduce(0) { $0 + $1.size } + commandSize
// Whatever holds content has to start after the commands. The check
// comes before anything is written: a section over them is exactly
@@ -340,6 +340,17 @@ struct VPhoneMachOImage {
+ (segment.sections.isEmpty && segment.fileSize > 0 ? [segment.fileOffset] : [])
}
let firstContent = content.filter { $0 > 0 }.min() ?? 0
+ // An earlier pipeline step may have spent the header padding already:
+ // the CFW launchd injection does exactly that on iOS 18.6.2, where 16
+ // bytes of slack leave no room for the fresh LC_CODE_SIGNATURE once
+ // LC_LOAD_WEAK_DYLIB is in. LC_SOURCE_VERSION is 16 informational
+ // bytes nothing loads — drop it and lay the area out again.
+ if UInt64(Self.headerSize + max(rebuilt, commandsSize)) > firstContent,
+ carried.contains(where: { $0.command == UInt32(LC_SOURCE_VERSION) })
+ {
+ carried.removeAll { $0.command == UInt32(LC_SOURCE_VERSION) }
+ rebuilt = carried.reduce(0) { $0 + $1.size } + commandSize
+ }
guard UInt64(Self.headerSize + max(rebuilt, commandsSize)) <= firstContent,
firstContent <= UInt64(codeEnd)
else {