2017-12-06 18:30:48 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-02-10 09:57:51 +01:00
|
|
|
#include <utils.h>
|
|
|
|
|
2018-08-09 18:40:23 +02:00
|
|
|
#include "magiskboot.h"
|
2017-12-06 18:30:48 +01:00
|
|
|
|
|
|
|
static int check_verity_pattern(const char *s) {
|
2017-12-28 21:25:03 +01:00
|
|
|
int skip = 0;
|
|
|
|
if (s[0] == ',') ++skip;
|
|
|
|
if (strncmp(s + skip, "verify", 6) == 0)
|
|
|
|
skip += 6;
|
|
|
|
else if (strncmp(s + skip, "avb", 3) == 0)
|
|
|
|
skip += 3;
|
2017-12-06 18:30:48 +01:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
2017-12-28 21:25:03 +01:00
|
|
|
if (s[skip] == '=') {
|
|
|
|
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',') ++skip;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2017-12-28 21:25:03 +01:00
|
|
|
return skip;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int check_encryption_pattern(const char *s) {
|
2019-09-21 11:30:04 +02:00
|
|
|
const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", nullptr };
|
2017-12-06 18:30:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-21 11:30:04 +02:00
|
|
|
char *patch_verity(const void *buf, uint32_t &size) {
|
|
|
|
auto src = static_cast<const char *>(buf);
|
|
|
|
int src_size = size;
|
2019-02-25 05:09:34 +01:00
|
|
|
bool found = false;
|
2019-09-21 11:30:04 +02:00
|
|
|
char patched[4096];
|
|
|
|
int write = 0;
|
|
|
|
for (int read = 0; read < src_size; ++read, ++write) {
|
|
|
|
if (int skip; (skip = check_verity_pattern(src + read)) > 0) {
|
|
|
|
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
|
|
|
|
size -= skip;
|
2017-12-28 21:25:03 +01:00
|
|
|
read += skip;
|
2019-02-25 05:09:34 +01:00
|
|
|
found = true;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2019-09-21 11:30:04 +02:00
|
|
|
patched[write] = src[read];
|
2017-12-31 12:30:56 +01:00
|
|
|
}
|
2019-09-21 11:30:04 +02:00
|
|
|
patched[write] = '\0';
|
|
|
|
return found ? strdup(patched) : nullptr;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 20:21:13 +01:00
|
|
|
void patch_encryption(void **buf, uint32_t *size) {
|
2017-12-28 21:25:03 +01:00
|
|
|
int skip, src_size = *size;
|
2018-10-25 03:08:06 +02:00
|
|
|
char *src = (char *) *buf, *patched = (char *) xcalloc(src_size, 1);
|
2017-12-28 21:25:03 +01:00
|
|
|
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;
|
2017-12-06 18:30:48 +01:00
|
|
|
*size -= (skip - 11);
|
|
|
|
}
|
2017-12-28 21:25:03 +01:00
|
|
|
patched[write] = src[read];
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2017-12-28 21:25:03 +01:00
|
|
|
free(*buf);
|
|
|
|
*buf = patched;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|