Skip to content

Commit edc3259

Browse files
彻底解决uprobe在map文件中的痕迹问题
1 parent 2d64542 commit edc3259

File tree

3 files changed

+108
-24
lines changed

3 files changed

+108
-24
lines changed

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export TARGET_COMPILE=aarch64-none-elf-
2+
3+
ifndef TARGET_COMPILE
4+
$(error TARGET_COMPILE not set)
5+
endif
6+
7+
ifndef KP_DIR
8+
KP_DIR = ../..
9+
endif
10+
11+
12+
CC = $(TARGET_COMPILE)gcc
13+
LD = $(TARGET_COMPILE)ld
14+
15+
INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include
16+
17+
INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir))
18+
19+
objs := kernel_trace.o
20+
21+
all: kernel_trace.kpm
22+
23+
kernel_trace.kpm: ${objs}
24+
${CC} -r -o $@ $^
25+
26+
%.o: %.c
27+
${CC} $(CFLAGS) $(INCLUDE_FLAGS) -c -O2 -o $@ $<
28+
29+
.PHONY: clean
30+
clean:
31+
rm -rf *.kpm
32+
find . -name "*.o" | xargs rm -f

kernel_trace.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
#include <linux/printk.h>
55
#include <linux/cred.h>
66
#include <taskext.h>
7-
#include <linux/printk.h>
87
#include <linux/uaccess.h>
98
#include <asm/current.h>
109
#include <linux/string.h>
1110
#include <syscall.h>
12-
#include <asm/current.h>
1311
#include <hook.h>
1412
#include "kernel_trace.h"
1513

1614
KPM_NAME("kernel_trace");
17-
KPM_VERSION("3.7.0");
15+
KPM_VERSION("4.0.0");
1816
KPM_LICENSE("GPL v2");
1917
KPM_AUTHOR("Test");
2018
KPM_DESCRIPTION("use uprobe trace some fun in kpm");
@@ -30,7 +28,9 @@ int (*trace_printk)(unsigned long ip, const char *fmt, ...) = 0;
3028

3129
int (*bpf_probe_read_user)(void *dst, u32 size,const void __user *unsafe_ptr) = 0;
3230

33-
void *show_map_vma_addr;
31+
unsigned long (*get_unmapped_area)(struct file *file, unsigned long addr, unsigned long len,unsigned long pgoff, unsigned long flags) = 0;
32+
33+
void *xol_add_vma_addr;
3434
void *copy_insn_addr;
3535

3636

@@ -57,20 +57,16 @@ void before_copy_insn(hook_fargs5_t *args, void *udata){
5757
}
5858
}
5959

60-
void before_show_map_vma(hook_fargs2_t *args, void *udata)
61-
{
62-
struct seq_file* o_seq_file;
63-
struct vm_area_struct *ovma;
64-
unsigned long start, end;
65-
66-
o_seq_file = (struct seq_file*)args->arg0;
67-
ovma = (struct vm_area_struct*)args->arg1;
68-
start = ovma->vm_start;
69-
end = ovma->vm_end;
70-
if(start==0x7ffffff000 && end==0x8000000000){
71-
logkd("+Test-Log+ find uprobe item\n");
72-
args->skip_origin = 1;
60+
void before_xol_add_vma(hook_fargs2_t *args, void *udata){
61+
struct xol_area *o_area;
62+
63+
o_area = (struct xol_area*)args->arg1;
64+
if(!o_area->vaddr){
65+
o_area->xol_mapping.name = "Kernel-Trace";
66+
o_area->vaddr = get_unmapped_area(NULL, 0,PAGE_SIZE, 0, 0);
67+
logkd("+Test-Log+ create map item:Kernel-Trace\n");
7368
}
69+
7470
}
7571

7672
void before_mincore(hook_fargs3_t *args, void *udata){
@@ -234,7 +230,9 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
234230
trace_printk = (typeof(trace_printk))kallsyms_lookup_name("__trace_printk");
235231
bpf_probe_read_user = (typeof(bpf_probe_read_user))kallsyms_lookup_name("bpf_probe_read_user");
236232

237-
show_map_vma_addr = (void *)kallsyms_lookup_name("show_map_vma");
233+
get_unmapped_area = (typeof(get_unmapped_area))kallsyms_lookup_name("get_unmapped_area");
234+
235+
xol_add_vma_addr = (void *)kallsyms_lookup_name("xol_add_vma");
238236

239237
copy_insn_addr = (void *)kallsyms_lookup_name("__copy_insn");
240238

@@ -255,14 +253,16 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
255253
logkd("+Test-Log+ trace_printk:%llx\n",trace_printk);
256254
logkd("+Test-Log+ bpf_probe_read_user:%llx\n",bpf_probe_read_user);
257255

258-
logkd("+Test-Log+ show_map_vma_addr:%llx\n",show_map_vma_addr);
256+
logkd("+Test-Log+ get_unmapped_area:%llx\n",get_unmapped_area);
257+
258+
logkd("+Test-Log+ xol_add_vma_addr:%llx\n",xol_add_vma_addr);
259259

260260
logkd("+Test-Log+ copy_insn_addr:%llx\n",copy_insn_addr);
261261

262262
if(!(mtask_pid_nr_ns && uprobe_register && uprobe_unregister
263263
&& kern_path && igrab && path_put && rcu_read_unlock
264264
&& rb_erase && rb_insert_color && rb_first && trace_printk
265-
&& bpf_probe_read_user && show_map_vma_addr && copy_insn_addr)){
265+
&& bpf_probe_read_user && get_unmapped_area && xol_add_vma_addr && copy_insn_addr)){
266266
logke("+Test-Log+ can not find some fun addr\n");
267267
return -1;
268268
}
@@ -275,9 +275,9 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
275275
return -1;
276276
}
277277

278-
err = hook_wrap2(show_map_vma_addr, before_show_map_vma, NULL, 0);
278+
err = hook_wrap2(xol_add_vma_addr, before_xol_add_vma, NULL, 0);
279279
if(err){
280-
logke("+Test-Log+ hook show_map_vma error\n");
280+
logke("+Test-Log+ hook xol_add_vma error\n");
281281
return -1;
282282
}
283283

@@ -301,7 +301,7 @@ static long kernel_trace_control0(const char *args, char *__user out_msg, int ou
301301
static long kernel_trace_exit(void *__user reserved)
302302
{
303303
inline_unhook_syscall(__NR_mincore, before_mincore, 0);
304-
unhook(show_map_vma_addr);
304+
unhook(xol_add_vma_addr);
305305
unhook(copy_insn_addr);
306306
rcu_read_unlock();//解锁,不然内核会崩
307307
for (int i = 0; i < hook_num; ++i) {

kernel_trace.h

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
#define INS_LEN 4
1010
#define LOOKUP_FOLLOW 0x0001
1111
#define HASH_LEN_DECLARE u32 hash; u32 len
12+
#define PAGE_SIZE 4096
1213

1314
struct inode;
1415
struct mm_struct;
1516
struct vfsmount;
1617
struct seq_file;
18+
struct page;
19+
20+
typedef __bitwise unsigned int vm_fault_t;
21+
struct vm_fault;
1722

1823
struct hlist_bl_node {
1924
struct hlist_bl_node *next, **pprev;
@@ -82,4 +87,51 @@ struct vm_area_struct {
8287
unsigned long vm_end;
8388
};
8489

85-
struct pid_namespace;
90+
91+
struct vm_special_mapping {
92+
const char *name; /* The name, e.g. "[vdso]". */
93+
94+
/*
95+
* If .fault is not provided, this points to a
96+
* NULL-terminated array of pages that back the special mapping.
97+
*
98+
* This must not be NULL unless .fault is provided.
99+
*/
100+
struct page **pages;
101+
102+
/*
103+
* If non-NULL, then this is called to resolve page faults
104+
* on the special mapping. If used, .pages is not checked.
105+
*/
106+
vm_fault_t (*fault)(const struct vm_special_mapping *sm,
107+
struct vm_area_struct *vma,
108+
struct vm_fault *vmf);
109+
110+
int (*mremap)(const struct vm_special_mapping *sm,
111+
struct vm_area_struct *new_vma);
112+
};
113+
114+
115+
struct wait_queue_head {
116+
spinlock_t lock;
117+
struct list_head head;
118+
};
119+
typedef struct wait_queue_head wait_queue_head_t;
120+
121+
122+
struct xol_area {
123+
wait_queue_head_t wq; /* if all slots are busy */
124+
atomic_t slot_count; /* number of in-use slots */
125+
unsigned long *bitmap; /* 0 = free slot */
126+
127+
struct vm_special_mapping xol_mapping;
128+
struct page *pages[2];
129+
/*
130+
* We keep the vma's vm_start rather than a pointer to the vma
131+
* itself. The probed process or a naughty kernel module could make
132+
* the vma go away, and we must handle that reasonably gracefully.
133+
*/
134+
unsigned long vaddr; /* Page(s) of instruction slots */
135+
};
136+
137+
struct pid_namespace;

0 commit comments

Comments
 (0)