Fix patch verity and forceencrypt

This commit is contained in:
topjohnwu 2019-09-26 03:14:56 -04:00
parent b763b81f56
commit 3d4081d0af
3 changed files with 29 additions and 32 deletions

View File

@ -18,6 +18,6 @@ int hexpatch(const char *image, const char *from, const char *to);
int cpio_commands(int argc, char *argv[]); int cpio_commands(int argc, char *argv[]);
int dtb_commands(int argc, char *argv[]); int dtb_commands(int argc, char *argv[]);
char *patch_verity(const 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); void patch_encryption(void *&buf, uint32_t &size);
bool check_env(const char *name); bool check_env(const char *name);

View File

@ -22,20 +22,20 @@ static int check_verity_pattern(const char *s) {
} }
static int check_encryption_pattern(const char *s) { static int check_encryption_pattern(const char *s) {
const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", nullptr }; static const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe" };
for (int i = 0 ; encrypt_list[i]; ++i) { for (auto enc : encrypt_list) {
int len = strlen(encrypt_list[i]); int len = strlen(enc);
if (strncmp(s, encrypt_list[i], len) == 0) if (strncmp(s, enc, len) == 0)
return len; return len;
} }
return -1; 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<const char *>(buf); auto src = static_cast<const char *>(buf);
int src_size = size; int src_size = size;
bool found = false; bool found = false;
char patched[4096]; auto patched = (char *)(inplace ? buf : xmalloc(size));
int write = 0; int write = 0;
for (int read = 0; read < src_size; ++read, ++write) { for (int read = 0; read < src_size; ++read, ++write) {
if (int skip; (skip = check_verity_pattern(src + read)) > 0) { 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] = src[read];
} }
patched[write] = '\0'; patched[write] = '\0';
return found ? strdup(patched) : nullptr; if (!found) {
} if (!inplace)
free(patched);
void patch_encryption(void **buf, uint32_t *size) { return nullptr;
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];
} }
free(*buf); return patched;
*buf = patched;
} }
void patch_encryption(void *&buf, uint32_t &size) {
auto src = static_cast<char *>(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';
}

View File

@ -52,11 +52,7 @@ void magisk_cpio::patch() {
if (!keepverity) { if (!keepverity) {
if (fstab) { if (fstab) {
fprintf(stderr, "Found fstab file [%s]\n", cur->first.data()); fprintf(stderr, "Found fstab file [%s]\n", cur->first.data());
auto buf = patch_verity(cur->second->data, cur->second->filesize); patch_verity(cur->second->data, cur->second->filesize, true);
if (buf) {
free(cur->second->data);
cur->second->data = buf;
}
} else if (cur->first == "verity_key") { } else if (cur->first == "verity_key") {
rm(cur); rm(cur);
continue; continue;
@ -64,7 +60,7 @@ void magisk_cpio::patch() {
} }
if (!keepforceencrypt) { if (!keepforceencrypt) {
if (fstab) { if (fstab) {
patch_encryption(&cur->second->data, &cur->second->filesize); patch_encryption(cur->second->data, cur->second->filesize);
} }
} }
} }