Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
bmax committed Mar 1, 2024
1 parent 7f23aab commit c84c895
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 19 deletions.
9 changes: 6 additions & 3 deletions kernel/include/preset.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ _Static_assert(sizeof(patch_symbol_t) == PATCH_SYMBOL_LEN, "sizeof patch_symbol_
#define EXTRA_NAME_LEN 0x20
#define EXTRA_EVENT_LEN 0x20

#define EXTRA_HDR_MAGIC "kpe"

typedef int32_t extra_item_type;

#define EXTRA_TYPE_NONE 0
Expand Down Expand Up @@ -170,12 +172,13 @@ struct _patch_extra_item
{
struct
{
extra_item_type type;
char name[EXTRA_NAME_LEN];
char event[EXTRA_EVENT_LEN];
char magic[4];
int32_t priority;
int32_t args_size;
int32_t con_size;
extra_item_type type;
char name[EXTRA_NAME_LEN];
char event[EXTRA_EVENT_LEN];
};
char _cap[PATCH_EXTRA_ITEM_LEN];
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 2 additions & 7 deletions kpm-demo/shmem/main.c → kpm-demo/stat-sel-fs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
#include <kputils.h>
#include <linux/string.h>

KPM_NAME("kpm-shmem");
KPM_NAME("stat-sel-fs");
KPM_VERSION("1.0.0");
KPM_LICENSE("GPL v2");
KPM_AUTHOR("bmax121");
KPM_DESCRIPTION("Share memory between processes");

/*
* This module's main functionality is to map any address of any process to any other process.
* Of course, this means you can easily manipulate data of other processes.
*/
KPM_DESCRIPTION("Modify the attribute of selinux-fs");

static long init(const char *args, const char *event, void *__user reserved)
{
Expand Down
239 changes: 239 additions & 0 deletions preset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2023 bmax121. All Rights Reserved.
*/

#ifndef _KP_PRESET_H_
#define _KP_PRESET_H_

#ifndef __ASSEMBLY__
#include <stdint.h>
#endif

#define KP_MAGIC "KP1158"
#define MAGIC_LEN 0x8
#define KP_HEADER_SIZE 0x40
#define SUPER_KEY_LEN 0x40
#define HDR_BACKUP_SIZE 0x8
#define COMPILE_TIME_LEN 0x18
#define MAP_MAX_SIZE 0xa00
#define HOOK_ALLOC_SIZE (1 << 20)
#define MEMORY_ROX_SIZE (2 << 20)
#define MEMORY_RW_SIZE (2 << 20)
#define MAP_ALIGN 0x10

#define CONFIG_DEBUG 0x1
#define CONFIG_ANDROID 0x2

#define MAP_SYMBOL_NUM (5)
#define MAP_SYMBOL_SIZE (MAP_SYMBOL_NUM * 8)

#define PATCH_SYMBOL_LEN (512)

#define ADDITIONAL_LEN (512)

#define PATCH_EXTRA_ITEM_LEN (128)

#define VERSION(major, minor, patch) (((major) << 16) + ((minor) << 8) + (patch))

#ifndef __ASSEMBLY__
typedef struct version_t
{
uint8_t _;
uint8_t patch;
uint8_t minor;
uint8_t major;
} version_t;
#endif

#ifndef __ASSEMBLY__

typedef uint64_t config_t;

typedef struct _setup_header_t // 64-bytes
{
union
{
struct
{
char magic[MAGIC_LEN]; //
version_t kp_version;
uint32_t _;
config_t config_flags;
char compile_time[COMPILE_TIME_LEN];
};
char _cap[64];
};
} setup_header_t;

_Static_assert(sizeof(setup_header_t) == KP_HEADER_SIZE, "sizeof setup_header_t mismatch");

#else
#define header_magic_offset 0
#define header_kp_version_offset (MAGIC_LEN)
#define header_config_flags (header_kp_version_offset + 4 + 4)
#define header_compile_time_offset (header_config_flags + 8)
#endif

#ifndef __ASSEMBLY__
struct map_symbol
{
union
{
struct
{
uint64_t memblock_reserve_relo;
uint64_t memblock_free_relo;
uint64_t memblock_phys_alloc_relo;
uint64_t memblock_virt_alloc_relo;
uint64_t memblock_mark_nomap_relo;
};
char _cap[MAP_SYMBOL_SIZE];
};
};
typedef struct map_symbol map_symbol_t;
_Static_assert(sizeof(map_symbol_t) == MAP_SYMBOL_SIZE, "sizeof map_symbol_t mismatch");
#endif

#ifndef __ASSEMBLY__
struct patch_symbol
{
union
{
struct
{
uint64_t kallsyms_lookup_name;
uint64_t printk;
uint64_t vm_area_add_early;

uint64_t panic;
uint64_t rest_init;
uint64_t cgroup_init;
uint64_t kernel_init;
uint64_t report_cfi_failure;
uint64_t __cfi_slowpath_diag;
uint64_t __cfi_slowpath;
uint64_t copy_process;
uint64_t cgroup_post_fork;
uint64_t avc_denied;
uint64_t slow_avc_audit;
uint64_t input_handle_event;
};
char _cap[PATCH_SYMBOL_LEN];
};
};
typedef struct patch_symbol patch_symbol_t;
_Static_assert(sizeof(patch_symbol_t) == PATCH_SYMBOL_LEN, "sizeof patch_symbol_t mismatch");
#endif

#ifndef __ASSEMBLY__

#define EXTRA_ALIGN 0x10
#define EXTRA_NAME_LEN 0x20
#define EXTRA_EVENT_LEN 0x20

typedef int32_t extra_item_type;

#define EXTRA_TYPE_NONE 0
#define EXTRA_TYPE_KPM 1
#define EXTRA_TYPE_SHELL 2
#define EXTRA_TYPE_EXEC 3
#define EXTRA_TYPE_RAW 4
#define EXTRA_TYPE_ANDROID_RC 5

#define EXTRA_TYPE_NONE_STR "none"
#define EXTRA_TYPE_KPM_STR "kpm"
#define EXTRA_TYPE_SHELL_STR "shell"
#define EXTRA_TYPE_EXEC_STR "exec"
#define EXTRA_TYPE_RAW_STR "raw"
#define EXTRA_TYPE_ANDROID_RC_STR "android_rc"

// todo
#define EXTRA_EVENT_PAGING_INIT "paging-init"

#define EXTRA_EVENT_PRE_KERNEL_INIT "pre-kernel-init"
#define EXTRA_EVENT_KPM_DEFAULT EXTRA_EVENT_PRE_KERNEL_INIT
#define EXTRA_EVENT_POST_KERNEL_INIT "post-kernel-init"

#define EXTRA_EVENT_PRE_FIRST_STAGE "pre-init-first-stage"
#define EXTRA_EVENT_POST_FIRST_STAGE "post-init-first-stage"

#define EXTRA_EVENT_PRE_EXEC_INIT "pre-exec-init"
#define EXTRA_EVENT_POST_EXEC_INIT "post-exec-init"

#define EXTRA_EVENT_PRE_SECOND_STAGE "pre-init-second-stage"
#define EXTRA_EVENT_POST_SECOND_STAGE "post-init-second-stage"

struct _patch_extra_item
{
union
{
struct
{
extra_item_type type;
char name[EXTRA_NAME_LEN];
char event[EXTRA_EVENT_LEN];
int32_t priority;
int32_t args_size;
int32_t con_size;
};
char _cap[PATCH_EXTRA_ITEM_LEN];
};
};
typedef struct _patch_extra_item patch_extra_item_t;
_Static_assert(sizeof(patch_extra_item_t) == PATCH_EXTRA_ITEM_LEN, "sizeof patch_extra_item_t mismatch");
#endif

#ifndef __ASSEMBLY__
typedef struct _setup_preset_t
{
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_t;
#else
#define setup_kernel_version_offset 0
#define setup_kimg_size_offset (setup_kernel_version_offset + 8)
#define setup_kpimg_size_offset (setup_kimg_size_offset + 8)
#define setup_kernel_size_offset (setup_kpimg_size_offset + 8)
#define setup_page_shift_offset (setup_kernel_size_offset + 8)
#define setup_setup_offset_offset (setup_page_shift_offset + 8)
#define setup_start_offset_offset (setup_setup_offset_offset + 8)
#define setup_extra_size_offset (setup_start_offset_offset + 8)
#define setup_map_offset_offset (setup_extra_size_offset + 8)
#define setup_map_max_size_offset (setup_map_offset_offset + 8)
#define setup_kallsyms_lookup_name_offset_offset (setup_map_max_size_offset + 8)
#define setup_paging_init_offset_offset (setup_kallsyms_lookup_name_offset_offset + 8)
#define setup_printk_offset_offset (setup_paging_init_offset_offset + 8)
#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_end (setup_patch_symbol_offset + PATCH_SYMBOL_LEN)
#endif

#ifndef __ASSEMBLY__
typedef struct
{
setup_header_t header;
setup_preset_t setup;
} preset_t;
#endif

#endif // _KP_PRESET_H_
22 changes: 14 additions & 8 deletions tools/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,20 @@ int parse_image_patch_info(const char *kimg, int kimg_len, patched_kimg_t *pimg)

// extra
int extra_offset = align_kimg_len + old_preset->setup.kpimg_size;
int extra_size = old_preset->setup.extra_size;
if (extra_offset > kimg_len) tools_loge_exit("kpimg length mismatch\n");
if (extra_offset == kimg_len) return 0;

const char *item_addr = kimg + extra_offset;
int extra_size = old_preset->setup.extra_size;
const char *item_pos = kimg + extra_offset;

while (item_addr < item_addr + extra_size) {
patch_extra_item_t *item = (patch_extra_item_t *)item_addr;
while (item_pos < kimg + extra_offset + extra_size) {
patch_extra_item_t *item = (patch_extra_item_t *)item_pos;
if (strcmp(EXTRA_HDR_MAGIC, item->magic)) break;
if (item->type == EXTRA_TYPE_NONE) break;
pimg->embed_item[pimg->embed_item_num++] = item;
item_addr += sizeof(patch_extra_item_t);
item_addr += item->args_size;
item_addr += item->con_size;
item_pos += sizeof(patch_extra_item_t);
item_pos += item->args_size;
item_pos += item->con_size;
}

return 0;
Expand Down Expand Up @@ -200,11 +203,13 @@ int print_image_patch_info(patched_kimg_t *pimg)

if (pimg->banner[strlen(pimg->banner) - 1] != '\n') fprintf(stdout, "\n");
fprintf(stdout, "patched=%s\n", preset ? "true" : "false");
fprintf(stdout, "extra_num=%d\n", pimg->embed_item_num);

if (preset) {
print_preset_info(preset);

fprintf(stdout, INFO_EXTRA_SESSION "\n");
fprintf(stdout, "num=%d\n", pimg->embed_item_num);

for (int i = 0; i < pimg->embed_item_num; i++) {
patch_extra_item_t *item = pimg->embed_item[i];
const char *type = extra_type_str(item->type);
Expand Down Expand Up @@ -356,6 +361,7 @@ int patch_update_img(const char *kimg_path, const char *kpimg_path, const char *
}
}
if (!item) tools_loge_exit("empty extra item\n");
strcpy(item->magic, EXTRA_HDR_MAGIC);
config->item = item;
item->type = config->extra_type;
if (config->set_args) item->args_size = align_ceil(strlen(config->set_args), EXTRA_ALIGN);
Expand Down
2 changes: 1 addition & 1 deletion tools/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define INFO_KERNEL_IMG_SESSION "[kernel]"
#define INFO_KP_IMG_SESSION "[kpimg]"
#define INFO_ADDITIONAL_SESSION "[additional]"
#define INFO_EXTRA_SESSION "[extra]"
#define INFO_EXTRA_SESSION "[extras]"
#define INFO_EXTRA_SESSION_N "[extra %d]"

#define EXTRA_ITEM_MAX_NUM 32
Expand Down

0 comments on commit c84c895

Please sign in to comment.