From 9f6205f47feec98e23c36c34ee2dc040ad2aa481 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 15 Sep 2017 01:45:39 +0800 Subject: [PATCH] Refactor ramdisk pattern patches --- jni/Android.mk | 3 +- jni/magiskboot/boot_utils.c | 50 +++++++++++++++++ jni/magiskboot/cpio.c | 108 +++++------------------------------- jni/magiskboot/cpio.h | 8 --- jni/magiskboot/magiskboot.h | 3 + 5 files changed, 69 insertions(+), 103 deletions(-) diff --git a/jni/Android.mk b/jni/Android.mk index c9980a81c..5206eeb74 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -82,8 +82,7 @@ LOCAL_SRC_FILES := \ magiskboot/sha1.c \ magiskboot/types.c \ utils/xwrap.c \ - utils/vector.c \ - utils/list.c + utils/vector.c LOCAL_CFLAGS := -DZLIB_CONST include $(BUILD_EXECUTABLE) diff --git a/jni/magiskboot/boot_utils.c b/jni/magiskboot/boot_utils.c index 3c7cd9fe6..b003e9275 100644 --- a/jni/magiskboot/boot_utils.c +++ b/jni/magiskboot/boot_utils.c @@ -50,3 +50,53 @@ void file_align(int fd, size_t align, int out) { int open_new(const char *filename) { return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); } + +void *patch_init_rc(char *data, uint32_t *size) { + int injected = 0; + char *new_data = xmalloc(*size + 23); + char *old_data = data; + uint32_t pos = 0; + + for (char *tok = strsep(&old_data, "\n"); tok; tok = strsep(&old_data, "\n")) { + if (!injected && strncmp(tok, "import", 6) == 0) { + if (strstr(tok, "init.magisk.rc")) { + injected = 1; + } else { + fprintf(stderr, "Inject [import /init.magisk.rc] to [init.rc]\n"); + strcpy(new_data + pos, "import /init.magisk.rc\n"); + pos += 23; + injected = 1; + } + } else if (strstr(tok, "selinux.reload_policy")) { + continue; + } + // Copy the line + strcpy(new_data + pos, tok); + pos += strlen(tok); + new_data[pos++] = '\n'; + } + + *size = pos; + return new_data; +} + +int check_verity_pattern(const char *s) { + int pos = 0; + if (s[0] == ',') ++pos; + if (strncmp(s + pos, "verify", 6) != 0) return -1; + pos += 6; + if (s[pos] == '=') { + while (s[pos] != ' ' && s[pos] != '\n' && s[pos] != ',') ++pos; + } + return pos; +} + +int check_encryption_pattern(const char *s) { + const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", "fileencryptioninline", "fileencryption", NULL }; + for (int i = 0 ; encrypt_list[i]; ++i) { + int len = strlen(encrypt_list[i]); + if (strncmp(s, encrypt_list[i], len) == 0) + return len; + } + return -1; +} diff --git a/jni/magiskboot/cpio.c b/jni/magiskboot/cpio.c index 2864cabbe..a1f391911 100644 --- a/jni/magiskboot/cpio.c +++ b/jni/magiskboot/cpio.c @@ -204,98 +204,24 @@ static void cpio_test(struct vector *v) { exit(ret); } -static int check_verity_pattern(const char *s) { - int pos = 0; - if (s[0] == ',') ++pos; - if (strncmp(s + pos, "verify", 6) != 0) return -1; - pos += 6; - if (s[pos] == '=') { - while (s[pos] != ' ' && s[pos] != '\n' && s[pos] != ',') ++pos; - } - return pos; -} - -static struct list_head *block_to_list(char *data) { - struct list_head *head = xmalloc(sizeof(*head)); - line_list *line; - init_list_head(head); - char *tok; - tok = strsep(&data, "\n"); - while (tok) { - line = xcalloc(sizeof(*line), 1); - line->line = tok; - list_insert_end(head, &line->pos); - tok = strsep(&data, "\n"); - } - return head; -} - -static char *list_to_block(struct list_head *head, uint32_t filesize) { - line_list *line; - char *data = xmalloc(filesize); - uint32_t off = 0; - list_for_each(line, head, line_list, pos) { - strcpy(data + off, line->line); - off += strlen(line->line); - data[off++] = '\n'; - } - return data; -} - -static void free_newline(line_list *line) { - if (line->isNew) - free(line->line); -} - static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) { - struct list_head *head; - line_list *line; cpio_entry *f; - int skip, injected = 0; - size_t read, write; - const char *ENCRYPT_LIST[] = { "forceencrypt", "forcefdeorfbe", "fileencryptioninline", NULL }; + int skip; vec_for_each(v, f) { if (strcmp(f->filename, "init.rc") == 0) { - head = block_to_list(f->data); - list_for_each(line, head, line_list, pos) { - if (strstr(line->line, "import")) { - if (strstr(line->line, "init.magisk.rc")) - injected = 1; - if (injected) - continue; - // Inject magisk script as import - fprintf(stderr, "Inject new line [import /init.magisk.rc] in [init.rc]\n"); - line = xcalloc(sizeof(*line), 1); - line->line = strdup("import /init.magisk.rc"); - line->isNew = 1; - f->filesize += 23; - list_insert(__->prev, &line->pos); - injected = 1; - } else if (strstr(line->line, "selinux.reload_policy")) { - // Remove this line - fprintf(stderr, "Remove line [%s] in [init.rc]\n", line->line); - f->filesize -= strlen(line->line) + 1; - __ = list_pop(&line->pos); - free(line); - } - } - char *temp = list_to_block(head, f->filesize); + void *new_data = patch_init_rc(f->data, &f->filesize); free(f->data); - f->data = temp; - list_destory(head, list_head, pos, free_newline); - free(head); + f->data = new_data; } else { if (!keepverity) { if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) { - for (read = 0, write = 0; read < f->filesize; ++read, ++write) { - skip = check_verity_pattern(f->data + read); - if (skip > 0) { - fprintf(stderr, "Remove pattern [%.*s] in [%s]\n", skip, f->data + read, f->filename); - read += skip; + for (int i = 0; i < f->filesize; ++i) { + if ((skip = check_verity_pattern(f->data + i)) > 0) { + fprintf(stderr, "Remove pattern [%.*s] in [%s]\n", skip, f->data + i, f->filename); + memcpy(f->data + i, f->data + i + skip, f->filesize - i - skip); + f->filesize -= skip; } - f->data[write] = f->data[read]; } - f->filesize = write; } else if (strcmp(f->filename, "verity_key") == 0) { fprintf(stderr, "Remove [verity_key]\n"); f->remove = 1; @@ -303,19 +229,15 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) { } if (!keepforceencrypt) { if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) { - for (read = 0, write = 0; read < f->filesize; ++read, ++write) { - for (int i = 0 ; ENCRYPT_LIST[i]; ++i) { - if (strncmp(f->data + read, ENCRYPT_LIST[i], strlen(ENCRYPT_LIST[i])) == 0) { - memcpy(f->data + write, "encryptable", 11); - fprintf(stderr, "Replace [%s] with [%s] in [%s]\n", ENCRYPT_LIST[i], "encryptable", f->filename); - write += 11; - read += strlen(ENCRYPT_LIST[i]); - break; - } + for (int i = 0; i < f->filesize; ++i) { + if ((skip = check_encryption_pattern(f->data + i)) > 0) { + fprintf(stderr, "Replace pattern [%.*s] with [encryptable] in [%s]\n", skip, f->data + i, f->filename); + memcpy(f->data + i, "encryptable", 11); + // assert(skip > 11)! + memcpy(f->data + i + 11, f->data + i + skip, f->filesize - i - skip); + f->filesize = f->filesize - skip + 11; } - f->data[write] = f->data[read]; } - f->filesize = write; } } } diff --git a/jni/magiskboot/cpio.h b/jni/magiskboot/cpio.h index fe050b4f9..9a1a2b85a 100644 --- a/jni/magiskboot/cpio.h +++ b/jni/magiskboot/cpio.h @@ -3,8 +3,6 @@ #include -#include "list.h" - typedef struct cpio_entry { // uint32_t ino; uint32_t mode; @@ -24,12 +22,6 @@ typedef struct cpio_entry { int remove; } cpio_entry; -typedef struct line_list { - char *line; - int isNew; - struct list_head pos; -} line_list; - typedef struct cpio_newc_header { char magic[6]; char ino[8]; diff --git a/jni/magiskboot/magiskboot.h b/jni/magiskboot/magiskboot.h index a0d473e53..3c228f61b 100644 --- a/jni/magiskboot/magiskboot.h +++ b/jni/magiskboot/magiskboot.h @@ -39,5 +39,8 @@ extern void write_zero(int fd, size_t size); extern void mem_align(size_t *pos, size_t align); extern void file_align(int fd, size_t align, int out); extern int open_new(const char *filename); +extern void *patch_init_rc(char *data, uint32_t *size); +extern int check_verity_pattern(const char *s); +extern int check_encryption_pattern(const char *s); #endif