2023-01-25 21:53:19 +08:00
|
|
|
#include "linux/fs.h"
|
|
|
|
#include "linux/moduleparam.h"
|
2022-12-09 22:03:03 +08:00
|
|
|
|
|
|
|
#include "apk_sign.h"
|
2023-01-25 22:24:00 +08:00
|
|
|
#include "klog.h" // IWYU pragma: keep
|
2023-02-01 19:48:36 +08:00
|
|
|
#include "kernel_compat.h"
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2023-01-25 21:53:19 +08:00
|
|
|
static __always_inline int
|
|
|
|
check_v2_signature(char *path, unsigned expected_size, unsigned expected_hash)
|
2022-12-27 18:21:10 +07:00
|
|
|
{
|
|
|
|
unsigned char buffer[0x11] = { 0 };
|
|
|
|
u32 size4;
|
|
|
|
u64 size8, size_of_block;
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
loff_t pos;
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
int sign = -1;
|
2023-02-20 18:30:11 +07:00
|
|
|
int i;
|
2022-12-27 18:21:10 +07:00
|
|
|
struct file *fp = filp_open(path, O_RDONLY, 0);
|
|
|
|
if (IS_ERR(fp)) {
|
|
|
|
pr_err("open %s error.", path);
|
|
|
|
return PTR_ERR(fp);
|
|
|
|
}
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2023-02-12 12:15:58 +07:00
|
|
|
// disable inotify for this file
|
|
|
|
fp->f_mode |= FMODE_NONOTIFY;
|
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
sign = 1;
|
|
|
|
// https://en.wikipedia.org/wiki/Zip_(file_format)#End_of_central_directory_record_(EOCD)
|
2023-02-20 18:30:11 +07:00
|
|
|
for (i = 0;; ++i) {
|
2022-12-27 18:21:10 +07:00
|
|
|
unsigned short n;
|
|
|
|
pos = generic_file_llseek(fp, -i - 2, SEEK_END);
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &n, 2, &pos);
|
2022-12-27 18:21:10 +07:00
|
|
|
if (n == i) {
|
|
|
|
pos -= 22;
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 4, &pos);
|
2022-12-27 18:21:10 +07:00
|
|
|
if ((size4 ^ 0xcafebabeu) == 0xccfbf1eeu) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == 0xffff) {
|
|
|
|
pr_info("error: cannot find eocd\n");
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
}
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
pos += 12;
|
|
|
|
// offset
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4, &pos);
|
2022-12-27 18:21:10 +07:00
|
|
|
pos = size4 - 0x18;
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size8, 0x8, &pos);
|
|
|
|
ksu_kernel_read_compat(fp, buffer, 0x10, &pos);
|
2022-12-27 18:21:10 +07:00
|
|
|
if (strcmp((char *)buffer, "APK Sig Block 42")) {
|
|
|
|
goto clean;
|
|
|
|
}
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
pos = size4 - (size8 + 0x8);
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size_of_block, 0x8, &pos);
|
2022-12-27 18:21:10 +07:00
|
|
|
if (size_of_block != size8) {
|
|
|
|
goto clean;
|
|
|
|
}
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
for (;;) {
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t offset;
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size8, 0x8, &pos); // sequence length
|
2022-12-27 18:21:10 +07:00
|
|
|
if (size8 == size_of_block) {
|
|
|
|
break;
|
|
|
|
}
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &id, 0x4, &pos); // id
|
2022-12-27 18:21:10 +07:00
|
|
|
offset = 4;
|
|
|
|
pr_info("id: 0x%08x\n", id);
|
|
|
|
if ((id ^ 0xdeadbeefu) == 0xafa439f5u ||
|
2023-01-25 21:53:19 +08:00
|
|
|
(id ^ 0xdeadbeefu) == 0x2efed62f) {
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4,
|
2023-01-25 21:53:19 +08:00
|
|
|
&pos); // signer-sequence length
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4, &pos); // signer length
|
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4,
|
2023-01-25 21:53:19 +08:00
|
|
|
&pos); // signed data length
|
2022-12-27 18:21:10 +07:00
|
|
|
offset += 0x4 * 3;
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4,
|
2023-01-25 21:53:19 +08:00
|
|
|
&pos); // digests-sequence length
|
2022-12-27 18:21:10 +07:00
|
|
|
pos += size4;
|
|
|
|
offset += 0x4 + size4;
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4,
|
2023-01-25 21:53:19 +08:00
|
|
|
&pos); // certificates length
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &size4, 0x4,
|
2023-01-25 21:53:19 +08:00
|
|
|
&pos); // certificate length
|
2022-12-27 18:21:10 +07:00
|
|
|
offset += 0x4 * 2;
|
2022-12-09 22:03:03 +08:00
|
|
|
#if 0
|
2023-01-15 17:05:08 +08:00
|
|
|
int hash = 1;
|
|
|
|
signed char c;
|
|
|
|
for (unsigned i = 0; i < size4; ++i) {
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &c, 0x1, &pos);
|
2023-01-15 17:05:08 +08:00
|
|
|
hash = 31 * hash + c;
|
|
|
|
}
|
|
|
|
offset += size4;
|
|
|
|
pr_info(" size: 0x%04x, hash: 0x%08x\n", size4, ((unsigned) hash) ^ 0x14131211u);
|
2022-12-09 22:03:03 +08:00
|
|
|
#else
|
2022-12-27 18:21:10 +07:00
|
|
|
if (size4 == expected_size) {
|
|
|
|
int hash = 1;
|
|
|
|
signed char c;
|
|
|
|
for (unsigned i = 0; i < size4; ++i) {
|
2023-02-20 18:51:55 +07:00
|
|
|
ksu_kernel_read_compat(fp, &c, 0x1, &pos);
|
2022-12-27 18:21:10 +07:00
|
|
|
hash = 31 * hash + c;
|
|
|
|
}
|
|
|
|
offset += size4;
|
|
|
|
if ((((unsigned)hash) ^ 0x14131211u) ==
|
2023-01-25 21:53:19 +08:00
|
|
|
expected_hash) {
|
2022-12-27 18:21:10 +07:00
|
|
|
sign = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// don't try again.
|
|
|
|
break;
|
2022-12-09 22:03:03 +08:00
|
|
|
#endif
|
2022-12-27 18:21:10 +07:00
|
|
|
}
|
|
|
|
pos += (size8 - offset);
|
|
|
|
}
|
2022-12-09 22:03:03 +08:00
|
|
|
|
|
|
|
clean:
|
2022-12-27 18:21:10 +07:00
|
|
|
filp_close(fp, 0);
|
2022-12-09 22:03:03 +08:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
return sign;
|
2022-12-09 22:03:03 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:45:34 +08:00
|
|
|
#ifdef CONFIG_KSU_DEBUG
|
|
|
|
|
|
|
|
unsigned ksu_expected_size = EXPECTED_SIZE;
|
|
|
|
unsigned ksu_expected_hash = EXPECTED_HASH;
|
|
|
|
|
2023-01-25 21:53:19 +08:00
|
|
|
#include "manager.h"
|
|
|
|
|
|
|
|
static int set_expected_size(const char *val, const struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
int rv = param_set_uint(val, kp);
|
|
|
|
ksu_invalidate_manager_uid();
|
|
|
|
pr_info("ksu_expected_size set to %x", ksu_expected_size);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_expected_hash(const char *val, const struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
int rv = param_set_uint(val, kp);
|
|
|
|
ksu_invalidate_manager_uid();
|
|
|
|
pr_info("ksu_expected_hash set to %x", ksu_expected_hash);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kernel_param_ops expected_size_ops = {
|
|
|
|
.set = set_expected_size,
|
|
|
|
.get = param_get_uint,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kernel_param_ops expected_hash_ops = {
|
|
|
|
.set = set_expected_hash,
|
|
|
|
.get = param_get_uint,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_param_cb(ksu_expected_size, &expected_size_ops, &ksu_expected_size,
|
|
|
|
S_IRUSR | S_IWUSR);
|
|
|
|
module_param_cb(ksu_expected_hash, &expected_hash_ops, &ksu_expected_hash,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2023-01-14 21:45:34 +08:00
|
|
|
|
|
|
|
int is_manager_apk(char *path)
|
|
|
|
{
|
|
|
|
return check_v2_signature(path, ksu_expected_size, ksu_expected_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
int is_manager_apk(char *path)
|
|
|
|
{
|
|
|
|
return check_v2_signature(path, EXPECTED_SIZE, EXPECTED_HASH);
|
2023-01-14 21:45:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|