Magisk/core/jni/utils/cpio.c

412 lines
10 KiB
C
Raw Normal View History

2017-09-14 15:11:56 +00:00
#include <stdio.h>
#include <unistd.h>
2017-11-09 17:51:41 +00:00
#include <fcntl.h>
2017-09-14 15:11:56 +00:00
2017-03-07 16:54:23 +00:00
#include "cpio.h"
2017-09-14 15:11:56 +00:00
#include "logging.h"
#include "utils.h"
2017-03-07 16:54:23 +00:00
static uint32_t x8u(char *hex) {
uint32_t val, inpos = 8, outpos;
char pattern[6];
while (*hex == '0') {
hex++;
if (!--inpos) return 0;
}
// Because scanf gratuitously treats %*X differently than printf does.
sprintf(pattern, "%%%dx%%n", inpos);
sscanf(hex, pattern, &val, &outpos);
2017-09-14 15:11:56 +00:00
if (inpos != outpos) LOGE("bad cpio header\n");
2017-03-07 16:54:23 +00:00
return val;
}
2017-09-14 02:47:10 +00:00
static void cpio_free(cpio_entry *f) {
2017-03-09 23:52:59 +00:00
if (f) {
free(f->filename);
free(f->data);
free(f);
}
}
2017-09-14 02:47:10 +00:00
static void cpio_vec_insert(struct vector *v, cpio_entry *n) {
cpio_entry *f;
2017-03-07 16:54:23 +00:00
vec_for_each(v, f) {
if (strcmp(f->filename, n->filename) == 0) {
// Replace, then all is done
2017-03-09 23:52:59 +00:00
cpio_free(f);
2017-09-07 11:22:30 +00:00
vec_cur(v) = n;
2017-03-07 16:54:23 +00:00
return;
}
}
2017-09-07 11:22:30 +00:00
vec_push_back(v, n);
2017-03-07 16:54:23 +00:00
}
2017-09-09 15:05:50 +00:00
static int cpio_cmp(const void *a, const void *b) {
2017-09-14 02:47:10 +00:00
return strcmp((*(cpio_entry **) a)->filename, (*(cpio_entry **) b)->filename);
2017-03-09 20:08:17 +00:00
}
2017-09-14 02:47:10 +00:00
// Parse cpio file to a vector of cpio_entry
2017-12-03 19:14:04 +00:00
void parse_cpio(struct vector *v, const char *filename) {
2017-07-18 03:53:28 +00:00
fprintf(stderr, "Loading cpio: [%s]\n\n", filename);
2017-12-03 19:14:04 +00:00
int fd = open(filename, O_RDONLY);
if (fd < 0) return;
2017-03-07 16:54:23 +00:00
cpio_newc_header header;
2017-09-14 02:47:10 +00:00
cpio_entry *f;
2017-09-07 11:22:30 +00:00
while(xxread(fd, &header, 110) != -1) {
2017-04-28 13:48:38 +00:00
f = xcalloc(sizeof(*f), 1);
2017-03-07 16:54:23 +00:00
// f->ino = x8u(header.ino);
f->mode = x8u(header.mode);
f->uid = x8u(header.uid);
f->gid = x8u(header.gid);
// f->nlink = x8u(header.nlink);
// f->mtime = x8u(header.mtime);
f->filesize = x8u(header.filesize);
// f->devmajor = x8u(header.devmajor);
// f->devminor = x8u(header.devminor);
// f->rdevmajor = x8u(header.rdevmajor);
// f->rdevminor = x8u(header.rdevminor);
2017-12-03 19:14:04 +00:00
uint32_t namesize = x8u(header.namesize);
2017-03-07 16:54:23 +00:00
// f->check = x8u(header.check);
2017-12-03 19:14:04 +00:00
f->filename = xmalloc(namesize);
xxread(fd, f->filename, namesize);
2017-03-07 16:54:23 +00:00
file_align(fd, 4, 0);
2017-03-15 22:46:19 +00:00
if (strcmp(f->filename, ".") == 0 || strcmp(f->filename, "..") == 0) {
2017-03-09 23:52:59 +00:00
cpio_free(f);
continue;
2017-03-09 20:08:17 +00:00
}
2017-03-15 22:46:19 +00:00
if (strcmp(f->filename, "TRAILER!!!") == 0) {
cpio_free(f);
break;
}
2017-03-07 16:54:23 +00:00
if (f->filesize) {
2017-12-03 19:14:04 +00:00
f->data = xmalloc(f->filesize);
2017-04-28 13:48:38 +00:00
xxread(fd, f->data, f->filesize);
2017-03-07 16:54:23 +00:00
file_align(fd, 4, 0);
}
vec_push_back(v, f);
}
close(fd);
}
2017-12-03 19:14:04 +00:00
void dump_cpio(struct vector *v, const char *filename) {
2017-07-18 03:53:28 +00:00
fprintf(stderr, "\nDump cpio: [%s]\n\n", filename);
2017-11-09 17:51:41 +00:00
int fd = creat(filename, 0644);
2017-03-07 16:54:23 +00:00
unsigned inode = 300000;
char header[111];
2017-09-07 11:22:30 +00:00
// Sort by name
2017-09-09 15:05:50 +00:00
vec_sort(v, cpio_cmp);
2017-09-14 02:47:10 +00:00
cpio_entry *f;
2017-03-07 16:54:23 +00:00
vec_for_each(v, f) {
if (f->remove) continue;
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
inode++, // f->ino
f->mode,
f->uid,
f->gid,
1, // f->nlink
0, // f->mtime
f->filesize,
0, // f->devmajor
0, // f->devminor
0, // f->rdevmajor
0, // f->rdevminor
2017-12-03 19:14:04 +00:00
(uint32_t) strlen(f->filename) + 1,
2017-03-07 16:54:23 +00:00
0 // f->check
);
2017-04-28 13:48:38 +00:00
xwrite(fd, header, 110);
2017-12-03 19:14:04 +00:00
xwrite(fd, f->filename, strlen(f->filename) + 1);
2017-03-07 16:54:23 +00:00
file_align(fd, 4, 1);
if (f->filesize) {
2017-04-28 13:48:38 +00:00
xwrite(fd, f->data, f->filesize);
2017-03-07 16:54:23 +00:00
file_align(fd, 4, 1);
}
}
2017-03-09 20:08:17 +00:00
// Write trailer
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", inode++, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 11, 0);
2017-04-28 13:48:38 +00:00
xwrite(fd, header, 110);
xwrite(fd, "TRAILER!!!\0", 11);
2017-03-09 20:08:17 +00:00
file_align(fd, 4, 1);
2017-03-09 23:52:59 +00:00
close(fd);
2017-03-07 16:54:23 +00:00
}
2017-12-01 09:17:24 +00:00
void cpio_vec_destroy(struct vector *v) {
2017-09-14 02:47:10 +00:00
// Free each cpio_entry
cpio_entry *f;
2017-03-07 16:54:23 +00:00
vec_for_each(v, f) {
2017-03-09 23:52:59 +00:00
cpio_free(f);
2017-03-07 16:54:23 +00:00
}
vec_destroy(v);
}
2017-12-03 19:14:04 +00:00
void cpio_rm(struct vector *v, int recursive, const char *entry) {
2017-09-14 02:47:10 +00:00
cpio_entry *f;
2017-03-07 16:54:23 +00:00
vec_for_each(v, f) {
2017-09-09 15:05:50 +00:00
if (strncmp(f->filename, entry, strlen(entry)) == 0) {
char next = f->filename[strlen(entry)];
if ((recursive && next == '/') || next == '\0') {
if (!f->remove) {
fprintf(stderr, "Remove [%s]\n", f->filename);
f->remove = 1;
}
if (!recursive) return;
2017-03-09 23:52:59 +00:00
}
2017-03-07 16:54:23 +00:00
}
}
}
2017-12-03 19:14:04 +00:00
void cpio_mkdir(struct vector *v, mode_t mode, const char *entry) {
2017-09-14 02:47:10 +00:00
cpio_entry *f = xcalloc(sizeof(*f), 1);
2017-03-07 16:54:23 +00:00
f->mode = S_IFDIR | mode;
2017-09-07 11:22:30 +00:00
f->filename = strdup(entry);
2017-03-07 16:54:23 +00:00
cpio_vec_insert(v, f);
2017-07-18 03:53:28 +00:00
fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode);
2017-03-07 16:54:23 +00:00
}
2017-12-03 19:22:26 +00:00
void cpio_ln(struct vector *v, const char *target, const char *entry) {
cpio_entry *f = xcalloc(sizeof(*f), 1);
f->mode = S_IFLNK;
f->filename = strdup(entry);
f->data = strdup(target);
cpio_vec_insert(v, f);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", entry, target);
}
2017-12-03 19:14:04 +00:00
void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename) {
2017-04-28 13:48:38 +00:00
int fd = xopen(filename, O_RDONLY);
2017-09-14 02:47:10 +00:00
cpio_entry *f = xcalloc(sizeof(*f), 1);
2017-03-07 16:54:23 +00:00
f->mode = S_IFREG | mode;
2017-09-07 11:22:30 +00:00
f->filename = strdup(entry);
2017-03-07 16:54:23 +00:00
f->filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
2017-09-07 11:22:30 +00:00
f->data = xmalloc(f->filesize);
2017-04-28 13:48:38 +00:00
xxread(fd, f->data, f->filesize);
2017-03-09 23:52:59 +00:00
close(fd);
2017-03-07 16:54:23 +00:00
cpio_vec_insert(v, f);
2017-07-18 03:53:28 +00:00
fprintf(stderr, "Add entry [%s] (%04o)\n", entry, mode);
2017-03-07 16:54:23 +00:00
}
2017-03-09 20:08:17 +00:00
2017-12-03 19:14:04 +00:00
int cpio_mv(struct vector *v, const char *from, const char *to) {
2017-12-01 09:17:24 +00:00
struct cpio_entry *f, *t;
vec_for_each(v, f) {
if (strcmp(f->filename, from) == 0) {
fprintf(stderr, "Move [%s] -> [%s]\n", from, to);
vec_for_each(v, t) {
if (strcmp(t->filename, to) == 0) {
t->remove = 1;
break;
}
}
free(f->filename);
f->filename = strdup(to);
2017-12-03 19:14:04 +00:00
return 0;
2017-12-01 09:17:24 +00:00
}
}
fprintf(stderr, "Cannot find entry %s\n", from);
2017-12-03 19:14:04 +00:00
return 1;
2017-12-01 09:17:24 +00:00
}
2017-12-03 19:14:04 +00:00
int cpio_extract(struct vector *v, const char *entry, const char *filename) {
cpio_entry *f;
vec_for_each(v, f) {
if (strcmp(f->filename, entry) == 0) {
fprintf(stderr, "Extracting [%s] to [%s]\n\n", entry, filename);
unlink(filename);
if (S_ISREG(f->mode)) {
int fd = creat(filename, f->mode & 0777);
xwrite(fd, f->data, f->filesize);
fchown(fd, f->uid, f->gid);
close(fd);
} else if (S_ISLNK(f->mode)) {
symlink(f->data, filename);
}
return 0;
}
}
fprintf(stderr, "Cannot find the file entry [%s]\n", entry);
return 1;
}
int cpio_test(struct vector *v) {
2017-09-12 07:27:28 +00:00
#define STOCK_BOOT 0x0
#define MAGISK_PATCH 0x1
#define OTHER_PATCH 0x2
int ret = STOCK_BOOT;
2017-09-14 02:47:10 +00:00
cpio_entry *f;
2017-09-12 07:27:28 +00:00
const char *OTHER_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc", "boot/sbin/launch_daemonsu.sh", NULL };
const char *MAGISK_LIST[] = { ".backup/.magisk", "init.magisk.rc", "overlay/init.magisk.rc", NULL };
2017-03-09 20:08:17 +00:00
vec_for_each(v, f) {
2017-06-02 21:08:52 +00:00
for (int i = 0; OTHER_LIST[i]; ++i) {
2017-09-12 07:27:28 +00:00
if (strcmp(f->filename, OTHER_LIST[i]) == 0) {
2017-06-02 21:08:52 +00:00
ret |= OTHER_PATCH;
2017-09-12 07:27:28 +00:00
// Already find other files, abort
exit(OTHER_PATCH);
}
}
for (int i = 0; MAGISK_LIST[i]; ++i) {
if (strcmp(f->filename, MAGISK_LIST[i]) == 0)
ret = MAGISK_PATCH;
2017-03-09 20:08:17 +00:00
}
}
cpio_vec_destroy(v);
2017-12-03 19:14:04 +00:00
return ret;
2017-03-09 20:08:17 +00:00
}
2017-12-03 19:14:04 +00:00
void cpio_backup(struct vector *v, const char *orig, const char *sha1) {
2017-04-27 19:15:48 +00:00
struct vector o_body, *o = &o_body, bak;
cpio_entry *m, *n, *rem, *cksm;
2017-04-30 17:58:52 +00:00
char buf[PATH_MAX];
2017-03-09 23:52:59 +00:00
int res, doBak;
vec_init(o);
vec_init(&bak);
m = xcalloc(sizeof(*m), 1);
m->filename = strdup(".backup");
m->mode = S_IFDIR;
vec_push_back(&bak, m);
m = xcalloc(sizeof(*m), 1);
m->filename = strdup(".backup/.magisk");
m->mode = S_IFREG;
vec_push_back(&bak, m);
rem = xcalloc(sizeof(*rem), 1);
rem->filename = strdup(".backup/.rmlist");
rem->mode = S_IFREG;
2017-03-09 23:52:59 +00:00
vec_push_back(&bak, rem);
if (sha1) {
fprintf(stderr, "Save SHA1: [%s] -> [.backup/.sha1]\n", sha1);
2017-12-03 19:14:04 +00:00
cksm = xcalloc(sizeof(*cksm), 1);
vec_push_back(&bak, cksm);
cksm->filename = strdup(".backup/.sha1");
cksm->mode = S_IFREG;
cksm->data = strdup(sha1);
cksm->filesize = strlen(sha1) + 1;
}
2017-03-09 23:52:59 +00:00
2017-12-03 19:14:04 +00:00
parse_cpio(o, orig);
// Remove possible backups in original ramdisk
cpio_rm(o, 1, ".backup");
cpio_rm(v, 1, ".backup");
// Sort both vectors before comparing
vec_sort(v, cpio_cmp);
vec_sort(o, cpio_cmp);
2017-03-09 23:52:59 +00:00
// Start comparing
size_t i = 0, j = 0;
while(i != vec_size(o) || j != vec_size(v)) {
doBak = 0;
if (i != vec_size(o) && j != vec_size(v)) {
m = vec_entry(o)[i];
n = vec_entry(v)[j];
res = strcmp(m->filename, n->filename);
} else if (i == vec_size(o)) {
n = vec_entry(v)[j];
res = 1;
} else if (j == vec_size(v)) {
m = vec_entry(o)[i];
res = -1;
}
if (res < 0) {
// Something is missing in new ramdisk, backup!
++i;
doBak = 1;
2017-07-18 03:53:28 +00:00
fprintf(stderr, "Backup missing entry: ");
2017-03-09 23:52:59 +00:00
} else if (res == 0) {
++i; ++j;
2017-03-15 22:46:19 +00:00
if (m->filesize == n->filesize && memcmp(m->data, n->data, m->filesize) == 0)
continue;
// Not the same!
doBak = 1;
2017-07-18 03:53:28 +00:00
fprintf(stderr, "Backup mismatch entry: ");
2017-03-09 23:52:59 +00:00
} else {
// Someting new in ramdisk, record in rem
++j;
if (n->remove) continue;
2017-12-03 19:14:04 +00:00
rem->data = xrealloc(rem->data, rem->filesize + strlen(n->filename) + 1);
memcpy(rem->data + rem->filesize, n->filename, strlen(n->filename) + 1);
rem->filesize += strlen(n->filename) + 1;
2017-07-18 03:53:28 +00:00
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", n->filename);
2017-03-09 23:52:59 +00:00
}
if (doBak) {
2017-12-03 19:14:04 +00:00
sprintf(buf, ".backup/%s", m->filename);
free(m->filename);
m->filename = strdup(buf);
2017-07-18 03:53:28 +00:00
fprintf(stderr, "[%s] -> [%s]\n", buf, m->filename);
2017-03-09 23:52:59 +00:00
vec_push_back(&bak, m);
// NULL the original entry, so it won't be freed
vec_entry(o)[i - 1] = NULL;
}
}
// Add the backup files to the original ramdisk
vec_for_each(&bak, m) {
vec_push_back(v, m);
}
if (rem->filesize == 0)
2017-03-09 23:52:59 +00:00
rem->remove = 1;
// Cleanup
cpio_vec_destroy(o);
}
2017-12-01 09:17:24 +00:00
int cpio_restore(struct vector *v) {
2017-09-14 02:47:10 +00:00
cpio_entry *f, *n;
int ret = 1;
2017-03-09 23:52:59 +00:00
vec_for_each(v, f) {
if (strstr(f->filename, ".backup") != NULL) {
ret = 0;
2017-03-09 23:52:59 +00:00
f->remove = 1;
if (f->filename[7] == '\0') continue;
if (f->filename[8] == '.') {
if (strcmp(f->filename, ".backup/.rmlist") == 0) {
for (int pos = 0; pos < f->filesize; pos += strlen(f->data + pos) + 1)
2017-12-03 19:14:04 +00:00
cpio_rm(v, 0, f->data + pos);
}
2017-03-09 23:52:59 +00:00
continue;
} else {
n = xcalloc(sizeof(*n), 1);
memcpy(n, f, sizeof(*f));
n->filename = strdup(f->filename + 8);
n->data = f->data;
f->data = NULL;
n->remove = 0;
fprintf(stderr, "Restore [%s] -> [%s]\n", f->filename, n->filename);
cpio_vec_insert(v, n);
2017-03-09 23:52:59 +00:00
}
}
}
// Some known stuff we can remove
2017-12-03 19:14:04 +00:00
cpio_rm(v, 0, "sbin/magic_mask.sh");
cpio_rm(v, 0, "init.magisk.rc");
cpio_rm(v, 0, "magisk");
return ret;
2017-03-09 23:52:59 +00:00
}
2017-12-03 19:14:04 +00:00
char *cpio_stocksha1(struct vector *v) {
2017-09-14 02:47:10 +00:00
cpio_entry *f;
char sha1[41];
vec_for_each(v, f) {
2017-09-15 10:03:56 +00:00
if (strcmp(f->filename, "init.magisk.rc") == 0
|| strcmp(f->filename, "overlay/init.magisk.rc") == 0) {
for (char *pos = f->data; pos < f->data + f->filesize; pos = strchr(pos + 1, '\n') + 1) {
if (memcmp(pos, "# STOCKSHA1=", 12) == 0) {
pos += 12;
memcpy(sha1, pos, 40);
sha1[40] = '\0';
2017-12-03 19:14:04 +00:00
return strdup(sha1);
}
}
} else if (strcmp(f->filename, ".backup/.sha1") == 0) {
2017-12-03 19:14:04 +00:00
return f->data;
}
}
2017-12-03 19:14:04 +00:00
return NULL;
}