1
mirror of https://github.com/topjohnwu/Magisk synced 2024-11-14 22:28:37 +01:00

Proper pattern matching

Fix #3998
This commit is contained in:
topjohnwu 2021-03-09 04:08:16 -08:00
parent 48bde7375f
commit 1ff45ac5f5

View File

@ -1,44 +1,47 @@
#include <malloc.h>
#include <string.h>
#include <utils.hpp>
#include "magiskboot.hpp"
#define CHECKED_MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
#define MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
static int check_verity_pattern(const char *s) {
static int skip_verity_pattern(const char *s) {
int skip = s[0] == ',';
if (0) {}
CHECKED_MATCH("verifyatboot");
CHECKED_MATCH("verify");
CHECKED_MATCH("avb_keys");
CHECKED_MATCH("avb");
CHECKED_MATCH("support_scfs");
CHECKED_MATCH("fsverity");
MATCH("verifyatboot");
MATCH("verify");
MATCH("avb_keys");
MATCH("avb");
MATCH("support_scfs");
MATCH("fsverity");
else return -1;
if (s[skip] == '=') {
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',')
while (!strchr(" \n,", s[skip]))
++skip;
}
return skip;
}
#undef CHECKED_MATCH
#define CHECKED_MATCH(p) else if (strncmp(s, p, sizeof(p) - 1) == 0) return (sizeof(p) - 1)
static int skip_encryption_pattern(const char *s) {
int skip = s[0] == ',';
static int check_encryption_pattern(const char *s) {
if (0) {}
CHECKED_MATCH("forceencrypt");
CHECKED_MATCH("forcefdeorfbe");
CHECKED_MATCH("fileencryption");
MATCH("forceencrypt");
MATCH("forcefdeorfbe");
MATCH("fileencryption");
else return -1;
if (s[skip] == '=') {
while (!strchr(" \n,", s[skip]))
++skip;
}
return skip;
}
static uint32_t remove_pattern(void *buf, uint32_t size, int(*pattern_skip)(const char *)) {
auto src = static_cast<char *>(buf);
static uint32_t remove_pattern(char *src, uint32_t size, int(*pattern_skip)(const char *)) {
int orig_sz = size;
int write = 0;
for (int read = 0; read < orig_sz;) {
@ -55,9 +58,9 @@ static uint32_t remove_pattern(void *buf, uint32_t size, int(*pattern_skip)(cons
}
uint32_t patch_verity(void *buf, uint32_t size) {
return remove_pattern(buf, size, check_verity_pattern);
return remove_pattern(static_cast<char *>(buf), size, skip_verity_pattern);
}
uint32_t patch_encryption(void *buf, uint32_t size) {
return remove_pattern(buf, size, check_encryption_pattern);
return remove_pattern(static_cast<char *>(buf), size, skip_encryption_pattern);
}