Run a virtual iPhone on an Apple Silicon Mac.
Swift
Fix vphoned snapshot entitlement and system app backups across volumes
Jacky2a41d10cdeeb
diff --git a/Research/Guest/post_setup_signin_and_appstore.md b/Research/Guest/post_setup_signin_and_appstore.mdindex 895a0811..80e75d0d 100644--- a/Research/Guest/post_setup_signin_and_appstore.md+++ b/Research/Guest/post_setup_signin_and_appstore.md@@ -205,9 +205,16 @@ the researcher installs may link StoreKit.vphoned now folds the durable sequence into one call:`apps.remove_system {"bundle_ids":[…],"force":true}` reads each app's currentcontainer from the live app list, backs the whole container up to-`/private/var/mobile/Library/removed-system-apps/<bundle_id>.container` with a+`/private/var/db/vphoned/removed-system-apps/<bundle_id>.container` with amanifest beside it, unregisters the app, removes the container and resprings-once. It refuses apps outside `/private/var/containers/Bundle/Application/<UUID>/`+once. The backup directory is root-only (0700) and on the Data volume, like+the bundle containers, so the backup is an APFS clone that costs next to+nothing and the restore is a rename. The first version of the verb used+`/private/var/mobile/Library/removed-system-apps`, the hand recipe's+directory; `/private/var/mobile` is the User volume, so those backups were full+copies and their restore failed with EXDEV. Backups there are still listed+(`legacy: true`) and restored, by a copy that keeps owners, modes, extended+attributes and flags, after which the legacy copy is removed. It refuses apps outside `/private/var/containers/Bundle/Application/<UUID>/`(Phone and the rest of `/Applications`). `apps.restore_system` moves a backupback to its original UUID path and registers it, and `apps.removed_system`lists the backups. icli's `apps.register` refuses Apple's apps since 0.7.17, sodiff --git a/Research/Guest/template_snapshot_deletion.md b/Research/Guest/template_snapshot_deletion.mdindex 07ded21e..d7920978 100644--- a/Research/Guest/template_snapshot_deletion.md+++ b/Research/Guest/template_snapshot_deletion.md@@ -46,7 +46,7 @@ From the strings of `kernelcache.research.vphone600` and the experiment:| Requirement | Evidence | Result || --- | --- | --- |-| A vfs snapshot entitlement | `"%s (pid %d) is missing vfs snapshot entitlements"`; candidates `com.apple.developer.vfs.snapshot`, `com.apple.private.vfs.snapshot`, `com.apple.private.vfs.snapshot.user` | The prototype was signed with all three and succeeded. vphoned carries only `com.apple.developer.vfs.snapshot` (see "Not yet verified") |+| `com.apple.private.vfs.snapshot` | `"%s (pid %d) is missing vfs snapshot entitlements"`; candidates `com.apple.developer.vfs.snapshot`, `com.apple.private.vfs.snapshot`, `com.apple.private.vfs.snapshot.user`. Measured 2026-10-07 with a root prototype also signed `platform-application`: `developer` alone fails with EPERM, `private` alone deletes | vphoned carries `com.apple.private.vfs.snapshot` only. With `com.apple.developer.vfs.snapshot` alone, vphoned answered `not_permitted` || Root | XNU `fs_snapshot` checks `PRIV_VFS_SNAPSHOT` | vphoned runs as root || Not the volume's default root snapshot, not set to root or revert to, not mounted | `cannot delete snapshot '%s' because it is the default root snapshot…`, `…set to root from it`, `…set to revert to it…`, `cannot delete mounted snapshot` | `orig-fs.disabled.rn-*` meets none of them after CFW || No replication and no merge from an earlier deletion in progress | `Can't delete snapshots while volume is replicating`, `merge in progress due to previous snapshot deletion` | Not hit with a single snapshot; vphoned reports EBUSY as retryable |diff --git a/Research/vphoned_http_api.md b/Research/vphoned_http_api.mdindex e9c60aee..bbec8705 100644--- a/Research/vphoned_http_api.md+++ b/Research/vphoned_http_api.md@@ -539,10 +539,15 @@ nothing is cached) and requires a `com.apple.` app whose bundle is`/Applications` or `/System` (Phone, Settings) is refused, as is any otherpath shape (`GuestSystemAppPolicy.swift`). Then, in this order:-1. the whole container is cloned (or copied) to- `/private/var/mobile/Library/removed-system-apps/<bundle_id>.container`, with- `<bundle_id>.manifest.json` beside it (`bundle_id`, `container_uuid`, `app`,- `container_path`, `removed_at`); `backup: false` skips this;+1. the whole container is cloned to+ `/private/var/db/vphoned/removed-system-apps/<bundle_id>.container` (root+ only, mode 0700, on the Data volume with the bundle containers, so the clone+ shares every block), with `<bundle_id>.manifest.json` beside it+ (`bundle_id`, `container_uuid`, `app`, `container_path`, `removed_at`). If+ the clone fails, a copy that keeps owners, modes, extended attributes and+ flags is made instead; `backup_method` in the result says `clone` or+ `copy`. A backup of the same app in the legacy directory (below) is removed+ once the new removal is done. `backup: false` skips this;2. the app is unregistered from LaunchServices (icli's `unregisterApp`, as`apps.unregister`);3. the container is removed recursively, with `SerializedPlaceholder.ipa`,@@ -553,7 +558,7 @@ repaired by installd from the placeholder on the next boot(`Research/Guest/post_setup_signin_and_appstore.md`). SpringBoard restarts onceat the end when anything was removed and `respring` is not false. The resultis `{results: [{bundle_id, status, removed, container, app, backup,-unregistered, error?}], removed, failed, backup_directory, respring}`.+backup_method, unregistered, error?}], removed, failed, backup_directory, respring}`.`status` is `removed`, `absent` (not installed: a no-op that succeeds; aremoval an earlier call left half done, with its backup in place, is finishedinstead), `unregistered_stale` (LaunchServices listed a container that is@@ -566,7 +571,7 @@ body carries the same fields.→ error: {"code":"command_failed","message":"1 of 2 apps were not removed; see results","removed":1,"failed":1,"results":[{"bundle_id":"com.apple.news","status":"removed","removed":true,"unregistered":true,"container":"/private/var/containers/Bundle/Application/6EB8…55A1","app":"News.app",- "backup":"/private/var/mobile/Library/removed-system-apps/com.apple.news.container"},+ "backup":"/private/var/db/vphoned/removed-system-apps/com.apple.news.container","backup_method":"clone"},{"bundle_id":"com.apple.mobilephone","status":"failed","removed":false,"error":"/Applications/MobilePhone.app is not in a bundle container under /private/var/containers/Bundle/Application; …"}],"respring":{"method":"frontboard_relaunch",…}}@@ -581,13 +586,22 @@ back). icli's `registerApp`, behind `apps.register`, refuses Apple's apps since0.7.17. Without `bundle_ids` every backup is restored. An app that isinstalled is reported `present` and its backup left alone; a restore thatmoved the container but could not register it keeps the manifest, so calling-it again retries the registration. Results mirror the removal (`status`-`restored`, `present` or `failed`, plus `registration`, the call that worked;-`error: "restore_incomplete"` when any failed). **`apps.removed_system`** lists-the backups: `{directory, backups: [{bundle_id, backup, container,-container_uuid, app, removed_at, restorable}], other}`, where `other` names-entries without a bundle identifier, such as `News.container` backups made by-hand before this verb existed.+it again retries the registration. A backup in the backup directory goes back+with `rename(2)` (`restore_method: "rename"`). The first version of this verb+kept backups in `/private/var/mobile/Library/removed-system-apps`, on the User+volume, where `rename(2)` answers EXDEV; a backup found there (`legacy: true`;+the new directory is searched first) is copied back with owners, modes,+extended attributes and flags to a staging name beside the destination, renamed+into place and then removed (`restore_method: "copy"`; `warning` if the legacy+copy could not be removed). Results mirror the removal (`status` `restored`,+`present` or `failed`, plus `backup`, `legacy`, `restore_method` and+`registration`, the call that worked; `error: "restore_incomplete"` when any+failed). **`apps.removed_system`** lists the backups in both directories:+`{directory, legacy_directory, backups: [{bundle_id, backup, legacy,+container, container_uuid, app, removed_at, restorable}], other,+legacy_other}`, where `other` and `legacy_other` name entries without a bundle+identifier, such as `News.container` backups made by hand before this verb+existed.**`apfs.snapshots {mount?: "/"}`** returns `{mount, snapshots: [name]}` from`fs_snapshot_list` (`VPhoneDaemon/Native/vphoned_apfs.m`).@@ -601,7 +615,8 @@ already gone (ENOENT) is listed under `already_deleted`. The result is `{mount,before, deleted, already_deleted, after, remaining}`. A refusal from thekernel stops the call: EINVAL and ENOTSUP are invalid requests; EPERM orEACCES (`reason: "not_permitted"`: vphoned is not root or lacks-`com.apple.developer.vfs.snapshot`), EBUSY (`reason: "busy"`, `retryable:+`com.apple.private.vfs.snapshot`; `com.apple.developer.vfs.snapshot` alone is+refused), EBUSY (`reason: "busy"`, `retryable:true`: mounted, or APFS is still merging an earlier deletion) and anythingelse return `command_failed` with `reason`, `retryable`, `errno`, `snapshot`and the `before`/`deleted`/`after` listings. Why and when to call it:diff --git a/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements b/VPhoneDaemon/Configuration/VPhoneDaemon.entitlementsindex d062d6db..ac50f255 100644--- a/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements+++ b/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements@@ -65,8 +65,6 @@<true/><key>com.apple.developer.usernotifications.time-sensitive</key><true/>- <key>com.apple.developer.vfs.snapshot</key>- <true/><key>com.apple.frontboard.launchapplications</key><true/><key>com.apple.frontboard.shutdown</key>@@ -335,6 +333,8 @@<array><string>critical-alerts</string></array>+ <key>com.apple.private.vfs.snapshot</key>+ <true/><key>com.apple.rootless.critical</key><true/><key>com.apple.rootless.datavault.metadata</key>diff --git a/VPhoneDaemon/Daemon/GuestAPI+FirstBoot.swift b/VPhoneDaemon/Daemon/GuestAPI+FirstBoot.swiftindex f4e7fe24..bf7a8ef5 100644--- a/VPhoneDaemon/Daemon/GuestAPI+FirstBoot.swift+++ b/VPhoneDaemon/Daemon/GuestAPI+FirstBoot.swift@@ -73,8 +73,8 @@ extension GuestAPI {} else if let entries = try? FileManager.default.contentsOfDirectory(atPath: staged) {sample.stagedSystemApps = entries.filter { !$0.hasPrefix(".") }.count}- if let apps = try? listApps(), let count = apps["count"] as? Int {- sample.appCount = count+ if let apps = try? listedApps() {+ sample.appCount = apps.count}let processes = (try? listProcesses(filter: "installd"))?["processes"] as? [[String: Any]] ?? []if let pid = processes.first(where: { ($0["executable"] as? String)?.hasSuffix("/installd") == true })?["pid"] as? Int {diff --git a/VPhoneDaemon/Daemon/GuestAPI+SystemApps.swift b/VPhoneDaemon/Daemon/GuestAPI+SystemApps.swiftindex 076b7708..20ef2d26 100644--- a/VPhoneDaemon/Daemon/GuestAPI+SystemApps.swift+++ b/VPhoneDaemon/Daemon/GuestAPI+SystemApps.swift@@ -103,7 +103,9 @@ extension GuestAPI {result["backup"] = NSNull()if backup {- result["backup"] = try backUpContainer(id, location)+ let made = try backUpContainer(id, location)+ result["backup"] = made.path+ result["backup_method"] = made.method}do {let unregistered = try unregisterApp(bundlePath, force: true)@@ -126,6 +128,11 @@ extension GuestAPI {}result["status"] = "removed"result["removed"] = true+ if backup {+ // The new backup supersedes one an older vphoned left on the+ // User volume.+ discardBackup(id, in: GuestSystemAppPolicy.legacyBackupDirectory)+ }} catch {result["status"] = "failed"result["error"] = describe(error)@@ -140,10 +147,12 @@ extension GuestAPI {private static func finishInterruptedRemoval(_ id: String, result: [String: Any]) throws -> [String: Any] {var result = resultresult["status"] = "absent"- let backupPath = GuestSystemAppPolicy.backupContainerPath(id)+ result["backup"] = NSNull()+ guard let directory = GuestSystemAppPolicy.backupDirectory(holding: id, exists: pathExists) else { return result }+ let backupPath = GuestSystemAppPolicy.backupContainerPath(id, in: directory)result["backup"] = pathExists(backupPath) ? backupPath : NSNull()- guard pathExists(GuestSystemAppPolicy.manifestPath(id)), pathExists(backupPath) else { return result }- let location = try readManifest(id).location()+ guard pathExists(GuestSystemAppPolicy.manifestPath(id, in: directory)), pathExists(backupPath) else { return result }+ let location = try readManifest(id, in: directory).location()guard try bundleContainerExists(location) else { return result }try checkBundleIdentifier(location, id)_ = try removePath(location.containerPath, recursive: true, force: true)@@ -154,16 +163,19 @@ extension GuestAPI {return result}- /// Copies the whole container to `<bundle_id>.container` (an APFS clone- /// when it can) and writes `<bundle_id>.manifest.json` beside it. An older- /// backup of the same app is replaced once the new copy is complete.- private static func backUpContainer(_ id: String, _ location: GuestSystemAppLocation) throws -> String {+ /// Clones the whole container to `<bundle_id>.container` in the backup+ /// directory and writes `<bundle_id>.manifest.json` beside it. The backup+ /// directory is on the containers' volume, so the clone shares every block+ /// and costs next to nothing; if the clone fails anyway, a copy that keeps+ /// owners, modes, extended attributes and flags is made instead, and+ /// `method` says which. An older backup of the same app is replaced once+ /// the new one is complete.+ private static func backUpContainer(+ _ id: String,+ _ location: GuestSystemAppLocation,+ ) throws -> (path: String, method: String) {let files = FileManager.default- try files.createDirectory(- atPath: GuestSystemAppPolicy.backupDirectory,- withIntermediateDirectories: true,- attributes: [.posixPermissions: 0o755],- )+ try prepareBackupDirectory()let destination = GuestSystemAppPolicy.backupContainerPath(id)let staging = destination + ".partial"if pathExists(staging) {@@ -171,19 +183,19 @@ extension GuestAPI {}// clonefile keeps owners and modes when root calls it; the container// must come back as installd left it.+ var method = "clone"if clonefile(location.containerPath, staging, UInt32(CLONE_NOFOLLOW)) != 0 {let reason = String(cString: strerror(errno))if pathExists(staging) {try? files.removeItem(atPath: staging)}- do {- try files.copyItem(atPath: location.containerPath, toPath: staging)- } catch {+ if let copyFailure = preservingCopy(location.containerPath, to: staging) {try? files.removeItem(atPath: staging)throw GuestAPIError.operationFailed(- "Could not back up \(location.containerPath): clone failed (\(reason)), copy failed (\(describe(error)))",+ "Could not back up \(location.containerPath): clone failed (\(reason)), copy failed (\(copyFailure))",)}+ method = "copy"}if pathExists(destination) {try files.removeItem(atPath: destination)@@ -203,12 +215,43 @@ extension GuestAPI {try? files.removeItem(atPath: destination)throw GuestAPIError.operationFailed("Could not write the backup manifest: \(describe(error))")}- return destination+ return (destination, method)}- private static func discardBackup(_ id: String) {- try? FileManager.default.removeItem(atPath: GuestSystemAppPolicy.backupContainerPath(id))- try? FileManager.default.removeItem(atPath: GuestSystemAppPolicy.manifestPath(id))+ /// `/private/var/db/vphoned/removed-system-apps`, root's alone: mode 0700,+ /// and a real directory, not a link somebody left in its place.+ private static func prepareBackupDirectory() throws {+ let directory = GuestSystemAppPolicy.backupDirectory+ try FileManager.default.createDirectory(+ atPath: (directory as NSString).deletingLastPathComponent,+ withIntermediateDirectories: true,+ attributes: [.posixPermissions: 0o755],+ )+ if mkdir(directory, 0o700) != 0, errno != EEXIST {+ throw GuestAPIError.operationFailed("Could not create \(directory): \(String(cString: strerror(errno)))")+ }+ var info = stat()+ guard lstat(directory, &info) == 0, info.st_mode & mode_t(S_IFMT) == mode_t(S_IFDIR), info.st_uid == 0 else {+ throw GuestAPIError.operationFailed("\(directory) is not a directory owned by root")+ }+ if info.st_mode & 0o777 != 0o700 {+ _ = chmod(directory, 0o700)+ }+ }++ /// Copies a directory tree as it is: owners, modes, extended attributes,+ /// flags and links. Returns why it failed, or nil.+ private static func preservingCopy(_ source: String, to destination: String) -> String? {+ let flags = copyfile_flags_t(COPYFILE_ALL | COPYFILE_RECURSIVE | COPYFILE_NOFOLLOW)+ guard copyfile(source, destination, nil, flags) == 0 else {+ return String(cString: strerror(errno))+ }+ return nil+ }++ private static func discardBackup(_ id: String, in directory: String = GuestSystemAppPolicy.backupDirectory) {+ try? FileManager.default.removeItem(atPath: GuestSystemAppPolicy.backupContainerPath(id, in: directory))+ try? FileManager.default.removeItem(atPath: GuestSystemAppPolicy.manifestPath(id, in: directory))}// MARK: - Restore@@ -232,27 +275,32 @@ extension GuestAPI {}/// Moves the backup container back to its original UUID path and- /// registers the app in it. The manifest stays until LaunchServices lists- /// the app, so a restore that moved the container but could not register- /// it is finished by running it again.+ /// registers the app in it. A backup in the backup directory goes back by+ /// rename; one in the legacy directory is on the User volume and is copied+ /// back with owners, modes, extended attributes and flags, then removed.+ /// The manifest stays until LaunchServices lists the app, so a restore+ /// that moved the container but could not register it is finished by+ /// running it again.private static func restoreSystemApp(_ id: String) -> [String: Any] {var result: [String: Any] = ["bundle_id": id, "restored": false]do {try GuestSystemAppPolicy.validateBundleID(id)- let backupPath = GuestSystemAppPolicy.backupContainerPath(id)- guard pathExists(GuestSystemAppPolicy.manifestPath(id)) else {+ let directory = GuestSystemAppPolicy.backupDirectory(holding: id, exists: pathExists)+ guard let directory, pathExists(GuestSystemAppPolicy.manifestPath(id, in: directory)) else {if let record = try installedApp(id) {result["status"] = "present"result["bundle_path"] = record["bundle_path"] ?? ""return result}throw GuestAPIError.operationFailed(- pathExists(backupPath)- ? "\(backupPath) has no manifest naming its original container"- : "No backup of \(id) in \(GuestSystemAppPolicy.backupDirectory)",+ directory.map { "\(GuestSystemAppPolicy.backupContainerPath(id, in: $0)) has no manifest naming its original container" }+ ?? "No backup of \(id) in \(GuestSystemAppPolicy.backupDirectories.joined(separator: " or "))",)}- let location = try readManifest(id).location()+ let backupPath = GuestSystemAppPolicy.backupContainerPath(id, in: directory)+ result["backup"] = backupPath+ result["legacy"] = GuestSystemAppPolicy.isLegacy(directory)+ let location = try readManifest(id, in: directory).location()result["container"] = location.containerPathresult["app"] = location.appDirectoryNameif let record = try installedApp(id) {@@ -265,10 +313,10 @@ extension GuestAPI {guard !pathExists(location.containerPath) else {throw GuestAPIError.operationFailed("\(location.containerPath) already exists; the backup was left in place")}- guard rename(backupPath, location.containerPath) == 0 else {- throw GuestAPIError.operationFailed(- "Could not move \(backupPath) back: \(String(cString: strerror(errno)))",- )+ let moved = try moveBack(backupPath, to: location.containerPath)+ result["restore_method"] = moved.method+ if let warning = moved.warning {+ result["warning"] = warning}} else {guard try bundleContainerExists(location) else {@@ -290,7 +338,7 @@ extension GuestAPI {guard registration["registered"] as? Bool == true else {throw GuestAPIError.operationFailed("LaunchServices does not list \(location.appPath) after registration")}- try? FileManager.default.removeItem(atPath: GuestSystemAppPolicy.manifestPath(id))+ try? FileManager.default.removeItem(atPath: GuestSystemAppPolicy.manifestPath(id, in: directory))result["registration"] = method.map { String(cString: $0) } ?? ""result["status"] = "restored"result["restored"] = true@@ -301,44 +349,99 @@ extension GuestAPI {return result}+ /// Puts a backup container back at `destination`. A rename on the same+ /// volume; across volumes (EXDEV) a preserving copy to a staging name+ /// beside the destination, a rename into place, then the source removed.+ private static func moveBack(_ source: String, to destination: String) throws -> (method: String, warning: String?) {+ var info = stat()+ guard lstat(source, &info) == 0, info.st_mode & mode_t(S_IFMT) == mode_t(S_IFDIR) else {+ throw GuestAPIError.operationFailed("\(source) is not a backup container directory")+ }+ if rename(source, destination) == 0 {+ return ("rename", nil)+ }+ let code = errno+ guard GuestSystemAppPolicy.restoreCopiesAcrossVolumes(renameErrno: code) else {+ throw GuestAPIError.operationFailed("Could not move \(source) back: \(String(cString: strerror(code)))")+ }+ let files = FileManager.default+ let staging = destination + ".partial"+ if pathExists(staging) {+ try files.removeItem(atPath: staging)+ }+ if let failure = preservingCopy(source, to: staging) {+ try? files.removeItem(atPath: staging)+ throw GuestAPIError.operationFailed("Could not copy \(source) back across volumes: \(failure)")+ }+ guard rename(staging, destination) == 0 else {+ let reason = String(cString: strerror(errno))+ try? files.removeItem(atPath: staging)+ throw GuestAPIError.operationFailed("Could not move the copy of \(source) into place: \(reason)")+ }+ do {+ try files.removeItem(atPath: source)+ } catch {+ return ("copy", "The container is back, but \(source) was not removed: \(describe(error))")+ }+ return ("copy", nil)+ }+// MARK: - Backups- /// Every app with a backup or a manifest in the backup directory.+ /// Every app with a backup or a manifest in either backup directory.private static func backedUpSystemAppIDs() -> [String] {- let names = (try? FileManager.default.contentsOfDirectory(atPath: GuestSystemAppPolicy.backupDirectory)) ?? []+ let names = GuestSystemAppPolicy.backupDirectories.flatMap(directoryEntries)return Set(names.compactMap(GuestSystemAppPolicy.bundleID(forBackupEntry:))).sorted()}+ private static func directoryEntries(_ directory: String) -> [String] {+ (try? FileManager.default.contentsOfDirectory(atPath: directory)) ?? []+ }++ /// The backups in the backup directory, then those an older vphoned left+ /// in the legacy directory, each marked `legacy`.private static func removedSystemApps() -> [String: Any] {- let names = (try? FileManager.default.contentsOfDirectory(atPath: GuestSystemAppPolicy.backupDirectory)) ?? []- let backups = backedUpSystemAppIDs().map { id -> [String: Any] in- let backupPath = GuestSystemAppPolicy.backupContainerPath(id)- var entry: [String: Any] = [- "bundle_id": id,- "backup": pathExists(backupPath) ? backupPath : NSNull(),- ]- do {- let manifest = try readManifest(id)- let location = try manifest.location()- entry["container"] = location.containerPath- entry["container_uuid"] = location.containerUUID- entry["app"] = location.appDirectoryName- entry["removed_at"] = ISO8601DateFormatter().string(from: manifest.removedAt)- entry["restorable"] = pathExists(backupPath) || pathExists(location.containerPath)- } catch {- entry["restorable"] = false- entry["error"] = describe(error)+ var backups: [[String: Any]] = []+ for directory in GuestSystemAppPolicy.backupDirectories {+ let ids = Set(directoryEntries(directory).compactMap(GuestSystemAppPolicy.bundleID(forBackupEntry:))).sorted()+ for id in ids {+ let backupPath = GuestSystemAppPolicy.backupContainerPath(id, in: directory)+ var entry: [String: Any] = [+ "bundle_id": id,+ "backup": pathExists(backupPath) ? backupPath : NSNull(),+ "legacy": GuestSystemAppPolicy.isLegacy(directory),+ ]+ do {+ let manifest = try readManifest(id, in: directory)+ let location = try manifest.location()+ entry["container"] = location.containerPath+ entry["container_uuid"] = location.containerUUID+ entry["app"] = location.appDirectoryName+ entry["removed_at"] = ISO8601DateFormatter().string(from: manifest.removedAt)+ entry["restorable"] = pathExists(backupPath) || pathExists(location.containerPath)+ } catch {+ entry["restorable"] = false+ entry["error"] = describe(error)+ }+ backups.append(entry)}- return entry}// Backups made by hand before this verb existed (`News.container`)// name no bundle identifier and have no manifest.- let other = names.filter { GuestSystemAppPolicy.bundleID(forBackupEntry: $0) == nil }.sorted()- return ["directory": GuestSystemAppPolicy.backupDirectory, "backups": backups, "other": other]+ func other(_ directory: String) -> [String] {+ directoryEntries(directory).filter { GuestSystemAppPolicy.bundleID(forBackupEntry: $0) == nil }.sorted()+ }+ return [+ "directory": GuestSystemAppPolicy.backupDirectory,+ "legacy_directory": GuestSystemAppPolicy.legacyBackupDirectory,+ "backups": backups,+ "other": other(GuestSystemAppPolicy.backupDirectory),+ "legacy_other": other(GuestSystemAppPolicy.legacyBackupDirectory),+ ]}- private static func readManifest(_ id: String) throws -> GuestSystemAppManifest {- let path = GuestSystemAppPolicy.manifestPath(id)+ private static func readManifest(_ id: String, in directory: String) throws -> GuestSystemAppManifest {+ let path = GuestSystemAppPolicy.manifestPath(id, in: directory)var info = stat()guard lstat(path, &info) == 0, info.st_mode & mode_t(S_IFMT) == mode_t(S_IFREG), info.st_size <= 64 * 1024 else {throw GuestAPIError.operationFailed("\(path) is not a manifest file")diff --git a/VPhoneDaemon/Daemon/GuestAPI.swift b/VPhoneDaemon/Daemon/GuestAPI.swiftindex 33f8d146..053ee113 100644--- a/VPhoneDaemon/Daemon/GuestAPI.swift+++ b/VPhoneDaemon/Daemon/GuestAPI.swift@@ -163,7 +163,7 @@ enum GuestAPI {return try takeScreenshot(base64: true, nativeResolution: true)case "apps.list":let filter = params["filter"] as? String ?? "all"- let apps = try listApps()["apps"] as? [[String: Any]] ?? []+ let apps = try listedApps()let running = try runningApps()["apps"] as? [[String: Any]] ?? []let pids = Dictionary(uniqueKeysWithValues: running.compactMap { app -> (String, Int)? in@@ -487,6 +487,15 @@ enum GuestAPI {return result}+ /// The LaunchServices records `apps.list` reports: those with a bundle+ /// identifier. `setup.settle` counts this list, not icli's `count`, which+ /// was one higher than `apps.list` on an iOS 27 guest.+ static func listedApps() throws -> [[String: Any]] {+ (try listApps()["apps"] as? [[String: Any]] ?? []).filter {+ !(($0["bundle_id"] as? String) ?? "").isEmpty+ }+ }+static func jailbreakInfo() -> [String: Any] {func isDirectory(_ path: String) -> Bool {var directory: ObjCBool = falsediff --git a/VPhoneDaemon/Daemon/GuestSnapshotPolicy.swift b/VPhoneDaemon/Daemon/GuestSnapshotPolicy.swiftindex 45064952..6d23cbf8 100644--- a/VPhoneDaemon/Daemon/GuestSnapshotPolicy.swift+++ b/VPhoneDaemon/Daemon/GuestSnapshotPolicy.swift@@ -75,7 +75,7 @@ enum GuestSnapshotPolicy {enum Failure: String, Equatable {/// The snapshot is not there: a delete counts it as done.case alreadyDeleted = "already_deleted"- /// vphoned lacks `com.apple.developer.vfs.snapshot` or is not root.+ /// vphoned lacks `com.apple.private.vfs.snapshot` or is not root.case notPermitted = "not_permitted"/// Mounted, or APFS is still merging an earlier deletion. Retry later.case busy@@ -102,7 +102,7 @@ enum GuestSnapshotPolicy {case .alreadyDeleted:"\(operation): the snapshot no longer exists (\(reason))"case .notPermitted:- "\(operation): \(reason). vphoned must run as root and carry com.apple.developer.vfs.snapshot"+ "\(operation): \(reason). vphoned must run as root and carry com.apple.private.vfs.snapshot (com.apple.developer.vfs.snapshot alone is refused)"case .busy:"\(operation): \(reason). The snapshot is mounted or APFS is still merging an earlier deletion; retry later"case .invalidRequest:diff --git a/VPhoneDaemon/Daemon/GuestSystemAppPolicy.swift b/VPhoneDaemon/Daemon/GuestSystemAppPolicy.swiftindex 718d3741..84659e70 100644--- a/VPhoneDaemon/Daemon/GuestSystemAppPolicy.swift+++ b/VPhoneDaemon/Daemon/GuestSystemAppPolicy.swift@@ -16,7 +16,16 @@ import Foundation/// `Research/Guest/post_setup_signin_and_appstore.md` has the measurements.enum GuestSystemAppPolicy {static let containersRoot = "/private/var/containers/Bundle/Application"- static let backupDirectory = "/private/var/mobile/Library/removed-system-apps"+ /// Where new backups go: vphoned's own root-only state directory on the+ /// data volume, the volume the bundle containers are on, so a backup is an+ /// APFS clone and a restore is a rename.+ static let backupDirectory = "/private/var/db/vphoned/removed-system-apps"+ /// Where backups went first. `/private/var/mobile` is the User volume, so+ /// a backup there is a full copy and a restore has to copy it back+ /// (`rename(2)` answers EXDEV). Still listed and restored.+ static let legacyBackupDirectory = "/private/var/mobile/Library/removed-system-apps"+ /// The directories a backup is looked for in, in order.+ static let backupDirectories = [backupDirectory, legacyBackupDirectory]static let backupSuffix = ".container"static let manifestSuffix = ".manifest.json"@@ -35,12 +44,31 @@ enum GuestSystemAppPolicy {}}- static func backupContainerPath(_ bundleID: String) -> String {- backupDirectory + "/" + bundleID + backupSuffix+ static func backupContainerPath(_ bundleID: String, in directory: String = backupDirectory) -> String {+ directory + "/" + bundleID + backupSuffix}- static func manifestPath(_ bundleID: String) -> String {- backupDirectory + "/" + bundleID + manifestSuffix+ static func manifestPath(_ bundleID: String, in directory: String = backupDirectory) -> String {+ directory + "/" + bundleID + manifestSuffix+ }++ static func isLegacy(_ directory: String) -> Bool {+ directory == legacyBackupDirectory+ }++ /// The directory that holds the backup of `bundleID`: the first one, in+ /// `backupDirectories` order, with its manifest, else the first with its+ /// container, else nil. `exists` is `lstat` in the daemon.+ static func backupDirectory(holding bundleID: String, exists: (String) -> Bool) -> String? {+ backupDirectories.first { exists(manifestPath(bundleID, in: $0)) }+ ?? backupDirectories.first { exists(backupContainerPath(bundleID, in: $0)) }+ }++ /// A restore moves the container back with `rename(2)`. Only a backup on+ /// another volume (EXDEV, the legacy directory) is copied back instead;+ /// any other failure stops the restore.+ static func restoreCopiesAcrossVolumes(renameErrno code: Int32) -> Bool {+ code == EXDEV}/// The bundle identifier a backup directory entry belongs to, or nil for@@ -156,7 +184,7 @@ struct GuestSystemAppManifest: Codable, Equatable {}/// The location the restore moves the container back to, validated as a- /// removal is: the backup directory is the mobile user's, so the manifest+ /// removal is: the legacy backup directory is the mobile user's, so the manifest/// is not trusted to name any other path.func location() throws(GuestSystemAppPolicyError) -> GuestSystemAppLocation {try GuestSystemAppPolicy.validateBundleID(bundleID)diff --git a/VPhoneDaemon/Native/Include/VphonedNative.h b/VPhoneDaemon/Native/Include/VphonedNative.hindex 990a2914..1302ee1f 100644--- a/VPhoneDaemon/Native/Include/VphonedNative.h+++ b/VPhoneDaemon/Native/Include/VphonedNative.h@@ -69,7 +69,7 @@ void vp_apfs_snapshot_names_free(char **names, int count);int vp_apfs_snapshot_parse(const char *buffer, size_t size, int entries, char ***names, int *count, int *added);/// Delete the snapshot `name` of the volume mounted at `mount`. Returns 0 or-/// an errno value. Needs root and a vfs snapshot entitlement.+/// an errno value. Needs root and com.apple.private.vfs.snapshot.int vp_apfs_snapshot_delete(const char *mount, const char *name);/// Register the Apple app bundle at `app_path`, inside the bundle containerdiff --git a/VPhoneDaemon/Native/vphoned_apfs.m b/VPhoneDaemon/Native/vphoned_apfs.mindex 628c8cd3..6b4951f0 100644--- a/VPhoneDaemon/Native/vphoned_apfs.m+++ b/VPhoneDaemon/Native/vphoned_apfs.m@@ -4,7 +4,7 @@* fs_snapshot_list reads the snapshots of the volume a directory descriptor* is on, in getattrlistbulk's packed format; fs_snapshot_delete removes one* by name. The kernel lets a root caller holding a vfs snapshot entitlement- * (vphoned carries com.apple.developer.vfs.snapshot) delete a snapshot that+ * (vphoned carries com.apple.private.vfs.snapshot) delete a snapshot that* is not the volume's root or revert target and is not mounted.** Both return 0 or an errno value. Which snapshot may be deleted is decideddiff --git a/VPhoneDaemon/Tests/SystemMaintenanceTests.swift b/VPhoneDaemon/Tests/SystemMaintenanceTests.swiftindex fb9028e0..a61f997c 100644--- a/VPhoneDaemon/Tests/SystemMaintenanceTests.swift+++ b/VPhoneDaemon/Tests/SystemMaintenanceTests.swift@@ -36,6 +36,7 @@ enum SystemMaintenanceTests {containerPaths()bundleIdentifiers()backupNames()+ backupSelection()manifests()snapshotSelection()snapshotErrno()@@ -117,8 +118,17 @@ enum SystemMaintenanceTests {// MARK: - Backup namesstatic func backupNames() {- let directory = "/private/var/mobile/Library/removed-system-apps"- check(GuestSystemAppPolicy.backupDirectory == directory, "backup directory")+ let directory = "/private/var/db/vphoned/removed-system-apps"+ let legacy = "/private/var/mobile/Library/removed-system-apps"+ check(GuestSystemAppPolicy.backupDirectory == directory, "backup directory is on the data volume")+ check(GuestSystemAppPolicy.legacyBackupDirectory == legacy, "legacy backup directory")+ check(GuestSystemAppPolicy.backupDirectories == [directory, legacy], "search order: new, then legacy")+ check(GuestSystemAppPolicy.isLegacy(legacy) && !GuestSystemAppPolicy.isLegacy(directory), "legacy detection")+ check(!GuestSystemAppPolicy.isLegacy(legacy + "/"), "legacy detection is exact")+ check(GuestSystemAppPolicy.backupContainerPath("com.apple.news", in: legacy) == "\(legacy)/com.apple.news.container",+ "legacy backup container path")+ check(GuestSystemAppPolicy.manifestPath("com.apple.news", in: legacy) == "\(legacy)/com.apple.news.manifest.json",+ "legacy manifest path")check(GuestSystemAppPolicy.backupContainerPath("com.apple.news") == "\(directory)/com.apple.news.container","backup container path")check(GuestSystemAppPolicy.manifestPath("com.apple.news") == "\(directory)/com.apple.news.manifest.json",@@ -132,6 +142,38 @@ enum SystemMaintenanceTests {}}+ // MARK: - Backup selection++ static func backupSelection() {+ let current = GuestSystemAppPolicy.backupDirectory+ let legacy = GuestSystemAppPolicy.legacyBackupDirectory+ let id = "com.apple.news"+ func holding(_ present: Set<String>) -> String? {+ GuestSystemAppPolicy.backupDirectory(holding: id) { present.contains($0) }+ }+ let currentManifest = "\(current)/\(id).manifest.json"+ let currentContainer = "\(current)/\(id).container"+ let legacyManifest = "\(legacy)/\(id).manifest.json"+ let legacyContainer = "\(legacy)/\(id).container"++ check(holding([]) == nil, "no backup anywhere")+ check(holding([currentManifest, currentContainer]) == current, "new backup")+ check(holding([legacyManifest, legacyContainer]) == legacy, "legacy backup only")+ check(holding([currentManifest, currentContainer, legacyManifest, legacyContainer]) == current,+ "new backup wins over a legacy one")+ check(holding([legacyManifest, legacyContainer, currentContainer]) == legacy,+ "a manifest wins over a stray container")+ check(holding([currentManifest]) == current, "manifest alone (container already moved back)")+ check(holding([legacyContainer]) == legacy, "container without a manifest is still found")+ check(holding(["\(legacy)/News.container"]) == nil, "a hand-made backup names no app")+ check(holding(["\(current)/com.apple.tv.manifest.json"]) == nil, "another app's backup")++ check(GuestSystemAppPolicy.restoreCopiesAcrossVolumes(renameErrno: EXDEV), "EXDEV copies across volumes")+ for code in [EPERM, EACCES, ENOENT, EEXIST, ENOTEMPTY, EBUSY, EIO] {+ check(!GuestSystemAppPolicy.restoreCopiesAcrossVolumes(renameErrno: code), "errno \(code) does not copy")+ }+ }+// MARK: - Manifestsstatic func manifests() {@@ -250,7 +292,7 @@ enum SystemMaintenanceTests {check(Failure.notPermitted.rawValue == "not_permitted" && Failure.alreadyDeleted.rawValue == "already_deleted"&& Failure.invalidRequest.rawValue == "invalid_request", "wire reasons")let message = GuestSnapshotPolicy.message(.notPermitted, errno: EPERM, operation: "fs_snapshot_delete(x)")- check(message.contains("com.apple.developer.vfs.snapshot") && message.contains("root"),+ check(message.contains("carry com.apple.private.vfs.snapshot") && message.contains("root"),"permission message names the entitlement")check(GuestSnapshotPolicy.message(.busy, errno: EBUSY, operation: "x").contains("retry"), "busy message says retry")}