generated from deepin-community/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathla_ow_syscall_main.c
332 lines (282 loc) · 8.55 KB
/
la_ow_syscall_main.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/kprobes.h> /* Needed for kprobe calls */
///< The license type -- this affects runtime behavior
MODULE_LICENSE("GPL");
///< The author -- visible when you use modinfo
MODULE_AUTHOR("Miao Wang");
///< The description -- see modinfo
MODULE_DESCRIPTION("LoongArch old-world syscall compatibility module");
///< The version of the module
MODULE_VERSION("0.1.0");
#include <linux/kallsyms.h>
#include <linux/syscalls.h>
#define __EXTERN
#include "fsstat.h"
#include "signal.h"
#define __ARCH_WANT_SET_GET_RLIMIT
#define __ARCH_WANT_NEW_STAT
#undef __SYSCALL
#define __SYSCALL(nr, call) [nr] = (#call),
const char *sys_call_table_name[__NR_syscalls] = {
[0 ... __NR_syscalls - 1] = "sys_ni_syscall",
#include <asm/unistd.h>
};
#ifndef __loongarch64
#error This Linux kernel module is only supported on LoongArch
#endif
#ifdef HAVE_KSYM_ADDR
#include "ksym_addr.h"
#endif
static struct {
long syscall_num;
void *symbol_addr;
void *orig;
} syscall_to_replace[] = {
{ __NR_fstat, sys_newfstat },
{ __NR_newfstatat, sys_newfstatat },
{ __NR_getrlimit, NULL },
{ __NR_setrlimit, NULL },
{ __NR_rt_sigprocmask, sys_rt_sigprocmask },
{ __NR_rt_sigpending, sys_rt_sigpending },
{ __NR_rt_sigtimedwait, sys_rt_sigtimedwait },
{ __NR_rt_sigaction, sys_rt_sigaction },
{ __NR_rt_sigsuspend, sys_rt_sigsuspend },
{ __NR_pselect6, sys_pselect6 },
{ __NR_ppoll, sys_ppoll },
#ifdef CONFIG_SIGNALFD
{ __NR_signalfd4, sys_signalfd4 },
#endif
#ifdef CONFIG_EPOLL
{ __NR_epoll_pwait, sys_epoll_pwait },
{ __NR_epoll_pwait2, sys_epoll_pwait2 },
#endif
};
#define nr_syscalls_to_replace \
(sizeof(syscall_to_replace) / sizeof(syscall_to_replace[0]))
static unsigned long kallsyms_lookup_name_addr =
#ifdef HAVE_KSYM_ADDR
LAOWSYS_KALLSYMS_LOOKUP_NAME_ADDR
#else
0
#endif
;
static unsigned int allow_mod_unreg = 0;
#include <asm-generic/sections.h>
#ifdef HAVE_KSYM_ADDR
static int __init find_kallsyms_lookup_name(void){ return 0; }
#else
// Taken from https://github.com/zizzu0/LinuxKernelModules/blob/main/FindKallsymsLookupName.c
#define KPROBE_PRE_HANDLER(fname) \
static int __kprobes fname(struct kprobe *p, struct pt_regs *regs)
static struct kprobe kp0, kp1;
KPROBE_PRE_HANDLER(handler_pre0)
{
kallsyms_lookup_name_addr = regs->csr_era;
return 0;
}
KPROBE_PRE_HANDLER(handler_pre1)
{
return 0;
}
#undef KPROBE_PRE_HANDLER
static int do_register_kprobe(struct kprobe *kp, char *symbol_name,
void *handler)
{
int ret = 0;
kp->symbol_name = symbol_name;
kp->pre_handler = handler;
ret = register_kprobe(kp);
if (ret < 0) {
pr_err("register_probe() for symbol %s failed, returned %d\n",
symbol_name, ret);
return ret;
}
pr_debug("planted kprobe for symbol %s at %p\n", symbol_name, kp->addr);
return ret;
}
static int __init kprobe_kallsyms_lookup_name(void)
{
int ret = 0;
ret = do_register_kprobe(&kp0, "kallsyms_lookup_name", handler_pre0);
if (ret < 0)
return ret;
ret = do_register_kprobe(&kp1, "kallsyms_lookup_name", handler_pre1);
if (ret < 0) {
unregister_kprobe(&kp0);
return ret;
}
unregister_kprobe(&kp0);
unregister_kprobe(&kp1);
return ret;
}
static int __init find_kallsyms_lookup_name(void)
{
char fn_name[KSYM_SYMBOL_LEN];
int ret = 0;
if (kallsyms_lookup_name_addr == 0 ||
kallsyms_lookup_name_addr == (unsigned long)-1) {
ret = kprobe_kallsyms_lookup_name();
if ( ret < 0 ) {
return ret;
}
if (kallsyms_lookup_name_addr == 0 ||
kallsyms_lookup_name_addr == (unsigned long)-1) {
return -EINVAL;
}
}
sprint_symbol(fn_name, kallsyms_lookup_name_addr);
if (strncmp(fn_name, "kallsyms_lookup_name+0x0",
strlen("kallsyms_lookup_name+0x0")) == 0) {
pr_debug("got kallsyms_lookup_name = %lx\n",
kallsyms_lookup_name_addr);
return 0;
} else {
pr_debug("got %s at %lx, not kallsyms_lookup_name\n", fn_name,
kallsyms_lookup_name_addr);
return -EINVAL;
}
}
#endif
int (*p_vfs_fstatat)(int dfd, const char __user *filename, struct kstat *stat,
int flags);
int (*p_vfs_fstat)(int fd, struct kstat *stat);
void *p_sys_setxattr, *p_sys_close, *p_sys_clone;
static struct {
const char *func_name;
void **stor;
} relocation_table[] = {
#define __rel(func) \
{ \
(#func), ((void **)&(p_##func)) \
}
__rel(vfs_fstatat), __rel(vfs_fstat),
__rel(sys_setxattr), __rel(sys_close),
__rel(sys_clone), __rel(sys_rt_sigprocmask),
__rel(sys_rt_sigpending), __rel(sys_rt_sigtimedwait),
__rel(sys_rt_sigaction), __rel(sys_rt_sigsuspend),
__rel(sys_pselect6), __rel(sys_ppoll),
#ifdef CONFIG_SIGNALFD
__rel(sys_signalfd4),
#endif
#ifdef CONFIG_EPOLL
__rel(sys_epoll_pwait), __rel(sys_epoll_pwait2),
#endif
};
#define nr_rel_tab (sizeof(relocation_table) / sizeof(relocation_table[0]))
#ifdef HAVE_KSYM_ADDR
static void **p_sys_call_table = (void **)LAOWSYS_SYS_CALL_TABLE_ADDR;
static int __init find_sys_call_table(void){ return 0; };
#else
static void **p_sys_call_table;
#include <linux/jiffies.h>
#include <linux/reboot.h>
static int __init find_sys_call_table(void)
{
unsigned long (*p_kallsyms_lookup_name)(const char *name) =
(void *)kallsyms_lookup_name_addr;
unsigned long *sys_table;
if (kallsyms_lookup_name_addr == 0) {
return -ENOSYS;
}
if ((sys_table = (unsigned long *)p_kallsyms_lookup_name(
"sys_call_table"))) {
p_sys_call_table = (void **)sys_table;
pr_debug("found sys_call_table=%px\n", p_sys_call_table);
return 0;
}
pr_info("failed to find sys_call_table using kallsyms_lookup_name()\n");
pr_info("trying to find sys_call_table using memory scanning\n");
for (sys_table = (void *)&jiffies;
(void *)sys_table < (void *)&reboot_mode; sys_table++) {
if (sys_table[__NR_setxattr] == (unsigned long)p_sys_setxattr &&
sys_table[__NR_close] == (unsigned long)p_sys_close &&
sys_table[__NR_clone] == (unsigned long)p_sys_clone) {
p_sys_call_table = (void **)sys_table;
pr_debug("found sys_call_table=%px\n",
p_sys_call_table);
return 0;
}
}
return -ENOSYS;
}
#endif
static int __init oldsyscall_start(void)
{
unsigned long (*p_kallsyms_lookup_name)(const char *name);
int rc = find_kallsyms_lookup_name();
if (rc < 0) {
return rc;
}
p_kallsyms_lookup_name = (void *)kallsyms_lookup_name_addr;
for (int i = 0; i < nr_rel_tab; i++) {
unsigned long p =
p_kallsyms_lookup_name(relocation_table[i].func_name);
if (p == 0) {
pr_warn("cannot find symbol %s\n",
relocation_table[i].func_name);
return -EINVAL;
}
pr_debug("found symbol %s at %px\n",
relocation_table[i].func_name, (void *)p);
*relocation_table[i].stor = (void *)p;
}
rc = find_sys_call_table();
if (rc < 0) {
return rc;
}
for (int i = 0; i < nr_syscalls_to_replace; i++) {
if (syscall_to_replace[i].symbol_addr) {
continue;
}
const char *symbol_name =
sys_call_table_name[syscall_to_replace[i].syscall_num];
unsigned long symbol_addr = p_kallsyms_lookup_name(symbol_name);
if (symbol_addr) {
pr_debug("found %s at %px\n", symbol_name,
(void *)symbol_addr);
} else {
pr_warn("cannot find symbol %s\n", symbol_name);
return -EINVAL;
}
syscall_to_replace[i].symbol_addr = (void *)symbol_addr;
}
if (!allow_mod_unreg) {
bool succ = try_module_get(THIS_MODULE);
if (!succ) {
return -EINVAL;
}
}
for (int i = 0; i < nr_syscalls_to_replace; i++) {
pr_debug("will replace syscall_%ld with %px, orig %px\n",
syscall_to_replace[i].syscall_num,
syscall_to_replace[i].symbol_addr,
p_sys_call_table[syscall_to_replace[i].syscall_num]);
syscall_to_replace[i].orig =
p_sys_call_table[syscall_to_replace[i].syscall_num];
p_sys_call_table[syscall_to_replace[i].syscall_num] =
syscall_to_replace[i].symbol_addr;
}
pr_info("la_ow_syscall module successfully initialized\n");
return 0;
}
static void __exit oldsyscall_end(void)
{
for (int i = 0; i < nr_syscalls_to_replace; i++) {
pr_debug("will restore syscall_%ld to %px\n",
syscall_to_replace[i].syscall_num,
syscall_to_replace[i].orig);
p_sys_call_table[syscall_to_replace[i].syscall_num] =
syscall_to_replace[i].orig;
}
}
module_init(oldsyscall_start);
module_exit(oldsyscall_end);
module_param(allow_mod_unreg, uint, 0000);
MODULE_PARM_DESC(allow_mod_unreg,
"Allow this module to be unload (Danger! Debug use only)");
#ifndef HAVE_KSYM_ADDR
module_param(kallsyms_lookup_name_addr, ulong, 0000);
MODULE_PARM_DESC(kallsyms_lookup_name_addr, "Address for kallsyms_lookup_name, provide this when unable to find using kprobe");
#endif