mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-01-11 23:43:36 +00:00
Refactor ramdisk pattern patches
This commit is contained in:
parent
8b2ec23a89
commit
9f6205f47f
@ -82,8 +82,7 @@ LOCAL_SRC_FILES := \
|
|||||||
magiskboot/sha1.c \
|
magiskboot/sha1.c \
|
||||||
magiskboot/types.c \
|
magiskboot/types.c \
|
||||||
utils/xwrap.c \
|
utils/xwrap.c \
|
||||||
utils/vector.c \
|
utils/vector.c
|
||||||
utils/list.c
|
|
||||||
LOCAL_CFLAGS := -DZLIB_CONST
|
LOCAL_CFLAGS := -DZLIB_CONST
|
||||||
include $(BUILD_EXECUTABLE)
|
include $(BUILD_EXECUTABLE)
|
||||||
|
|
||||||
|
@ -50,3 +50,53 @@ void file_align(int fd, size_t align, int out) {
|
|||||||
int open_new(const char *filename) {
|
int open_new(const char *filename) {
|
||||||
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
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;
|
||||||
|
}
|
||||||
|
@ -204,98 +204,24 @@ static void cpio_test(struct vector *v) {
|
|||||||
exit(ret);
|
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) {
|
static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
|
||||||
struct list_head *head;
|
|
||||||
line_list *line;
|
|
||||||
cpio_entry *f;
|
cpio_entry *f;
|
||||||
int skip, injected = 0;
|
int skip;
|
||||||
size_t read, write;
|
|
||||||
const char *ENCRYPT_LIST[] = { "forceencrypt", "forcefdeorfbe", "fileencryptioninline", NULL };
|
|
||||||
vec_for_each(v, f) {
|
vec_for_each(v, f) {
|
||||||
if (strcmp(f->filename, "init.rc") == 0) {
|
if (strcmp(f->filename, "init.rc") == 0) {
|
||||||
head = block_to_list(f->data);
|
void *new_data = patch_init_rc(f->data, &f->filesize);
|
||||||
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);
|
|
||||||
free(f->data);
|
free(f->data);
|
||||||
f->data = temp;
|
f->data = new_data;
|
||||||
list_destory(head, list_head, pos, free_newline);
|
|
||||||
free(head);
|
|
||||||
} else {
|
} else {
|
||||||
if (!keepverity) {
|
if (!keepverity) {
|
||||||
if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) {
|
if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) {
|
||||||
for (read = 0, write = 0; read < f->filesize; ++read, ++write) {
|
for (int i = 0; i < f->filesize; ++i) {
|
||||||
skip = check_verity_pattern(f->data + read);
|
if ((skip = check_verity_pattern(f->data + i)) > 0) {
|
||||||
if (skip > 0) {
|
fprintf(stderr, "Remove pattern [%.*s] in [%s]\n", skip, f->data + i, f->filename);
|
||||||
fprintf(stderr, "Remove pattern [%.*s] in [%s]\n", skip, f->data + read, f->filename);
|
memcpy(f->data + i, f->data + i + skip, f->filesize - i - skip);
|
||||||
read += skip;
|
f->filesize -= skip;
|
||||||
}
|
}
|
||||||
f->data[write] = f->data[read];
|
|
||||||
}
|
}
|
||||||
f->filesize = write;
|
|
||||||
} else if (strcmp(f->filename, "verity_key") == 0) {
|
} else if (strcmp(f->filename, "verity_key") == 0) {
|
||||||
fprintf(stderr, "Remove [verity_key]\n");
|
fprintf(stderr, "Remove [verity_key]\n");
|
||||||
f->remove = 1;
|
f->remove = 1;
|
||||||
@ -303,19 +229,15 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
|
|||||||
}
|
}
|
||||||
if (!keepforceencrypt) {
|
if (!keepforceencrypt) {
|
||||||
if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) {
|
if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) {
|
||||||
for (read = 0, write = 0; read < f->filesize; ++read, ++write) {
|
for (int i = 0; i < f->filesize; ++i) {
|
||||||
for (int i = 0 ; ENCRYPT_LIST[i]; ++i) {
|
if ((skip = check_encryption_pattern(f->data + i)) > 0) {
|
||||||
if (strncmp(f->data + read, ENCRYPT_LIST[i], strlen(ENCRYPT_LIST[i])) == 0) {
|
fprintf(stderr, "Replace pattern [%.*s] with [encryptable] in [%s]\n", skip, f->data + i, f->filename);
|
||||||
memcpy(f->data + write, "encryptable", 11);
|
memcpy(f->data + i, "encryptable", 11);
|
||||||
fprintf(stderr, "Replace [%s] with [%s] in [%s]\n", ENCRYPT_LIST[i], "encryptable", f->filename);
|
// assert(skip > 11)!
|
||||||
write += 11;
|
memcpy(f->data + i + 11, f->data + i + skip, f->filesize - i - skip);
|
||||||
read += strlen(ENCRYPT_LIST[i]);
|
f->filesize = f->filesize - skip + 11;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f->data[write] = f->data[read];
|
|
||||||
}
|
|
||||||
f->filesize = write;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "list.h"
|
|
||||||
|
|
||||||
typedef struct cpio_entry {
|
typedef struct cpio_entry {
|
||||||
// uint32_t ino;
|
// uint32_t ino;
|
||||||
uint32_t mode;
|
uint32_t mode;
|
||||||
@ -24,12 +22,6 @@ typedef struct cpio_entry {
|
|||||||
int remove;
|
int remove;
|
||||||
} cpio_entry;
|
} cpio_entry;
|
||||||
|
|
||||||
typedef struct line_list {
|
|
||||||
char *line;
|
|
||||||
int isNew;
|
|
||||||
struct list_head pos;
|
|
||||||
} line_list;
|
|
||||||
|
|
||||||
typedef struct cpio_newc_header {
|
typedef struct cpio_newc_header {
|
||||||
char magic[6];
|
char magic[6];
|
||||||
char ino[8];
|
char ino[8];
|
||||||
|
@ -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 mem_align(size_t *pos, size_t align);
|
||||||
extern void file_align(int fd, size_t align, int out);
|
extern void file_align(int fd, size_t align, int out);
|
||||||
extern int open_new(const char *filename);
|
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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user