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 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);

View File

@ -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<const char *>(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<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 (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);
}
}
}