GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: 性能优化,移除无用指令 · VVeakee/SKRoot-linuxKernelRoot@506fd1f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • patch_kernel_root
    • 3rdparty
      • aarch64_asm_helper.h
    • analyze
      • base_func.h
      • symbol_analyze.cpp
      • symbol_analyze.h
    • exe
      • patch_kernel_root.exe
    • patch_avc_denied.cpp
    • patch_avc_denied.h
    • patch_do_execve.cpp
    • patch_filldir64.cpp
    • patch_filldir64.h
    • patch_freeze_task.cpp
    • patch_kernel_root.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎patch_kernel_root/3rdparty/aarch64_asm_helper.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/3rdparty/aarch64_asm_helper.h
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ static std::pair<std::shared_ptr<char>, size_t> aarch64_asm_to_bytes(const aarch
5454
return { data_container, data_size };
5555
}
5656

57-
5857
static bool aarch64_asm_b(std::unique_ptr<asmjit::a64::Assembler>& a, int32_t b_value) {
5958
if (b_value % 4 != 0) {
6059
std::cout << "[发生错误] The B instruction offset must be a multiple of 4" << std::endl;
Collapse file

‎patch_kernel_root/analyze/base_func.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/base_func.h
-6Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ static std::string generate_random_str(std::size_t len) {
4040
return key;
4141
}
4242

43-
static std::string generate_random_root_key(int root_key_len) {
44-
std::shared_ptr<char> root_key_data(new (std::nothrow) char[root_key_len], std::default_delete<char[]>());
45-
get_rand_str(root_key_data.get(), root_key_len);
46-
std::string str_root_key(root_key_data.get(), root_key_len);
47-
return str_root_key;
48-
}
4943

5044
static auto hex2bytes(uint8_t* hex, uint8_t* str) -> void {
5145
char high, low;
Collapse file

‎patch_kernel_root/analyze/symbol_analyze.cpp‎

Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/symbol_analyze.cpp
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ bool SymbolAnalyze::find_symbol_offset() {
3333
m_kernel_sym_offset.kernel_halt = parse_symbol_region(kallsyms_matching_single("kernel_halt"));
3434
m_kernel_sym_offset.drm_dev_printk = parse_symbol_region(kallsyms_matching_single("drm_dev_printk"));
3535
m_kernel_sym_offset.dev_printk = parse_symbol_region(kallsyms_matching_single("__dev_printk"));
36-
m_kernel_sym_offset.register_die_notifier = parse_symbol_region(kallsyms_matching_single("register_die_notifier"));
37-
m_kernel_sym_offset.unregister_die_notifier = parse_symbol_region(kallsyms_matching_single("unregister_die_notifier"));
3836

3937
m_kernel_sym_offset.__do_execve_file = kallsyms_matching_single("__do_execve_file");
4038
m_kernel_sym_offset.do_execveat_common = kallsyms_matching_single("do_execveat_common");
Collapse file

‎patch_kernel_root/analyze/symbol_analyze.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/symbol_analyze.h
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ struct KernelSymbolOffset {
2020
SymbolRegion kernel_halt = { 0 };
2121
SymbolRegion drm_dev_printk = { 0 };
2222
SymbolRegion dev_printk = { 0 };
23-
SymbolRegion register_die_notifier = { 0 };
24-
SymbolRegion unregister_die_notifier = { 0 };
2523

2624
size_t __do_execve_file = 0;
2725
size_t do_execveat_common = 0;
Collapse file

‎patch_kernel_root/exe/patch_kernel_root.exe‎

Copy file name to clipboard
6.03 MB
Binary file not shown.
Collapse file

‎patch_kernel_root/patch_avc_denied.cpp‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_avc_denied.cpp
+18-48Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,17 @@ size_t PatchAvcDenied::patch_avc_denied_first_guide(const SymbolRegion& hook_fun
2424
if (hook_func_start_addr == 0) { return 0; }
2525
std::cout << "Start hooking addr: " << std::hex << hook_func_start_addr << std::endl << std::endl;
2626
size_t avc_denied_addr = m_avc_denied.offset + m_avc_denied.size - 4;
27-
2827
aarch64_asm_info asm_info = init_aarch64_asm();
2928
auto& a = asm_info.a;
3029
uint32_t sp_el0_id = SysReg::encode(3, 0, 4, 1, 0);
3130

32-
a->stp(x7, x8, ptr(sp).pre(-16));
33-
a->stp(x9, x10, ptr(sp).pre(-16));
34-
35-
36-
a->mrs(x7, sp_el0_id);
31+
a->mrs(x11, sp_el0_id);
3732
for (auto x = 0; x < task_struct_offset_cred.size(); x++) {
3833
if (x != task_struct_offset_cred.size() - 1) {
39-
a->ldr(x7, ptr(x7, task_struct_offset_cred[x]));
34+
a->ldr(x11, ptr(x11, task_struct_offset_cred[x]));
4035
}
4136
}
42-
a->ldr(x7, ptr(x7, task_struct_offset_cred.back()));
37+
a->ldr(x11, ptr(x11, task_struct_offset_cred.back()));
4338

4439
std::cout << print_aarch64_asm(asm_info) << std::endl;
4540

@@ -67,61 +62,36 @@ size_t PatchAvcDenied::patch_avc_denied_core(const SymbolRegion& hook_func_start
6762
uint64_t cap_ability_max = get_cap_ability_max();
6863
int cap_cnt = get_need_read_cap_cnt();
6964

70-
7165
aarch64_asm_info asm_info = init_aarch64_asm();
7266
auto& a = asm_info.a;
7367
Label label_end = a->newLabel();
7468
Label label_cycle_uid = a->newLabel();
7569
Label label_cycle_cap = a->newLabel();
76-
a->add(x7, x7, Imm(atomic_usage_len));
77-
a->mov(x8, Imm(8));
70+
71+
a->add(x11, x11, Imm(atomic_usage_len));
72+
a->mov(x12, Imm(8));
7873
a->bind(label_cycle_uid);
79-
a->ldr(w9, ptr(x7).post(4));
80-
a->cbnz(w9, label_end);
81-
a->subs(x8, x8, Imm(1));
74+
a->ldr(w13, ptr(x11).post(4));
75+
a->cbnz(w13, label_end);
76+
a->subs(x12, x12, Imm(1));
8277
a->b(CondCode::kNE, label_cycle_uid);
83-
a->mov(w8, Imm(0xc));
84-
a->ldr(w9, ptr(x7).post(4 + securebits_padding));
85-
a->cmp(w8, w9);
78+
a->mov(w12, Imm(0xc));
79+
a->ldr(w13, ptr(x11).post(4 + securebits_padding));
80+
a->cmp(w12, w13);
8681
a->b(CondCode::kNE, label_end);
87-
a->mov(x8, Imm(cap_ability_max));
88-
a->mov(x9, Imm(cap_cnt));
82+
a->mov(x12, Imm(cap_ability_max));
83+
a->mov(x13, Imm(cap_cnt));
8984
a->bind(label_cycle_cap);
90-
a->ldr(x10, ptr(x7).post(8));
91-
a->cmp(x10, x8);
85+
a->ldr(x14, ptr(x11).post(8));
86+
a->cmp(x14, x12);
9287
a->b(CondCode::kCC, label_end);
93-
a->subs(x9, x9, Imm(1));
88+
a->subs(x13, x13, Imm(1));
9489
a->b(CondCode::kNE, label_cycle_cap);
9590
a->mov(w0, wzr);
9691
a->bind(label_end);
97-
98-
std::cout << print_aarch64_asm(asm_info) << std::endl;
99-
100-
auto [sp_bytes, data_size] = aarch64_asm_to_bytes(asm_info);
101-
if (!sp_bytes) {
102-
return 0;
103-
}
104-
std::string str_bytes = bytes2hex((const unsigned char*)sp_bytes.get(), data_size);
105-
size_t shellcode_size = str_bytes.length() / 2;
106-
if (shellcode_size > hook_func_start_region.size) {
107-
std::cout << "[发生错误] patch_avc_denied failed: not enough kernel space." << std::endl;
108-
return 0;
109-
}
110-
vec_out_patch_bytes_data.push_back({ str_bytes, hook_func_start_addr });
111-
return shellcode_size;
112-
}
113-
114-
size_t PatchAvcDenied::patch_avc_denied_end_guide(const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data) {
115-
size_t hook_func_start_addr = hook_func_start_region.offset;
116-
if (hook_func_start_addr == 0) { return 0; }
117-
std::cout << "Start hooking addr: " << std::hex << hook_func_start_addr << std::endl << std::endl;
118-
119-
aarch64_asm_info asm_info = init_aarch64_asm();
120-
auto& a = asm_info.a;
121-
a->ldp(x9, x10, ptr(sp).post(16));
122-
a->ldp(x7, x8, ptr(sp).post(16));
12392
a->ret(x30);
12493
std::cout << print_aarch64_asm(asm_info) << std::endl;
94+
12595
auto [sp_bytes, data_size] = aarch64_asm_to_bytes(asm_info);
12696
if (!sp_bytes) {
12797
return 0;
Collapse file

‎patch_kernel_root/patch_avc_denied.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_avc_denied.h
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ class PatchAvcDenied : public PatchBase
1212
std::vector<patch_bytes_data>& vec_out_patch_bytes_data);
1313

1414
size_t patch_avc_denied_core(const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data);
15-
16-
size_t patch_avc_denied_end_guide(const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data);
1715
private:
1816
int get_need_read_cap_cnt();
1917
SymbolRegion m_avc_denied = {0};
Collapse file

‎patch_kernel_root/patch_do_execve.cpp‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_do_execve.cpp
+37-41Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,65 +79,61 @@ size_t PatchDoExecve::patch_do_execve(const SymbolRegion& hook_func_start_region
7979
Label label_cycle_name = a->newLabel();
8080
a->embed((const uint8_t*)empty_root_key_buf, sizeof(empty_root_key_buf));
8181
a->mov(x0, x0);
82-
a->stp(x7, x8, ptr(sp).pre(-16));
83-
a->stp(x9, x10, ptr(sp).pre(-16));
84-
a->stp(x11, x12, ptr(sp).pre(-16));
85-
a->mov(x7, Imm(0xFFFFFFFFFFFFF001));
86-
a->cmp(a64::x(m_reg_param.do_execve_key_reg), x7);
82+
a->mov(x11, Imm(0xFFFFFFFFFFFFF001));
83+
a->cmp(a64::x(m_reg_param.do_execve_key_reg), x11);
8784
a->b(CondCode::kCS, label_end);
8885
if (m_reg_param.is_single_filename) {
89-
a->mov(x7, a64::x(m_reg_param.do_execve_key_reg));
86+
a->mov(x11, a64::x(m_reg_param.do_execve_key_reg));
9087
} else {
91-
a->ldr(x7, ptr(a64::x(m_reg_param.do_execve_key_reg)));
88+
a->ldr(x11, ptr(a64::x(m_reg_param.do_execve_key_reg)));
9289
}
9390
int32_t key_offset = -a->offset();
94-
aarch64_asm_adr_x(a, x8, key_offset);
95-
a->mov(x9, Imm(0));
91+
aarch64_asm_adr_x(a, x12, key_offset);
92+
a->mov(x13, Imm(0));
9693
a->bind(label_cycle_name);
97-
a->ldrb(w10, ptr(x7, x9));
98-
a->ldrb(w11, ptr(x8, x9));
99-
a->cmp(w10, w11);
94+
a->ldrb(w14, ptr(x11, x13));
95+
a->ldrb(w15, ptr(x12, x13));
96+
a->cmp(w14, w15);
10097
a->b(CondCode::kNE, label_end);
101-
a->add(x9, x9, Imm(1));
102-
a->cmp(x9, Imm(ROOT_KEY_LEN));
98+
99+
a->add(x13, x13, Imm(1));
100+
a->cmp(x13, Imm(ROOT_KEY_LEN));
103101
a->b(CondCode::kLT, label_cycle_name);
104-
a->mrs(x8, sp_el0_id);
105-
a->mov(x10, x8);
102+
a->mrs(x12, sp_el0_id);
103+
a->mov(x14, x12);
106104
for (auto x = 0; x < task_struct_offset_cred.size(); x++) {
107105
if (x != task_struct_offset_cred.size() - 1) {
108-
a->ldr(x10, ptr(x10, task_struct_offset_cred[x]));
106+
a->ldr(x14, ptr(x14, task_struct_offset_cred[x]));
109107
}
110108
}
111-
a->ldr(x10, ptr(x10, task_struct_offset_cred.back()));
112-
a->add(x10, x10, Imm(atomic_usage_len));
113-
a->str(xzr, ptr(x10).post(8));
114-
a->str(xzr, ptr(x10).post(8));
115-
a->str(xzr, ptr(x10).post(8));
116-
a->str(xzr, ptr(x10).post(8));
117-
a->mov(w9, Imm(0xc));
118-
a->str(w9, ptr(x10).post(4 + securebits_padding));
119-
a->mov(x9, Imm(cap_ability_max));
120-
a->stp(x9, x9, ptr(x10).post(16));
121-
a->stp(x9, x9, ptr(x10).post(16));
109+
110+
a->ldr(x14, ptr(x14, task_struct_offset_cred.back()));
111+
a->add(x14, x14, Imm(atomic_usage_len));
112+
a->str(xzr, ptr(x14).post(8));
113+
a->str(xzr, ptr(x14).post(8));
114+
a->str(xzr, ptr(x14).post(8));
115+
a->str(xzr, ptr(x14).post(8));
116+
a->mov(w13, Imm(0xc));
117+
a->str(w13, ptr(x14).post(4 + securebits_padding));
118+
a->mov(x13, Imm(cap_ability_max));
119+
a->stp(x13, x13, ptr(x14).post(16));
120+
a->stp(x13, x13, ptr(x14).post(16));
122121
if (cap_cnt == 5) {
123-
a->str(x9, ptr(x10).post(8));
122+
a->str(x13, ptr(x14).post(8));
124123
}
125124
if (is_thread_info_in_stack) {
126-
a->mov(x9, x8);
127-
a->and_(x9, x9, Imm((uint64_t)~(0x4000 - 1)));
128-
a->ldxr(w10, ptr(x9));
129-
a->bic(w10, w10, Imm(0xFFF));
130-
a->stxr(w11, w10, ptr(x9));
125+
a->mov(x13, x12);
126+
a->and_(x13, x13, Imm((uint64_t)~(0x4000 - 1)));
127+
a->ldxr(w14, ptr(x13));
128+
a->bic(w14, w14, Imm(0xFFF));
129+
a->stxr(w15, w14, ptr(x13));
131130
} else {
132-
a->ldxr(w10, ptr(x8));
133-
a->bic(w10, w10, Imm(0xFFF));
134-
a->stxr(w11, w10, ptr(x8));
131+
a->ldxr(w14, ptr(x12));
132+
a->bic(w14, w14, Imm(0xFFF));
133+
a->stxr(w15, w14, ptr(x12));
135134
}
136-
a->str(xzr, ptr(x8, task_struct_offset_seccomp.back()));
135+
a->str(xzr, ptr(x12, task_struct_offset_seccomp.back()));
137136
a->bind(label_end);
138-
a->ldp(x11, x12, ptr(sp).post(16));
139-
a->ldp(x9, x10, ptr(sp).post(16));
140-
a->ldp(x7, x8, ptr(sp).post(16));
141137
aarch64_asm_b(a, (int32_t)(do_execve_entry_hook_jump_back_addr - (hook_func_start_addr + a->offset())));
142138
std::cout << print_aarch64_asm(asm_info) << std::endl;
143139

Collapse file

‎patch_kernel_root/patch_filldir64.cpp‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_filldir64.cpp
+11-34Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ size_t PatchFilldir64::patch_filldir64_root_key_guide(size_t root_key_mem_addr,
1414
std::cout << "Start hooking addr: " << std::hex << hook_func_start_addr << std::endl << std::endl;
1515
aarch64_asm_info asm_info = init_aarch64_asm();
1616
auto& a = asm_info.a;
17-
a->stp(x7, x8, ptr(sp).pre(-16));
18-
a->stp(x9, x10, ptr(sp).pre(-16));
1917
int root_key_adr_offset = root_key_mem_addr - (hook_func_start_addr + a->offset());
20-
aarch64_asm_adr_x(a, x7, root_key_adr_offset);
18+
aarch64_asm_adr_x(a, x11, root_key_adr_offset);
2119
std::cout << print_aarch64_asm(asm_info) << std::endl;
2220
auto [sp_bytes, data_size] = aarch64_asm_to_bytes(asm_info);
2321
if (!sp_bytes) {
@@ -38,31 +36,32 @@ size_t PatchFilldir64::patch_filldir64_core(const SymbolRegion& hook_func_start_
3836
size_t hook_func_start_addr = hook_func_start_region.offset;
3937
if (hook_func_start_addr == 0) { return 0; }
4038
std::cout << "Start hooking addr: " << std::hex << hook_func_start_addr << std::endl << std::endl;
39+
size_t filldir64_entry_hook_jump_back_addr = m_filldir64 + 4;
4140
aarch64_asm_info asm_info = init_aarch64_asm();
4241
auto& a = asm_info.a;
4342
Label label_end = a->newLabel();
4443
Label label_cycle_name = a->newLabel();
4544

4645
a->cmp(w2, Imm(16));
4746
a->b(CondCode::kNE, label_end);
48-
a->mov(x8, Imm(0));
47+
a->mov(x12, Imm(0));
4948
a->bind(label_cycle_name);
50-
a->ldrb(w9, ptr(x1, x8));
51-
a->ldrb(w10, ptr(x7, x8));
52-
a->cmp(w9, w10);
49+
a->ldrb(w13, ptr(x1, x12));
50+
a->ldrb(w14, ptr(x11, x12));
51+
a->cmp(w13, w14);
5352
a->b(CondCode::kNE, label_end);
54-
a->add(x8, x8, Imm(1));
55-
a->cmp(x8, Imm(16));
53+
a->add(x12, x12, Imm(1));
54+
a->cmp(x12, Imm(16));
5655
a->b(CondCode::kLT, label_cycle_name);
57-
a->ldp(x9, x10, ptr(sp).post(16));
58-
a->ldp(x7, x8, ptr(sp).post(16));
5956
if (m_kernel_ver_parser.is_kernel_version_less("6.1.0")) {
6057
a->mov(x0, xzr);
6158
} else {
6259
a->mov(x0, Imm(1));
6360
}
6461
a->ret(x30);
6562
a->bind(label_end);
63+
a->mov(x0, x0);
64+
aarch64_asm_b(a, (int32_t)(filldir64_entry_hook_jump_back_addr - (hook_func_start_addr + a->offset())));
6665
std::cout << print_aarch64_asm(asm_info) << std::endl;
6766
auto [sp_bytes, data_size] = aarch64_asm_to_bytes(asm_info);
6867
if (!sp_bytes) {
@@ -74,32 +73,10 @@ size_t PatchFilldir64::patch_filldir64_core(const SymbolRegion& hook_func_start_
7473
std::cout << "[发生错误] patch_filldir64 failed: not enough kernel space." << std::endl;
7574
return 0;
7675
}
77-
vec_out_patch_bytes_data.push_back({ str_bytes, hook_func_start_addr });
78-
return shellcode_size;
79-
80-
}
81-
82-
size_t PatchFilldir64::patch_filldir64_end_guide(const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data) {
83-
size_t hook_func_start_addr = hook_func_start_region.offset;
84-
if (hook_func_start_addr == 0) { return 0; }
85-
std::cout << "Start hooking addr: " << std::hex << hook_func_start_addr << std::endl << std::endl;
86-
size_t filldir64_entry_hook_jump_back_addr = m_filldir64 + 4;
87-
aarch64_asm_info asm_info = init_aarch64_asm();
88-
auto& a = asm_info.a;
89-
a->ldp(x9, x10, ptr(sp).post(16));
90-
a->ldp(x7, x8, ptr(sp).post(16));
91-
a->mov(x0, x0);
92-
aarch64_asm_b(a, (int32_t)(filldir64_entry_hook_jump_back_addr - (hook_func_start_addr + a->offset())));
93-
std::cout << print_aarch64_asm(asm_info) << std::endl;
94-
auto [sp_bytes, data_size] = aarch64_asm_to_bytes(asm_info);
95-
if (!sp_bytes) {
96-
return 0;
97-
}
98-
std::string str_bytes = bytes2hex((const unsigned char*)sp_bytes.get(), data_size);
99-
size_t shellcode_size = str_bytes.length() / 2;
10076
char hookOrigCmd[4] = { 0 };
10177
memcpy(&hookOrigCmd, (void*)((size_t)&m_file_buf[0] + m_filldir64), sizeof(hookOrigCmd));
10278
std::string strHookOrigCmd = bytes2hex((const unsigned char*)hookOrigCmd, sizeof(hookOrigCmd));
79+
10380
int end_order_len = a->offset() - 2 * 4;
10481
str_bytes = str_bytes.substr(0, (end_order_len) * 2) + strHookOrigCmd + str_bytes.substr((end_order_len + 4) * 2);
10582
if (shellcode_size > hook_func_start_region.size) {
Collapse file

‎patch_kernel_root/patch_filldir64.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_filldir64.h
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class PatchFilldir64 : public PatchBase
1010

1111
size_t patch_filldir64_root_key_guide(size_t root_key_mem_addr, const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data);
1212
size_t patch_filldir64_core(const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data);
13-
size_t patch_filldir64_end_guide(const SymbolRegion& hook_func_start_region, std::vector<patch_bytes_data>& vec_out_patch_bytes_data);
1413

1514
private:
1615
size_t m_filldir64 = 0;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.