Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Name the caller in every libmisfix diagnostic line

Lakr9abcc600db08
diff --git a/VPhoneGuestComponents/MISFix/MISFixConfig.c b/VPhoneGuestComponents/MISFix/MISFixConfig.c
index 12af3492..1ce9ef16 100644
--- a/VPhoneGuestComponents/MISFix/MISFixConfig.c
+++ b/VPhoneGuestComponents/MISFix/MISFixConfig.c
@@ -1,8 +1,11 @@
#include "MISFixConfig.h"
+#include <dlfcn.h>
#include <os/log.h>
+#include <ptrauth.h>
#include <stdarg.h>
#include <stdio.h>
+#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -169,6 +172,22 @@ int MISFixConfiguredFlag(CFStringRef key) {
return CFBooleanGetValue((CFBooleanRef)value) ? 1 : 0;
}
+const char *MISFixCallerImage(const void *address) {
+ if (address == NULL)
+ return "?";
+ // A return address on arm64e carries a pointer-authentication code; dladdr
+ // compares it against image ranges as a plain address and would find
+ // nothing.
+ Dl_info info;
+ if (dladdr(ptrauth_strip((void *)address, ptrauth_key_return_address), &info) == 0
+ || info.dli_fname == NULL)
+ {
+ return "?";
+ }
+ const char *slash = strrchr(info.dli_fname, '/');
+ return slash != NULL && slash[1] != '\0' ? slash + 1 : info.dli_fname;
+}
+
void MISFixLog(const char *format, ...) {
if (!MISFixConfiguredFlag(kMISFixLogQueriesKey))
return;
diff --git a/VPhoneGuestComponents/MISFix/MISFixConfig.h b/VPhoneGuestComponents/MISFix/MISFixConfig.h
index f0c8c1a8..3297b677 100644
--- a/VPhoneGuestComponents/MISFix/MISFixConfig.h
+++ b/VPhoneGuestComponents/MISFix/MISFixConfig.h
@@ -43,6 +43,25 @@ int MISFixConfiguredFlag(CFStringRef key);
/// A no-op unless the flag is set.
void MISFixLog(const char *format, ...) __attribute__((format(printf, 1, 2)));
+/// The name of the image `address` belongs to — the last path component of the
+/// Mach-O that contains it, or `"?"` when nothing claims it.
+///
+/// Always safe to print: the result is a pointer into dyld's own image name,
+/// or a static string, never allocated.
+///
+/// This exists to tell two very different worlds apart in one log line. A
+/// `__DATA,__interpose` replacement is applied to a *call site*, and a call
+/// made from inside the shared cache into another cache image may never reach
+/// it. So "installd never logged a query" has two readings — the daemon does
+/// not ask, or the daemon's frameworks ask past us — and the caller's image is
+/// what separates them: `installd` means the main executable asked and the
+/// interpose works, anything under the cache means it reaches cache-to-cache
+/// calls too.
+const char *MISFixCallerImage(const void *address);
+
+/// The image of whoever called the function this appears in.
+#define MISFixCaller() MISFixCallerImage(__builtin_return_address(0))
+
/// The flag every diagnostic in this dylib is behind.
#define kMISFixLogQueriesKey CFSTR("LogQueries")
diff --git a/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c b/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c
index d17e0093..17d0e6e8 100644
--- a/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c
+++ b/VPhoneGuestComponents/MISFix/MISFixDeviceIdentity.c
@@ -79,14 +79,27 @@ extern CFTypeRef MGCopyAnswerWithError(CFStringRef property, uint32_t *error);
/// 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) {
+///
+/// The caller's image is part of the line because the first run answered the
+/// question only halfway: installd logged `MGCopyAnswer(BuildVersion)` and no
+/// `UniqueDeviceID`, while libmis plainly resolved a UDID — it skipped every
+/// profile with `0xE8008012`. Either installd's own code asked for the build
+/// version and the frameworks ask past this interpose, or the interpose does
+/// reach them and libmis finds the UDID somewhere other than MobileGestalt.
+/// `MISFixCallerImage` tells the two apart in one line.
+static void vpLogQuery(CFStringRef property, int answered, const char *caller) {
char name[128];
if (property == NULL
|| !CFStringGetCString(property, name, sizeof(name), kCFStringEncodingUTF8))
{
return;
}
- MISFixLog("MGCopyAnswer(%s) %s", name, answered ? "-> override" : "passed through");
+ MISFixLog(
+ "MGCopyAnswer(%s) from %s %s",
+ name,
+ caller,
+ answered ? "-> override" : "passed through"
+ );
}
/// Say, once, that this dylib is in this process.
@@ -123,14 +136,16 @@ static CFTypeRef vpOverrideFor(CFStringRef property) {
}
static CFTypeRef vpMGCopyAnswer(CFStringRef property) {
+ const char *caller = MISFixCaller();
CFTypeRef override = vpOverrideFor(property);
- vpLogQuery(property, override != NULL);
+ vpLogQuery(property, override != NULL, caller);
return override != NULL ? override : MGCopyAnswer(property);
}
static CFTypeRef vpMGCopyAnswerWithError(CFStringRef property, uint32_t *error) {
+ const char *caller = MISFixCaller();
CFTypeRef override = vpOverrideFor(property);
- vpLogQuery(property, override != NULL);
+ vpLogQuery(property, override != NULL, caller);
if (override == NULL)
return MGCopyAnswerWithError(property, error);
if (error != NULL)
diff --git a/VPhoneGuestComponents/MISFix/MISFixSignature.c b/VPhoneGuestComponents/MISFix/MISFixSignature.c
index 44a3a4c9..10e78fb0 100644
--- a/VPhoneGuestComponents/MISFix/MISFixSignature.c
+++ b/VPhoneGuestComponents/MISFix/MISFixSignature.c
@@ -142,11 +142,34 @@ static CFDictionaryRef vpWidenedOptions(CFDictionaryRef options) {
/// and the `MGCopyAnswer` line does not, the two calls are being treated
/// differently and the difference is in MobileGestalt, not in whether an
/// interpose can cross the cache at all.
-static void vpLogValidation(MISPath path, int result) {
+/// Never returns early. A first run logged nothing here from installd, which
+/// was read as "the interpose was not reached" — but a `path` this could not
+/// turn into a C string would have produced exactly the same silence. The line
+/// is unconditional now, and says what the argument was when it is not a
+/// string, so an absent line means one thing only.
+static void vpLogValidation(MISPath path, int result, const char *caller) {
char buffer[1024];
- if (path == NULL || !CFStringGetCString(path, buffer, sizeof(buffer), kCFStringEncodingUTF8))
+ if (path == NULL) {
+ MISFixLog("MISValidateSignatureAndCopyInfo(NULL) from %s -> 0x%x", caller, (unsigned)result);
return;
- MISFixLog("MISValidateSignatureAndCopyInfo(%s) -> 0x%x", buffer, (unsigned)result);
+ }
+ if (CFGetTypeID(path) != CFStringGetTypeID()
+ || !CFStringGetCString(path, buffer, sizeof(buffer), kCFStringEncodingUTF8))
+ {
+ MISFixLog(
+ "MISValidateSignatureAndCopyInfo(<non-string %lu>) from %s -> 0x%x",
+ (unsigned long)CFGetTypeID(path),
+ caller,
+ (unsigned)result
+ );
+ return;
+ }
+ MISFixLog(
+ "MISValidateSignatureAndCopyInfo(%s) from %s -> 0x%x",
+ buffer,
+ caller,
+ (unsigned)result
+ );
}
static int vpMISValidateSignatureAndCopyInfo(
@@ -154,13 +177,14 @@ static int vpMISValidateSignatureAndCopyInfo(
CFDictionaryRef options,
CFDictionaryRef *info
) {
+ const char *caller = MISFixCaller();
CFDictionaryRef widened = vpWidenedOptions(options);
// Out of memory: pass the caller's own options through rather than fail.
if (widened == NULL)
return MISValidateSignatureAndCopyInfo(path, options, info);
int result = MISValidateSignatureAndCopyInfo(path, widened, info);
CFRelease(widened);
- vpLogValidation(path, result);
+ vpLogValidation(path, result, caller);
return result;
}
@@ -170,12 +194,13 @@ static int vpMISValidateSignatureAndCopyInfoWithProgress(
CFDictionaryRef *info,
void *progress
) {
+ const char *caller = MISFixCaller();
CFDictionaryRef widened = vpWidenedOptions(options);
if (widened == NULL)
return MISValidateSignatureAndCopyInfoWithProgress(path, options, info, progress);
int result = MISValidateSignatureAndCopyInfoWithProgress(path, widened, info, progress);
CFRelease(widened);
- vpLogValidation(path, result);
+ vpLogValidation(path, result, caller);
return result;
}