Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
bmax committed Mar 26, 2024
1 parent 6242623 commit e292f98
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 156 deletions.
6 changes: 6 additions & 0 deletions kernel/base/setup1.S
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ start_prepare:
mov x2, #SUPER_KEY_LEN
bl memcpy8

// memcpy(start_preset.superkey_hash, setup_preset.superkey_hash, SUPER_KEY_HASH_LEN);
add x0, x11, #start_superkey_hash_offset;
add x1, x10, #setup_superkey_hash_offset
mov x2, #SUPER_KEY_HASH_LEN
bl memcpy8

// memcpy(&start_preset.patch_symbol, &setup_preset.patch_symbol, sizeof(header.patch_symbol));
add x0, x11, #start_patch_symbol_offset;
add x1, x10, #setup_patch_symbol_offset
Expand Down
4 changes: 3 additions & 1 deletion kernel/base/start.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct
int64_t map_backup_len;
uint8_t map_backup[MAP_MAX_SIZE];
uint8_t superkey[SUPER_KEY_LEN];
uint8_t superkey_hash[SUPER_KEY_HASH_LEN];
patch_symbol_t patch_symbol;
} start_preset_t;
#else
Expand All @@ -37,7 +38,8 @@ typedef struct
#define start_map_backup_len_offset (start_map_offset_offset + 8)
#define start_map_backup_offset (start_map_backup_len_offset + 8)
#define start_superkey_offset (start_map_backup_offset + MAP_MAX_SIZE)
#define start_patch_symbol_offset (start_superkey_offset + SUPER_KEY_LEN)
#define start_superkey_hash_offset (start_superkey_offset + SUPER_KEY_LEN)
#define start_patch_symbol_offset (start_superkey_hash_offset + SUPER_KEY_HASH_LEN)
#define start_patch_extra_offset_offset (start_patch_symbol_offset + PATCH_SYMBOL_LEN)
#define start_patch_extra_size_offset (start_patch_extra_offset_offset + 8)
#define start_end (start_patch_extra_size_offset + 8)
Expand Down
32 changes: 31 additions & 1 deletion kernel/include/preset.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define MAGIC_LEN 0x8
#define KP_HEADER_SIZE 0x40
#define SUPER_KEY_LEN 0x40
#define SUPER_KEY_HASH_LEN 0x10
#define SETUP_PRESERVE_LEN 0x30
#define HDR_BACKUP_SIZE 0x8
#define COMPILE_TIME_LEN 0x18
#define MAP_MAX_SIZE 0xa00
Expand Down Expand Up @@ -188,6 +190,31 @@ _Static_assert(sizeof(patch_extra_item_t) == PATCH_EXTRA_ITEM_LEN, "sizeof patch
#endif

#ifndef __ASSEMBLY__

// TODO: remove
typedef struct
{
version_t kernel_version;
int32_t _;
int64_t kimg_size; // must aligned
int64_t kpimg_size; // must aligned
int64_t kernel_size; // must aligned
int64_t page_shift;
int64_t setup_offset; // must aligned
int64_t start_offset; // must aligned
int64_t extra_size; // must aligned
int64_t map_offset; // must aligned MAP_ALIGN
int64_t map_max_size;
int64_t kallsyms_lookup_name_offset;
int64_t paging_init_offset;
int64_t printk_offset;
map_symbol_t map_symbol;
uint8_t header_backup[HDR_BACKUP_SIZE];
uint8_t superkey[SUPER_KEY_LEN];
patch_symbol_t patch_symbol;
char additional[ADDITIONAL_LEN];
} setup_preset_be_000a04_t;

typedef struct _setup_preset_t
{
version_t kernel_version;
Expand All @@ -207,6 +234,8 @@ typedef struct _setup_preset_t
map_symbol_t map_symbol;
uint8_t header_backup[HDR_BACKUP_SIZE];
uint8_t superkey[SUPER_KEY_LEN];
uint8_t superkey_hash[SUPER_KEY_HASH_LEN];
uint8_t __[SETUP_PRESERVE_LEN];
patch_symbol_t patch_symbol;
char additional[ADDITIONAL_LEN];
} setup_preset_t;
Expand All @@ -227,7 +256,8 @@ typedef struct _setup_preset_t
#define setup_map_symbol_offset (setup_printk_offset_offset + 8)
#define setup_header_backup_offset (setup_map_symbol_offset + MAP_SYMBOL_SIZE)
#define setup_superkey_offset (setup_header_backup_offset + HDR_BACKUP_SIZE)
#define setup_patch_symbol_offset (setup_superkey_offset + SUPER_KEY_LEN)
#define setup_superkey_hash_offset (setup_superkey_offset + SUPER_KEY_LEN)
#define setup_patch_symbol_offset (setup_superkey_hash_offset + SUPER_KEY_HASH_LEN + SETUP_PRESERVE_LEN)
#define setup_end (setup_patch_symbol_offset + PATCH_SYMBOL_LEN)
#endif

Expand Down
50 changes: 0 additions & 50 deletions kernel/linux/include/linux/seq_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,4 @@ extern int kfunc_def(seq_buf_putmem)(struct seq_buf *s, const void *mem, unsigne
extern int kfunc_def(seq_buf_putmem_hex)(struct seq_buf *s, const void *mem, unsigned int len);
extern int kfunc_def(seq_buf_bitmask)(struct seq_buf *s, const unsigned long *maskp, int nmaskbits);

// todo:
static inline int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
{
if (!kfunc(seq_buf_printf)) {
kfunc_not_found();
return 0;
}
va_list args;
va_start(args, fmt);
int rc = kfunc(seq_buf_printf)(s, fmt, args);
va_end(args);
return rc;
}
static inline int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
{
kfunc_call(seq_buf_to_user, s, ubuf, cnt);
kfunc_not_found();
return 0;
}
static inline int seq_buf_puts(struct seq_buf *s, const char *str)
{
kfunc_call(seq_buf_puts, s, str);
kfunc_not_found();
return 0;
}
static inline int seq_buf_putc(struct seq_buf *s, unsigned char c)
{
kfunc_call(seq_buf_putc, s, c);
kfunc_not_found();
return 0;
}
static inline int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
{
kfunc_call(seq_buf_putmem, s, mem, len);
kfunc_not_found();
return 0;
}
static inline int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, unsigned int len)
{
kfunc_call(seq_buf_putmem_hex, s, mem, len);
kfunc_not_found();
return 0;
}
static inline int seq_buf_bitmask(struct seq_buf *s, const unsigned long *maskp, int nmaskbits)
{
kfunc_call(seq_buf_bitmask, s, maskp, nmaskbits);
kfunc_not_found();
return 0;
}

#endif
49 changes: 0 additions & 49 deletions kernel/linux/include/linux/trace_seq.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,4 @@ extern int kfunc_def(trace_seq_putmem)(struct trace_seq *s, const void *mem, uns
extern int kfunc_def(trace_seq_putmem_hex)(struct trace_seq *s, const void *mem, unsigned int len);
extern int kfunc_def(trace_seq_bitmask)(struct trace_seq *s, const unsigned long *maskp, int nmaskbits);

static inline int trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
{
if (!kfunc(trace_seq_printf)) {
kfunc_not_found();
return 0;
}
va_list args;
va_start(args, fmt);
int rc = kfunc(trace_seq_printf)(s, fmt, args);
va_end(args);
return rc;
}
static inline int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt)
{
kfunc_call(trace_seq_to_user, s, ubuf, cnt);
kfunc_not_found();
return 0;
}
static inline int trace_seq_puts(struct trace_seq *s, const char *str)
{
kfunc_call(trace_seq_puts, s, str);
kfunc_not_found();
return 0;
}
static inline int trace_seq_putc(struct trace_seq *s, unsigned char c)
{
kfunc_call(trace_seq_putc, s, c);
kfunc_not_found();
return 0;
}
static inline int trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
{
kfunc_call(trace_seq_putmem, s, mem, len);
kfunc_not_found();
return 0;
}
static inline int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, unsigned int len)
{
kfunc_call(trace_seq_putmem_hex, s, mem, len);
kfunc_not_found();
return 0;
}
static inline int trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp, int nmaskbits)
{
kfunc_call(trace_seq_bitmask, s, maskp, nmaskbits);
kfunc_not_found();
return 0;
}

#endif
39 changes: 21 additions & 18 deletions kernel/patch/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
#include <linux/err.h>
#include <linux/errno.h>

int kfunc_def(xt_data_to_user)(void __user *dst, const void *src, int usersize, int size, int aligned_size);
extern int kfunc_def(xt_data_to_user)(void __user *dst, const void *src, int usersize, int size, int aligned_size);

static inline int compat_xt_data_copy_to_user(void __user *dst, const void *src, int size)
{
kfunc_direct_call(xt_data_to_user, dst, src, size, size, size);
}

// todo: static method
int kfunc_def(bits_to_user)(unsigned long *bits, unsigned int maxbit, unsigned int maxlen, void __user *p, int compat);
extern int kfunc_def(bits_to_user)(unsigned long *bits, unsigned int maxbit, unsigned int maxlen, void __user *p,
int compat);

static inline int compat_bits_copy_to_user(void __user *dst, const void *src, int size)
{
kfunc_direct_call(bits_to_user, src, size * sizeof(long), size, dst, 0);
kfunc_direct_call(bits_to_user, (unsigned long *)src, size * sizeof(unsigned long), size, dst, 0);
}

// todo: n > page_size
Expand All @@ -45,7 +46,7 @@ int trace_seq_copy_to_user(void __user *to, const void *from, int n)
*pfull = 0;
if (n > page_size) return 0;
memcpy(pbuffer, from, n);
int sz = trace_seq_to_user(trace_seq, to, n);
int sz = kfunc(trace_seq_to_user)(trace_seq, to, n);
return sz;
}

Expand All @@ -56,29 +57,31 @@ int seq_buf_copy_to_user(void __user *to, const void *from, int n)
seq_buf.len = n;
seq_buf.readpos = 0;
seq_buf.buffer = (void *)from;
return seq_buf_to_user(&seq_buf, to, n);
return kfunc(seq_buf_to_user)(&seq_buf, to, n);
}

// return copied length
int __must_check compat_copy_to_user(void __user *to, const void *from, int n)
{
int copy_len;
int copy_len = 0;
if (kfunc(seq_buf_to_user)) {
copy_len = seq_buf_copy_to_user((void *__user)to, from, n);
} else if (kfunc(bits_to_user)) {
// bits_to_user, str_to_user
// int ret = compat_bits_to_user(to, from, n);
// if (ret == n) return -EFAULT;
// copy_len -= ret;
} else if (kfunc(xt_data_to_user)) {
// xt_data_to_user, xt_obj_to_user
// int ret = compat_xt_data_copy_to_user(to, from, n);
// if (ret == n) return -EFAULT;
// copy_len -= ret;
} else {
// }
// else if (kfunc(bits_to_user)) {
// // bits_to_user, str_to_user
// int ret = compat_bits_copy_to_user(to, from, n);
// if (ret == n) return -EFAULT;
// copy_len = n - ret;
// } else if (kfunc(xt_data_to_user)) {
// // xt_data_to_user, xt_obj_to_user
// int ret = compat_xt_data_copy_to_user(to, from, n);
// if (ret == n) return -EFAULT;
// copy_len = n - ret;
} else if (kfunc(trace_seq_to_user)) {
copy_len = trace_seq_copy_to_user((void *__user)to, from, n);
} else {
// alt: copy_arg_to_user,
}
// alt: copy_arg_to_user,
return copy_len;
}
KP_EXPORT_SYMBOL(compat_copy_to_user);
Expand Down
6 changes: 3 additions & 3 deletions kernel/patch/include/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest

/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
typedef uint8_t BYTE; // 8-bit byte
typedef uint32_t WORD; // 32-bit word, change to "long" for 16-bit machines

typedef struct
{
BYTE data[64];
WORD datalen;
unsigned long long bitlen;
unsigned long bitlen;
WORD state[8];
} SHA256_CTX;

Expand Down
2 changes: 1 addition & 1 deletion kernel/patch/include/uapi/scdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct su_profile
#define APATCH_FLODER "/data/adb/ap/"
#define APATCH_BIN_FLODER "/data/adb/ap/bin/"
#define APATCH_LOG_FLODER "/data/adb/ap/log/"
#define SAFE_MODE_FLAG_FILE "/dev/.sefemode"
#define SAFE_MODE_FLAG_FILE "/dev/.safemode"
#define EARLY_INIT_LOG_0 "/dev/early_init_0.log"
#define EARLY_INIT_LOG_1 "/dev/early_init_1.log"

Expand Down
20 changes: 5 additions & 15 deletions kernel/patch/ksyms/libs.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,38 +207,29 @@ static void _linux_lib_argv_split_sym_match(const char *name, unsigned long addr
#include <linux/trace_seq.h>

int kfunc_def(seq_buf_printf)(struct seq_buf *s, const char *fmt, ...) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_printf));
int kfunc_def(seq_buf_to_user)(struct seq_buf *s, char __user *ubuf, int cnt) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_to_user));
int kfunc_def(seq_buf_puts)(struct seq_buf *s, const char *str) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_puts));
int kfunc_def(seq_buf_putc)(struct seq_buf *s, unsigned char c) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_putc));
int kfunc_def(seq_buf_putmem)(struct seq_buf *s, const void *mem, unsigned int len) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_putmem));
int kfunc_def(seq_buf_putmem_hex)(struct seq_buf *s, const void *mem, unsigned int len) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_putmem_hex));
int kfunc_def(seq_buf_bitmask)(struct seq_buf *s, const unsigned long *maskp, int nmaskbits) = 0;
KP_EXPORT_SYMBOL(kfunc(seq_buf_bitmask));

int kfunc_def(trace_seq_printf)(struct trace_seq *s, const char *fmt, ...) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_printf));
int kfunc_def(trace_seq_to_user)(struct trace_seq *s, char __user *ubuf, int cnt) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_to_user));
int kfunc_def(trace_seq_puts)(struct trace_seq *s, const char *str) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_puts));
int kfunc_def(trace_seq_putc)(struct trace_seq *s, unsigned char c) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_putc));
int kfunc_def(trace_seq_putmem)(struct trace_seq *s, const void *mem, unsigned int len) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_putmem));
int kfunc_def(trace_seq_putmem_hex)(struct trace_seq *s, const void *mem, unsigned int len) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_putmem_hex));
int kfunc_def(trace_seq_bitmask)(struct trace_seq *s, const unsigned long *maskp, int nmaskbits) = 0;
KP_EXPORT_SYMBOL(kfunc(trace_seq_bitmask));

int kfunc_def(xt_data_to_user)(void __user *dst, const void *src, int usersize, int size, int aligned_size) = 0;
int kfunc_def(bits_to_user)(unsigned long *bits, unsigned int maxbit, unsigned int maxlen, void __user *p,
int compat) = 0;

static void _linux_lib_seq_buf_sym_match(const char *name, unsigned long addr)
{
kfunc_match(seq_buf_to_user, name, addr);
kfunc_match(trace_seq_to_user, name, addr);
if (kfunc(seq_buf_to_user)) {
kfunc_match(seq_buf_printf, name, addr);
kfunc_match(seq_buf_puts, name, addr);
Expand All @@ -248,7 +239,6 @@ static void _linux_lib_seq_buf_sym_match(const char *name, unsigned long addr)
// kfunc_match(seq_buf_bitmask, name, addr);
} else {
kfunc_match(trace_seq_printf, name, addr);
kfunc_match(trace_seq_to_user, name, addr);
kfunc_match(trace_seq_puts, name, addr);
// kfunc_match(trace_seq_putc, name, addr);
kfunc_match(trace_seq_putmem, name, addr);
Expand Down
15 changes: 8 additions & 7 deletions kernel/patch/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ int patch()
ret |= rc;
}

// kernel_init
unsigned long kernel_init_addr = get_preset_patch_sym()->kernel_init;
if (kernel_init_addr) {
hook_err_t rc = hook_wrap4((void *)kernel_init_addr, before_kernel_init, after_kernel_init, 0);
log_boot("hook rc: %d\n", rc);
ret |= rc;
}
log_boot("kernel init: %llx\n", get_preset_patch_sym()->kernel_init - kernel_va);
// // kernel_init
// unsigned long kernel_init_addr = get_preset_patch_sym()->kernel_init;
// if (kernel_init_addr) {
// hook_err_t rc = hook_wrap4((void *)kernel_init_addr, before_kernel_init, after_kernel_init, 0);
// log_boot("hook rc: %d\n", rc);
// ret |= rc;
// }

return ret;
}
Loading

0 comments on commit e292f98

Please sign in to comment.