-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.file
More file actions
289 lines (281 loc) · 8.72 KB
/
patch.file
File metadata and controls
289 lines (281 loc) · 8.72 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
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
diff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/arch/x86/syscalls/syscall_32.tbl kernel/arch/x86/syscalls/syscall_32.tbl
--- kernel2/kernel/arch/x86/syscalls/syscall_32.tbl 2017-06-17 10:44:31.000000000 -0700
+++ kernel/arch/x86/syscalls/syscall_32.tbl 2018-12-02 12:21:29.421165242 -0700
@@ -365,3 +365,5 @@
356 i386 memfd_create sys_memfd_create
357 i386 bpf sys_bpf
358 i386 execveat sys_execveat stub32_execveat
+359 i386 insdump sys_insdump
+360 i386 rmdump sys_rmdump
diff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/include/linux/syscalls.h kernel/include/linux/syscalls.h
--- kernel2/kernel/include/linux/syscalls.h 2017-06-17 10:44:57.000000000 -0700
+++ kernel/include/linux/syscalls.h 2018-12-02 15:32:54.326122304 -0700
@@ -67,6 +67,8 @@ struct file_handle;
struct sigaltstack;
union bpf_attr;
+struct dumpmode_t;
+
#include <linux/types.h>
#include <linux/aio_abi.h>
#include <linux/capability.h>
@@ -882,4 +884,10 @@ asmlinkage long sys_execveat(int dfd, co
const char __user *const __user *argv,
const char __user *const __user *envp, int flags);
+
+asmlinkage long sys_insdump(const char __user *symbolname, struct dumpmode_t __user * dumpmode);
+
+asmlinkage long sys_rmdump(int dumpid);
+
+
#endif
ddiff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/kernel/exit.c kernel/kernel/exit.c
--- kernel2/kernel/kernel/exit.c 2017-06-17 10:44:58.000000000 -0700
+++ kernel/kernel/exit.c 2018-12-02 15:21:57.057985017 -0700
@@ -59,6 +59,8 @@
#include <asm/pgtable.h>
#include <asm/mmu_context.h>
+#include <linux/dynamic_dump_stack.h>
+
static void exit_mm(struct task_struct *tsk);
static void __unhash_process(struct task_struct *p, bool group_dead)
@@ -654,7 +656,9 @@ void do_exit(long code)
struct task_struct *tsk = current;
int group_dead;
TASKS_RCU(int tasks_rcu_i);
-
+#ifdef CONFIG_DYNAMIC_DUMP_STACK
+ remove_krpobes(tsk->pid);
+#endif
profile_task_exit(tsk);
WARN_ON(blk_needs_flush_plug(tsk));
diff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/lib/dynamic_dump_stack.c kernel/lib/dynamic_dump_stack.c
--- kernel2/kernel/lib/dynamic_dump_stack.c 1969-12-31 17:00:00.000000000 -0700
+++ kernel/lib/dynamic_dump_stack.c 2018-12-02 15:44:53.109352512 -0700
@@ -0,0 +1,186 @@
+#include<linux/kernel.h>
+#include<linux/init.h>
+#include<linux/sched.h>
+#include<linux/syscalls.h>
+#include<linux/string.h>
+#include<linux/kprobes.h>
+#include<linux/kallsyms.h>
+#include<linux/module.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+
+#include <linux/dynamic_dump_stack.h>
+
+#define MAX_SYMBOL_LEN 40
+
+int dumpstackid;
+struct list_head dump_stack_list = LIST_HEAD_INIT(dump_stack_list);
+struct dump_stack_struct *node;
+
+struct dump_stack_struct{
+
+ int dumpstackid;
+ char symbol_name[40];
+ struct kprobe p;
+ pid_t pid;
+ pid_t threadgid;
+ int dumpstackmode;
+ struct list_head dlist;
+
+};
+
+struct delete_node {
+ struct list_head listnode;
+ struct list_head* address;
+};
+
+int remove_krpobes(pid_t pid) {
+
+
+ struct list_head* iter = NULL;
+ struct dump_stack_struct* struct_iter = NULL;
+
+ struct delete_node *delList, *temp = NULL;
+ struct list_head nodes_to_delete = LIST_HEAD_INIT(nodes_to_delete);
+
+ pr_info("PROCESS %d IS EXITED. REMOVING THE KRPOBES\n", pid);
+
+ list_for_each(iter, &dump_stack_list) {
+ struct_iter = list_entry(iter, struct dump_stack_struct, dlist);
+ //pr_info("PROBES - symbol %s pid %d\n", struct_iter->symbol_name, struct_iter->pid);
+ if(struct_iter->pid == pid) {
+ unregister_kprobe(&struct_iter->p);
+ delList = kmalloc(sizeof(struct delete_node), GFP_KERNEL);
+ memset(delList, 0, sizeof(struct delete_node));
+ delList->address = iter;
+ list_add(&delList->listnode, &nodes_to_delete);
+ }
+ }
+
+ list_for_each(iter, &nodes_to_delete) {
+ temp = list_entry(iter, struct delete_node, listnode);
+ list_del(temp->address);
+ }
+
+ pr_info("REMOVED THE KRPOBES\n");
+
+ return 1;
+}
+
+int Pre_Handler(struct kprobe *probe, struct pt_regs *regs){
+
+ struct dump_stack_struct *current_struct;
+ struct task_struct* task = current;
+ pid_t tgid;
+ pid_t pid;
+ tgid = task->tgid;
+ pid = task->pid;
+
+ current_struct = container_of(probe, struct dump_stack_struct, p);
+
+ if(current_struct->dumpstackmode > 1){
+ dump_stack();
+ }
+ else if(current_struct->dumpstackmode == 0 && pid == current_struct->pid){
+ dump_stack();
+ }
+ else if(current_struct->dumpstackmode == 1 && (pid == current_struct->pid || tgid == current_struct->threadgid)){
+ dump_stack();
+ }
+
+ return 0;
+}
+
+
+SYSCALL_DEFINE1(rmdump, int, dumpid){
+
+#ifdef CONFIG_DYNAMIC_DUMP_STACK
+
+
+ struct list_head* iter = NULL;
+ bool dump_stack_id_exists = false;
+ struct dump_stack_struct* struct_iter = NULL;
+ struct task_struct* task = current;
+ pid_t pid = task->pid;
+ pr_info("IN THE SYSCALL RMDUMP\n");
+ list_for_each(iter, &dump_stack_list) {
+ struct_iter = list_entry(iter, struct dump_stack_struct, dlist);
+ if(struct_iter->dumpstackid == dumpid && struct_iter->pid == pid) {
+ dump_stack_id_exists = true;
+ break;
+ }
+ }
+
+ if(dump_stack_id_exists){
+ printk(KERN_INFO "DUMPSTACK FOUND!! REMOVING\n");
+ unregister_kprobe(&struct_iter->p);
+ list_del(iter);
+ }else{
+ printk(KERN_INFO "DUMPSTACK NOT FOUND\n");
+ return -EINVAL;
+ }
+ return 1;
+#else
+ return 0;
+#endif
+
+
+}
+
+SYSCALL_DEFINE2(insdump,const char __user *, symbolname, struct dumpmode_t __user *, dumpmode)
+{
+
+#ifdef CONFIG_DYNAMIC_DUMP_STACK
+
+ unsigned long address;
+
+ char *symbol_name;
+ struct task_struct* task;
+ struct dumpmode_t input_mode;
+ pid_t tgid;
+ pid_t pid;
+ task = current;
+ tgid = task->tgid;
+ pid = task->pid;
+ pr_info("IN THE SYSCALL INSDUMP\n");
+ symbol_name = kmalloc(sizeof(char)*MAX_SYMBOL_LEN, GFP_KERNEL);
+ strncpy_from_user((char *)symbol_name,
+ symbolname, MAX_SYMBOL_LEN);
+ address = kallsyms_lookup_name(symbol_name);
+
+ if(address == 0/* && is_kernel_text(address)*/){
+ printk(KERN_INFO "SYMBOL NOT FOUND\n");
+ return -EINVAL;
+ }
+
+ printk(KERN_INFO "SYMOBOL FOUND! ADDING THE KRPOBE\n");
+ node = (struct dump_stack_struct *)kmalloc(sizeof(struct dump_stack_struct), GFP_KERNEL);
+ memset(node, 0, sizeof(struct dump_stack_struct));
+ if (copy_from_user(&input_mode, dumpmode,sizeof(input_mode))){
+ return -EFAULT;
+ }
+ printk(KERN_INFO "DUMPSTACK MODE IS - %d\n", input_mode.mode);
+ node->pid = pid;
+ node->threadgid = tgid;
+ node->dumpstackmode = input_mode.mode;
+ snprintf(node->symbol_name, sizeof(char)*MAX_SYMBOL_LEN, "%s", symbol_name);
+ //node->p = (struct kprobe*)kmalloc(sizeof(struct kprobe), GFP_KERNEL);
+ memset(&node->p, 0, sizeof(struct kprobe));
+ node->p.pre_handler = Pre_Handler;
+ node->p.addr = (kprobe_opcode_t *)address;
+ node->dumpstackid = dumpstackid++;
+
+ if(register_kprobe(&node->p)){
+ printk(KERN_INFO "Error while setting kprobe on address %p\n", (void*)(address));\
+ return -EINVAL;
+ }
+
+ list_add(&node->dlist, &dump_stack_list);
+ printk(KERN_INFO "KPROBE INSERTED\n");
+ return node->dumpstackid;
+#else
+ return 0;
+
+#endif
+
+}
diff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/include/linux/dynamic_dump_stack.h kernel/include/linux/dynamic_dump_stack.h
--- kernel2/kernel/include/linux/dynamic_dump_stack.h 1969-12-31 17:00:00.000000000 -0700
+++ kernel/include/linux/dynamic_dump_stack.h 2018-12-02 15:11:11.105969564 -0700
@@ -0,0 +1,10 @@
+
+
+
+extern struct list_head dump_stack_list;
+
+extern int remove_krpobes(pid_t pid);
+
+struct dumpmode_t {
+ unsigned int mode;
+};
diff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/lib/Kconfig.debug kernel/lib/Kconfig.debug
--- kernel2/kernel/lib/Kconfig.debug 2017-06-17 10:44:58.000000000 -0700
+++ kernel/lib/Kconfig.debug 2018-12-02 14:02:14.213286585 -0700
@@ -1595,6 +1595,14 @@ config TEST_RHASHTABLE
endmenu # runtime tests
+config DYNAMIC_DUMP_STACK
+ bool "Enable dynamic dump stack"
+ default n
+ help
+ Enable this option to test the dyanamically adding the dump stack.
+
+ If unsure, say N.
+
config PROVIDE_OHCI1394_DMA_INIT
bool "Remote debugging over FireWire early on boot"
depends on PCI && X86
diff -uprN -X kernel/Documentation/dontdiff kernel2/kernel/lib/Makefile kernel/lib/Makefile
--- kernel2/kernel/lib/Makefile 2017-06-17 10:44:58.000000000 -0700
+++ kernel/lib/Makefile 2018-12-02 15:14:07.295498933 -0700
@@ -21,7 +21,7 @@ lib-$(CONFIG_SMP) += cpumask.o
lib-y += kobject.o klist.o
obj-y += lockref.o
-
+obj-y += dynamic_dump_stack.o
obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \
gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \