Skip to content

Commit 6184331

Browse files
处理了uprobe在map文件中的特征
1 parent 79ba058 commit 6184331

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
1313

1414
**set_fun_info**函数用于在so文件的指定偏移处设置uprobe挂载点,并可传递指定函数名。
1515

16-
**set_fun_info2**函数用于在set_fun_info函数设置成功但失效的情况下,与set_fun_info函数作用一致
16+
**set_fun_info2**函数用于在set_fun_info函数设置成功但失效的情况下,与set_fun_info函数作用一致
1717

1818
**clear_all_uprobes**函数用于清除所有的uprobe挂载点。
1919

2020
上述函数的返回结果有SET_TRACE_SUCCESS、SET_TRACE_ERROR两种,分别表示设置成功和失败。
2121

22+
# 使用示例
23+
编程思路可以参考[示例](https://github.com/AndroidReverser-Test/Il2cppTraceModule/blob/main/app/src/main/cpp/il2cpp_trace.cpp)
24+
25+
# 支持的内核版本
26+
目前只在5.10以及5.15两个版本通过测试,理论上5.10以上版本都能正常使用。
27+
2228
# 一些疑惑
2329
set_fun_info2函数其实就是将传入的函数偏移-0x1000再传递到内核,为什么要这样做?其实是因为
2430
在内核使用uprobe_register函数注册uprobe挂载点的时候在一些情况下会出现实际注册的函数偏移比

kernel_trace.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#include <linux/string.h>
1111
#include <syscall.h>
1212
#include <asm/current.h>
13+
#include <hook.h>
1314
#include "kernel_trace.h"
1415

1516
KPM_NAME("kernel_trace");
16-
KPM_VERSION("2.0.0");
17+
KPM_VERSION("2.2.0");
1718
KPM_LICENSE("GPL v2");
1819
KPM_AUTHOR("Test");
1920
KPM_DESCRIPTION("use uprobe trace some fun in kpm");
@@ -27,6 +28,8 @@ void (*path_put)(const struct path *path) = 0;
2728
void (*rcu_read_unlock)(void) = 0;
2829
int (*trace_printk)(unsigned long ip, const char *fmt, ...) = 0;
2930

31+
void *show_map_vma_addr;
32+
3033

3134
char file_name[MAX_PATH_LEN];
3235
uid_t target_uid = -1;
@@ -37,6 +40,24 @@ static struct inode *inode;
3740
unsigned long module_base = 0;
3841
static struct uprobe_consumer trace_uc;
3942

43+
44+
45+
void before_show_map_vma(hook_fargs2_t *args, void *udata)
46+
{
47+
struct seq_file* o_seq_file;
48+
struct vm_area_struct *ovma;
49+
unsigned long start, end;
50+
51+
o_seq_file = (struct seq_file*)args->arg0;
52+
ovma = (struct vm_area_struct*)args->arg1;
53+
start = ovma->vm_start;
54+
end = ovma->vm_end;
55+
if(start==0x7ffffff000 && end==0x8000000000){
56+
logkd("+Test-Log+ find uprobe item\n");
57+
args->skip_origin = 1;
58+
}
59+
}
60+
4061
void before_mincore(hook_fargs3_t *args, void *udata){
4162
int trace_flag = (int)syscall_argn(args, 1);
4263
if(trace_flag<TRACE_FLAG || trace_flag>TRACE_FLAG+CLEAR_UPROBE){
@@ -147,15 +168,15 @@ static int trace_handler(struct uprobe_consumer *self, struct mpt_regs *regs){
147168
}else{
148169
goto no_target_out;
149170
}
150-
171+
151172
target_out:
152173
// logkd("+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n",tfun->value,fun_offset);
153174
int trace_printk_ret = trace_printk(_THIS_IP_,"+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n",tfun->value,fun_offset);
154175
if(unlikely(trace_printk_ret<0)){
155176
logke("+Test-Log+ trace_printk error\n");
156177
}
157178
return 0;
158-
179+
159180
no_target_out:
160181
return 0;
161182
}
@@ -178,9 +199,11 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
178199
rb_first = (typeof(rb_first))kallsyms_lookup_name("rb_first");
179200
kmalloc = (typeof(kmalloc))kallsyms_lookup_name("__kmalloc");
180201
kfree = (typeof(kfree))kallsyms_lookup_name("kfree");
181-
202+
182203
trace_printk = (typeof(trace_printk))kallsyms_lookup_name("__trace_printk");
183204

205+
show_map_vma_addr = (void *)kallsyms_lookup_name("show_map_vma");
206+
184207
logkd("+Test-Log+ mtask_pid_nr_ns:%llx\n",mtask_pid_nr_ns);
185208
logkd("+Test-Log+ uprobe_register:%llx\n",uprobe_register);
186209
logkd("+Test-Log+ uprobe_unregister:%llx\n",uprobe_unregister);
@@ -194,12 +217,15 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
194217
logkd("+Test-Log+ rb_first:%llx\n",rb_first);
195218
logkd("+Test-Log+ kmalloc:%llx\n",kmalloc);
196219
logkd("+Test-Log+ kfree:%llx\n",kfree);
197-
220+
198221
logkd("+Test-Log+ trace_printk:%llx\n",trace_printk);
199222

223+
logkd("+Test-Log+ show_map_vma_addr:%llx\n",show_map_vma_addr);
224+
200225
if(!(mtask_pid_nr_ns && uprobe_register && uprobe_unregister
201226
&& kern_path && igrab && path_put && rcu_read_unlock
202-
&& rb_erase && rb_insert_color && rb_first && trace_printk)){
227+
&& rb_erase && rb_insert_color && rb_first && trace_printk
228+
&& show_map_vma_addr)){
203229
logke("+Test-Log+ can not find some fun addr\n");
204230
return -1;
205231
}
@@ -212,6 +238,12 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
212238
return -1;
213239
}
214240

241+
err = hook_wrap2(show_map_vma_addr, before_show_map_vma, NULL, 0);
242+
if(err){
243+
logke("+Test-Log+ hook show_map_vma error\n");
244+
return -1;
245+
}
246+
215247

216248
logkd("+Test-Log+ success init\n");
217249
return 0;
@@ -227,6 +259,7 @@ static long kernel_trace_control0(const char *args, char *__user out_msg, int ou
227259
static long kernel_trace_exit(void *__user reserved)
228260
{
229261
inline_unhook_syscall(__NR_mincore, before_mincore, 0);
262+
unhook(show_map_vma_addr);
230263
rcu_read_unlock();//解锁,不然内核会崩
231264
for (int i = 0; i < hook_num; ++i) {
232265
uprobe_unregister(inode,fun_offsets[i],&trace_uc);

kernel_trace.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
struct inode;
1313
struct mm_struct;
1414
struct vfsmount;
15+
struct seq_file;
1516

1617
struct hlist_bl_node {
1718
struct hlist_bl_node *next, **pprev;
@@ -73,5 +74,11 @@ struct uprobe_consumer {
7374
struct uprobe_consumer *next;
7475
};
7576

77+
struct vm_area_struct {
78+
/* The first cache line has the info for VMA tree walking. */
79+
80+
unsigned long vm_start; /* Our start address within vm_mm. */
81+
unsigned long vm_end;
82+
};
7683

7784
struct pid_namespace;

0 commit comments

Comments
 (0)