Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Confine privileged kernel inputs and cached mix reads

Lakrb0374e326c4c
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift
index 712a6c4b..45649b00 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift
@@ -147,7 +147,7 @@ public final class KernelCacheMixer {
public func mix(_ input: Input) throws -> Data {
let (base, paths) = try locations(for: input)
log(" [kc] base \(base.id) (\(base.productVersion)), donors \(base.donors.joined(separator: ", "))")
- if let data = try? Data(contentsOf: paths.mixed), !data.isEmpty {
+ if let data = try cachedMix(at: paths.mixed), !data.isEmpty {
log(" [kc] using the cached mix \(paths.mixed.path)")
return data
}
@@ -219,7 +219,7 @@ public final class KernelCacheMixer {
@discardableResult
public func link(_ input: Input) throws -> Data {
let (base, paths) = try locations(for: input)
- if let data = try? Data(contentsOf: paths.mixed), !data.isEmpty {
+ if let data = try cachedMix(at: paths.mixed), !data.isEmpty {
return data
}
let location = try KernelCollectionBuilder.location(for: base)
@@ -300,6 +300,28 @@ public final class KernelCacheMixer {
// MARK: - Cache
+ /// A privileged patch run must not follow a cache link to another host
+ /// file. Keep the descriptor alive through the read, including a rename.
+ func cachedMix(at url: URL) throws -> Data? {
+ do {
+ let root = try VPhoneConfinedDirectory.pin(absolutePath: cacheDirectory.path)
+ guard url.path.hasPrefix(cacheDirectory.path + "/") else {
+ throw PatcherError.invalidFormat("The kernelcache input is outside its cache directory.")
+ }
+ let relative = String(url.path.dropFirst(cacheDirectory.path.count + 1))
+ let file = try root.openRegularFile(relative, requireSingleLink: true)
+ let uid = VPhoneInvokingUser.current?.uid ?? getuid()
+ guard file.owner == 0 || file.owner == uid else {
+ throw PatcherError.invalidFormat("The cached kernelcache belongs to another user.")
+ }
+ return try withExtendedLifetime(file) {
+ try FileHandle(fileDescriptor: file.descriptor, closeOnDealloc: false).readToEnd()
+ }
+ } catch VPhoneConfinedDirectoryError.missing {
+ return nil
+ }
+ }
+
static let stagedMarker = ".staged"
static func isStaged(_ directory: URL) -> Bool {
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift
index a2f6995a..c4741c33 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift
@@ -712,11 +712,15 @@ struct VPhoneFirmwareKernelcacheCommand: ParsableCommand {
var linkOnly = false
@Option(help: ArgumentHelp("The ipsw to stage with (default: $VPHONE_IPSW)", valueName: "path"))
var ipsw: String?
+ @Option(name: .customLong("work-parent"), help: ArgumentHelp("Private kernelcache work directory", visibility: .hidden))
+ var workParent = "/private/var/tmp"
func run() throws {
let name = try VPhoneVirtualMachineSelection.resolveExisting(name, in: lib.library)
let bundle = try lib.library.bundle(named: name)
- guard let input = try FirmwarePipeline(vmDirectory: bundle.url, variant: .jb).researchBoardKernelcache() else {
+ let workspace = try geteuid() == 0 ? FirmwarePatchWorkspace(machine: bundle.url, workParent: workParent) : nil
+ defer { try? workspace?.remove() }
+ guard let input = try FirmwarePipeline(vmDirectory: workspace?.machine ?? bundle.url, variant: .jb).researchBoardKernelcache() else {
print("[=] \(name)'s cloudOS has vphone600 components; its kernelcache needs no mix.")
return
}
diff --git a/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperFirmwareRequest.swift b/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperFirmwareRequest.swift
index 09b6ab07..9699a211 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperFirmwareRequest.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperFirmwareRequest.swift
@@ -93,7 +93,8 @@ struct VPhoneLaunchpadHelperFirmwareRequest {
case .patchRestoreImage:
arguments = ["fw", "patch", machineName, "--library-root", libraryRoot, "--restore-image-only", "--work-parent", workParent]
case .linkKernelcache:
- arguments = ["fw", "kernelcache", machineName, "--library-root", libraryRoot, "--link-only"]
+ arguments = ["fw", "kernelcache", machineName, "--library-root", libraryRoot,
+ "--link-only", "--work-parent", workParent]
}
self.executable = executable