-
Notifications
You must be signed in to change notification settings - Fork 166
/
DarwinLoader.cpp
396 lines (310 loc) · 14 KB
/
DarwinLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* Cydia Substrate - Powerful Code Insertion Platform
* Copyright (C) 2008-2011 Jay Freeman (saurik)
*/
/* GNU Lesser General Public License, Version 3 {{{ */
/*
* Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFPriv.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <unistd.h>
#include <objc/runtime.h>
#include "CydiaSubstrate.h"
#include "Log.hpp"
#include "Environment.hpp"
#define ForSaurik 0
static char MSWatch[PATH_MAX];
static char MSStack[SIGSTKSZ];
static void MSAction(int sig, siginfo_t *info, void *uap) {
open(MSWatch, O_CREAT | O_RDWR, 0644);
raise(sig);
}
#define Libraries_ "/Library/MobileSubstrate/DynamicLibraries"
#define Safety_ "/Library/Frameworks/CydiaSubstrate.framework/MobileSafety.dylib"
extern "C" char ***_NSGetArgv(void);
MSInitialize {
#ifndef __arm__
if (dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY | RTLD_NOLOAD) == NULL)
return;
#endif
#if 1
CFBundleRef bundle(CFBundleGetMainBundle());
CFStringRef identifier(bundle == NULL ? NULL : CFBundleGetIdentifier(bundle));
#else
CFBundleRef bundle;
CFStringRef identifier;
if (
dlopen("/usr/sbin/mediaserverd", RTLD_LAZY | RTLD_NOLOAD) != NULL ||
dlopen("/System/Library/PrivateFrameworks/CoreTelephony.framework/Support/CommCenter", RTLD_LAZY | RTLD_NOLOAD) != NULL
) {
bundle = NULL;
identifier = NULL;
} else if (dlopen(Foundation_f, RTLD_LAZY | RTLD_NOLOAD) == NULL)
return;
else {
bundle = CFBundleGetMainBundle();
identifier = bundle == NULL ? NULL : CFBundleGetIdentifier(bundle);
if (identifier == NULL)
return;
}
#endif
char *argv0(**_NSGetArgv());
char *slash(strrchr(argv0, '/'));
slash = slash == NULL ? argv0 : slash + 1;
Class (*NSClassFromString)(CFStringRef) = reinterpret_cast<Class (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "NSClassFromString"));
MSLog(MSLogLevelNotice, "MS:Notice: Installing: %@ [%s] (%.2f)", identifier, slash, kCFCoreFoundationVersionNumber);
CFURLRef home(CFCopyHomeDirectoryURLForUser(NULL));
if (home == NULL) {
MSLog(MSLogLevelError, "MS:Error: Unable to Copy HOME");
return;
}
char watch[PATH_MAX];
CFURLGetFileSystemRepresentation(home, TRUE, reinterpret_cast<UInt8 *>(watch), sizeof(watch));
CFRelease(home);
const char *dead(NULL);
if (identifier != NULL && CFEqual(identifier, CFSTR("com.apple.springboard")))
dead = "com.saurik.MobileSubstrate.SpringBoard.Dead.dat";
if (dead != NULL) {
sprintf(MSWatch, "%s/Library/Preferences/%s", watch, dead);
if (access(MSWatch, R_OK) == 0) {
MSClearEnvironment();
MSLog(MSLogLevelWarning, "MS:Warning: Deactivating Substrate");
if (unlink(MSWatch) == -1)
MSLog(MSLogLevelError, "MS:Error: Cannot Clear: %s", strerror(errno));
return;
}
}
bool safe(false);
const char *dat(NULL);
if (identifier != NULL && CFEqual(identifier, CFSTR("com.apple.springboard")))
dat = "com.saurik.mobilesubstrate.dat";
if (identifier == NULL && (strcmp(slash, "CommCenter") == 0 || strcmp(slash, "CommCenterClassic") == 0))
dat = "com.saurik.MobileSubstrate.CommCenter.Safe.dat";
if (dat != NULL) {
strcat(watch, "/Library/Preferences/");
strcat(watch, dat);
if (access(watch, R_OK) == 0) {
MSClearEnvironment();
MSLog(MSLogLevelWarning, "MS:Warning: Entering Safe Mode");
if (unlink(watch) == -1)
MSLog(MSLogLevelError, "MS:Error: Cannot Clear: %s", strerror(errno));
safe = true;
}
stack_t stack;
stack.ss_size = sizeof(MSStack);
stack.ss_flags = 0;
stack.ss_sp = MSStack;
bool stacked;
if (sigaltstack(&stack, NULL) != -1)
stacked = true;
else {
stacked = false;
MSLog(MSLogLevelWarning, "MS:Error: Cannot Stack: %s", strerror(errno));
}
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_sigaction = &MSAction;
action.sa_flags = SA_SIGINFO | SA_RESETHAND;
if (stacked)
action.sa_flags |= SA_ONSTACK;
sigemptyset(&action.sa_mask);
struct sigaction old;
#define HookSignal(signum) \
sigaction(signum, NULL, &old); { \
sigaction(signum, &action, NULL); \
}
HookSignal(SIGQUIT)
HookSignal(SIGILL)
HookSignal(SIGTRAP)
HookSignal(SIGABRT)
HookSignal(SIGEMT)
HookSignal(SIGFPE)
HookSignal(SIGBUS)
HookSignal(SIGSEGV)
HookSignal(SIGSYS)
}
if (dead == NULL && dat == NULL)
MSWatch[0] = '\0';
else if (dead == NULL || !safe)
strcpy(MSWatch, watch);
CFURLRef libraries(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(Libraries_), sizeof(Libraries_) - 1, TRUE));
CFBundleRef folder(CFBundleCreate(kCFAllocatorDefault, libraries));
CFRelease(libraries);
if (folder == NULL)
return;
CFArrayRef dylibs(CFBundleCopyResourceURLsOfType(folder, CFSTR("dylib"), NULL));
CFRelease(folder);
for (CFIndex i(0), count(CFArrayGetCount(dylibs)); i != count; ++i) {
CFURLRef dylib(reinterpret_cast<CFURLRef>(CFArrayGetValueAtIndex(dylibs, i)));
char path[PATH_MAX];
CFURLGetFileSystemRepresentation(dylib, TRUE, reinterpret_cast<UInt8 *>(path), sizeof(path));
size_t length(strlen(path));
memcpy(path + length - 5, "plist", 5);
CFURLRef plist(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(path), length, FALSE));
CFDataRef data;
if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, plist, &data, NULL, NULL, NULL))
data = NULL;
CFRelease(plist);
CFDictionaryRef meta(NULL);
if (data != NULL) {
CFStringRef error(NULL);
meta = reinterpret_cast<CFDictionaryRef>(CFPropertyListCreateFromXMLData(kCFAllocatorDefault, data, kCFPropertyListImmutable, &error));
CFRelease(data);
if (meta == NULL && error != NULL) {
MSLog(MSLogLevelError, "MS:Error: Corrupt PropertyList: %@", dylib);
continue;
}
}
// XXX: eventually this should become false: dylibs must have a filter
bool load(!safe);
if (meta != NULL) {
if (CFDictionaryRef filter = reinterpret_cast<CFDictionaryRef>(CFDictionaryGetValue(meta, CFSTR("Filter")))) {
load = true;
int value(0);
if (CFNumberRef flags = reinterpret_cast<CFNumberRef>(CFDictionaryGetValue(filter, CFSTR("Flags")))) {
if (CFGetTypeID(flags) != CFNumberGetTypeID() || !CFNumberGetValue(flags, kCFNumberIntType, &value)) {
MSLog(MSLogLevelError, "MS:Error: Unable to Read Flags: %@", flags);
load = false;
goto release;
}
}
#define MSFlagWhenSafe (1 << 0)
#define MSFlagNotNoSafe (1 << 1)
if ((value & MSFlagWhenSafe) == 0 && safe) {
load = false;
goto release;
}
if ((value & MSFlagNotNoSafe) != 0 && !safe) {
load = false;
goto release;
}
if (CFArrayRef version = reinterpret_cast<CFArrayRef>(CFDictionaryGetValue(filter, CFSTR("CoreFoundationVersion")))) {
load = false;
if (CFIndex count = CFArrayGetCount(version)) {
if (count > 2) {
MSLog(MSLogLevelError, "MS:Error: Invalid CoreFoundationVersion: %@", version);
goto release;
}
CFNumberRef number;
double value;
number = reinterpret_cast<CFNumberRef>(CFArrayGetValueAtIndex(version, 0));
if (CFGetTypeID(number) != CFNumberGetTypeID() || !CFNumberGetValue(number, kCFNumberDoubleType, &value)) {
MSLog(MSLogLevelError, "MS:Error: Unable to Read CoreFoundationVersion[0]: %@", number);
goto release;
}
if (value > kCFCoreFoundationVersionNumber)
goto release;
if (count != 1) {
number = reinterpret_cast<CFNumberRef>(CFArrayGetValueAtIndex(version, 1));
if (CFGetTypeID(number) != CFNumberGetTypeID() || !CFNumberGetValue(number, kCFNumberDoubleType, &value)) {
MSLog(MSLogLevelError, "MS:Error: Unable to Read CoreFoundationVersion[1]: %@", number);
goto release;
}
if (value <= kCFCoreFoundationVersionNumber)
goto release;
}
}
load = true;
}
bool any;
if (CFStringRef mode = reinterpret_cast<CFStringRef>(CFDictionaryGetValue(filter, CFSTR("Mode"))))
any = CFEqual(mode, CFSTR("Any"));
else
any = false;
if (any)
load = false;
if (CFArrayRef executables = reinterpret_cast<CFArrayRef>(CFDictionaryGetValue(filter, CFSTR("Executables")))) {
if (!any)
load = false;
CFStringRef name(CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, slash, kCFStringEncodingUTF8, kCFAllocatorNull));
for (CFIndex i(0), count(CFArrayGetCount(executables)); i != count; ++i) {
CFStringRef executable(reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(executables, i)));
if (CFEqual(executable, name)) {
if (ForSaurik)
MSLog(MSLogLevelNotice, "MS:Notice: Found: %@", name);
load = true;
break;
}
}
CFRelease(name);
if (!any && !load)
goto release;
}
if (CFArrayRef bundles = reinterpret_cast<CFArrayRef>(CFDictionaryGetValue(filter, CFSTR("Bundles")))) {
if (!any)
load = false;
for (CFIndex i(0), count(CFArrayGetCount(bundles)); i != count; ++i) {
CFStringRef bundle(reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(bundles, i)));
if (CFBundleGetBundleWithIdentifier(bundle) != NULL) {
if (ForSaurik)
MSLog(MSLogLevelNotice, "MS:Notice: Found: %@", bundle);
load = true;
break;
}
}
if (!any && !load)
goto release;
}
if (CFArrayRef classes = reinterpret_cast<CFArrayRef>(CFDictionaryGetValue(filter, CFSTR("Classes")))) {
if (!any)
load = false;
if (NSClassFromString != NULL)
for (CFIndex i(0), count(CFArrayGetCount(classes)); i != count; ++i) {
CFStringRef _class(reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(classes, i)));
if (NSClassFromString(_class) != NULL) {
if (ForSaurik)
MSLog(MSLogLevelNotice, "MS:Notice: Found: %@", _class);
load = true;
break;
}
}
if (!any && !load)
goto release;
}
}
release:
CFRelease(meta);
}
if (!load)
continue;
memcpy(path + length - 5, "dylib", 5);
MSLog(MSLogLevelNotice, "MS:Notice: Loading: %s", path);
if (MSWatch[0] != '\0') {
int fd(open(MSWatch, O_CREAT | O_RDWR, 0644));
if (fd == -1)
MSLog(MSLogLevelError, "MS:Error: Cannot Set: %s", strerror(errno));
else if (close(fd) == -1)
MSLog(MSLogLevelError, "MS:Error: Cannot Close: %s", strerror(errno));
}
void *handle(dlopen(path, RTLD_LAZY | RTLD_GLOBAL));
if (MSWatch[0] != '\0')
if (unlink(MSWatch) == -1)
MSLog(MSLogLevelError, "MS:Error: Cannot Reset: %s", strerror(errno));
if (handle == NULL) {
MSLog(MSLogLevelError, "MS:Error: %s", dlerror());
continue;
}
}
if (!safe)
setenv(SubstrateSafeMode_, "0", false);
if (false) {
MSLog(MSLogLevelNotice, "MobileSubstrate fell asleep... I'll wake him up in 10 seconds ;P");
sleep(10);
}
}