Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Pin the guest's device name to the one the host writes to NVRAM

Jackye5bcf8006af3
diff --git a/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c
new file mode 100644
index 00000000..f19e84e8
--- /dev/null
+++ b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c
@@ -0,0 +1,163 @@
+#include "DeviceNamePolicy.h"
+
+#include <stdlib.h>
+
+// MARK: - Name
+
+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.
+ while (length > 0 && bytes[length - 1] == 0)
+ length--;
+ if (length == 0 || length > VP_DEVICE_NAME_MAX_BYTES)
+ return NULL;
+ // No control characters, and so no embedded NUL either. Bytes of a
+ // multibyte UTF-8 sequence are all 0x80 or above.
+ for (CFIndex index = 0; index < length; index++) {
+ if (bytes[index] < 0x20 || bytes[index] == 0x7f)
+ return NULL;
+ }
+ // NULL for a malformed sequence.
+ return CFStringCreateWithBytes(kCFAllocatorDefault, bytes, length, kCFStringEncodingUTF8, false);
+}
+
+CFStringRef VPDeviceNameCreateFromProperty(CFTypeRef property) {
+ if (!property)
+ return NULL;
+ if (CFGetTypeID(property) == CFDataGetTypeID())
+ return VPDeviceNameCreateFromBytes(CFDataGetBytePtr((CFDataRef)property),
+ CFDataGetLength((CFDataRef)property));
+ if (CFGetTypeID(property) == CFStringGetTypeID()) {
+ // Checked as the bytes would be, so both forms take the same names.
+ UInt8 bytes[VP_DEVICE_NAME_MAX_BYTES + 1];
+ CFIndex used = 0;
+ CFStringRef string = (CFStringRef)property;
+ CFIndex converted = CFStringGetBytes(string, CFRangeMake(0, CFStringGetLength(string)),
+ kCFStringEncodingUTF8, 0, false, bytes, sizeof(bytes), &used);
+ if (converted != CFStringGetLength(string))
+ return NULL;
+ return VPDeviceNameCreateFromBytes(bytes, used);
+ }
+ return NULL;
+}
+
+// MARK: - configd
+
+CFDictionaryRef VPDeviceNameCreatePinnedSystem(CFTypeRef system, CFStringRef name) {
+ if (!name)
+ return NULL;
+ CFDictionaryRef dictionary =
+ system && CFGetTypeID(system) == CFDictionaryGetTypeID() ? (CFDictionaryRef)system : NULL;
+ CFTypeRef current = dictionary ? CFDictionaryGetValue(dictionary, VP_DEVICE_NAME_COMPUTER_NAME) : NULL;
+ if (current && CFEqual(current, name))
+ return NULL;
+ CFMutableDictionaryRef pinned =
+ dictionary ? CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, dictionary)
+ : CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+ if (!pinned)
+ return NULL;
+ CFDictionarySetValue(pinned, VP_DEVICE_NAME_COMPUTER_NAME, name);
+ // The name came from UTF-8. The recorded encoding is only a hint for
+ // converting the name to a legacy encoding, and the one the preferences
+ // carry belongs to the name being replaced.
+ SInt32 encoding = (SInt32)kCFStringEncodingUTF8;
+ CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &encoding);
+ if (number) {
+ CFDictionarySetValue(pinned, VP_DEVICE_NAME_COMPUTER_NAME_ENCODING, number);
+ CFRelease(number);
+ }
+ return pinned;
+}
+
+static Boolean vpHasSetupPrefix(CFTypeRef key) {
+ return key && CFGetTypeID(key) == CFStringGetTypeID() && CFStringHasPrefix((CFStringRef)key, CFSTR("Setup:"));
+}
+
+static Boolean vpArrayContainsSystemKey(CFArrayRef keys) {
+ return keys && CFArrayContainsValue(keys, CFRangeMake(0, CFArrayGetCount(keys)), VP_DEVICE_NAME_SYSTEM_KEY);
+}
+
+Boolean VPDeviceNameIsSetupPublication(CFDictionaryRef keysToSet, CFArrayRef keysToRemove) {
+ if (keysToSet) {
+ CFIndex count = CFDictionaryGetCount(keysToSet);
+ if (count > 0) {
+ const void **keys = malloc((size_t)count * sizeof(*keys));
+ if (!keys)
+ return false;
+ CFDictionaryGetKeysAndValues(keysToSet, keys, NULL);
+ Boolean found = false;
+ for (CFIndex index = 0; index < count && !found; index++)
+ found = vpHasSetupPrefix(keys[index]);
+ free(keys);
+ if (found)
+ return true;
+ }
+ }
+ if (keysToRemove) {
+ for (CFIndex index = 0; index < CFArrayGetCount(keysToRemove); index++) {
+ if (vpHasSetupPrefix(CFArrayGetValueAtIndex(keysToRemove, index)))
+ return true;
+ }
+ }
+ return false;
+}
+
+Boolean VPDeviceNameNeedsStoredSystem(CFDictionaryRef keysToSet, CFArrayRef keysToRemove) {
+ return !(keysToSet && CFDictionaryContainsKey(keysToSet, VP_DEVICE_NAME_SYSTEM_KEY)) &&
+ !vpArrayContainsSystemKey(keysToRemove);
+}
+
+Boolean VPDeviceNameCreatePinnedPublication(CFDictionaryRef keysToSet, CFArrayRef keysToRemove, CFTypeRef stored,
+ CFStringRef name, CFDictionaryRef *outSet, CFArrayRef *outRemove) {
+ *outSet = NULL;
+ *outRemove = NULL;
+ if (!name)
+ return false;
+ CFTypeRef published = keysToSet ? CFDictionaryGetValue(keysToSet, VP_DEVICE_NAME_SYSTEM_KEY) : NULL;
+ const Boolean removed = vpArrayContainsSystemKey(keysToRemove);
+ CFTypeRef base = published ? published : removed ? NULL : stored;
+ CFDictionaryRef pinned = VPDeviceNameCreatePinnedSystem(base, name);
+ // NULL: the value already names `name`. A removal always gets a value,
+ // since an empty one names nothing.
+ if (!pinned)
+ return false;
+
+ CFMutableDictionaryRef set =
+ keysToSet ? CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, keysToSet)
+ : CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+ if (!set) {
+ CFRelease(pinned);
+ return false;
+ }
+ CFDictionarySetValue(set, VP_DEVICE_NAME_SYSTEM_KEY, pinned);
+ CFRelease(pinned);
+
+ CFArrayRef remove = NULL;
+ if (removed) {
+ CFMutableArrayRef kept = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
+ if (!kept) {
+ CFRelease(set);
+ return false;
+ }
+ for (CFIndex index = 0; index < CFArrayGetCount(keysToRemove); index++) {
+ CFTypeRef key = CFArrayGetValueAtIndex(keysToRemove, index);
+ if (!CFEqual(key, VP_DEVICE_NAME_SYSTEM_KEY))
+ CFArrayAppendValue(kept, key);
+ }
+ remove = kept;
+ } else if (keysToRemove) {
+ remove = CFRetain(keysToRemove);
+ }
+ *outSet = set;
+ *outRemove = remove;
+ return true;
+}
+
+// MARK: - lockdownd
+
+Boolean VPDeviceNameAllowsRename(CFStringRef pinned, CFStringRef requested) {
+ return !pinned || (requested && CFEqual(pinned, requested));
+}
diff --git a/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.h b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.h
new file mode 100644
index 00000000..7d1286bf
--- /dev/null
+++ b/VPhoneGuestComponents/DeviceName/DeviceNamePolicy.h
@@ -0,0 +1,68 @@
+#ifndef VPHONE_DEVICE_NAME_POLICY_H
+#define VPHONE_DEVICE_NAME_POLICY_H
+
+// The decisions libdevicename makes, as pure CoreFoundation, so the host tests
+// can check them without a guest. See libdevicename.c for where each is used.
+
+#include <CoreFoundation/CoreFoundation.h>
+
+// 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 longest name taken, in UTF-8 bytes. A longer value is invalid, so the
+// feature is off rather than truncating the name.
+#define VP_DEVICE_NAME_MAX_BYTES 255
+
+// The dynamic store key configd publishes the name under
+// (`SCDynamicStoreKeyCreateComputerName`), and the entries in it
+// (`kSCPropSystemComputerName`, `kSCPropSystemComputerNameEncoding`).
+#define VP_DEVICE_NAME_SYSTEM_KEY CFSTR("Setup:/System")
+#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
+// 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);
+
+// 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`,
+// so the caller passes the original through.
+VP_DEVICE_NAME_INTERNAL CFDictionaryRef VPDeviceNameCreatePinnedSystem(CFTypeRef system, CFStringRef name);
+
+// Whether an SCDynamicStoreSetMultiple call sets or removes anything in the
+// `Setup:` domain, which in configd is the preferences monitor publishing
+// preferences.plist.
+VP_DEVICE_NAME_INTERNAL Boolean VPDeviceNameIsSetupPublication(CFDictionaryRef keysToSet, CFArrayRef keysToRemove);
+
+// Whether VPDeviceNameCreatePinnedPublication needs the store's current
+// `Setup:/System`: true when the call neither sets nor removes it, because the
+// monitor leaves out a key whose value has not changed.
+VP_DEVICE_NAME_INTERNAL Boolean VPDeviceNameNeedsStoredSystem(CFDictionaryRef keysToSet, CFArrayRef keysToRemove);
+
+// Rewrites one publication so `Setup:/System` names `name`:
+// - set: its value gets the pinned ComputerName;
+// - removed (the preferences lost their System entry): the removal is dropped
+// and a value holding only the pinned name is set instead;
+// - neither: `stored`, the store's current value, is pinned and set if it
+// does not already name `name`.
+// Returns true with *outSet (and *outRemove, which may be NULL when
+// `keysToRemove` is) to pass on instead, both owned by the caller; false, with
+// both NULL, when the call can go through unchanged.
+VP_DEVICE_NAME_INTERNAL Boolean VPDeviceNameCreatePinnedPublication(CFDictionaryRef keysToSet, CFArrayRef keysToRemove,
+ CFTypeRef 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);
+
+#endif
diff --git a/VPhoneGuestComponents/DeviceName/libdevicename.c b/VPhoneGuestComponents/DeviceName/libdevicename.c
new file mode 100644
index 00000000..ef8e15ee
--- /dev/null
+++ b/VPhoneGuestComponents/DeviceName/libdevicename.c
@@ -0,0 +1,319 @@
+// libdevicename.c — pin the guest's device name to the one the host chose,
+// and refuse renaming it inside the guest.
+//
+// ## Where the name lives
+//
+// The device name is `System/System/ComputerName` in
+// /private/var/preferences/SystemConfiguration/preferences.plist. configd
+// (/usr/libexec/configd, a standalone image) publishes it into the dynamic
+// store as `Setup:/System`, and every reader goes through the store:
+// lockdownd's `copy_device_name` (`SCDynamicStoreCopyComputerName`, what
+// lockdown `GetValue DeviceName`, Finder and Xcode see), MobileGestalt's
+// UserAssignedDeviceName and UIDevice.name. Renames come in through lockdownd's
+// `set_device_name`, which calls `SCPreferencesSetComputerName`,
+// `SCPreferencesSetHostName` and `SCPreferencesSetLocalHostName` (its only
+// callers of them) and commits.
+//
+// ## 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.
+//
+// ## configd: the published name is the pinned one
+//
+// configd's preferences monitor (`updateConfiguration` in configd's
+// PreferencesMonitor, statically linked into configd; 24A435:
+// 0x100062098..0x100062d00) flattens `SCPreferencesGetValue(prefs,
+// 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)`.
+//
+// The pin goes in at that publication, not at the preferences read:
+// `SCDynamicStoreSetMultiple` is interposed and, for a call that touches the
+// `Setup:` domain, `Setup:/System`'s ComputerName becomes the pinned name
+// (VPDeviceNameCreatePinnedPublication). When the call leaves the key out
+// because it did not change, the store's current value is read and pinned if it
+// does not already say so, and when it removes the key it is set instead. So a
+// guest whose preferences never had a name gets the pinned one too.
+// `SCDynamicStoreSetValue` of `Setup:/System` is pinned the same way, though no
+// configd caller is known to set that key on its own.
+//
+// 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.
+//
+// set-hostname's `__SCPreferencesCopyComputerName` reads the preferences file
+// directly, to derive a DNS host name from a reverse lookup; it is not a
+// display name and is left alone.
+//
+// ## 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.
+//
+// ## Reach
+//
+// configd and lockdownd are standalone images, so their imports bind through
+// dyld's interposing table; calls inside the shared cache do not, which is why
+// the hooks sit on the daemons' own imports. Both spawn hooks insert this into
+// those two processes only (`vpIsDeviceNameTarget` in
+// Shared/InjectionEnvironment.h); lockdownd takes it after libmisfix. Each
+// interpose checks which of the two it is in.
+//
+// ## 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>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+// MARK: - SystemConfiguration
+
+// Exported by SystemConfiguration on iOS, but its headers mark them
+// unavailable there, so they are declared here.
+typedef const struct __SCDynamicStore *VPSCDynamicStoreRef;
+typedef const struct __SCPreferences *VPSCPreferencesRef;
+
+extern Boolean SCDynamicStoreSetMultiple(VPSCDynamicStoreRef store, CFDictionaryRef keysToSet,
+ CFArrayRef keysToRemove, CFArrayRef keysToNotify);
+extern Boolean SCDynamicStoreSetValue(VPSCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value);
+extern CFPropertyListRef SCDynamicStoreCopyValue(VPSCDynamicStoreRef store, CFStringRef key);
+extern Boolean SCPreferencesSetComputerName(VPSCPreferencesRef prefs, CFStringRef name, CFStringEncoding encoding);
+extern Boolean SCPreferencesSetHostName(VPSCPreferencesRef prefs, CFStringRef name);
+extern Boolean SCPreferencesSetLocalHostName(VPSCPreferencesRef prefs, CFStringRef name);
+// SCPrivate.h; what SCError() returns next.
+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
+// the other guest libraries' writers, as in SystemHook. Bounded twice: lines
+// per process, and the file's size, since configd republishes on every
+// preferences change for the life of the guest.
+#define VP_LOG_PATH "/var/mobile/Library/Caches/vphone-devicename.log"
+#define VP_LOG_MAX_LINES 64
+#define VP_LOG_MAX_BYTES (256 * 1024)
+
+static void vpLog(const char *format, ...) __attribute__((format(printf, 1, 2)));
+static void vpLog(const char *format, ...) {
+ static atomic_int lines;
+ if (atomic_fetch_add(&lines, 1) >= VP_LOG_MAX_LINES)
+ return;
+ int fd = open(VP_LOG_PATH, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0666);
+ if (fd < 0)
+ return;
+ fchmod(fd, 0666);
+ struct stat info;
+ if (fstat(fd, &info) == 0 && info.st_size < VP_LOG_MAX_BYTES) {
+ char line[768];
+ va_list arguments;
+ va_start(arguments, format);
+ vsnprintf(line, sizeof(line), format, arguments);
+ va_end(arguments);
+ dprintf(fd, "pid=%d %s %s\n", getpid(), getprogname(), line);
+ }
+ close(fd);
+}
+
+// A name for the log, truncated to fit.
+static const char *vpDescribe(CFStringRef string, char *buffer, size_t size) {
+ if (!string)
+ return "<null>";
+ if (!CFStringGetCString(string, buffer, (CFIndex)size, kCFStringEncodingUTF8))
+ snprintf(buffer, size, "<unprintable>");
+ return buffer;
+}
+
+// MARK: - Pinned Name
+
+enum { VPRoleOther, VPRoleConfigd, VPRoleLockdownd };
+
+static int vpRole;
+static CFStringRef vpPinned;
+static pthread_once_t vpPinnedOnce = PTHREAD_ONCE_INIT;
+
+static CFStringRef vpCopyNVRAMName(void) {
+ io_registry_entry_t options = IORegistryEntryFromPath(kIOMainPortDefault, "IODeviceTree:/options");
+ if (options == MACH_PORT_NULL)
+ return NULL;
+ static const CFStringRef names[] = {
+ CFSTR(VP_DEVICE_NAME_NVRAM_VARIABLE),
+ CFSTR(VP_APPLE_NVRAM_GUID ":" VP_DEVICE_NAME_NVRAM_VARIABLE),
+ };
+ 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;
+ }
+ IOObjectRelease(options);
+ return name;
+}
+
+static void vpResolvePinned(void) {
+ vpPinned = vpCopyNVRAMName();
+ char buffer[VP_DEVICE_NAME_MAX_BYTES + 1];
+ if (vpPinned)
+ vpLog("pinned name \"%s\"", vpDescribe(vpPinned, buffer, sizeof(buffer)));
+}
+
+// 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) {
+ if (vpRole != role)
+ return NULL;
+ pthread_once(&vpPinnedOnce, vpResolvePinned);
+ return vpPinned;
+}
+
+// MARK: - configd
+
+static Boolean vpSetMultiple(VPSCDynamicStoreRef store, CFDictionaryRef keysToSet, CFArrayRef keysToRemove,
+ CFArrayRef keysToNotify) {
+ CFStringRef name = vpPinnedName(VPRoleConfigd);
+ if (!name || !VPDeviceNameIsSetupPublication(keysToSet, keysToRemove))
+ return SCDynamicStoreSetMultiple(store, keysToSet, keysToRemove, keysToNotify);
+ CFPropertyListRef stored = VPDeviceNameNeedsStoredSystem(keysToSet, keysToRemove)
+ ? SCDynamicStoreCopyValue(store, VP_DEVICE_NAME_SYSTEM_KEY)
+ : NULL;
+ CFDictionaryRef set = NULL;
+ CFArrayRef remove = NULL;
+ const 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);
+ 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))
+ 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);
+ return result;
+}
+
+// MARK: - lockdownd
+
+// The preferences object whose ComputerName was just refused on this thread.
+// set_device_name makes one per rename and calls the three setters on it in
+// turn, so the host name setters follow the ComputerName decision.
+static __thread VPSCPreferencesRef vpRefusedPreferences;
+
+static Boolean vpRefuse(const char *setter, CFStringRef requested) {
+ char wanted[VP_DEVICE_NAME_MAX_BYTES + 1];
+ char pinned[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)));
+ _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)) {
+ vpRefusedPreferences = NULL;
+ return SCPreferencesSetComputerName(prefs, name, encoding);
+ }
+ vpRefusedPreferences = prefs;
+ return vpRefuse("SCPreferencesSetComputerName", name);
+}
+
+static Boolean vpSetHostName(VPSCPreferencesRef prefs, CFStringRef name) {
+ if (vpPinnedName(VPRoleLockdownd) && prefs && prefs == vpRefusedPreferences)
+ return vpRefuse("SCPreferencesSetHostName", name);
+ return SCPreferencesSetHostName(prefs, name);
+}
+
+static Boolean vpSetLocalHostName(VPSCPreferencesRef prefs, CFStringRef name) {
+ if (vpPinnedName(VPRoleLockdownd) && prefs && prefs == vpRefusedPreferences) {
+ // The last of set_device_name's three setters.
+ vpRefusedPreferences = NULL;
+ return vpRefuse("SCPreferencesSetLocalHostName", name);
+ }
+ return SCPreferencesSetLocalHostName(prefs, name);
+}
+
+// MARK: - Load
+
+__attribute__((constructor)) static void vpDeviceNameInit(void) {
+ const char *name = getprogname();
+ vpRole = name && strcmp(name, "configd") == 0 ? VPRoleConfigd
+ : name && strcmp(name, "lockdownd") == 0 ? VPRoleLockdownd
+ : VPRoleOther;
+}
+
+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.
+__attribute__((used, section("__DATA,__interpose"))) static const struct {
+ const void *replacement;
+ const void *replacee;
+} vpInterpose[] = {
+ {(const void *)vpSetMultiple, (const void *)SCDynamicStoreSetMultiple},
+ {(const void *)vpSetValue, (const void *)SCDynamicStoreSetValue},
+ {(const void *)vpSetComputerName, (const void *)SCPreferencesSetComputerName},
+ {(const void *)vpSetHostName, (const void *)SCPreferencesSetHostName},
+ {(const void *)vpSetLocalHostName, (const void *)SCPreferencesSetLocalHostName},
+};
diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefile
index eb571a03..c406401a 100644
--- a/VPhoneGuestComponents/Makefile
+++ b/VPhoneGuestComponents/Makefile
@@ -65,6 +65,13 @@ DEVICEHUBFIX_INCLUDES := $(wildcard DeviceHubFix/*.h)
DEVICEHUBFIX_DYNAMIC_LOOKUP_SYMS := \
'-Wl,-U,_$$s19CoreDeviceUtilities28MediaStreamSupportedFeaturesV7currentACvgZ' \
'-Wl,-U,_$$s19CoreDeviceUtilities07CurrentB0V20isCustomerRestrictedSbvg'
+# SystemConfiguration for the imports it interposes in configd and lockdownd
+# (its iOS headers mark them unavailable, so the source declares them), IOKit
+# for the NVRAM read. The policy beside it is pure CoreFoundation, so the host
+# tests build it too.
+DEVICENAME := $(STAGE)/devicename/libdevicename.dylib
+DEVICENAME_SOURCES := $(wildcard DeviceName/*.c)
+DEVICENAME_INCLUDES := $(wildcard DeviceName/*.h)
GPU_PLUGIN := $(STAGE)/gpu/libAppleParavirtCompilerPluginIOGPUFamily.dylib
GPU_PLUGIN_SOURCES := $(wildcard GraphicLoader/*.mm)
GPU_PLUGIN_INCLUDES := $(wildcard GraphicLoader/*.h GraphicLoader/*.hpp GraphicLoader/*.inc)
@@ -98,14 +105,14 @@ GPU_DYNAMIC_LOOKUP_SYMS := \
VIRTIOSOUND := $(STAGE)/virtiosound/VPhoneVirtIOSound.driver
VIRTIOSOUND_SOURCES := $(wildcard VirtIOSound/*.m VirtIOSound/*.c)
VIRTIOSOUND_INCLUDES := $(wildcard VirtIOSound/*.h) VirtIOSound/Info.plist VirtIOSound/AudioServerDriver.tbd
-ARTIFACTS := $(CAMFIX) $(VCAMCAPTURED) $(LAUNCHHOOK) $(SYSTEMHOOK) $(MISFIX) $(HAPTICSFIX) $(BATTERYHEALTHFIX) $(DEVICEHUBFIX) $(GPU_PLUGIN) $(VIRTIOSOUND)
+ARTIFACTS := $(CAMFIX) $(VCAMCAPTURED) $(LAUNCHHOOK) $(SYSTEMHOOK) $(MISFIX) $(HAPTICSFIX) $(BATTERYHEALTHFIX) $(DEVICEHUBFIX) $(DEVICENAME) $(GPU_PLUGIN) $(VIRTIOSOUND)
PLISTS := $(STAGE)/camfix/libcamfix.plist $(STAGE)/vcamcaptured/libvcamcaptured.plist \
$(STAGE)/misfix/libmisfix.plist
GPU_PROVENANCE := $(STAGE)/gpu/README.md
# Directory timestamps also catch files removed from the wildcard inputs.
-.PHONY: all gpu package test-loader-links test-injection-environment test-vcam-dataplane test-virtiosound clean
+.PHONY: all gpu package test-loader-links test-injection-environment test-devicename test-vcam-dataplane test-virtiosound clean
all: $(ARTIFACTS) $(PLISTS) $(GPU_PROVENANCE)
@@ -113,7 +120,7 @@ gpu: $(GPU_PLUGIN)
package: all
@rm -f $(ARCHIVE)
- @/usr/bin/tar -cf $(ARCHIVE) -C $(STAGE) camfix vcamcaptured launchhook systemhook misfix hapticsfix batteryhealthfix devicehubfix gpu virtiosound
+ @/usr/bin/tar -cf $(ARCHIVE) -C $(STAGE) camfix vcamcaptured launchhook systemhook misfix hapticsfix batteryhealthfix devicehubfix devicename gpu virtiosound
@echo "Packaged $(ARCHIVE)"
test-loader-links:
@@ -129,6 +136,16 @@ test-injection-environment:
-o $(OUT)/InjectionEnvironmentTests Tests/InjectionEnvironmentTests.c
@$(OUT)/InjectionEnvironmentTests
+# Host proof harness for libdevicename's name decoding, configd publication
+# rewrite and lockdownd rename decision.
+test-devicename:
+ @mkdir -p $(OUT)
+ xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IDeviceName \
+ -o $(OUT)/DeviceNameTests \
+ Tests/DeviceNameTests.c DeviceName/DeviceNamePolicy.c \
+ -framework CoreFoundation
+ @$(OUT)/DeviceNameTests
+
# Host proof harness for the virtio-snd format choice, the rings, the input
# reader, the capture's low cut, the clock, the capture rate converter and the
# speaker's reported latency.
@@ -163,7 +180,7 @@ test-vcam-dataplane:
-framework CoreGraphics
@$(OUT)/VCamDataPlaneTests
-$(STAGE)/camfix $(STAGE)/vcamcaptured $(STAGE)/launchhook $(STAGE)/systemhook $(STAGE)/misfix $(STAGE)/hapticsfix $(STAGE)/batteryhealthfix $(STAGE)/devicehubfix $(STAGE)/gpu $(STAGE)/virtiosound:
+$(STAGE)/camfix $(STAGE)/vcamcaptured $(STAGE)/launchhook $(STAGE)/systemhook $(STAGE)/misfix $(STAGE)/hapticsfix $(STAGE)/batteryhealthfix $(STAGE)/devicehubfix $(STAGE)/devicename $(STAGE)/gpu $(STAGE)/virtiosound:
@mkdir -p $@
$(CAMFIX): $(CAMFIX_SOURCES) $(CAMFIX_INPUTS) | $(STAGE)/camfix
@@ -226,6 +243,14 @@ $(DEVICEHUBFIX): $(DEVICEHUBFIX_SOURCES) $(DEVICEHUBFIX_INCLUDES) DeviceHubFix |
-Wl,-not_for_dyld_shared_cache
@codesign --force --sign - $@
+$(DEVICENAME): $(DEVICENAME_SOURCES) $(DEVICENAME_INCLUDES) DeviceName | $(STAGE)/devicename
+ $(CLANG) $(GUEST_C_FLAGS) -Wall -Wextra -Werror -dynamiclib \
+ -install_name /usr/lib/libdevicename.dylib \
+ -o $@ $(DEVICENAME_SOURCES) \
+ -framework CoreFoundation -framework IOKit -framework SystemConfiguration \
+ -Wl,-not_for_dyld_shared_cache
+ @codesign --force --sign - $@
+
$(STAGE)/misfix/libmisfix.plist: MISFix/libmisfix.plist | $(STAGE)/misfix
@cp $< $@
diff --git a/VPhoneGuestComponents/Tests/DeviceNameTests.c b/VPhoneGuestComponents/Tests/DeviceNameTests.c
new file mode 100644
index 00000000..ade6641a
--- /dev/null
+++ b/VPhoneGuestComponents/Tests/DeviceNameTests.c
@@ -0,0 +1,286 @@
+#include "DeviceNamePolicy.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+// MARK: - Helpers
+
+static CFStringRef nameFromBytes(const char *bytes, CFIndex length) {
+ CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)bytes, length);
+ CFStringRef name = VPDeviceNameCreateFromProperty(data);
+ CFRelease(data);
+ return name;
+}
+
+static int namesEqual(CFStringRef name, const char *expected) {
+ CFStringRef want = CFStringCreateWithCString(kCFAllocatorDefault, expected, kCFStringEncodingUTF8);
+ int equal = name && CFEqual(name, want);
+ CFRelease(want);
+ return equal;
+}
+
+static CFDictionaryRef systemValue(CFStringRef name, int encoding, int withHostName) {
+ CFMutableDictionaryRef value = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+ if (name)
+ CFDictionarySetValue(value, VP_DEVICE_NAME_COMPUTER_NAME, name);
+ CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &encoding);
+ CFDictionarySetValue(value, VP_DEVICE_NAME_COMPUTER_NAME_ENCODING, number);
+ CFRelease(number);
+ if (withHostName)
+ CFDictionarySetValue(value, CFSTR("HostName"), CFSTR("lab"));
+ return value;
+}
+
+static CFStringRef computerName(CFDictionaryRef set) {
+ CFDictionaryRef system = CFDictionaryGetValue(set, VP_DEVICE_NAME_SYSTEM_KEY);
+ return system ? CFDictionaryGetValue(system, VP_DEVICE_NAME_COMPUTER_NAME) : NULL;
+}
+
+static int encodingIsUTF8(CFDictionaryRef set) {
+ CFDictionaryRef system = CFDictionaryGetValue(set, VP_DEVICE_NAME_SYSTEM_KEY);
+ CFNumberRef number = CFDictionaryGetValue(system, VP_DEVICE_NAME_COMPUTER_NAME_ENCODING);
+ SInt32 encoding = 0;
+ return number && CFNumberGetValue(number, kCFNumberSInt32Type, &encoding) &&
+ (CFStringEncoding)encoding == kCFStringEncodingUTF8;
+}
+
+// MARK: - Name
+
+static void decodesTheNVRAMName(void) {
+ CFStringRef name = nameFromBytes("Lab iPhone", 10);
+ assert(namesEqual(name, "Lab iPhone"));
+ CFRelease(name);
+
+ // A C-string writer's terminator is dropped.
+ name = nameFromBytes("Lab iPhone\0\0", 12);
+ assert(namesEqual(name, "Lab iPhone"));
+ CFRelease(name);
+
+ // UTF-8 beyond ASCII.
+ name = nameFromBytes("\xe6\xb5\x8b\xe8\xaf\x95 iPhone \xe2\x80\x99", 17);
+ assert(namesEqual(name, "\xe6\xb5\x8b\xe8\xaf\x95 iPhone \xe2\x80\x99"));
+ CFRelease(name);
+
+ // A CFString property is taken too.
+ name = VPDeviceNameCreateFromProperty(CFSTR("Lab iPhone"));
+ assert(namesEqual(name, "Lab iPhone"));
+ CFRelease(name);
+}
+
+static void refusesWhatIsNotAName(void) {
+ assert(!VPDeviceNameCreateFromProperty(NULL));
+ assert(!nameFromBytes("", 0));
+ assert(!nameFromBytes("\0\0", 2));
+ assert(!VPDeviceNameCreateFromBytes(NULL, 4));
+ assert(!VPDeviceNameCreateFromProperty(CFSTR("")));
+ // Malformed UTF-8: a lone continuation byte, a truncated sequence, an
+ // overlong encoding.
+ assert(!nameFromBytes("Lab \x80", 5));
+ assert(!nameFromBytes("Lab \xe6\xb5", 6));
+ assert(!nameFromBytes("Lab \xc0\xaf", 6));
+ // Control characters, an embedded NUL among them.
+ assert(!nameFromBytes("Lab\niPhone", 10));
+ assert(!nameFromBytes("Lab\0iPhone", 10));
+ assert(!nameFromBytes("Lab\x7fiPhone", 10));
+ assert(!VPDeviceNameCreateFromProperty(CFSTR("Lab\tiPhone")));
+ // Neither data nor a string.
+ int number = 1;
+ CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &number);
+ assert(!VPDeviceNameCreateFromProperty(value));
+ CFRelease(value);
+
+ // VP_DEVICE_NAME_MAX_BYTES bytes are taken, one more is not.
+ char longest[VP_DEVICE_NAME_MAX_BYTES + 1];
+ memset(longest, 'a', sizeof(longest));
+ CFStringRef name = nameFromBytes(longest, VP_DEVICE_NAME_MAX_BYTES);
+ assert(name && CFStringGetLength(name) == VP_DEVICE_NAME_MAX_BYTES);
+ CFRelease(name);
+ assert(!nameFromBytes(longest, VP_DEVICE_NAME_MAX_BYTES + 1));
+ CFStringRef tooLong = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)longest, sizeof(longest),
+ kCFStringEncodingUTF8, false);
+ assert(!VPDeviceNameCreateFromProperty(tooLong));
+ CFRelease(tooLong);
+}
+
+// MARK: - configd
+
+static void pinsTheSystemValue(void) {
+ // Other entries are kept; the name and its encoding are replaced.
+ CFDictionaryRef original = systemValue(CFSTR("iPhone"), 0, 1);
+ CFDictionaryRef pinned = VPDeviceNameCreatePinnedSystem(original, CFSTR("Lab"));
+ assert(pinned);
+ assert(CFEqual(CFDictionaryGetValue(pinned, VP_DEVICE_NAME_COMPUTER_NAME), CFSTR("Lab")));
+ assert(CFEqual(CFDictionaryGetValue(pinned, CFSTR("HostName")), CFSTR("lab")));
+ assert(CFEqual(CFDictionaryGetValue(original, VP_DEVICE_NAME_COMPUTER_NAME), CFSTR("iPhone")));
+ CFRelease(pinned);
+ CFRelease(original);
+
+ // Already pinned: nothing to do.
+ original = systemValue(CFSTR("Lab"), 0, 0);
+ assert(!VPDeviceNameCreatePinnedSystem(original, CFSTR("Lab")));
+ CFRelease(original);
+
+ // No value, or one that is not a dictionary, gets one holding the name.
+ pinned = VPDeviceNameCreatePinnedSystem(NULL, CFSTR("Lab"));
+ assert(pinned && CFDictionaryGetCount(pinned) == 2);
+ CFRelease(pinned);
+ pinned = VPDeviceNameCreatePinnedSystem(CFSTR("junk"), CFSTR("Lab"));
+ assert(pinned && CFEqual(CFDictionaryGetValue(pinned, VP_DEVICE_NAME_COMPUTER_NAME), CFSTR("Lab")));
+ CFRelease(pinned);
+
+ assert(!VPDeviceNameCreatePinnedSystem(original, NULL));
+}
+
+static CFMutableDictionaryRef publication(void) {
+ return CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+}
+
+static void recognisesThePreferencesMonitor(void) {
+ CFMutableDictionaryRef set = publication();
+ assert(!VPDeviceNameIsSetupPublication(NULL, NULL));
+ assert(!VPDeviceNameIsSetupPublication(set, NULL));
+ CFDictionarySetValue(set, CFSTR("State:/Network/Global/IPv4"), CFSTR("x"));
+ assert(!VPDeviceNameIsSetupPublication(set, NULL));
+ CFDictionarySetValue(set, CFSTR("Setup:/Network/Global/IPv4"), CFSTR("x"));
+ assert(VPDeviceNameIsSetupPublication(set, NULL));
+ CFRelease(set);
+
+ const void *keys[] = {CFSTR("Setup:/Network/HostNames")};
+ CFArrayRef remove = CFArrayCreate(kCFAllocatorDefault, keys, 1, &kCFTypeArrayCallBacks);
+ assert(VPDeviceNameIsSetupPublication(NULL, remove));
+ assert(VPDeviceNameNeedsStoredSystem(NULL, remove));
+ CFRelease(remove);
+}
+
+// The monitor sets Setup:/System with the preferences' own name.
+static void pinsAPublishedName(void) {
+ CFMutableDictionaryRef set = publication();
+ CFDictionaryRef system = systemValue(CFSTR("iPhone"), 0, 1);
+ CFDictionarySetValue(set, VP_DEVICE_NAME_SYSTEM_KEY, system);
+ CFDictionarySetValue(set, CFSTR("Setup:/"), CFSTR("x"));
+ CFRelease(system);
+ assert(!VPDeviceNameNeedsStoredSystem(set, NULL));
+
+ CFDictionaryRef outSet = NULL;
+ CFArrayRef outRemove = NULL;
+ assert(VPDeviceNameCreatePinnedPublication(set, NULL, NULL, CFSTR("Lab"), &outSet, &outRemove));
+ assert(outSet && !outRemove);
+ assert(CFEqual(computerName(outSet), CFSTR("Lab")));
+ assert(encodingIsUTF8(outSet));
+ assert(CFDictionaryGetCount(outSet) == 2);
+ // The caller's dictionary is untouched.
+ assert(CFEqual(computerName(set), CFSTR("iPhone")));
+ CFRelease(outSet);
+
+ // A stored value is not consulted when the call sets the key itself.
+ CFDictionaryRef stored = systemValue(CFSTR("Lab"), 0, 0);
+ assert(VPDeviceNameCreatePinnedPublication(set, NULL, stored, CFSTR("Lab"), &outSet, &outRemove));
+ CFRelease(outSet);
+ CFRelease(stored);
+ CFRelease(set);
+}
+
+// The monitor left Setup:/System out because it did not change.
+static void pinsAnUnchangedName(void) {
+ CFMutableDictionaryRef set = publication();
+ CFDictionarySetValue(set, CFSTR("Setup:/"), CFSTR("x"));
+ assert(VPDeviceNameNeedsStoredSystem(set, NULL));
+
+ // The store already has the pinned name: pass the call through.
+ CFDictionaryRef stored = systemValue(CFSTR("Lab"), 0, 1);
+ CFDictionaryRef outSet = NULL;
+ CFArrayRef outRemove = NULL;
+ assert(!VPDeviceNameCreatePinnedPublication(set, NULL, stored, CFSTR("Lab"), &outSet, &outRemove));
+ assert(!outSet && !outRemove);
+ CFRelease(stored);
+
+ // It has another name: the store's value, pinned, is added.
+ stored = systemValue(CFSTR("iPhone"), 0, 1);
+ assert(VPDeviceNameCreatePinnedPublication(set, NULL, stored, CFSTR("Lab"), &outSet, &outRemove));
+ assert(CFEqual(computerName(outSet), CFSTR("Lab")));
+ CFDictionaryRef system = CFDictionaryGetValue(outSet, VP_DEVICE_NAME_SYSTEM_KEY);
+ assert(CFEqual(CFDictionaryGetValue(system, CFSTR("HostName")), CFSTR("lab")));
+ CFRelease(outSet);
+ CFRelease(stored);
+
+ // It has none — a guest whose preferences never had a name.
+ assert(VPDeviceNameCreatePinnedPublication(set, NULL, NULL, CFSTR("Lab"), &outSet, &outRemove));
+ assert(CFEqual(computerName(outSet), CFSTR("Lab")));
+ assert(CFDictionaryGetCount(outSet) == 2);
+ CFRelease(outSet);
+
+ // A first publication with nothing to set at all.
+ assert(VPDeviceNameCreatePinnedPublication(NULL, NULL, NULL, CFSTR("Lab"), &outSet, &outRemove));
+ assert(CFDictionaryGetCount(outSet) == 1 && CFEqual(computerName(outSet), CFSTR("Lab")));
+ CFRelease(outSet);
+ CFRelease(set);
+}
+
+// The preferences lost their System entry, so the monitor removes the key.
+static void keepsARemovedName(void) {
+ const void *keys[] = {CFSTR("Setup:/Network/HostNames"), VP_DEVICE_NAME_SYSTEM_KEY};
+ CFArrayRef remove = CFArrayCreate(kCFAllocatorDefault, keys, 2, &kCFTypeArrayCallBacks);
+ assert(!VPDeviceNameNeedsStoredSystem(NULL, remove));
+ CFDictionaryRef stored = systemValue(CFSTR("Lab"), 0, 0);
+ CFDictionaryRef outSet = NULL;
+ CFArrayRef outRemove = NULL;
+ // The stored value is not what decides: the removal must not go through.
+ assert(VPDeviceNameCreatePinnedPublication(NULL, remove, stored, CFSTR("Lab"), &outSet, &outRemove));
+ assert(CFEqual(computerName(outSet), CFSTR("Lab")));
+ assert(outRemove && CFArrayGetCount(outRemove) == 1);
+ assert(CFEqual(CFArrayGetValueAtIndex(outRemove, 0), CFSTR("Setup:/Network/HostNames")));
+ assert(CFArrayGetCount(remove) == 2);
+ CFRelease(outSet);
+ CFRelease(outRemove);
+ CFRelease(stored);
+ CFRelease(remove);
+
+ // Other removals are passed on as they are.
+ const void *other[] = {CFSTR("Setup:/Network/HostNames")};
+ remove = CFArrayCreate(kCFAllocatorDefault, other, 1, &kCFTypeArrayCallBacks);
+ assert(VPDeviceNameCreatePinnedPublication(NULL, remove, NULL, CFSTR("Lab"), &outSet, &outRemove));
+ assert(outRemove && CFEqual(outRemove, remove));
+ CFRelease(outSet);
+ CFRelease(outRemove);
+ CFRelease(remove);
+}
+
+static void passesThroughWithoutAName(void) {
+ CFMutableDictionaryRef set = publication();
+ CFDictionaryRef system = systemValue(CFSTR("iPhone"), 0, 0);
+ CFDictionarySetValue(set, VP_DEVICE_NAME_SYSTEM_KEY, system);
+ CFRelease(system);
+ CFDictionaryRef outSet = NULL;
+ CFArrayRef outRemove = NULL;
+ assert(!VPDeviceNameCreatePinnedPublication(set, NULL, NULL, NULL, &outSet, &outRemove));
+ assert(!outSet && !outRemove);
+ CFRelease(set);
+}
+
+// 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));
+}
+
+int main(void) {
+ decodesTheNVRAMName();
+ refusesWhatIsNotAName();
+ pinsTheSystemValue();
+ recognisesThePreferencesMonitor();
+ pinsAPublishedName();
+ pinsAnUnchangedName();
+ keepsARemovedName();
+ passesThroughWithoutAName();
+ allowsOnlyThePinnedName();
+ puts("DeviceNameTests: ok");
+ return 0;
+}