2017-12-06 17:30:48 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-02-10 08:57:51 +00:00
|
|
|
#include <utils.h>
|
|
|
|
|
2018-08-09 16:40:23 +00:00
|
|
|
#include "magiskboot.h"
|
2017-12-06 17:30:48 +00:00
|
|
|
|
|
|
|
static int check_verity_pattern(const char *s) {
|
2017-12-28 20:25:03 +00: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 17:30:48 +00:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
2017-12-28 20:25:03 +00:00
|
|
|
if (s[skip] == '=') {
|
|
|
|
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',') ++skip;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
2017-12-28 20:25:03 +00:00
|
|
|
return skip;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int check_encryption_pattern(const char *s) {
|
2019-09-26 07:14:56 +00:00
|
|
|
static const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe" };
|
|
|
|
for (auto enc : encrypt_list) {
|
|
|
|
int len = strlen(enc);
|
|
|
|
if (strncmp(s, enc, len) == 0)
|
2017-12-06 17:30:48 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:14:56 +00:00
|
|
|
char *patch_verity(const void *buf, uint32_t &size, bool inplace) {
|
2019-09-21 09:30:04 +00:00
|
|
|
auto src = static_cast<const char *>(buf);
|
|
|
|
int src_size = size;
|
2019-02-25 04:09:34 +00:00
|
|
|
bool found = false;
|
2019-09-26 07:14:56 +00:00
|
|
|
auto patched = (char *)(inplace ? buf : xmalloc(size));
|
2019-09-21 09:30:04 +00:00
|
|
|
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 20:25:03 +00:00
|
|
|
read += skip;
|
2019-02-25 04:09:34 +00:00
|
|
|
found = true;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
2019-09-21 09:30:04 +00:00
|
|
|
patched[write] = src[read];
|
2017-12-31 11:30:56 +00:00
|
|
|
}
|
2019-09-21 09:30:04 +00:00
|
|
|
patched[write] = '\0';
|
2019-09-26 07:14:56 +00:00
|
|
|
if (!found) {
|
|
|
|
if (!inplace)
|
|
|
|
free(patched);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return patched;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 07:49:05 +00:00
|
|
|
void patch_encryption(void *buf, uint32_t &size) {
|
2019-09-26 07:14:56 +00:00
|
|
|
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;
|
2017-12-28 20:25:03 +00:00
|
|
|
read += skip;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
2019-09-26 07:14:56 +00:00
|
|
|
src[write] = src[read];
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
2019-09-26 07:14:56 +00:00
|
|
|
src[write] = '\0';
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|