This repository was archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdyld_ramdisk.c
More file actions
353 lines (306 loc) · 8.42 KB
/
dyld_ramdisk.c
File metadata and controls
353 lines (306 loc) · 8.42 KB
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
/*
* bakera1n - dyld_ramdisk.c
*
* Copyright (c) 2022 - 2023 tihmstar
* Copyright (c) 2023 dora2ios
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
*/
#include <stdint.h>
#include <plog.h>
#include "printf.h"
#include "dyld_utils.h"
#include "../haxx_dylib.h"
#include "../haxx.h"
asm(
".globl __dyld_start \n"
".align 4 \n"
"__dyld_start: \n"
"movn x8, #0xf \n"
"mov x7, sp \n"
"and x7, x7, x8 \n"
"mov sp, x7 \n"
"bl _main \n"
"movz x16, #0x1 \n"
"svc #0x80 \n"
);
static checkrain_option_t pflags;
static char *root_device = NULL;
static int isOS = 0;
static char statbuf[0x400];
static inline __attribute__((always_inline)) int checkrain_option_enabled(checkrain_option_t flags, checkrain_option_t opt)
{
if(flags == checkrain_option_failure)
{
switch(opt)
{
case checkrain_option_safemode:
return 1;
default:
return 0;
}
}
return (flags & opt) != 0;
}
static inline __attribute__((always_inline)) int getFlags(void)
{
uint32_t err = 0;
size_t sz = 0;
struct kerninfo info;
int fd = open("/dev/rmd0", O_RDONLY|O_RDWR, 0);
if (fd >= 0x1)
{
read(fd, &sz, 4);
lseek(fd, (long)(sz), SEEK_SET);
if(read(fd, &info, sizeof(struct kerninfo)) == sizeof(struct kerninfo))
{
pflags = info.flags;
LOG("got flags: %d from stage1", pflags);
err = 0;
} else
{
ERR("Read kinfo failed");
err = -1;
}
close(fd);
}
else
{
ERR("Open rd failed");
err = -1;
}
return err;
}
static inline __attribute__((always_inline)) int main2(void)
{
LOG("Remounting fs");
{
char *path = ROOTFS_RAMDISK;
if (mount("apfs", "/", MNT_UPDATE, &path))
{
FATAL("Failed to remount ramdisk");
goto fatal_err;
}
}
LOG("Unlinking dyld");
{
unlink(CUSTOM_DYLD_PATH);
if(!stat(CUSTOM_DYLD_PATH, statbuf))
{
FATAL("Why does that %s exist!?", CUSTOM_DYLD_PATH);
goto fatal_err;
}
}
LOG("Remounting fs");
{
char *path = ROOTFS_RAMDISK;
if (mount("apfs", "/", MNT_UPDATE | MNT_RDONLY, &path))
{
FATAL("Failed to remount ramdisk");
goto fatal_err;
}
}
int mntflag = MOUNT_WITH_SNAPSHOT;
if(checkrain_option_enabled(pflags, checkrain_option_not_snapshot))
{
mntflag = MOUNT_WITHOUT_SNAPSHOT;
}
{
char *mntpath = "/";
LOG("Mounting rootfs (non snapshot) to %s", mntpath);
int err = 0;
char buf[0x100];
struct mounarg {
char *path;
uint64_t _null;
uint64_t mountAsRaw;
uint32_t _pad;
char snapshot[0x100];
} arg = {
root_device,
0,
mntflag,
0,
};
retry_rootfs_mount:
err = mount("apfs", mntpath, MNT_RDONLY, &arg);
if (err)
{
ERR("Failed to mount rootfs (%d)", err);
sleep(1);
}
if (stat("/private/", statbuf))
{
ERR("Failed to find directory, retry.");
sleep(1);
goto retry_rootfs_mount;
}
LOG("Mounting devfs");
{
if (mount_devfs("/dev"))
{
FATAL("Failed to mount devfs");
goto fatal_err;
}
}
}
{
LOG("Mounting tmpfs");
struct tmpfs_mountarg
{
uint64_t max_pages;
uint64_t max_nodes;
uint8_t case_insensitive;
};
int64_t pagesize;
unsigned long pagesize_len = sizeof(pagesize);
if (sys_sysctlbyname("hw.pagesize", sizeof("hw.pagesize"), &pagesize, &pagesize_len, NULL, 0))
{
FATAL("Failed to get pagesize");
goto fatal_err;
}
struct tmpfs_mountarg arg = {.max_pages = (1572864 / pagesize), .max_nodes = UINT8_MAX, .case_insensitive = 0};
if (mount("tmpfs", "/cores", 0, &arg))
{
FATAL("Failed to mount tmpfs onto /cores");
goto fatal_err;
}
}
{
if(mkdir("/cores/binpack", 0755))
{
FATAL("Failed to make directory %s", "/cores/binpack");
goto fatal_err;
}
if (stat("/cores/binpack", statbuf))
{
FATAL("Failed to stat directory %s", "/cores/binpack");
goto fatal_err;
}
}
if(deploy_file_from_memory(LIBRARY_PATH, haxx_dylib, haxx_dylib_len))
{
FATAL("Failed to open %s", LIBRARY_PATH);
goto fatal_err;
}
if(deploy_file_from_memory(PAYLOAD_PATH, haxx, haxx_len))
{
FATAL("Failed to open %s", LIBRARY_PATH);
goto fatal_err;
}
void *data = mmap(NULL, 0x4000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
DEVLOG("data: 0x%016llx", data);
if (data == (void*)-1)
{
FATAL("Failed to mmap");
goto fatal_err;
}
{
if (stat(LAUNCHD_PATH, statbuf))
{
FATAL("%s: No such file or directory", LAUNCHD_PATH);
goto fatal_err;
}
if (stat(PAYLOAD_PATH, statbuf))
{
FATAL("%s: No such file or directory", PAYLOAD_PATH);
goto fatal_err;
}
if (stat(LIBRARY_PATH, statbuf))
{
FATAL("%s: No such file or directory", PAYLOAD_PATH);
goto fatal_err;
}
}
/*
Launchd doesn't like it when the console is open already
*/
for (size_t i = 0; i < 10; i++)
close(i);
int err = 0;
{
char **argv = (char **)data;
char **envp = argv+2;
char *strbuf = (char*)(envp+2);
argv[0] = strbuf;
argv[1] = NULL;
memcpy(strbuf, LAUNCHD_PATH, sizeof(LAUNCHD_PATH));
strbuf += sizeof(LAUNCHD_PATH);
envp[0] = strbuf;
envp[1] = NULL;
char dyld_insert_libs[] = "DYLD_INSERT_LIBRARIES";
char dylibs[] = LIBRARY_PATH;
uint8_t eqBuf = 0x3D;
memcpy(strbuf, dyld_insert_libs, sizeof(dyld_insert_libs));
memcpy(strbuf+sizeof(dyld_insert_libs)-1, &eqBuf, 1);
memcpy(strbuf+sizeof(dyld_insert_libs)-1+1, dylibs, sizeof(dylibs));
err = execve(argv[0], argv, envp);
}
if (err) {
FATAL("Failed to execve (%d)", err);
goto fatal_err;
}
fatal_err:
FATAL("see you my friend...");
spin();
return 0;
}
int main(void)
{
int console = open("/dev/console", O_RDWR, 0);
sys_dup2(console, 0);
sys_dup2(console, 1);
sys_dup2(console, 2);
printf("#==================\n");
printf("#\n");
printf("# bakera1n loader %s\n", VERSION);
printf("#\n");
printf("# (c) 2023 bakera1n developer\n");
printf("#==================\n");
LOG("Checking rootfs");
{
while ((stat(ROOTFS_IOS16, statbuf)) &&
(stat(ROOTFS_IOS15, statbuf)))
{
LOG("Waiting for roots...");
sleep(1);
}
}
if(stat(ROOTFS_IOS15, statbuf))
{
root_device = ROOTFS_IOS16;
isOS = IS_IOS16;
}
else
{
root_device = ROOTFS_IOS15;
isOS = IS_IOS15;
}
if(!root_device)
{
FATAL("Failed to get root_device");
goto fatal_err;
}
LOG("Got root_device: %s", root_device);
if(getFlags())
{
pflags = checkrain_option_failure;
}
{
// rootless without bindfs
return main2();
}
fatal_err:
FATAL("see you my friend...");
spin();
return 0;
}