diff --git a/native/jni/magiskboot/magiskboot.h b/native/jni/magiskboot/magiskboot.h index f51c7e8e3..59bab4439 100644 --- a/native/jni/magiskboot/magiskboot.h +++ b/native/jni/magiskboot/magiskboot.h @@ -18,6 +18,6 @@ int hexpatch(const char *image, const char *from, const char *to); int cpio_commands(int argc, char *argv[]); int dtb_commands(int argc, char *argv[]); -char *patch_verity(const void *buf, uint32_t &size); -void patch_encryption(void **buf, uint32_t *size); +char *patch_verity(const void *buf, uint32_t &size, bool inplace = false); +void patch_encryption(void *&buf, uint32_t &size); bool check_env(const char *name); diff --git a/native/jni/magiskboot/pattern.cpp b/native/jni/magiskboot/pattern.cpp index 0630b4794..5df10356d 100644 --- a/native/jni/magiskboot/pattern.cpp +++ b/native/jni/magiskboot/pattern.cpp @@ -22,20 +22,20 @@ static int check_verity_pattern(const char *s) { } static int check_encryption_pattern(const char *s) { - const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", nullptr }; - for (int i = 0 ; encrypt_list[i]; ++i) { - int len = strlen(encrypt_list[i]); - if (strncmp(s, encrypt_list[i], len) == 0) + static const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe" }; + for (auto enc : encrypt_list) { + int len = strlen(enc); + if (strncmp(s, enc, len) == 0) return len; } return -1; } -char *patch_verity(const void *buf, uint32_t &size) { +char *patch_verity(const void *buf, uint32_t &size, bool inplace) { auto src = static_cast(buf); int src_size = size; bool found = false; - char patched[4096]; + auto patched = (char *)(inplace ? buf : xmalloc(size)); int write = 0; for (int read = 0; read < src_size; ++read, ++write) { if (int skip; (skip = check_verity_pattern(src + read)) > 0) { @@ -47,24 +47,25 @@ char *patch_verity(const void *buf, uint32_t &size) { patched[write] = src[read]; } patched[write] = '\0'; - return found ? strdup(patched) : nullptr; -} - -void patch_encryption(void **buf, uint32_t *size) { - int skip, src_size = *size; - char *src = (char *) *buf, *patched = (char *) xcalloc(src_size, 1); - for (int read = 0, write = 0; read < src_size; ++read, ++write) { - if ((skip = check_encryption_pattern(src + read)) > 0) { - fprintf(stderr, "Replace pattern [%.*s] with [encryptable]\n", skip, src + read); - memcpy(patched + read, "encryptable", 11); - read += skip; - write += 11; - *size -= (skip - 11); - } - patched[write] = src[read]; + if (!found) { + if (!inplace) + free(patched); + return nullptr; } - free(*buf); - *buf = patched; + return patched; } - +void patch_encryption(void *&buf, uint32_t &size) { + auto src = static_cast(buf); + int src_size = size; + int write = 0; + for (int read = 0; read < src_size; ++read, ++write) { + if (int skip; (skip = check_encryption_pattern(src + read)) > 0) { + fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read); + size -= skip; + read += skip; + } + src[write] = src[read]; + } + src[write] = '\0'; +} diff --git a/native/jni/magiskboot/ramdisk.cpp b/native/jni/magiskboot/ramdisk.cpp index e0898118e..157ae5ac4 100644 --- a/native/jni/magiskboot/ramdisk.cpp +++ b/native/jni/magiskboot/ramdisk.cpp @@ -52,11 +52,7 @@ void magisk_cpio::patch() { if (!keepverity) { if (fstab) { fprintf(stderr, "Found fstab file [%s]\n", cur->first.data()); - auto buf = patch_verity(cur->second->data, cur->second->filesize); - if (buf) { - free(cur->second->data); - cur->second->data = buf; - } + patch_verity(cur->second->data, cur->second->filesize, true); } else if (cur->first == "verity_key") { rm(cur); continue; @@ -64,7 +60,7 @@ void magisk_cpio::patch() { } if (!keepforceencrypt) { if (fstab) { - patch_encryption(&cur->second->data, &cur->second->filesize); + patch_encryption(cur->second->data, cur->second->filesize); } } }