-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_trigger_bug.c
479 lines (385 loc) · 10.2 KB
/
sys_trigger_bug.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <linux/linkage.h>
#include <linux/moduleloader.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <crypto/hash.h>
#include <linux/crypto.h>
#include <linux/kthread.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/rwsem.h>
#include "common.h"
#include <linux/list.h>
#include <linux/dma-buf.h>
#include <linux/fdtable.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/time.h>
#include <linux/ptrace.h>
#include <linux/scatterlist.h>
#define LARGE_SIZE 1024
asmlinkage extern long (*sysptr)(int id);
static spinlock_t test_spin_lock1 = __SPIN_LOCK_UNLOCKED();
static DEFINE_MUTEX(test_mutex_lock1);
static DEFINE_MUTEX(test_mutex_lock2);
static RAW_NOTIFIER_HEAD(my_notifier_chain);
struct my_list {
struct list_head list;
int data;
};
void trigger_kernel_mem_leak(void)
{
char *buff = NULL;
buff = kmalloc(1000000, GFP_KERNEL);
if (!buff)
pr_info("Unable to allocate memory\n");
}
static int t2_func(void *unused)
{
int ret;
ssleep(1);
pr_info("Thread 2 Started\n");
pr_info("Grabbing lock2 by thread2\n");
mutex_lock(&test_mutex_lock2);
if (mutex_is_locked(&test_mutex_lock2))
pr_info("thread 2 is holding Lock 2\n");
pr_info("Grabbing lock1 by thread2\n");
ret = mutex_lock_interruptible(&test_mutex_lock1);
if (mutex_is_locked(&test_mutex_lock1))
pr_info("thread 2 is holding Lock 1\n");
mutex_unlock(&test_mutex_lock1);
if (!mutex_is_locked(&test_mutex_lock1))
pr_info("thread 2 released lock 1\n");
mutex_unlock(&test_mutex_lock2);
if (!mutex_is_locked(&test_mutex_lock2))
pr_info("thread 2 released lock 2\n");
pr_info("Stopping Thread 2\n");
do_exit(0);
}
static int t1_func(void *unused)
{
int ret;
pr_info("Thread 1 Started");
pr_info("Grabbing lock1 by thread1\n");
mutex_lock(&test_mutex_lock1);
if (mutex_is_locked(&test_mutex_lock1))
pr_info("thread 1 is holding Lock 1\n");
ssleep(2);
pr_info("Grabbing lock2 by thread1\n");
ret = mutex_lock_interruptible(&test_mutex_lock2);
if (mutex_is_locked(&test_mutex_lock2))
pr_info("thread 1 is holding Lock 2\n");
mutex_unlock(&test_mutex_lock2);
if (!mutex_is_locked(&test_mutex_lock2))
pr_info("thread 1 released lock 2\n");
mutex_unlock(&test_mutex_lock1);
if (!mutex_is_locked(&test_mutex_lock1))
pr_info("thread 1 released lock 1\n");
pr_info("Stopping Thread 1\n");
do_exit(0);
}
void trigger_deadlock(void)
{
struct task_struct *t1 = NULL;
struct task_struct *t2 = NULL;
pr_info("inside trigger deadlock\n");
t1 = kthread_create(t1_func, NULL, "thread1");
if (!t1) {
pr_info("cannot create thread 1");
return;
}
pr_info("thread 1 created");
t2 = kthread_create(t2_func, NULL, "thread2");
if (!t2) {
pr_info("cannot create thread 2");
return;
}
pr_info("thread 2 created");
wake_up_process(t1);
wake_up_process(t2);
ssleep(5);
force_sig(9, t1);
}
void trigger_sleep_inside_atomic_section(void)
{
char *ar = NULL;
pr_info("inside trigger sleep inside atomic section\n");
spin_lock(&test_spin_lock1);
if (spin_is_locked(&test_spin_lock1))
pr_info("current thread is holding spin Lock 1\n");
ar = kmalloc(10000, GFP_KERNEL);
spin_unlock(&test_spin_lock1);
if (!spin_is_locked(&test_spin_lock1))
pr_info("current thread released spin lock 1\n");
kfree(ar);
}
void trigger_bug_rw_semaphore(void)
{
struct rw_semaphore rwsem;
int data = 0;
init_rwsem(&rwsem);
pr_info("Initialized read-write semaphore\n");
down_read(&rwsem);
pr_info("Got the Semaphore in read mode\n");
data = data + 1;
pr_info("Going to Sleep\n");
ssleep(2);
up_write(&rwsem);
pr_info("Releasing the Semaphore in Write mode\n");
}
struct mystruct {
int a;
struct list_head list;
};
void trigger_list_corruption(void)
{
int i = 0;
struct mystruct obj;
struct mystruct *tmp;
INIT_LIST_HEAD(&(obj.list));
for (i = 0; i < 5; i++) {
tmp = kmalloc(sizeof(struct mystruct), GFP_KERNEL);
tmp->a = i+1;
list_add_tail(&(tmp->list), &(obj.list));
}
tmp->list.next = (void *)0xDEADBEEF;
tmp = kmalloc(sizeof(struct mystruct), GFP_KERNEL);
list_add_tail(&(tmp->list), &(obj.list));
}
void trigger_cred_management(void)
{
const struct cred *curr_cred, *new_cred;
curr_cred = get_current_cred();
new_cred = prepare_creds();
put_cred(new_cred);
put_cred(new_cred);
}
//https://stackoverflow.com/a/49508131/2802539
long currentTimeMillis(void)
{
struct timespec time;
getnstimeofday(&time);
return time.tv_sec * 1000 + time.tv_nsec / 1000000;
}
void trigger_soft_lockup(void)
{
long start = 0, curr = 0;
long elapsed_time = 0;
start = currentTimeMillis();
while (1) {
curr = currentTimeMillis();
elapsed_time = (curr - start)/1000;
if (elapsed_time > 30)
break;
}
pr_info("SoftLock Done\n");
}
static struct device dev = {
.init_name = "mydevice",
.coherent_dma_mask = ~0,
.dma_mask = &dev.coherent_dma_mask,
};
void trigger_bug_dma_api(void)
{
void *kbuf = NULL;
size_t size = 100;
dma_addr_t handle;
int ret;
ret = device_register(&dev);
kbuf = dma_alloc_coherent(&dev, size, &handle, GFP_KERNEL);
dma_free_coherent(NULL, size, kbuf, handle);
}
int event_handler(struct notifier_block *self, unsigned long val, void *data)
{
return NOTIFY_OK;
}
static struct notifier_block notifier = {
.notifier_call = 0,
};
static int trigger_invalid_notifier(void)
{
pr_info("Registering notifier with invalid handler\n");
raw_notifier_chain_register(&my_notifier_chain, ¬ifier);
pr_info("Publishing event to all the notifiers\n");
raw_notifier_call_chain(&my_notifier_chain, 10, NULL);
raw_notifier_chain_unregister(&my_notifier_chain, ¬ifier);
return 0;
}
int trigger_sg_issue(void)
{
struct scatterlist sg1[5], sg2;
sg_init_table(sg1, 4);
sg_init_table(&sg2, 1);
sg_chain(&sg1[0], 4, &sg2);
pr_info("The Page could not be assigned to a scatterlist that points to another scatterlist\n");
sg_assign_page(&sg1[3], 0);
return 0;
}
int trigger_slab_verifier(void)
{
char *ar = kmalloc(10, GFP_KERNEL);
kfree(ar);
strcpy(ar, "abc");
return 0;
}
static int th2_func(void *unused)
{
pr_info("Thread 2 Started\n");
pr_info("Grabbing lock2 by thread2\n");
mutex_lock(&test_mutex_lock2);
if (mutex_is_locked(&test_mutex_lock2))
pr_info("thread 2 is holding Lock 2\n");
pr_info("Grabbing lock1 by thread2\n");
mutex_lock(&test_mutex_lock1);
if (mutex_is_locked(&test_mutex_lock1))
pr_info("thread 2 is holding Lock 1\n");
mutex_unlock(&test_mutex_lock1);
if (!mutex_is_locked(&test_mutex_lock1))
pr_info("thread 2 released lock 1\n");
mutex_unlock(&test_mutex_lock2);
if (!mutex_is_locked(&test_mutex_lock2))
pr_info("thread 2 released lock 2\n");
pr_info("Stopping Thread 2\n");
do_exit(0);
}
static int th1_func(void *unused)
{
pr_info("Thread 1 Started");
pr_info("Grabbing lock1 by thread1\n");
mutex_lock(&test_mutex_lock1);
if (mutex_is_locked(&test_mutex_lock1))
pr_info("thread 1 is holding Lock 1\n");
ssleep(5);
pr_info("Grabbing lock2 by thread1\n");
mutex_lock(&test_mutex_lock2);
if (mutex_is_locked(&test_mutex_lock2))
pr_info("thread 1 is holding Lock 2\n");
mutex_unlock(&test_mutex_lock2);
if (!mutex_is_locked(&test_mutex_lock2))
pr_info("thread 1 released lock 2\n");
mutex_unlock(&test_mutex_lock1);
if (!mutex_is_locked(&test_mutex_lock1))
pr_info("thread 1 released lock 1\n");
pr_info("Stopping Thread 1\n");
do_exit(0);
}
void trigger_hung_task(void)
{
struct task_struct *t1 = NULL;
struct task_struct *t2 = NULL;
pr_info("inside trigger deadlock\n");
t1 = kthread_create(th1_func, NULL, "thread1");
if (!t1) {
pr_info("cannot create thread 1");
return;
}
pr_info("thread 1 created");
t2 = kthread_create(th2_func, NULL, "thread2");
if (!t2) {
pr_info("cannot create thread 2");
return;
}
pr_info("thread 2 created");
wake_up_process(t1);
wake_up_process(t2);
}
int poison_page_flag(void)
{
struct page *page;
unsigned int order = 0;
static unsigned long gpb_mask = GFP_KERNEL;
pr_info("inside %s\n", __func__);
pr_info("Allocating 1 page\n");
page = alloc_pages(gpb_mask, order);
pr_info("Page is allocated. Refcount : %d\n", page_ref_count(page));
//poisoning the flag
page->flags = -1l;
pr_info("Freeing allocated page\n");
free_page((unsigned long) page_address(page));
pr_info("Exiting poison_page.\n");
return 0;
}
/* Main Entry Point for the SYSCALL */
asmlinkage long sys_trigger_bug(int id)
{
int ret = 0;
pr_info("TRIGGER_BUG::trigger_bug received id %d\n", id);
if (id <= 0) {
pr_info("Returning error\n");
ret = -EINVAL;
goto out;
}
switch (id) {
case BUG_RW_SEMAPHORE:
pr_info("triggering BUG_RW_SEMAPHORE\n");
trigger_bug_rw_semaphore();
break;
case BUG_SLEEP_INSIDE_ATOMIC_SECTION:
pr_info("triggering sleep inside atomic section\n");
trigger_sleep_inside_atomic_section();
break;
case BUG_KERNEL_MEM_LEAK:
pr_info("Triggering kernel memory leak\n");
trigger_kernel_mem_leak();
break;
case BUG_DEBUG_VM_PAGE:
pr_info("Trigger debug virtual memory bug\n");
poison_page_flag();
break;
case BUG_DEADLOCK:
pr_info("triggering spinlock deadlock\n");
trigger_deadlock();
break;
case BUG_SOFT_LOCKUP:
pr_info("triggering bug softlockup\n");
trigger_soft_lockup();
break;
case BUG_LINKED_LIST_CORRUPTION:
pr_info("triggering list corruption\n");
trigger_list_corruption();
break;
case BUG_DMA_API:
pr_info("triggering bug dma api\n");
trigger_bug_dma_api();
break;
case BUG_INVALID_NOTIFIER:
pr_info("Triggering invalid notifier bug\n");
trigger_invalid_notifier();
break;
case BUG_SCATTERLIST_CHAINED:
pr_info("Triggering bug on scattelist being chained\n");
trigger_sg_issue();
break;
case SLAB_VALIDATOR:
pr_info("Triggering incorrect memory access\n");
trigger_slab_verifier();
break;
case BUG_HUNG_TASK:
trigger_hung_task();
break;
case BUG_CRED_MANAGEMENT:
pr_info("Trigger credential management bug\n");
trigger_cred_management();
break;
default:
pr_info("Invalid bug id sent from user program\n");
}
out:
return 0;
}
static int __init init_sys_trigger_bug(void)
{
pr_info("installed new sys_trigger_bug module\n");
if (sysptr == NULL)
sysptr = sys_trigger_bug;
return 0;
}
static void __exit exit_sys_trigger_bug(void)
{
if (sysptr != NULL)
sysptr = NULL;
pr_info("removed sys_trigger_bug module\n");
}
module_init(init_sys_trigger_bug);
module_exit(exit_sys_trigger_bug);
MODULE_LICENSE("GPL");