-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprocess.h
More file actions
117 lines (100 loc) · 3.07 KB
/
Copy pathprocess.h
File metadata and controls
117 lines (100 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef PROCESS_H
#define PROCESS_H
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/tty.h>
#include <linux/mm.h>
#include <linux/version.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,14,83))
#include <linux/sched/mm.h>
#include <linux/sched/task.h>
#include <linux/sched/signal.h>
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0))
#include <linux/mmap_lock.h>
#define MM_READ_LOCK(mm) mmap_read_lock(mm);
#define MM_READ_UNLOCK(mm) mmap_read_unlock(mm);
#else
#include <linux/rwsem.h>
#define MM_READ_LOCK(mm) down_read(&(mm)->mmap_sem);
#define MM_READ_UNLOCK(mm) up_read(&(mm)->mmap_sem);
#endif
#define CONFIG_DIRECT_API_USER_COPY
static inline unsigned long x_copy_from_user(void *to, const void __user *from, unsigned long n);
static inline unsigned long x_copy_to_user(void __user *to, const void *from, unsigned long n);
static inline uintptr_t get_module_base(pid_t pid, char* name);
static inline unsigned long x_copy_from_user(void *to, const void __user *from, unsigned long n) {
#ifdef CONFIG_DIRECT_API_USER_COPY
unsigned long __arch_copy_from_user(void* to, const void __user * from, unsigned long n);
return __arch_copy_from_user(to, from, n);
#else
return copy_from_user(to, from, n);
#endif
}
static inline unsigned long x_copy_to_user(void __user *to, const void *from, unsigned long n) {
#ifdef CONFIG_DIRECT_API_USER_COPY
unsigned long __arch_copy_to_user(void __user * to, const void* from, unsigned long n);
return __arch_copy_to_user(to, from, n);
#else
return copy_to_user(to, from, n);
#endif
}
#define ARC_PATH_MAX 256
//获取模块基址
uintptr_t get_module_base(pid_t pid, char* name) {
struct pid* pid_struct;
struct task_struct* task;
struct mm_struct* mm;
struct vm_area_struct* vma;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
struct vma_iterator vmi;
#endif
uintptr_t result;
struct dentry* dentry;
size_t name_len, dname_len;
result = 0;
name_len = strlen(name);
if (name_len == 0) {
//dream_debug("module name is empty\n");
return 0;
}
pid_struct = find_get_pid(pid);
if (!pid_struct) {
//dream_debug("failed to find pid_struct\n");
return 0;
}
task = get_pid_task(pid_struct, PIDTYPE_PID);
put_pid(pid_struct);
if (!task) {
//dream_debug("failed to get task from pid_struct\n");
return 0;
}
mm = get_task_mm(task);
put_task_struct(task);
if (!mm) {
//dream_debug("failed to get mm from task\n");
return 0;
}
MM_READ_LOCK(mm)
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
vma_iter_init(&vmi, mm, 0);
for_each_vma(vmi, vma)
#else
for (vma = mm->mmap; vma; vma = vma->vm_next)
#endif
{
if (vma->vm_file) {
dentry = vma->vm_file->f_path.dentry;
dname_len = dentry->d_name.len;
if (!memcmp(dentry->d_name.name, name, min(name_len, dname_len))) {
result = vma->vm_start;
goto ret;
}
}
}
ret:
MM_READ_UNLOCK(mm)
mmput(mm);
return result;
}
#endif