Skip to content

Commit

Permalink
rename patch_symbol to patch_config
Browse files Browse the repository at this point in the history
  • Loading branch information
bmax committed Sep 22, 2024
1 parent b6c45ae commit 925f287
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 44 deletions.
14 changes: 6 additions & 8 deletions kernel/base/predata.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <common.h>
#include <log.h>
#include <sha256.h>
#include <symbol.h>

#include "start.h"
#include "pgtable.h"
Expand All @@ -16,7 +17,9 @@ extern start_preset_t start_preset;

static char *superkey = 0;
static char *root_superkey = 0;
static struct patch_symbol *patch_symbol = 0;

struct patch_config *patch_config = 0;
KP_EXPORT_SYMBOL(patch_config);

static const char bstr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

Expand Down Expand Up @@ -71,11 +74,6 @@ const char *get_superkey()
return superkey;
}

struct patch_symbol *get_preset_patch_sym()
{
return patch_symbol;
}

int on_each_extra_item(int (*callback)(const patch_extra_item_t *extra, const char *arg, const void *con, void *udata),
void *udata)
{
Expand Down Expand Up @@ -128,9 +126,9 @@ void predata_init()
}
log_boot("gen rand key: %s\n", superkey);

patch_symbol = &start_preset.patch_symbol;
patch_config = &start_preset.patch_config;

for (uintptr_t addr = (uint64_t)patch_symbol; addr < (uintptr_t)patch_symbol + PATCH_SYMBOL_LEN;
for (uintptr_t addr = (uint64_t)patch_config; addr < (uintptr_t)patch_config + PATCH_CONFIG_LEN;
addr += sizeof(uintptr_t)) {
uintptr_t *p = (uintptr_t *)addr;
if (*p) *p += kernel_va;
Expand Down
8 changes: 4 additions & 4 deletions kernel/base/setup1.S
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ start_prepare:
mov x2, #ROOT_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
mov x2, #PATCH_SYMBOL_LEN
// memcpy(&start_preset.patch_config, &setup_preset.patch_config, sizeof(header.patch_config));
add x0, x11, #start_patch_config_offset;
add x1, x10, #setup_patch_config_offset
mov x2, #PATCH_CONFIG_LEN
bl memcpy8

// backup map area
Expand Down
6 changes: 3 additions & 3 deletions kernel/base/start.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct
uint8_t map_backup[MAP_MAX_SIZE];
uint8_t superkey[SUPER_KEY_LEN];
uint8_t root_superkey[ROOT_SUPER_KEY_HASH_LEN];
patch_symbol_t patch_symbol;
patch_config_t patch_config;
} start_preset_t;
#else
#define start_header_offset 0
Expand All @@ -39,8 +39,8 @@ typedef struct
#define start_map_backup_offset (start_map_backup_len_offset + 8)
#define start_superkey_offset (start_map_backup_offset + MAP_MAX_SIZE)
#define start_root_superkey_offset (start_superkey_offset + SUPER_KEY_LEN)
#define start_patch_symbol_offset (start_root_superkey_offset + ROOT_SUPER_KEY_HASH_LEN)
#define start_patch_extra_offset_offset (start_patch_symbol_offset + PATCH_SYMBOL_LEN)
#define start_patch_config_offset (start_root_superkey_offset + ROOT_SUPER_KEY_HASH_LEN)
#define start_patch_extra_offset_offset (start_patch_config_offset + PATCH_CONFIG_LEN)
#define start_patch_extra_size_offset (start_patch_extra_offset_offset + 8)
#define start_end (start_patch_extra_size_offset + 8)
#endif
Expand Down
5 changes: 2 additions & 3 deletions kernel/include/predata.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
#include <ktypes.h>
#include <preset.h>

extern struct patch_config *patch_config;

int auth_superkey(const char *key);
void reset_superkey(const char *key);
void enable_auth_root_key(bool enable);
const char *get_superkey();

uint64_t rand_next();
uint64_t get_build_config();
struct patch_symbol *get_preset_patch_sym();

int on_each_extra_item(int (*callback)(const patch_extra_item_t *extra, const char *arg, const void *data, void *udata),
void *udata);
Expand Down
18 changes: 9 additions & 9 deletions kernel/include/preset.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define MAP_SYMBOL_NUM (5)
#define MAP_SYMBOL_SIZE (MAP_SYMBOL_NUM * 8)

#define PATCH_SYMBOL_LEN (512)
#define PATCH_CONFIG_LEN (512)

#define ADDITIONAL_LEN (512)

Expand Down Expand Up @@ -98,7 +98,7 @@ _Static_assert(sizeof(map_symbol_t) == MAP_SYMBOL_SIZE, "sizeof map_symbol_t mis
#endif

#ifndef __ASSEMBLY__
struct patch_symbol
struct patch_config
{
union
{
Expand All @@ -120,11 +120,11 @@ struct patch_symbol
uint64_t slow_avc_audit;
uint64_t input_handle_event;
};
char _cap[PATCH_SYMBOL_LEN];
char _cap[PATCH_CONFIG_LEN];
};
};
typedef struct patch_symbol patch_symbol_t;
_Static_assert(sizeof(patch_symbol_t) == PATCH_SYMBOL_LEN, "sizeof patch_symbol_t mismatch");
typedef struct patch_config patch_config_t;
_Static_assert(sizeof(patch_config_t) == PATCH_CONFIG_LEN, "sizeof patch_config_t mismatch");
#endif

#ifndef __ASSEMBLY__
Expand Down Expand Up @@ -210,7 +210,7 @@ typedef struct
map_symbol_t map_symbol;
uint8_t header_backup[HDR_BACKUP_SIZE];
uint8_t superkey[SUPER_KEY_LEN];
patch_symbol_t patch_symbol;
patch_config_t patch_config;
char additional[ADDITIONAL_LEN];
} setup_preset_be_000a04_t;

Expand All @@ -235,7 +235,7 @@ typedef struct _setup_preset_t
uint8_t superkey[SUPER_KEY_LEN];
uint8_t root_superkey[ROOT_SUPER_KEY_HASH_LEN];
uint8_t __[SETUP_PRESERVE_LEN];
patch_symbol_t patch_symbol;
patch_config_t patch_config;
char additional[ADDITIONAL_LEN];
} setup_preset_t;
#else
Expand All @@ -256,8 +256,8 @@ typedef struct _setup_preset_t
#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_root_superkey_offset (setup_superkey_offset + SUPER_KEY_LEN)
#define setup_patch_symbol_offset (setup_root_superkey_offset + ROOT_SUPER_KEY_HASH_LEN + SETUP_PRESERVE_LEN)
#define setup_end (setup_patch_symbol_offset + PATCH_SYMBOL_LEN)
#define setup_patch_config_offset (setup_root_superkey_offset + ROOT_SUPER_KEY_HASH_LEN + SETUP_PRESERVE_LEN)
#define setup_end (setup_patch_config_offset + PATCH_CONFIG_LEN)
#endif

#ifndef __ASSEMBLY__
Expand Down
2 changes: 1 addition & 1 deletion kernel/patch/android/userd.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ int android_user_init()
log_boot("hook __NR_openat rc: %d\n", rc);
ret |= rc;

unsigned long input_handle_event_addr = get_preset_patch_sym()->input_handle_event;
unsigned long input_handle_event_addr = patch_config->input_handle_event;
if (input_handle_event_addr) {
rc = hook_wrap4((void *)input_handle_event_addr, before_input_handle_event, 0, 0);
ret |= rc;
Expand Down
4 changes: 2 additions & 2 deletions kernel/patch/common/accctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ static int slow_avc_audit_replace(struct selinux_state *_state, void *_ssid, voi

int bypass_selinux()
{
unsigned long avc_denied_addr = get_preset_patch_sym()->avc_denied;
unsigned long avc_denied_addr = patch_config->avc_denied;
if (avc_denied_addr) {
hook_err_t err = hook((void *)avc_denied_addr, (void *)avc_denied_replace, (void **)&avc_denied_backup);
if (err != HOOK_NO_ERR) {
log_boot("hook avc_denied_addr: %llx, error: %d\n", avc_denied_addr, err);
}
}

unsigned long slow_avc_audit_addr = get_preset_patch_sym()->slow_avc_audit;
unsigned long slow_avc_audit_addr = patch_config->slow_avc_audit;
if (slow_avc_audit_addr) {
hook_err_t err =
hook((void *)slow_avc_audit_addr, (void *)slow_avc_audit_replace, (void **)&slow_avc_audit_backup);
Expand Down
6 changes: 3 additions & 3 deletions kernel/patch/common/secpass.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int bypass_kcfi()

// 6.1.0
// todo: Is there more elegant way?
unsigned long report_cfi_failure_addr = get_preset_patch_sym()->report_cfi_failure;
unsigned long report_cfi_failure_addr = patch_config->report_cfi_failure;
if (report_cfi_failure_addr) {
hook_err_t err = hook((void *)report_cfi_failure_addr, (void *)replace_report_cfi_failure,
(void **)&backup_report_cfi_failure);
Expand All @@ -64,9 +64,9 @@ int bypass_kcfi()
}

// todo: direct modify cfi_shadow, __cfi_check?
unsigned long __cfi_slowpath_addr = get_preset_patch_sym()->__cfi_slowpath_diag;
unsigned long __cfi_slowpath_addr = patch_config->__cfi_slowpath_diag;
if (!__cfi_slowpath_addr) {
__cfi_slowpath_addr = get_preset_patch_sym()->__cfi_slowpath;
__cfi_slowpath_addr = patch_config->__cfi_slowpath;
}
if (__cfi_slowpath_addr) {
hook_err_t err =
Expand Down
4 changes: 2 additions & 2 deletions kernel/patch/common/selinuxhook.c.bak
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ static int slow_avc_audit_replace(struct selinux_state *_state, void *_ssid, voi

int selinux_hook_install()
{
unsigned long avc_denied_addr = get_preset_patch_sym()->avc_denied;
unsigned long avc_denied_addr = patch_config->avc_denied;
if (avc_denied_addr) {
hook_err_t err = hook((void *)avc_denied_addr, (void *)avc_denied_replace, (void **)&avc_denied_backup);
if (err != HOOK_NO_ERR) {
log_boot("hook avc_denied_addr: %llx, error: %d\n", avc_denied_addr, err);
}
}

unsigned long slow_avc_audit_addr = get_preset_patch_sym()->slow_avc_audit;
unsigned long slow_avc_audit_addr = patch_config->slow_avc_audit;
if (slow_avc_audit_addr) {
hook_err_t err =
hook((void *)slow_avc_audit_addr, (void *)slow_avc_audit_replace, (void **)&slow_avc_audit_backup);
Expand Down
4 changes: 2 additions & 2 deletions kernel/patch/common/taskob.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ int task_observer()

prepare_init_ext(init_task);

unsigned long copy_process_addr = get_preset_patch_sym()->copy_process;
unsigned long copy_process_addr = patch_config->copy_process;
if (copy_process_addr) {
rc |= hook_wrap8((void *)copy_process_addr, 0, after_copy_process, 0);
log_boot("hook copy_process: %llx, rc: %d\n", copy_process_addr, rc);
} else {
unsigned long cgroup_post_fork_addr = get_preset_patch_sym()->cgroup_post_fork;
unsigned long cgroup_post_fork_addr = patch_config->cgroup_post_fork;
if (cgroup_post_fork_addr) {
rc |= hook_wrap4((void *)cgroup_post_fork_addr, 0, after_cgroup_post_fork, 0);
log_boot("hook cgroup_post_fork: %llx, rc: %d\n", cgroup_post_fork_addr, rc);
Expand Down
8 changes: 4 additions & 4 deletions kernel/patch/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int patch()

hook_err_t rc = 0;

unsigned long panic_addr = get_preset_patch_sym()->panic;
unsigned long panic_addr = patch_config->panic;
logkd("panic addr: %llx\n", panic_addr);
if (panic_addr) {
rc = hook_wrap12((void *)panic_addr, before_panic, 0, 0);
Expand All @@ -134,16 +134,16 @@ int patch()
if (rc) return rc;

// rest_init or cgroup_init
unsigned long init_addr = get_preset_patch_sym()->rest_init;
if (!init_addr) init_addr = get_preset_patch_sym()->cgroup_init;
unsigned long init_addr = patch_config->rest_init;
if (!init_addr) init_addr = patch_config->cgroup_init;
if (init_addr) {
rc = hook_wrap4((void *)init_addr, before_rest_init, 0, (void *)init_addr);
log_boot("hook rest_init rc: %d\n", rc);
}
if (rc) return rc;

// kernel_init
unsigned long kernel_init_addr = get_preset_patch_sym()->kernel_init;
unsigned long kernel_init_addr = patch_config->kernel_init;
if (kernel_init_addr) {
rc = hook_wrap4((void *)kernel_init_addr, before_kernel_init, after_kernel_init, 0);
log_boot("hook kernel_init rc: %d\n", rc);
Expand Down
2 changes: 1 addition & 1 deletion tools/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ int patch_update_img(const char *kimg_path, const char *kpimg_path, const char *
memcpy(setup->header_backup, kallsym_kimg, sizeof(setup->header_backup));

// start symbol
fillin_patch_symbol(&kallsym, kallsym_kimg, ori_kimg_len, &setup->patch_symbol, kinfo->is_be, 0);
fillin_patch_config(&kallsym, kallsym_kimg, ori_kimg_len, &setup->patch_config, kinfo->is_be, 0);

// superkey
if (!root_key) {
Expand Down
2 changes: 1 addition & 1 deletion tools/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static int get_cand_arr_symbol_offset_zero(kallsym_t *kallsym, char *img_buf, ch
return offset;
}

int fillin_patch_symbol(kallsym_t *kallsym, char *img_buf, int imglen, patch_symbol_t *symbol, int32_t target_is_be,
int fillin_patch_config(kallsym_t *kallsym, char *img_buf, int imglen, patch_config_t *symbol, int32_t target_is_be,
bool is_android)
{
symbol->panic = get_symbol_offset_zero(kallsym, img_buf, "panic");
Expand Down
2 changes: 1 addition & 1 deletion tools/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int32_t get_symbol_offset_exit(kallsym_t *info, char *img, char *symbol);
int32_t find_suffixed_symbol(kallsym_t *kallsym, char *img_buf, const char *symbol);
void select_map_area(kallsym_t *kallsym, char *image_buf, int32_t *map_start, int32_t *max_size);
int fillin_map_symbol(kallsym_t *kallsym, char *img_buf, map_symbol_t *symbol, int32_t target_is_be);
int fillin_patch_symbol(kallsym_t *kallsym, char *img_buf, int imglen, patch_symbol_t *symbol, int32_t target_is_be,
int fillin_patch_config(kallsym_t *kallsym, char *img_buf, int imglen, patch_config_t *symbol, int32_t target_is_be,
bool is_android);

#endif

0 comments on commit 925f287

Please sign in to comment.