Run a virtual iPhone on an Apple Silicon Mac.
Swift
Read the pinned device name from a file vphoned keeps
Jackyc7fdceda05d7
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swiftindex 264d9cc6..abaef9cd 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift@@ -198,9 +198,9 @@ public enum FirmwareGuestSystemPatchSet {libdevicehubfix.dylib, inserted into cryptexd, which then stages the iOS 27 \developer disk image, and into the image's dtremotedisplayd, which then reports \the media stream features Xcode's DeviceHub needs to show the screen. \- libdevicename.dylib, inserted into configd and lockdownd, does nothing unless the \- host writes a device name to NVRAM; then configd publishes that name and lockdownd \- refuses to rename the device.+ libdevicename.dylib, inserted into configd and lockdownd, does nothing until \+ vphoned stores a device name (the host sends the VM's name); then configd \+ publishes that name and lockdownd refuses to rename the device.""",target: .guestFile(path: "/Library/LaunchDaemons"),bootEssential: true,diff --git a/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.cindex f19e84e8..7224904c 100644--- a/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c+++ b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c@@ -7,7 +7,7 @@CFStringRef VPDeviceNameCreateFromBytes(const UInt8 *bytes, CFIndex length) {if (!bytes || length <= 0)return NULL;- // The host writes no terminator, but a C-string writer would; drop it.+ // A C-string writer leaves a terminator; drop it.while (length > 0 && bytes[length - 1] == 0)length--;if (length == 0 || length > VP_DEVICE_NAME_MAX_BYTES)@@ -42,6 +42,20 @@ CFStringRef VPDeviceNameCreateFromProperty(CFTypeRef property) {return NULL;}+CFStringRef VPDeviceNameCreateFromConfiguration(CFDataRef contents) {+ if (!contents)+ return NULL;+ CFPropertyListRef plist =+ CFPropertyListCreateWithData(kCFAllocatorDefault, contents, kCFPropertyListImmutable, NULL, NULL);+ if (!plist)+ return NULL;+ CFStringRef name = NULL;+ if (CFGetTypeID(plist) == CFDictionaryGetTypeID())+ name = VPDeviceNameCreateFromProperty(CFDictionaryGetValue((CFDictionaryRef)plist, VP_DEVICE_NAME_CONFIG_KEY));+ CFRelease(plist);+ return name;+}+// MARK: - configdCFDictionaryRef VPDeviceNameCreatePinnedSystem(CFTypeRef system, CFStringRef name) {@@ -158,6 +172,6 @@ Boolean VPDeviceNameCreatePinnedPublication(CFDictionaryRef keysToSet, CFArrayRe// MARK: - lockdownd-Boolean VPDeviceNameAllowsRename(CFStringRef pinned, CFStringRef requested) {- return !pinned || (requested && CFEqual(pinned, requested));+Boolean VPDeviceNameAllowsRename(CFStringRef pinned) {+ return !pinned;}diff --git a/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.h b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.hindex 7d1286bf..27cf269f 100644--- a/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.h+++ b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.h@@ -9,9 +9,11 @@// Internal to the library that compiles them in.#define VP_DEVICE_NAME_INTERNAL __attribute__((visibility("hidden")))-// The NVRAM variable the host writes before boot: the name's UTF-8 bytes, no-// terminator. Absent, empty or not a valid name means the feature is off.-#define VP_DEVICE_NAME_NVRAM_VARIABLE "vphone-device-name"+// The file vphoned writes when the host sets a device name (`device.name.set`):+// a property list whose `DeviceName` string is the name. Absent, unreadable, or+// without a valid name means nothing is pinned.+#define VP_DEVICE_NAME_CONFIG_PATH "/var/db/vphone/devicename.plist"+#define VP_DEVICE_NAME_CONFIG_KEY CFSTR("DeviceName")// The longest name taken, in UTF-8 bytes. A longer value is invalid, so the// feature is off rather than truncating the name.@@ -24,14 +26,18 @@#define VP_DEVICE_NAME_COMPUTER_NAME CFSTR("ComputerName")#define VP_DEVICE_NAME_COMPUTER_NAME_ENCODING CFSTR("ComputerNameEncoding")-// The name in an NVRAM property: CFData holding UTF-8 (trailing NULs are-// dropped) or a CFString. NULL when the property is absent, empty, longer than+// The name in a value: CFData holding UTF-8 (trailing NULs are dropped) or a+// CFString. NULL when the property is absent, empty, longer than// VP_DEVICE_NAME_MAX_BYTES, not UTF-8, or holds a control character.VP_DEVICE_NAME_INTERNAL CFStringRef VPDeviceNameCreateFromProperty(CFTypeRef property);// The same for raw bytes.VP_DEVICE_NAME_INTERNAL CFStringRef VPDeviceNameCreateFromBytes(const UInt8 *bytes, CFIndex length);+// The name in the contents of VP_DEVICE_NAME_CONFIG_PATH: NULL unless they are+// a property list dictionary whose VP_DEVICE_NAME_CONFIG_KEY holds a valid name.+VP_DEVICE_NAME_INTERNAL CFStringRef VPDeviceNameCreateFromConfiguration(CFDataRef contents);+// A copy of a `Setup:/System` value with ComputerName set to `name` and its// encoding to UTF-8; every other entry is kept. A missing value, or one that is// not a dictionary, counts as empty. NULL when the value already names `name`,@@ -61,8 +67,11 @@ VP_DEVICE_NAME_INTERNAL Boolean VPDeviceNameCreatePinnedPublication(CFDictionaryCFTypeRef stored, CFStringRef name,CFDictionaryRef *outSet, CFArrayRef *outRemove);-// Whether lockdownd may set the ComputerName `requested` while `pinned` is in-// force: only to the pinned name itself, or anything when nothing is pinned.-VP_DEVICE_NAME_INTERNAL Boolean VPDeviceNameAllowsRename(CFStringRef pinned, CFStringRef requested);+// Whether lockdownd may set the ComputerName while `pinned` is in force: only+// when nothing is pinned. The pinned name itself is refused too: lockdownd names+// the device after the store whenever it starts, and once configd has published+// the pin that would write the pinned name, and a host name and local host name+// derived from it, into preferences.plist.+VP_DEVICE_NAME_INTERNAL Boolean VPDeviceNameAllowsRename(CFStringRef pinned);#endifdiff --git a/VPhoneGuestComponents/DeviceName/libdevicename.c b/VPhoneGuestComponents/DeviceName/libdevicename.cindex ef8e15ee..4269d446 100644--- a/VPhoneGuestComponents/DeviceName/libdevicename.c+++ b/VPhoneGuestComponents/DeviceName/libdevicename.c@@ -16,11 +16,19 @@//// ## The channel//-// Before each boot with the feature on, the host writes NVRAM variable-// `vphone-device-name`: the name's UTF-8 bytes, no terminator. With it off the-// host removes it. The guest reads it as the `IODeviceTree:/options` property-// of that name. Absent, empty or invalid (DeviceNamePolicy.c) means the feature-// is off, and every interpose here passes its call through unchanged.+// The name is `DeviceName` in /var/db/vphone/devicename.plist. vphoned writes+// it, in one rename, when the host calls `device.name.set`, which vphone-vm+// does after every connect with the VM's name, and removes it for a null name.+// The file survives a reboot, so configd's first publication at boot already+// carries the name, before lockdownd first reads it. Both daemons check the+// file on every decision and read it again when it changed, so a new name+// applies while the guest runs: vphoned then has configd publish again.+// Absent, unreadable or not a valid name (DeviceNamePolicy.c) means nothing is+// pinned, and every interpose here passes its call through unchanged.+//+// An NVRAM variable written by the host before boot was the first channel. It+// never reaches the guest: iBoot keeps only the variables it knows+// (Research/Guest/device_name_pinning.md).//// ## configd: the published name is the pinned one//@@ -30,7 +38,8 @@// kSCPrefSystem)` into `Setup:/...` keys, compares them with what the store// already has (`SCDynamicStoreCopyMultiple("^Setup:.*")`), leaves out the keys// that did not change, and publishes the rest with one-// `SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, NULL)`.+// `SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, NULL)`. It runs on+// every apply of the preferences, also one that changed nothing in them.//// The pin goes in at that publication, not at the preferences read:// `SCDynamicStoreSetMultiple` is interposed and, for a call that touches the@@ -42,14 +51,21 @@// `SCDynamicStoreSetValue` of `Setup:/System` is pinned the same way, though no// configd caller is known to set that key on its own.//+// That is how a name set while the guest runs takes effect: vphoned applies+// the preferences unchanged and the monitor publishes again. With the store+// holding the guest's own name the call leaves `Setup:/System` out, and the+// store's value gets the new pin; with an older pin in the store the call+// sets the preferences' value, which is pinned. Once nothing is pinned, the+// store's pin differs from the preferences, so the call sets their value and+// the guest's own name returns (measured, 24A435).+//// Substituting the `kSCPrefSystem` value from `SCPreferencesGetValue` would be// read by the same flattening, but configd also reads that value to write it// back: the monitor's model-change path (24A435 sub_10006155c) does// `SCPreferencesGetValue(prefs, kSCPrefSystem)`,// `__SCNetworkConfigurationSaveModel`, `SCPreferencesSetValue(prefs,// kSCPrefSystem, same)`, which would commit the pinned name to disk. The-// publication route never changes preferences.plist, so turning the feature off-// brings the guest's own name back on the next boot.+// publication route never changes preferences.plist.//// set-hostname's `__SCPreferencesCopyComputerName` reads the preferences file// directly, to derive a DNS host name from a reverse lookup; it is not a@@ -57,15 +73,21 @@//// ## lockdownd: renames are refused//-// While a name is pinned, `SCPreferencesSetComputerName` with any other name-// returns false with `kSCStatusAccessError`, and the host name and local host-// name setters that follow on the same preferences object do too, so the-// preferences keep their name. A rename to the pinned name itself goes through.-// `set_device_name` only logs each setter's failure and its SetValue handler-// ignores the result, so a host that renames sees success and the name stays.-// lockdownd's startup naming (`MarketingDeviceFamilyName` when the store has-// no name) is refused the same way if it runs before configd has published;-// once configd has, lockdownd finds a name and does not try.+// While a name is pinned, `SCPreferencesSetComputerName` returns false with+// `kSCStatusAccessError`, and the host name and local host name setters that+// follow on the same preferences object do too, so the preferences keep their+// name. `set_device_name` only logs each setter's failure and its SetValue+// handler ignores the result, so a host that renames sees success and the name+// stays.+//+// The pinned name itself is refused as well. lockdownd names the device every+// time it starts (24A435 0x100011e4c): `set_device_name(copy_device_name())`,+// or `MarketingDeviceFamilyName` when there is none. At boot it runs before+// configd has published and gets the name the preferences already hold+// (measured). A lockdownd started later (a crash, or+// vphoned's `udid.set` stopping it) reads the pinned name from the store, and+// letting that through wrote it, a host name and a local host name derived+// from it into preferences.plist (measured: `dhtest-27-ceshi`).//// ## Reach//@@ -78,19 +100,13 @@//// ## Assumptions to confirm on a guest//-// - root processes read `vphone-device-name` from IODeviceTree:/options as-// written by the host (bare name, or under Apple's NVRAM GUID, both tried);// - configd's PreferencesMonitor is the only publisher of `Setup:/System`;-// - lockdownd may read NVRAM. If it cannot, its interposes see no pinned name-// and pass renames through to preferences.plist, while configd still-// publishes the pinned name;// - Settings' General > About > Name goes through lockdownd. If it writes the// preferences itself, configd still publishes the pinned name.#include "DeviceNamePolicy.h"#include <CoreFoundation/CoreFoundation.h>-#include <IOKit/IOKitLib.h>#include <fcntl.h>#include <pthread.h>#include <stdarg.h>@@ -121,9 +137,6 @@ extern void _SCErrorSet(int error);// SystemConfiguration.h: "Permission denied".#define VP_SC_STATUS_ACCESS_ERROR 1003-// Apple's NVRAM variable GUID, for a host that writes the name under it.-#define VP_APPLE_NVRAM_GUID "7C436110-AB2A-4BBB-A880-FE41995C9F82"-// MARK: - Log// configd and lockdownd both run as root, but the mode keeps the log open to@@ -169,55 +182,94 @@ static const char *vpDescribe(CFStringRef string, char *buffer, size_t size) {enum { VPRoleOther, VPRoleConfigd, VPRoleLockdownd };static int vpRole;++// The name last read from VP_DEVICE_NAME_CONFIG_PATH and the file it came from.+// The host sets the name after every connect, and a rename changes it while the+// guest runs, so the file is checked on every decision; it is read again only+// when its identity, size or modification time changed. vphoned replaces it in+// one rename, so a new inode or time marks every write.+static pthread_mutex_t vpPinnedLock = PTHREAD_MUTEX_INITIALIZER;static CFStringRef vpPinned;-static pthread_once_t vpPinnedOnce = PTHREAD_ONCE_INIT;+static struct stat vpPinnedStamp;+static Boolean vpPinnedRead;++static Boolean vpSameStamp(const struct stat *a, const struct stat *b) {+ return a->st_dev == b->st_dev && a->st_ino == b->st_ino && a->st_size == b->st_size &&+ a->st_mtimespec.tv_sec == b->st_mtimespec.tv_sec && a->st_mtimespec.tv_nsec == b->st_mtimespec.tv_nsec;+}-static CFStringRef vpCopyNVRAMName(void) {- io_registry_entry_t options = IORegistryEntryFromPath(kIOMainPortDefault, "IODeviceTree:/options");- if (options == MACH_PORT_NULL)+// The name in the file `fd` reads, or NULL.+static CFStringRef vpCopyConfiguredName(int fd, off_t size) {+ if (size <= 0 || size > 64 * 1024)return NULL;- static const CFStringRef names[] = {- CFSTR(VP_DEVICE_NAME_NVRAM_VARIABLE),- CFSTR(VP_APPLE_NVRAM_GUID ":" VP_DEVICE_NAME_NVRAM_VARIABLE),- };+ UInt8 *bytes = malloc((size_t)size);+ if (!bytes)+ return NULL;+ ssize_t count = pread(fd, bytes, (size_t)size, 0);CFStringRef name = NULL;- for (size_t index = 0; !name && index < sizeof(names) / sizeof(names[0]); index++) {- CFTypeRef property = IORegistryEntryCreateCFProperty(options, names[index], kCFAllocatorDefault, 0);- if (!property)- continue;- name = VPDeviceNameCreateFromProperty(property);- if (!name)- vpLog("NVRAM %s is present but not a valid name; nothing is pinned",- VP_DEVICE_NAME_NVRAM_VARIABLE);- CFRelease(property);- break;+ if (count == size) {+ CFDataRef contents = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, bytes, size, kCFAllocatorNull);+ if (contents) {+ name = VPDeviceNameCreateFromConfiguration(contents);+ CFRelease(contents);+ }}- IOObjectRelease(options);+ free(bytes);return name;}-static void vpResolvePinned(void) {- vpPinned = vpCopyNVRAMName();+// Make vpPinned match the file. Called with vpPinnedLock held.+static void vpRefreshPinned(void) {+ struct stat info;+ const Boolean present = stat(VP_DEVICE_NAME_CONFIG_PATH, &info) == 0;+ if (vpPinnedRead && (present ? vpSameStamp(&info, &vpPinnedStamp) : vpPinnedStamp.st_ino == 0))+ return;+ CFStringRef name = NULL;+ memset(&vpPinnedStamp, 0, sizeof(vpPinnedStamp));+ if (present) {+ int fd = open(VP_DEVICE_NAME_CONFIG_PATH, O_RDONLY | O_CLOEXEC);+ if (fd >= 0) {+ // The stamp of what is read, not of what stat saw a moment ago.+ if (fstat(fd, &vpPinnedStamp) == 0)+ name = vpCopyConfiguredName(fd, vpPinnedStamp.st_size);+ close(fd);+ }+ if (!name)+ vpLog("%s holds no valid name; nothing is pinned", VP_DEVICE_NAME_CONFIG_PATH);+ }+ vpPinnedRead = true;char buffer[VP_DEVICE_NAME_MAX_BYTES + 1];+ if (name && !(vpPinned && CFEqual(name, vpPinned)))+ vpLog("pinned name \"%s\"", vpDescribe(name, buffer, sizeof(buffer)));+ else if (!name && vpPinned)+ vpLog("no name pinned any more");if (vpPinned)- vpLog("pinned name \"%s\"", vpDescribe(vpPinned, buffer, sizeof(buffer)));+ CFRelease(vpPinned);+ vpPinned = name;}-// The pinned name for the process this hook acts in, or NULL to pass through.-// Read once: the host writes it before boot and nothing changes it after.-static CFStringRef vpPinnedName(int role) {+// The pinned name for the process this hook acts in, retained, or NULL to pass+// through.+static CFStringRef vpCopyPinnedName(int role) {if (vpRole != role)return NULL;- pthread_once(&vpPinnedOnce, vpResolvePinned);- return vpPinned;+ pthread_mutex_lock(&vpPinnedLock);+ vpRefreshPinned();+ CFStringRef name = vpPinned ? CFRetain(vpPinned) : NULL;+ pthread_mutex_unlock(&vpPinnedLock);+ return name;}// MARK: - configdstatic Boolean vpSetMultiple(VPSCDynamicStoreRef store, CFDictionaryRef keysToSet, CFArrayRef keysToRemove,CFArrayRef keysToNotify) {- CFStringRef name = vpPinnedName(VPRoleConfigd);- if (!name || !VPDeviceNameIsSetupPublication(keysToSet, keysToRemove))+ // configd's other plugins publish State: keys through here all the time;+ // they never touch the file.+ if (vpRole != VPRoleConfigd || !VPDeviceNameIsSetupPublication(keysToSet, keysToRemove))+ return SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, keysToNotify);+ CFStringRef name = vpCopyPinnedName(VPRoleConfigd);+ if (!name)return SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, keysToNotify);CFPropertyListRef stored = VPDeviceNameNeedsStoredSystem(keysToSet, keysToRemove)? SCDynamicStoreCopyValue(store, VP_DEVICE_NAME_SYSTEM_KEY)@@ -227,29 +279,38 @@ static Boolean vpSetMultiple(VPSCDynamicStoreRef store, CFDictionaryRef keysToSeconst Boolean changed = VPDeviceNameCreatePinnedPublication(keysToSet, keysToRemove, stored, name, &set, &remove);if (stored)CFRelease(stored);- if (!changed)- return SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, keysToNotify);- const Boolean result = SCDynamicStoreSetMultiple(store, set, remove, keysToNotify);- char buffer[VP_DEVICE_NAME_MAX_BYTES + 1];- vpLog("published Setup:/System ComputerName \"%s\" result=%d", vpDescribe(name, buffer, sizeof(buffer)),- result);- CFRelease(set);- if (remove)- CFRelease(remove);+ Boolean result;+ if (changed) {+ result = SCDynamicStoreSetMultiple(store, set, remove, keysToNotify);+ char buffer[VP_DEVICE_NAME_MAX_BYTES + 1];+ vpLog("published Setup:/System ComputerName \"%s\" result=%d", vpDescribe(name, buffer, sizeof(buffer)),+ result);+ CFRelease(set);+ if (remove)+ CFRelease(remove);+ } else {+ result = SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, keysToNotify);+ }+ CFRelease(name);return result;}static Boolean vpSetValue(VPSCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value) {- CFStringRef name = vpPinnedName(VPRoleConfigd);- if (!name || !key || !CFEqual(key, VP_DEVICE_NAME_SYSTEM_KEY))+ if (vpRole != VPRoleConfigd || !key || !CFEqual(key, VP_DEVICE_NAME_SYSTEM_KEY))return SCDynamicStoreSetValue(store, key, value);- CFDictionaryRef pinned = VPDeviceNameCreatePinnedSystem(value, name);- if (!pinned)- return SCDynamicStoreSetValue(store, key, value);- const Boolean result = SCDynamicStoreSetValue(store, key, pinned);- char buffer[VP_DEVICE_NAME_MAX_BYTES + 1];- vpLog("set Setup:/System ComputerName \"%s\" result=%d", vpDescribe(name, buffer, sizeof(buffer)), result);- CFRelease(pinned);+ CFStringRef name = vpCopyPinnedName(VPRoleConfigd);+ CFDictionaryRef pinned = name ? VPDeviceNameCreatePinnedSystem(value, name) : NULL;+ Boolean result;+ if (pinned) {+ result = SCDynamicStoreSetValue(store, key, pinned);+ char buffer[VP_DEVICE_NAME_MAX_BYTES + 1];+ vpLog("set Setup:/System ComputerName \"%s\" result=%d", vpDescribe(name, buffer, sizeof(buffer)), result);+ CFRelease(pinned);+ } else {+ result = SCDynamicStoreSetValue(store, key, value);+ }+ if (name)+ CFRelease(name);return result;}@@ -260,36 +321,52 @@ static Boolean vpSetValue(VPSCDynamicStoreRef store, CFStringRef key, CFProperty// turn, so the host name setters follow the ComputerName decision.static __thread VPSCPreferencesRef vpRefusedPreferences;-static Boolean vpRefuse(const char *setter, CFStringRef requested) {+static Boolean vpRefuse(const char *setter, CFStringRef requested, CFStringRef pinned) {char wanted[VP_DEVICE_NAME_MAX_BYTES + 1];- char pinned[VP_DEVICE_NAME_MAX_BYTES + 1];+ char kept[VP_DEVICE_NAME_MAX_BYTES + 1];vpLog("refused %s(\"%s\"): the name is pinned to \"%s\"", setter, vpDescribe(requested, wanted, sizeof(wanted)),- vpDescribe(vpPinned, pinned, sizeof(pinned)));+ vpDescribe(pinned, kept, sizeof(kept)));_SCErrorSet(VP_SC_STATUS_ACCESS_ERROR);return false;}static Boolean vpSetComputerName(VPSCPreferencesRef prefs, CFStringRef name, CFStringEncoding encoding) {- CFStringRef pinned = vpPinnedName(VPRoleLockdownd);- if (VPDeviceNameAllowsRename(pinned, name)) {+ CFStringRef pinned = vpCopyPinnedName(VPRoleLockdownd);+ Boolean result;+ if (VPDeviceNameAllowsRename(pinned)) {vpRefusedPreferences = NULL;- return SCPreferencesSetComputerName(prefs, name, encoding);+ result = SCPreferencesSetComputerName(prefs, name, encoding);+ } else {+ vpRefusedPreferences = prefs;+ result = vpRefuse("SCPreferencesSetComputerName", name, pinned);}- vpRefusedPreferences = prefs;- return vpRefuse("SCPreferencesSetComputerName", name);+ if (pinned)+ CFRelease(pinned);+ return result;+}++// The host name setters after a refused ComputerName on the same preferences.+static Boolean vpRefuseFollowing(const char *setter, VPSCPreferencesRef prefs, CFStringRef name) {+ if (vpRole != VPRoleLockdownd || !prefs || prefs != vpRefusedPreferences)+ return false;+ CFStringRef pinned = vpCopyPinnedName(VPRoleLockdownd);+ vpRefuse(setter, name, pinned);+ if (pinned)+ CFRelease(pinned);+ return true;}static Boolean vpSetHostName(VPSCPreferencesRef prefs, CFStringRef name) {- if (vpPinnedName(VPRoleLockdownd) && prefs && prefs == vpRefusedPreferences)- return vpRefuse("SCPreferencesSetHostName", name);+ if (vpRefuseFollowing("SCPreferencesSetHostName", prefs, name))+ return false;return SCPreferencesSetHostName(prefs, name);}static Boolean vpSetLocalHostName(VPSCPreferencesRef prefs, CFStringRef name) {- if (vpPinnedName(VPRoleLockdownd) && prefs && prefs == vpRefusedPreferences) {+ if (vpRefuseFollowing("SCPreferencesSetLocalHostName", prefs, name)) {// The last of set_device_name's three setters.vpRefusedPreferences = NULL;- return vpRefuse("SCPreferencesSetLocalHostName", name);+ return false;}return SCPreferencesSetLocalHostName(prefs, name);}@@ -306,7 +383,7 @@ __attribute__((constructor)) static void vpDeviceNameInit(void) {int vphone_devicename_version(void) { return 1; }// Easy to change: each row is one import of configd or lockdownd. Which of-// the two a row acts in is decided by `vpPinnedName`'s role check.+// the two a row acts in is decided by the role each interpose checks.__attribute__((used, section("__DATA,__interpose"))) static const struct {const void *replacement;const void *replacee;diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefileindex c406401a..810d6ab7 100644--- a/VPhoneGuestComponents/Makefile+++ b/VPhoneGuestComponents/Makefile@@ -247,7 +247,7 @@ $(DEVICENAME): $(DEVICENAME_SOURCES) $(DEVICENAME_INCLUDES) DeviceName | $(STAGE$(CLANG) $(GUEST_C_FLAGS) -Wall -Wextra -Werror -dynamiclib \-install_name /usr/lib/libdevicename.dylib \-o $@ $(DEVICENAME_SOURCES) \- -framework CoreFoundation -framework IOKit -framework SystemConfiguration \+ -framework CoreFoundation -framework SystemConfiguration \-Wl,-not_for_dyld_shared_cache@codesign --force --sign - $@diff --git a/VPhoneGuestComponents/README.md b/VPhoneGuestComponents/README.mdindex b4620087..a0692ca7 100644--- a/VPhoneGuestComponents/README.md+++ b/VPhoneGuestComponents/README.md@@ -30,10 +30,11 @@ SystemHook; it completes the internal battery's power source description withthe health keys a real battery has and answers the two questions only batteryhardware can, so Battery Health shows a healthy battery instead of loadingforever. Both also insert `libdevicename.dylib` into configd and lockdownd-(lockdownd takes it after `libmisfix.dylib`). It acts only when the host has-written the NVRAM variable `vphone-device-name`: configd then publishes that-name as the device name and lockdownd refuses renames, while-`preferences.plist` keeps the guest's own name. See+(lockdownd takes it after `libmisfix.dylib`). It acts only when vphoned has+stored a name in `/var/db/vphone/devicename.plist` (the host sends the VM's+name after every connect): configd then publishes that name as the device name+and lockdownd refuses renames, while `preferences.plist` keeps the guest's own+name. See`Research/Guest/device_name_pinning.md`.After a bootstrap installs ElleKit, the launchd hookinserts SystemHook into `xpcproxy`, bootstrap executables, and apps starteddiff --git a/VPhoneGuestComponents/Tests/DeviceNameTests.c b/VPhoneGuestComponents/Tests/DeviceNameTests.cindex ade6641a..e5169522 100644--- a/VPhoneGuestComponents/Tests/DeviceNameTests.c+++ b/VPhoneGuestComponents/Tests/DeviceNameTests.c@@ -48,7 +48,7 @@ static int encodingIsUTF8(CFDictionaryRef set) {// MARK: - Name-static void decodesTheNVRAMName(void) {+static void decodesAName(void) {CFStringRef name = nameFromBytes("Lab iPhone", 10);assert(namesEqual(name, "Lab iPhone"));CFRelease(name);@@ -104,6 +104,59 @@ static void refusesWhatIsNotAName(void) {CFRelease(tooLong);}+static CFDataRef configuration(CFTypeRef name, CFPropertyListFormat format) {+ CFMutableDictionaryRef plist = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,+ &kCFTypeDictionaryValueCallBacks);+ if (name)+ CFDictionarySetValue(plist, VP_DEVICE_NAME_CONFIG_KEY, name);+ CFDictionarySetValue(plist, CFSTR("Other"), CFSTR("kept"));+ CFDataRef data = CFPropertyListCreateData(kCFAllocatorDefault, plist, format, 0, NULL);+ CFRelease(plist);+ return data;+}++static void readsTheConfiguration(void) {+ // What vphoned writes: a binary property list. XML reads the same.+ CFStringRef wanted = CFStringCreateWithCString(kCFAllocatorDefault, "dhtest 27 \xe6\xb5\x8b\xe8\xaf\x95",+ kCFStringEncodingUTF8);+ CFPropertyListFormat formats[] = {kCFPropertyListBinaryFormat_v1_0, kCFPropertyListXMLFormat_v1_0};+ for (size_t index = 0; index < sizeof(formats) / sizeof(formats[0]); index++) {+ CFDataRef data = configuration(wanted, formats[index]);+ CFStringRef name = VPDeviceNameCreateFromConfiguration(data);+ assert(name && CFEqual(name, wanted));+ CFRelease(name);+ CFRelease(data);+ }+ CFRelease(wanted);++ // No key, an invalid name, a value of another type: nothing pinned.+ CFDataRef data = configuration(NULL, kCFPropertyListBinaryFormat_v1_0);+ assert(!VPDeviceNameCreateFromConfiguration(data));+ CFRelease(data);+ data = configuration(CFSTR("Lab\niPhone"), kCFPropertyListBinaryFormat_v1_0);+ assert(!VPDeviceNameCreateFromConfiguration(data));+ CFRelease(data);+ data = configuration(CFSTR(""), kCFPropertyListBinaryFormat_v1_0);+ assert(!VPDeviceNameCreateFromConfiguration(data));+ CFRelease(data);+ int number = 1;+ CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &number);+ data = configuration(value, kCFPropertyListBinaryFormat_v1_0);+ assert(!VPDeviceNameCreateFromConfiguration(data));+ CFRelease(data);+ CFRelease(value);++ // Not a property list, or not a dictionary.+ assert(!VPDeviceNameCreateFromConfiguration(NULL));+ data = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"Lab iPhone", 10);+ assert(!VPDeviceNameCreateFromConfiguration(data));+ CFRelease(data);+ data = CFPropertyListCreateData(kCFAllocatorDefault, CFSTR("Lab iPhone"), kCFPropertyListBinaryFormat_v1_0, 0,+ NULL);+ assert(!VPDeviceNameCreateFromConfiguration(data));+ CFRelease(data);+}+// MARK: - configdstatic void pinsTheSystemValue(void) {@@ -262,25 +315,23 @@ static void passesThroughWithoutAName(void) {// MARK: - lockdownd-static void allowsOnlyThePinnedName(void) {- assert(VPDeviceNameAllowsRename(NULL, CFSTR("Anything")));- assert(VPDeviceNameAllowsRename(NULL, NULL));- assert(VPDeviceNameAllowsRename(CFSTR("Lab"), CFSTR("Lab")));- assert(!VPDeviceNameAllowsRename(CFSTR("Lab"), CFSTR("iPhone")));- assert(!VPDeviceNameAllowsRename(CFSTR("Lab"), CFSTR("lab")));- assert(!VPDeviceNameAllowsRename(CFSTR("Lab"), NULL));+static void refusesEveryRenameWhilePinned(void) {+ assert(VPDeviceNameAllowsRename(NULL));+ // The pinned name itself too: lockdownd would store it in the preferences.+ assert(!VPDeviceNameAllowsRename(CFSTR("Lab")));}int main(void) {- decodesTheNVRAMName();+ decodesAName();refusesWhatIsNotAName();+ readsTheConfiguration();pinsTheSystemValue();recognisesThePreferencesMonitor();pinsAPublishedName();pinsAnUnchangedName();keepsARemovedName();passesThroughWithoutAName();- allowsOnlyThePinnedName();+ refusesEveryRenameWhilePinned();puts("DeviceNameTests: ok");return 0;}