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
|
|
|
|
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) {}
|
|
|
|
MATCH("verify");
|
|
|
|
MATCH("avb");
|
|
|
|
MATCH("support_scfs");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-26 09:14:56 +02:00
|
|
|
char *patch_verity(const void *buf, uint32_t &size, bool inplace) {
|
2019-09-21 11:30:04 +02:00
|
|
|
auto src = static_cast<const char *>(buf);
|
2019-12-02 10:34:21 +01:00
|
|
|
auto dest = (char *)(inplace ? buf : xmalloc(size));
|
2019-09-21 11:30:04 +02:00
|
|
|
int src_size = size;
|
2019-02-25 05:09:34 +01:00
|
|
|
bool found = false;
|
2019-09-21 11:30:04 +02:00
|
|
|
int write = 0;
|
2019-12-02 10:34:21 +01:00
|
|
|
for (int read = 0; read < src_size;) {
|
2019-09-21 11:30:04 +02:00
|
|
|
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;
|
2019-12-02 10:34:21 +01:00
|
|
|
} else {
|
|
|
|
dest[write++] = src[read++];
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2017-12-31 12:30:56 +01:00
|
|
|
}
|
2019-12-02 10:34:21 +01:00
|
|
|
dest[write] = '\0';
|
2019-09-26 09:14:56 +02:00
|
|
|
if (!found) {
|
|
|
|
if (!inplace)
|
2019-12-02 10:34:21 +01:00
|
|
|
free(dest);
|
2019-09-26 09:14:56 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-02 10:34:21 +01:00
|
|
|
return dest;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2019-09-26 09:49:05 +02:00
|
|
|
void patch_encryption(void *buf, uint32_t &size) {
|
2019-09-26 09:14:56 +02: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 21:25:03 +01:00
|
|
|
read += skip;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2019-09-26 09:14:56 +02:00
|
|
|
src[write] = src[read];
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
2019-09-26 09:14:56 +02:00
|
|
|
src[write] = '\0';
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|