Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
fairplayd watched from inside: it never asks IOKit, so the driver was not the next step
vcvkk0c647698a2c0
diff --git a/README.md b/README.mdindex be38276..f1efb92 100644--- a/README.md+++ b/README.md@@ -1874,6 +1874,28 @@ com.apple.fairplayd.H2: 34 запуска, каждый — «Service only ran fЗначит, следующая работа — вообще не в AMS: понять, почему `fairplayd.H2` выходит с `exit(0)` сразу, иудержать его живым. Всё остальное выше — следствие.+**Что `fairplayd` делает за свои десять миллисекунд** (`userland/src/inject_fairplay.c` — библиотека в+нём самом, только наблюдает, через подмену вызовов `__DATA,__interpose`): читает флаги возможностей+CoreFoundation и `containermanagerd`, открывает свой `Info.plist` — и уходит. **Ни одного обращения к+IOKit** (`IOServiceMatching`, `IOServiceGetMatchingService`, `IOServiceOpen`, `IOConnectCallMethod`), ни+одного `xpc_connection_create_mach_service`, ни `sysctlbyname`, ни `dlopen`. Обработчик `atexit`+срабатывает — значит, ушёл сам; но ни `exit`, ни `_exit`, ни `abort` подменёнными не оказались, то есть+`main` просто вернулся. А выборка счётчика команд главного потока (13–25 проб по полмиллисекунды) вся+лежит **внутри его собственного двоичного файла** (0x1000d9a40…0x1008565ac, то есть смещения+0xd9a40…0x8565ac), ни одной пробы в общем кеше.++Отсюда важное: он решает кодом, который не перехватить, и системные вызовы, видимо, встроены прямо в него+— чего и ждёшь от обфусцированного FairPlay. **И он не спрашивает IOKit вообще**, так что заглушка+`com_apple_driver_FairPlayIOKit` в ядре — а это и был напрашивающийся следующий шаг — просто не была бы+достигнута. (Право `com_apple_driver_FairPlayIOKitUserClient` у него в подписи есть, оттого и казалась.)+Проверено и отброшено ещё до прогона: дело не в флаге platform binary — у iOS свои двоичные файлы+подписаны ad hoc, и это ядро помечает именно такие (`bsd/kern/kern_minimac.c`), так что `fairplayd` здесь+платформенный.++Значит, следующий прибор — по ту сторону стены: ядро наше, и журнал системных вызовов процесса+`fairplayd.H2` видит сквозь встроенные вызовы, которые сводят на нет подмену в пользовательском+пространстве. Второй путь — прочитать двоичный файл по указанным смещениям.+Две вещи, которые стоили по прогону и которые стоит знать тому, кто будет писать следующую такуюбиблиотеку. `class_getInstanceMethod` идёт вверх по цепочке, и подмена метода, который класс только**наследует**, подменяет его всем остальным наследникам: подмена категории `NSObject` перезапустила@@ -2028,6 +2050,18 @@ fastboot boot ──► UEFI (Mu-Silicium, redwood) ──► xnuloader (EFI-птакого запроса (строки `top:` в журнале): «тормозит» система или чего-то ждёт, на экране не отличить.- `userland/jobinfo.py [-s СЛУЖБА | МЕТКА…]` — штатные задания launchd и кто даёт Mach-службу.- `OSLOG=all userland/build.sh` — os_log всех заданий на консоль (очень многословно).+- **Если что-то ждёт вечно — смотрите не только на то, чего launchd не нашёл, но и на то, что он+ перезапускает.** Служба, которую launchd успешно запустил и которая сама вышла с `exit(0)`, не даёт ни+ одной строки `failed lookup`: он её нашёл и поднял, клиент послал сообщение и ждёт ответа, которого не+ будет. Тем способом, которым нашлась `apsd` (счёт ненайденных обращений), такая форма не находится в+ принципе — и её не замечают, потому что свидетельство тут есть отсутствие свидетельств. Так полдня+ искали, почему пуст App Store: `com.apple.fairplayd.H2` — 34 запуска за шесть минут, каждый «Service+ only ran for 0 seconds», `exited due to exit(0)`, «service throttled by 10 seconds». Громкий сигнал+ там, куда никто не смотрел. Ищите в журнале `Service only ran for`, `throttled by` и `exited due to`.+- `/dev/console` на телефоне **теряет строки** под начальным потоком launchd, и `QUIET=1` этого не+ чинит (7755 строк launchd за 320 с против 8013 за 263 с без него). Поэтому: каждый шаг сценария —+ отдельной командой, измерения — после того как старт утихнет, а «в журнале этого нет» само по себе не+ доказывает ничего.## Драйвер Wi-Fidiff --git a/userland/build.sh b/userland/build.shindex afa317d..7ed3fe6 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -120,7 +120,8 @@ INJECT=${INJECT-"/redwood/lib/libredwood.dylib backboardd containermanagerd cont# a storefront is the only thing it is missing. It also writes down whether a bag request is ever# built at all. For an experiment, not for an image anybody else boots.[ -n "$STOREFRONT" ] && INJECT="$INJECT-/redwood/lib/libredwood-store.dylib appstored"+/redwood/lib/libredwood-store.dylib appstored+/redwood/lib/libredwood-fairplay.dylib fairplayd.H2"mkdir -p objif [ -z "$PREBUILT" ] && ! command -v ld64 >/dev/null; then PREBUILT=1; fi@@ -220,6 +221,10 @@ if [ -d $IOS/rootfs/usr/lib ]; then# request being built (src/inject_store.c)$DYN -O2 -dynamiclib -install_name /redwood/lib/libredwood-store.dylib -framework CoreFoundation -lobjc \-o obj/libredwood-store.dylib src/inject_store.c+ # ... and inside fairplayd.H2, which starts and leaves at once: what it looks for, and where it+ # gives up (src/inject_fairplay.c; watches only, replaces nothing)+ $DYN -O2 -dynamiclib -install_name /redwood/lib/libredwood-fairplay.dylib -framework CoreFoundation \+ -framework IOKit -o obj/libredwood-fairplay.dylib src/inject_fairplay.c# the output device of the audio server: a plug-in for CoreAudio's HAL$DYN -bundle -framework CoreFoundation -o obj/RedwoodAudio src/audio_plugin.cif [ -n "$GL" ]; then@@ -270,6 +275,7 @@ if [ -d $IOS/rootfs/usr/lib ]; thencp obj/libredwood.dylib obj/libredwood-sb.dylib root/lib/[ -f obj/libredwood-media.dylib ] && cp obj/libredwood-media.dylib root/lib/ # (as with cpu_test)[ -f obj/libredwood-store.dylib ] && cp obj/libredwood-store.dylib root/lib/+ [ -f obj/libredwood-fairplay.dylib ] && cp obj/libredwood-fairplay.dylib root/lib/[ -f obj/libredwood-app.dylib ] && cp obj/libredwood-app.dylib root/lib/[ -f obj/libredwood-tweaks.dylib ] && cp obj/libredwood-tweaks.dylib root/lib/if [ -f obj/RedwoodAudio ]; thendiff --git a/userland/src/inject_fairplay.c b/userland/src/inject_fairplay.cnew file mode 100644index 0000000..c6de831--- /dev/null+++ b/userland/src/inject_fairplay.c@@ -0,0 +1,444 @@+/*+ * libredwood-fairplay.dylib: code of ours inside fairplayd.H2 (see inject.c for how it gets there).+ *+ * Why. The App Store's front page never loads, and the root of that is not in the store: an+ * AppleMediaServices thread inside appstored sits in a Mach call in _AMSFairPlayGetDeviceType+ * waiting for a reply, and seventeen promises queue behind that one wait (src/inject_store.c). The+ * reply never comes because com.apple.fairplayd.H2 starts, exits 0 at once and is restarted by+ * launchd every ten seconds -- 34 times in a six-minute session, each "Service only ran for 0+ * seconds". It does not crash; it declines to work. A daemon in that state leaves no failed lookup+ * at all, which is why the shape was invisible to the method that found apsd.+ *+ * What this does. Nothing is replaced: it only watches, through dyld's interposing (__DATA,__interpose,+ * as src/inject_app.c does), the four calls that decide whether fairplayd has anything to work with:+ * IOServiceMatching -- THE NAME IT LOOKS FOR, which is the one thing a driver of ours+ * has to answer to; the binary is obfuscated and does not carry it+ * as a plain string, so it is asked for at run time instead of guessed+ * IOServiceGetMatchingServices -- and whether anything was found+ * IOServiceOpen -- the user client it wants (its entitlement names+ * com_apple_driver_FairPlayIOKitUserClient) and the type+ * IOConnectCallMethod -- every selector it then sends, which is the minimum set a stub+ * would have to implement+ * and exit(), with a walk up the frame pointers, which says where it decides to leave.+ *+ * The fault reporter is here as well: an Objective-C exception ends in abort(), and iOS answers a+ * crash in a stock daemon with "Failed to send exception EXC_CORPSE_NOTIFY" and nothing else.+ *+ * WHAT IT ANSWERED, and it says the obvious next step would have been the wrong one. Over three runs:+ * argv[0] = /usr/sbin/fairplayd.H2, one argument, as Apple's own job plist asks for (ours matches+ * it field for field but KeepAlive, which mkjobs.py drops; MachServices are all three there)+ * open() of the CoreFoundation and containermanagerd feature flags, and of /usr/sbin/Info.plist+ * -- CFBundle finding its own bundle, and nothing else+ * NOT ONE IOServiceMatching, IOServiceGetMatchingService(s), IOServiceOpen or IOConnectCallMethod+ * no xpc_connection_create_mach_service, no sysctlbyname, no dlopen, no stat or mkdir of its own+ * the exit handler runs, so it leaves of its own accord -- and neither exit() nor _exit() nor+ * abort() is caught, so main simply returns+ * and the sampler: every program counter of its ~10 ms of life, 13 to 25 samples, lies inside its+ * own binary (0x1000d9a40 to 0x1008565ac, which is 0xd9a40 to 0x8565ac into the file). Not one+ * sample in the shared cache.+ * So it decides with code we cannot interpose, inlining whatever syscalls it makes -- which is what+ * an obfuscated FairPlay binary would do on purpose. IT NEVER ASKS IOKIT FOR ANYTHING, so a stub of+ * com_apple_driver_FairPlayIOKit in the kernel, which was the plan after the last finding, would not+ * be reached at all: the daemon gives up before that. (Its entitlement does name+ * com_apple_driver_FairPlayIOKitUserClient, which is what made the driver look like the next step.)+ * One guess checked and killed before it cost a run: not the platform-binary flag. iOS's own+ * binaries are ad hoc signed and this kernel marks exactly those as platform binaries+ * (bsd/kern/kern_minimac.c), so fairplayd is one here.+ * The next instrument should therefore be on the other side of the wall: this kernel is ours, so+ * logging the system calls of the process named fairplayd.H2 sees through the inlining that defeats+ * interposing. Static reading of the binary at the offsets above is the other way in.+ */+#include <CoreFoundation/CoreFoundation.h>+#include <crt_externs.h>+#include <fcntl.h>+#include <sys/stat.h>+#include <sys/sysctl.h>+#include <dispatch/dispatch.h>++/* (the SDK here has no xpc/xpc.h; only this one call is wanted, and only its name is read) */+typedef void *xpc_connection_t;+extern xpc_connection_t xpc_connection_create_mach_service(const char *name, dispatch_queue_t queue, uint64_t flags);+#include <IOKit/IOKitLib.h>+#include <mach/mach.h>+#include <mach/thread_act.h>+#include <mach-o/dyld.h>+#include <pthread.h>+#include <signal.h>+#include <stdarg.h>+#include <stdbool.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/ucontext.h>+#include <dlfcn.h>+#include <unistd.h>++extern const void *_dyld_get_shared_cache_range(size_t *length);++#define CACHE_BASE 0x180000000UL++static uintptr_t slide;+static bool ours;++static void+say(const char *format, ...)+{+ char line[512];+ va_list ap;+ int n;++ va_start(ap, format);+ n = vsnprintf(line, sizeof(line), format, ap);+ va_end(ap);+ if (n > (int)sizeof(line) - 1) {+ n = sizeof(line) - 1;+ }+ write(2, line, (size_t)n);+}++/* up the frame pointers, unslid, for "ipsw dyld a2s" */+static void+frames_from(uintptr_t fp, char *text, size_t size)+{+ size_t used = 0;++ text[0] = 0;+ for (unsigned i = 0; i < 10 && fp != 0 && (fp & 7) == 0 && used + 20 < size; i++) {+ uintptr_t next = ((uintptr_t *)fp)[0], ret = ((uintptr_t *)fp)[1];++ if (ret == 0) {+ break;+ }+ used += (size_t)snprintf(text + used, size - used, " %#lx", ret - slide);+ fp = next > fp ? next : 0;+ }+}++static void+say_the_fault(int signal_number, siginfo_t *info, void *context_in)+{+ ucontext_t *context = context_in;+ uintptr_t pc = 0, fp = 0;+ char frames[10 * 20 + 1] = "";++ if (context != NULL && context->uc_mcontext != NULL) {+ pc = (uintptr_t)context->uc_mcontext->__ss.__pc;+ fp = (uintptr_t)context->uc_mcontext->__ss.__fp;+ }+ frames_from(fp, frames, sizeof(frames));+ say("redwood: fairplayd: signal %d at %p, pc %#lx, called from%s (unslid)\n", signal_number,+ info != NULL ? info->si_addr : NULL, pc - slide, frames);+ signal(signal_number, SIG_DFL);+ raise(signal_number);+}++/* ---- the four questions, and the leaving ------------------------------------------------------- */++static CFMutableDictionaryRef+watched_IOServiceMatching(const char *name)+{+ if (ours) {+ say("redwood: fairplayd: IOServiceMatching(\"%s\")\n", name != NULL ? name : "(null)");+ }+ return IOServiceMatching(name);+}++static kern_return_t+watched_IOServiceGetMatchingServices(mach_port_t port, CFDictionaryRef matching, io_iterator_t *existing)+{+ kern_return_t result = IOServiceGetMatchingServices(port, matching, existing);++ if (ours) {+ io_object_t one = existing != NULL ? IOIteratorNext(*existing) : 0;++ say("redwood: fairplayd: IOServiceGetMatchingServices -> %#x, %s\n", result,+ one != 0 ? "something matched" : "NOTHING MATCHED");+ if (one != 0) {+ IOObjectRelease(one);+ IOIteratorReset(*existing);+ }+ }+ return result;+}++static io_service_t+watched_IOServiceGetMatchingService(mach_port_t port, CFDictionaryRef matching)+{+ io_service_t service = IOServiceGetMatchingService(port, matching);++ if (ours) {+ say("redwood: fairplayd: IOServiceGetMatchingService -> %s\n", service != 0 ? "a service" : "NOTHING");+ }+ return service;+}++static kern_return_t+watched_IOServiceOpen(io_service_t service, task_port_t task, uint32_t type, io_connect_t *connect)+{+ kern_return_t result = IOServiceOpen(service, task, type, connect);++ if (ours) {+ say("redwood: fairplayd: IOServiceOpen(type %u) -> %#x\n", type, result);+ }+ return result;+}++static kern_return_t+watched_IOConnectCallMethod(mach_port_t connection, uint32_t selector, const uint64_t *input, uint32_t input_count,+ const void *input_struct, size_t input_struct_count, uint64_t *output, uint32_t *output_count,+ void *output_struct, size_t *output_struct_count)+{+ kern_return_t result = IOConnectCallMethod(connection, selector, input, input_count, input_struct,+ input_struct_count, output, output_count, output_struct, output_struct_count);++ if (ours) {+ say("redwood: fairplayd: IOConnectCallMethod selector %u -> %#x\n", selector, result);+ }+ return result;+}++/*+ * It leaves without ever asking IOKit for anything -- the first run of this library caught not one+ * IOServiceMatching -- so the net is wider: the files it opens, the sysctls and the libraries it+ * asks for, and every way out of a process. Whatever it looked at last is where it made up its mind.+ */+static int+watched_open(const char *path, int flags, ...)+{+ mode_t mode = 0;++ if ((flags & O_CREAT) != 0) {+ va_list ap;++ va_start(ap, flags);+ mode = (mode_t)va_arg(ap, int);+ va_end(ap);+ }+ if (ours) {+ say("redwood: fairplayd: open(\"%s\")\n", path != NULL ? path : "(null)");+ }+ return open(path, flags, mode);+}++static int+watched_sysctlbyname(const char *name, void *out, size_t *out_size, void *in, size_t in_size)+{+ if (ours) {+ say("redwood: fairplayd: sysctlbyname(\"%s\")\n", name != NULL ? name : "(null)");+ }+ return sysctlbyname(name, out, out_size, in, in_size);+}++static void *+watched_dlopen(const char *path, int mode)+{+ if (ours) {+ say("redwood: fairplayd: dlopen(\"%s\")\n", path != NULL ? path : "(null)");+ }+ return dlopen(path, mode);+}++/*+ * It reads the feature flags of containermanagerd and its own Info.plist and then leaves, so the+ * next question is who it asks over XPC and what it looks for on disk: its container, most likely+ * (its entitlements name com.apple.security.system-container and+ * com.apple.private.security.storage.fpsd), and a lookup that fails there would explain a daemon+ * that declines to work without complaining.+ */+static xpc_connection_t+watched_xpc_connection_create_mach_service(const char *name, dispatch_queue_t queue, uint64_t flags)+{+ if (ours) {+ say("redwood: fairplayd: xpc_connection_create_mach_service(\"%s\")\n", name != NULL ? name : "(null)");+ }+ return xpc_connection_create_mach_service(name, queue, flags);+}++static int+watched_stat(const char *path, struct stat *out)+{+ int result = stat(path, out);++ if (ours) {+ say("redwood: fairplayd: stat(\"%s\") -> %d\n", path != NULL ? path : "(null)", result);+ }+ return result;+}++static int+watched_access(const char *path, int mode)+{+ int result = access(path, mode);++ if (ours) {+ say("redwood: fairplayd: access(\"%s\") -> %d\n", path != NULL ? path : "(null)", result);+ }+ return result;+}++static int+watched_mkdir(const char *path, mode_t mode)+{+ int result = mkdir(path, mode);++ if (ours) {+ say("redwood: fairplayd: mkdir(\"%s\") -> %d\n", path != NULL ? path : "(null)", result);+ }+ return result;+}++static void+watched__exit(int status)+{+ char frames[10 * 20 + 1] = "";++ if (ours) {+ frames_from((uintptr_t)__builtin_frame_address(0), frames, sizeof(frames));+ say("redwood: fairplayd: LEAVING, _exit(%d), called from%s (unslid)\n", status, frames);+ }+ _exit(status);+}++static void+watched_abort(void)+{+ char frames[10 * 20 + 1] = "";++ if (ours) {+ frames_from((uintptr_t)__builtin_frame_address(0), frames, sizeof(frames));+ say("redwood: fairplayd: LEAVING, abort(), called from%s (unslid)\n", frames);+ }+ abort();+}++/*+ * A sampler, because it does not call anything we can watch. It reads the feature flags, opens its+ * own bundle and leaves, without IOKit, without XPC, without a sysctl or a dlopen -- so whatever it+ * decides on, it decides with code we cannot interpose. The main thread's program counter, read a+ * few hundred times a second from a thread of ours (the same thread_get_state the store library uses+ * for its thread dump), says where it was instead. The daemon lives about a fifth of a second, so a+ * sample every half a millisecond covers the whole of it.+ * Addresses are printed three ways -- as they are, less the shared cache's slide (for+ * "ipsw dyld a2s"), and less this program's own slide (for its own 19 MB of obfuscated code).+ */+#define SAMPLES 2048+static uintptr_t samples[SAMPLES];+static unsigned sample_count;+static mach_port_t main_thread;+static uintptr_t own_slide;++static void *+sampler(__unused void *ignored)+{+ while (sample_count < SAMPLES) {+ arm_thread_state64_t state;+ mach_msg_type_number_t count = ARM_THREAD_STATE64_COUNT;++ if (thread_get_state(main_thread, ARM_THREAD_STATE64, (thread_state_t)&state, &count) != KERN_SUCCESS) {+ break;+ }+ samples[sample_count++] = (uintptr_t)arm_thread_state64_get_pc(state);+ usleep(500);+ }+ return NULL;+}++static void+at_the_end(void)+{+ unsigned shown = 0;++ if (!ours) {+ return;+ }+ say("redwood: fairplayd: at the end (an exit handler ran: it left of its own accord), %u samples\n",+ sample_count);+ /* every distinct pc, newest first: a fifth of a second of a daemon is a short story */+ for (unsigned i = sample_count; i > 0 && shown < 40; i--) {+ uintptr_t pc = samples[i - 1];+ bool seen = false;++ for (unsigned j = i; j < sample_count; j++) {+ seen |= samples[j] == pc;+ }+ if (seen) {+ continue;+ }+ shown++;+ say("redwood: fairplayd: was at %#lx (cache %#lx, own %#lx)\n", pc, pc - slide, pc - own_slide);+ }+}++static void+watched_exit(int status)+{+ char frames[10 * 20 + 1] = "";++ if (ours) {+ frames_from((uintptr_t)__builtin_frame_address(0), frames, sizeof(frames));+ say("redwood: fairplayd: LEAVING, exit(%d), called from%s (unslid)\n", status, frames);+ }+ exit(status);+}++__attribute__((used)) static struct {+ const void *ours, *theirs;+} interposed[] __attribute__((section("__DATA,__interpose"))) = {+ { (const void *)watched_IOServiceMatching, (const void *)IOServiceMatching },+ { (const void *)watched_IOServiceGetMatchingServices, (const void *)IOServiceGetMatchingServices },+ { (const void *)watched_IOServiceGetMatchingService, (const void *)IOServiceGetMatchingService },+ { (const void *)watched_IOServiceOpen, (const void *)IOServiceOpen },+ { (const void *)watched_IOConnectCallMethod, (const void *)IOConnectCallMethod },+ { (const void *)watched_exit, (const void *)exit },+ { (const void *)watched__exit, (const void *)_exit },+ { (const void *)watched_abort, (const void *)abort },+ { (const void *)watched_open, (const void *)open },+ { (const void *)watched_sysctlbyname, (const void *)sysctlbyname },+ { (const void *)watched_dlopen, (const void *)dlopen },+ { (const void *)watched_xpc_connection_create_mach_service, (const void *)xpc_connection_create_mach_service },+ { (const void *)watched_stat, (const void *)stat },+ { (const void *)watched_access, (const void *)access },+ { (const void *)watched_mkdir, (const void *)mkdir },+};++__attribute__((constructor))+static void+redwood_fairplay_init(void)+{+ const char *name = getprogname();+ size_t length = 0;+ struct sigaction action;++ ours = name != NULL && strncmp(name, "fairplayd", 9) == 0;+ if (!ours) {+ return;+ }+ slide = (uintptr_t)_dyld_get_shared_cache_range(&length) - CACHE_BASE;+ memset(&action, 0, sizeof(action));+ action.sa_sigaction = say_the_fault;+ action.sa_flags = SA_SIGINFO | SA_RESETHAND;+ sigaction(SIGSEGV, &action, NULL);+ sigaction(SIGBUS, &action, NULL);+ sigaction(SIGABRT, &action, NULL);+ sigaction(SIGILL, &action, NULL);+ sigaction(SIGTRAP, &action, NULL);+ main_thread = mach_thread_self();+ own_slide = (uintptr_t)_dyld_get_image_vmaddr_slide(0);+ atexit(at_the_end);+ {+ pthread_t thread;++ if (pthread_create(&thread, NULL, sampler, NULL) == 0) {+ pthread_detach(thread);+ }+ }+ {+ char ***argv = _NSGetArgv();+ int *argc = _NSGetArgc();++ say("redwood: fairplayd: ours is in, %d argument(s)\n", argc != NULL ? *argc : -1);+ for (int i = 0; argv != NULL && argc != NULL && i < *argc && i < 6; i++) {+ say("redwood: fairplayd: argv[%d] = %s\n", i, (*argv)[i] != NULL ? (*argv)[i] : "(null)");+ }+ }+}