2017-12-06 17:30:48 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <utils.hpp>
|
2019-02-10 08:57:51 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include "magiskboot.hpp"
|
2017-12-06 17:30:48 +00:00
|
|
|
|
2021-03-03 04:37:37 +00:00
|
|
|
#define CHECKED_MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
|
2019-12-03 10:39:39 +00:00
|
|
|
|
2017-12-06 17:30:48 +00:00
|
|
|
static int check_verity_pattern(const char *s) {
|
2020-12-31 06:11:24 +00:00
|
|
|
int skip = s[0] == ',';
|
|
|
|
|
|
|
|
if (0) {}
|
2021-03-03 04:37:37 +00:00
|
|
|
CHECKED_MATCH("verifyatboot");
|
|
|
|
CHECKED_MATCH("verify");
|
|
|
|
CHECKED_MATCH("avb_keys");
|
|
|
|
CHECKED_MATCH("avb");
|
|
|
|
CHECKED_MATCH("support_scfs");
|
|
|
|
CHECKED_MATCH("fsverity");
|
2020-12-31 06:11:24 +00:00
|
|
|
else return -1;
|
|
|
|
|
|
|
|
if (s[skip] == '=') {
|
|
|
|
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',')
|
|
|
|
++skip;
|
|
|
|
}
|
|
|
|
return skip;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 04:37:37 +00:00
|
|
|
#undef CHECKED_MATCH
|
|
|
|
#define CHECKED_MATCH(p) else if (strncmp(s, p, sizeof(p) - 1) == 0) return (sizeof(p) - 1)
|
2019-12-03 10:39:39 +00:00
|
|
|
|
2017-12-06 17:30:48 +00:00
|
|
|
static int check_encryption_pattern(const char *s) {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (0) {}
|
2021-03-03 04:37:37 +00:00
|
|
|
CHECKED_MATCH("forceencrypt");
|
|
|
|
CHECKED_MATCH("forcefdeorfbe");
|
|
|
|
CHECKED_MATCH("fileencryption");
|
2020-12-31 06:11:24 +00:00
|
|
|
else return -1;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 08:03:09 +00:00
|
|
|
static uint32_t remove_pattern(void *buf, uint32_t size, int(*pattern_skip)(const char *)) {
|
2020-12-31 06:11:24 +00:00
|
|
|
auto src = static_cast<char *>(buf);
|
|
|
|
int orig_sz = size;
|
|
|
|
int write = 0;
|
|
|
|
for (int read = 0; read < orig_sz;) {
|
|
|
|
if (int skip = pattern_skip(src + read); skip > 0) {
|
|
|
|
fprintf(stderr, "Remove pattern [%.*s]\n", skip, src + read);
|
|
|
|
size -= skip;
|
|
|
|
read += skip;
|
|
|
|
} else {
|
|
|
|
src[write++] = src[read++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(src + write, 0, orig_sz - write);
|
|
|
|
return size;
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 08:03:09 +00:00
|
|
|
uint32_t patch_verity(void *buf, uint32_t size) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return remove_pattern(buf, size, check_verity_pattern);
|
2020-05-05 08:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t patch_encryption(void *buf, uint32_t size) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return remove_pattern(buf, size, check_encryption_pattern);
|
2017-12-06 17:30:48 +00:00
|
|
|
}
|