Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Keep firmware output permissions confined to pinned directories

Lakr8dbac7e8d1d0
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift
index ff41e09c..712a6c4b 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/KernelCache/KernelCacheMixer.swift
@@ -320,15 +320,27 @@ public final class KernelCacheMixer {
/// root writes there.
func storeMix(_ data: Data, at url: URL) throws {
let user = VPhoneInvokingUser.current
- let cache = cacheDirectory.standardizedFileURL.path
- try FileManager.default.createDirectory(atPath: cache, withIntermediateDirectories: true)
+ let cache = cacheDirectory.path
+ // A privileged linker only consumes an existing user stage. Do not
+ // create or revisit a path through its user-writable ancestors as root.
+ if geteuid() != 0 {
+ try FileManager.default.createDirectory(atPath: cache, withIntermediateDirectories: true)
+ }
let root = try VPhoneConfinedDirectory.pin(absolutePath: cache)
- let relative = String(url.standardizedFileURL.path.dropFirst(cache.count + 1))
+ guard url.path.hasPrefix(cache + "/") else {
+ throw PatcherError.invalidFormat("The kernelcache output is outside its cache directory.")
+ }
+ let relative = String(url.path.dropFirst(cache.count + 1))
let (directory, leaf) = try root.parent(of: relative, create: true)
try directory.writeFile(leaf, contents: data, mode: 0o644, owner: user.map { ($0.uid, $0.gid) })
// Under sudo, so the user's later runs, which link over ssh, can store here too.
if geteuid() == 0 {
- try VPhoneHostFilePermissions.makeAccessible(at: cacheDirectory)
+ var current = root
+ try current.setPermissions(mode: 0o777, owner: user.map { ($0.uid, $0.gid) })
+ for name in relative.split(separator: "/").dropLast() {
+ current = try current.directory(String(name))
+ try current.setPermissions(mode: 0o777, owner: user.map { ($0.uid, $0.gid) })
+ }
}
}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift
index 5b21b070..9f20bda0 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift
@@ -96,6 +96,7 @@ public struct FirmwarePatchWorkspace {
if try input.isDirectory(name) {
let sourceChild = try input.directory(name)
let targetChild = try output.directory(name, create: true, mode: 0o777)
+ try targetChild.setPermissions(mode: 0o777, owner: owner)
for child in try sourceChild.entries() {
try publish(child, from: sourceChild, to: targetChild, relative: relative + "/" + child)
}
diff --git a/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift b/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift
index 0da784be..bcf65be4 100644
--- a/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift
+++ b/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift
@@ -485,6 +485,12 @@ public final class VPhoneConfinedDirectory: Sendable {
}
}
+ /// Change this pinned directory itself, without resolving its path again.
+ /// A writable ancestor may have been renamed since it was opened.
+ public func setPermissions(mode: mode_t, owner: (uid: uid_t, gid: gid_t)? = nil) throws {
+ try Self.applyAttributes(to: descriptor, owner: owner, mode: mode, path: path)
+ }
+
public func setOwner(_ relative: String, uid: uid_t, gid: gid_t) throws {
let (directory, leaf) = try parent(of: relative)
guard fchownat(directory.descriptor, leaf, uid, gid, AT_SYMLINK_NOFOLLOW) == 0 else {