2017-12-06 18:30:48 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include <utils.hpp>
|
2019-02-10 09:57:51 +01:00
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include "magiskboot.hpp"
|
2017-12-06 18:30:48 +01:00
|
|
|
|
2019-12-03 11:39:39 +01:00
|
|
|
#define MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
|
|
|
|
|
2017-12-06 18:30:48 +01:00
|
|
|
static int check_verity_pattern(const char *s) {
|
2019-12-03 11:39:39 +01:00
|
|
|
int skip = s[0] == ',';
|
|
|
|
|
|
|
|
if (0) {}
|
2020-03-22 16:55:01 +01:00
|
|
|
MATCH("verifyatboot");
|
2019-12-03 11:39:39 +01:00
|
|
|
MATCH("verify");
|
2020-03-22 16:55:01 +01:00
|
|
|
MATCH("avb_keys");
|
2019-12-03 11:39:39 +01:00
|
|
|
MATCH("avb");
|
|
|
|
MATCH("support_scfs");
|
2020-03-22 16:55:01 +01:00
|
|
|
MATCH("fsverity");
|
2019-12-03 11:39:39 +01:00
|
|
|
else return -1;
|
2017-12-06 18:30:48 +01:00
|
|
|
|
2017-12-28 21:25:03 +01:00
|
|
|
if (s[skip] == '=') {
|
2019-12-02 10:34:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-03 11:39:39 +01:00
|
|
|
#undef MATCH
|
|
|
|
#define MATCH(p) else if (strncmp(s, p, sizeof(p) - 1) == 0) return (sizeof(p) - 1)
|
|
|
|
|
2017-12-06 18:30:48 +01:00
|
|
|
static int check_encryption_pattern(const char *s) {
|
2019-12-03 11:39:39 +01:00
|
|
|
if (0) {}
|
|
|
|
MATCH("forceencrypt");
|
|
|
|
MATCH("forcefdeorfbe");
|
|
|
|
MATCH("fileencryption");
|
|
|
|
else return -1;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 10:03:09 +02:00
|
|
|
static uint32_t remove_pattern(void *buf, uint32_t size, int(*pattern_skip)(const char *)) {
|
|
|
|
auto src = static_cast<char *>(buf);
|
|
|
|
int orig_sz = size;
|
2019-09-21 11:30:04 +02:00
|
|
|
int write = 0;
|
2020-05-05 10:03:09 +02:00
|
|
|
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);
|
2019-09-21 11:30:04 +02:00
|
|
|
size -= skip;
|
2017-12-28 21:25:03 +01:00
|
|
|
read += skip;
|
2019-12-02 10:34:21 +01:00
|
|
|
} else {
|
2020-05-05 10:03:09 +02:00
|
|
|
src[write++] = src[read++];
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2017-12-31 12:30:56 +01:00
|
|
|
}
|
2020-05-05 10:03:09 +02:00
|
|
|
memset(src + write, 0, orig_sz - write);
|
|
|
|
return size;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 10:03:09 +02:00
|
|
|
uint32_t patch_verity(void *buf, uint32_t size) {
|
|
|
|
return remove_pattern(buf, size, check_verity_pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t patch_encryption(void *buf, uint32_t size) {
|
|
|
|
return remove_pattern(buf, size, check_encryption_pattern);
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|