Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Let libmisfix say which MobileGestalt queries it actually sees

Lakrb7a7498a14ab
diff --git a/VPhoneGuestComponents/MISFix/MISFixConfig.c b/VPhoneGuestComponents/MISFix/MISFixConfig.c
index 61743cd5..2961a794 100644
--- a/VPhoneGuestComponents/MISFix/MISFixConfig.c
+++ b/VPhoneGuestComponents/MISFix/MISFixConfig.c
@@ -23,7 +23,9 @@ static const size_t kConfigPathCount = sizeof(kConfigPaths) / sizeof(kConfigPath
// change under a running daemon, or "apply the setting" would mean "reboot".
// Modification time plus size is enough to notice an edit: the plist is
// written by replacing it, never by editing bytes in place.
-static CFStringRef gDeviceIdentifier;
+// The whole dictionary is kept, not just the UDID, so a second setting costs
+// no second read and every value stays consistent with the file it came from.
+static CFDictionaryRef gConfiguration;
static const char *gPath;
static struct timespec gStamp;
static off_t gSize;
@@ -66,9 +68,9 @@ static CFPropertyListRef vpCopyConfigurationPlist(const char *path) {
}
static void vpForget(void) {
- if (gDeviceIdentifier != NULL) {
- CFRelease(gDeviceIdentifier);
- gDeviceIdentifier = NULL;
+ if (gConfiguration != NULL) {
+ CFRelease(gConfiguration);
+ gConfiguration = NULL;
}
gPath = NULL;
gStamp.tv_sec = 0;
@@ -89,18 +91,9 @@ static int vpAdopt(const char *path, const struct stat *info) {
if (plist == NULL)
return 0;
- if (gDeviceIdentifier != NULL) {
- CFRelease(gDeviceIdentifier);
- gDeviceIdentifier = NULL;
- }
-
- CFTypeRef value = CFDictionaryGetValue((CFDictionaryRef)plist, CFSTR("UniqueDeviceID"));
- if (value != NULL && CFGetTypeID(value) == CFStringGetTypeID()
- && CFStringGetLength((CFStringRef)value) > 0)
- {
- gDeviceIdentifier = CFStringCreateCopy(kCFAllocatorDefault, (CFStringRef)value);
- }
- CFRelease(plist);
+ if (gConfiguration != NULL)
+ CFRelease(gConfiguration);
+ gConfiguration = (CFDictionaryRef)plist;
gPath = path;
gStamp = info->st_mtimespec;
@@ -109,7 +102,8 @@ static int vpAdopt(const char *path, const struct stat *info) {
return 1;
}
-CFStringRef MISFixCopyConfiguredDeviceIdentifier(void) {
+/// Make `gConfiguration` current, reading again only when the file changed.
+static void vpEnsureLoaded(void) {
// Fast path: the file chosen last time, still there and unchanged. This is
// the common case — misagent asks once per profile, installd once per
// bundle — and it costs one `stat`.
@@ -119,7 +113,7 @@ CFStringRef MISFixCopyConfiguredDeviceIdentifier(void) {
&& info.st_mtimespec.tv_nsec == gStamp.tv_nsec
&& info.st_size == gSize)
{
- return gDeviceIdentifier;
+ return;
}
// Otherwise pick again: the first candidate that is there *and* reads.
@@ -136,11 +130,37 @@ CFStringRef MISFixCopyConfiguredDeviceIdentifier(void) {
if (stat(kConfigPaths[index], &candidate) != 0)
continue;
if (vpAdopt(kConfigPaths[index], &candidate))
- return gDeviceIdentifier;
+ return;
}
// Nothing readable anywhere. Forget whatever was cached, so removing the
// plist turns the override off.
vpForget();
- return NULL;
+}
+
+/// The value for `key` in the live configuration, or NULL.
+static CFTypeRef vpConfiguredValue(CFStringRef key) {
+ vpEnsureLoaded();
+ if (gConfiguration == NULL)
+ return NULL;
+ return CFDictionaryGetValue(gConfiguration, key);
+}
+
+CFStringRef MISFixCopyConfiguredDeviceIdentifier(void) {
+ CFTypeRef value = vpConfiguredValue(CFSTR("UniqueDeviceID"));
+ if (value == NULL || CFGetTypeID(value) != CFStringGetTypeID()
+ || CFStringGetLength((CFStringRef)value) == 0)
+ {
+ return NULL;
+ }
+ // Borrowed from the cached dictionary, which outlives the call and is only
+ // replaced when the file changes.
+ return (CFStringRef)value;
+}
+
+int MISFixConfiguredFlag(CFStringRef key) {
+ CFTypeRef value = vpConfiguredValue(key);
+ if (value == NULL || CFGetTypeID(value) != CFBooleanGetTypeID())
+ return 0;
+ return CFBooleanGetValue((CFBooleanRef)value) ? 1 : 0;
}
diff --git a/VPhoneGuestComponents/MISFix/MISFixConfig.h b/VPhoneGuestComponents/MISFix/MISFixConfig.h
index 4f380914..bd15f7cf 100644
--- a/VPhoneGuestComponents/MISFix/MISFixConfig.h
+++ b/VPhoneGuestComponents/MISFix/MISFixConfig.h
@@ -12,6 +12,11 @@
// that team's provisioning profiles install on
// this guest. Empty or absent: the guest answers
// with its own.
+// LogQueries (bool) Log every MobileGestalt query this hook sees.
+// Off by default: these daemons are asked a lot.
+// For finding out whether a process asks through
+// the symbol at all, which is not observable from
+// outside — see MISFixDeviceIdentity.c.
#ifndef MISFIX_CONFIG_H
#define MISFIX_CONFIG_H
@@ -25,4 +30,11 @@
/// next query without restarting anything.
CFStringRef MISFixCopyConfiguredDeviceIdentifier(void);
+/// Whether `key` is set to true in the configuration.
+///
+/// Reads the same file, through the same staleness check, as
+/// ``MISFixCopyConfiguredDeviceIdentifier``. False when the key is absent, not
+/// a boolean, or no configuration can be read.
+int MISFixConfiguredFlag(CFStringRef key);
+
#endif
diff --git a/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c b/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c
index 1ddbd4c0..7856644b 100644
--- a/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c
+++ b/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c
@@ -60,9 +60,38 @@
#include "MISFixConfig.h"
#include "MISFixInterpose.h"
+#include <os/log.h>
+
extern CFTypeRef MGCopyAnswer(CFStringRef property);
extern CFTypeRef MGCopyAnswerWithError(CFStringRef property, uint32_t *error);
+/// Log every MobileGestalt query this hook sees, and whether it answered.
+///
+/// Off unless the config sets `LogQueries`, because these daemons are asked a
+/// lot and the log is how a person watches an install. It exists because the
+/// interesting failure is *silence*: on test-26.4 the override reaches misagent
+/// and a profile installs, but installd then refuses the same app with
+/// `0xE8008015`, and the two explanations — installd asking and getting the
+/// wrong answer, versus installd never asking through this symbol at all —
+/// look identical from outside. `MICodeSigningVerifier` lives in
+/// MobileInstallation, not in installd, and it calls `libmis`, so the query
+/// that matters is made cache-to-cache; whether an interpose catches that is
+/// exactly what this answers. If an install produces no line here from
+/// installd, the call is not coming through `MGCopyAnswer` and the hook needs a
+/// different point to stand on.
+static void vpLogQuery(CFStringRef property, int answered) {
+ if (!MISFixConfiguredFlag(CFSTR("LogQueries")))
+ return;
+ char name[128];
+ if (property == NULL
+ || !CFStringGetCString(property, name, sizeof(name), kCFStringEncodingUTF8))
+ {
+ return;
+ }
+ os_log(OS_LOG_DEFAULT, "libmisfix: MGCopyAnswer(%{public}s) %{public}s",
+ name, answered ? "-> override" : "passed through");
+}
+
/// MobileGestalt's key for the UDID. A plain string, not the SDK constant:
/// there is no public header, and this is the literal misagent carries.
#define kMISFixUniqueDeviceIDProperty CFSTR("UniqueDeviceID")
@@ -85,11 +114,13 @@ static CFTypeRef vpOverrideFor(CFStringRef property) {
static CFTypeRef vpMGCopyAnswer(CFStringRef property) {
CFTypeRef override = vpOverrideFor(property);
+ vpLogQuery(property, override != NULL);
return override != NULL ? override : MGCopyAnswer(property);
}
static CFTypeRef vpMGCopyAnswerWithError(CFStringRef property, uint32_t *error) {
CFTypeRef override = vpOverrideFor(property);
+ vpLogQuery(property, override != NULL);
if (override == NULL)
return MGCopyAnswerWithError(property, error);
if (error != NULL)