-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathludtm.c
551 lines (471 loc) · 10.2 KB
/
ludtm.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <alloca.h>
#include <ctype.h>
#include <execinfo.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <threads.h>
#include <stdatomic.h>
#include "list.h"
struct a_list
{
struct list_head list;
int val;
const char *str;
};
#define CORE_PATH "/var/crash"
#define ULIMIT_CORE "ulimit -c unlimited"
#define CMD "LD_SHOW_AUXV=1 LD_PRELOAD=/lib/aarch64-linux-gnu/libSegFault.so ./ludtm"
#define __HELP "--help"
#define __H "-h"
#define CMD_SIZE 512
#define PARAM_SIZE 128
#define CMP(KEYWORD, BUF) strncmp(KEYWORD, BUF, sizeof(KEYWORD)) == 0
static char devcourse_mailing_list[] =
"Austin Kim <[email protected]>,"
"mike Seo <[email protected]>,"
"kkk kkk <[email protected]>,"
"Hello Stock <[email protected]>,"
"Paran Lee <[email protected]>";
void do_func(char[]);
#define SEGFAULT "segfault"
#define NULL_DEREFERENCE "null_dereference"
#define WRITE_WEIRED_AREA "write_weired_area"
#define STACK_CORRUPTION "stack_corruption"
#define STACK_OVERFLOW_RECURSIVE "stack_overflow_recursive"
#define STACK_OVERFLOW_OVERSIZE "stack_overflow_oversize"
#define HEAP_OVERFLOW "heap_overflow"
#define MEM_LEAK "mem_leak"
#define DOUBLE_FREE "double_free"
#define LIST_CONCURRENCY "list_concurrency"
#define WRONG_FUNTION_POINTER "wrong_funtion_pointer"
void segfault();
void null_dereference();
void write_weired_area();
void stack_corruption();
void stack_overflow_recursive();
void stack_overflow_oversize();
void heap_overflow();
void mem_leak();
void double_free();
void list_concurrency();
void wrong_funtion_pointer();
#define BIG_NUM 16384 * 2
void make_coredump(char buf[])
{
printf("Do %s\n", buf);
if (CMP(SEGFAULT, buf))
{
segfault();
}
else if (CMP(NULL_DEREFERENCE, buf))
{
null_dereference();
}
else if (CMP(WRITE_WEIRED_AREA, buf))
{
write_weired_area();
}
else if (CMP(STACK_CORRUPTION, buf))
{
stack_corruption();
}
else if (CMP(STACK_OVERFLOW_RECURSIVE, buf))
{
stack_overflow_recursive();
}
else if (CMP(STACK_OVERFLOW_OVERSIZE, buf))
{
stack_overflow_oversize();
}
else if (CMP(HEAP_OVERFLOW, buf))
{
heap_overflow();
}
else if (CMP(MEM_LEAK, buf))
{
mem_leak();
}
else if (CMP(DOUBLE_FREE, buf))
{
double_free();
}
else if (CMP(LIST_CONCURRENCY, buf))
{
list_concurrency();
}
else if (CMP(WRONG_FUNTION_POINTER, buf))
{
wrong_funtion_pointer();
}
}
void segfault()
{
int *a = 1;
*a = 1;
}
void null_dereference()
{
struct a_list *tmp = NULL;
tmp->str = "i am groot";
tmp->val = 0;
}
void write_weired_area()
{
int *p = malloc(0);
*(p + 0xffffffff) = 12345678;
}
void stack_corruption()
{
int a;
memset(&a, 0xdeadbeef, 0xdeadbeef);
}
static int stack_overflow_cnt = 0;
void stack_overflow_recursive()
{
char iamroot[] = "iamroot";
int cnt = stack_overflow_cnt++;
stack_overflow_recursive();
}
void stack_overflow_oversize()
{
typedef struct fat_struct
{
char str[BIG_NUM];
} fat;
fat bigboy[BIG_NUM] = { { {'B', }, }, };
fat biggirl[BIG_NUM] = { { {'G', }, } };
}
void heap_overflow()
{
char *pp = malloc(BIG_NUM);
memset(pp, 'X', BIG_NUM * BIG_NUM);
}
void mem_leak()
{
int i, n = 0;
char *pp[BIG_NUM];
for (n = 0; n < BIG_NUM * BIG_NUM; n++)
{
pp[n] = malloc(sizeof(devcourse_mailing_list) * 4);
if (pp[n] == NULL)
break;
}
printf("malloc failure after %d MiB\n", n);
for (i = 0; i < n; i++)
{
snprintf(pp[i],
devcourse_mailing_list,
devcourse_mailing_list,
devcourse_mailing_list,
devcourse_mailing_list,
"%s%s%s%s");
}
}
void double_free() {
struct a_list *tmp;
tmp = (struct a_list *)malloc(sizeof(struct a_list));
free(tmp);
free(tmp);
}
void append(struct a_list *ptr, const char *str, int val)
{
struct a_list *tmp;
tmp = (struct a_list *)malloc(sizeof(struct a_list));
if (!tmp)
{
perror("malloc");
exit(1);
}
tmp->str = str;
tmp->val = val;
list_add_tail(&(tmp->list), &(ptr->list));
}
void list_use_case()
{
struct a_list blist;
struct a_list *iter;
INIT_LIST_HEAD(&blist.list);
/* add item to list */
append(&blist, "NM", 87501);
append(&blist, "CA", 94041);
append(&blist, "IL", 60561);
/* iterates list */
list_for_each_entry(iter, &blist.list, list)
{
printf("%s %d\n", iter->str, iter->val);
}
/* remove all items in the list */
while (!list_empty(&blist.list))
{
iter = list_entry(blist.list.next, struct a_list, list);
list_del(&iter->list);
free(iter);
}
return 0;
}
#ifndef NUM_THREADS
#define NUM_THREADS 2
#endif
static atomic_int counter = 0;
static int max_iter = 1000;
void *pr_thrd(void *thr_id)
{
long tid;
tid = (long)thr_id;
printf("thread %ld started incrementing ID - %lu\n", tid, thrd_current());
for (int i = 0; i < max_iter; ++i)
{
counter += 1;
usleep(2000);
}
return NULL;
}
void concurrency_use_case()
{
thrd_t threads[NUM_THREADS];
int rc;
long t;
for (t = 0; t < NUM_THREADS; t++)
{
rc = thrd_create(&threads[t], (thrd_start_t)pr_thrd, (void *)t);
if (rc == thrd_error)
{
printf("ERORR; thrd_create() call failed\n");
exit(EXIT_FAILURE);
}
}
for (t = 0; t < NUM_THREADS; t++)
{
thrd_join(threads[t], NULL);
}
printf("count = %d\n", counter);
thrd_exit(EXIT_SUCCESS);
}
static struct a_list dirty_list;
void *list_concurrency_dirty_thrd(void *thr_id)
{
long tid;
tid = (long)thr_id;
printf("thread %ld started incrementing ID - %lu\n", tid, thrd_current());
for (int i = 0; i < max_iter; ++i)
{
counter += 1;
append(&dirty_list, "[email protected]", 1);
append(&dirty_list, "[email protected]", 2);
append(&dirty_list, "[email protected]", 3);
append(&dirty_list, "[email protected]", 4);
append(&dirty_list, "[email protected]", 5);
struct a_list *iter;
while (!list_empty(&dirty_list.list))
{
iter = list_entry(dirty_list.list.next, struct a_list, list);
list_del(&iter->list);
free(iter);
}
}
return NULL;
}
void list_concurrency()
{
thrd_t threads[NUM_THREADS];
int rc;
long t;
for (t = 0; t < NUM_THREADS; t++)
{
rc = thrd_create(&threads[t], (thrd_start_t)list_concurrency_dirty_thrd, (void *)t);
if (rc == thrd_error)
{
printf("ERORR; thrd_create() call failed\n");
exit(EXIT_FAILURE);
}
}
for (t = 0; t < NUM_THREADS; t++)
{
thrd_join(threads[t], NULL);
}
printf("count = %d\n", counter);
thrd_exit(EXIT_SUCCESS);
}
void fun(int a)
{
printf("Value of a is %d\n", a);
}
void wrong_funtion_pointer()
{
int notfun;
void (*fun_ptr)(int) = &fun;
(*fun_ptr)(10);
int (*notfun_ptr)(char*) = ¬fun;
(*notfun_ptr)('i am groot');
}
void ssu_show_limit(int rlim_type, char *rlim_name);
void self_ulimit_core_setup();
int self_sys_core_setup();
#define ssu_show(limit) ssu_show_limit(limit, #limit)
void ssu_show_limit(int rlim_type, char *rlim_name)
{
struct rlimit rlim;
char cur[11], max[11];
// 리소스 한계치를 얻어옴
getrlimit(rlim_type, &rlim);
// 무한대인지 아닌지 구분
if (rlim.rlim_cur == RLIM_INFINITY)
strcpy(cur, "infinity");
else
sprintf(cur, "%10ld", rlim.rlim_cur);
// 무한대인지 아닌지 구분
if (rlim.rlim_max == RLIM_INFINITY)
strcpy(max, "infinity");
else
sprintf(cur, "%10ld", rlim.rlim_cur);
printf("%15s : %10s %10s\n", rlim_name, cur, max);
}
void self_ulimit_core_setup()
{
struct rlimit l;
l.rlim_cur = RLIM_INFINITY;
l.rlim_max = RLIM_INFINITY;
int res = setrlimit(RLIMIT_CORE, &l);
if (res < 0)
{
perror("setrlimit");
exit(EXIT_FAILURE);
}
printf("Core dump size limit set to unlimited.\n");
}
int self_sys_core_setup()
{
int ret;
int fd;
fd = open("/proc/sys/kernel/core_pattern", O_WRONLY);
if (fd < 0)
{
perror("open core pattern fail");
exit(EXIT_FAILURE);
}
dprintf(fd, "%s/%s", CORE_PATH, "core-%e.%p.%h.%t");
close(fd);
fd = open("/proc/sys/kernel/core_pipe_limit", O_WRONLY);
if (fd < 0)
{
perror("open core pipe limit fail");
exit(EXIT_FAILURE);
}
dprintf(fd, "%d", 0);
close(fd);
}
int main(int argc, char *argv[])
{
pid_t cpid, w;
int status;
char cmd[CMD_SIZE];
char param[PARAM_SIZE];
self_sys_core_setup();
do
{
cpid = fork();
if (cpid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) /* Code executed by child */
{
printf("Child PID is %ld\n", (long)getpid());
if (argc == 1)
{
fgets(param, PARAM_SIZE, stdin);
fflush(stdin);
printf("%s\n", param);
snprintf(cmd, sizeof(cmd), "%s && %s %s", ULIMIT_CORE, CMD, param);
printf("%s\n", cmd);
system(cmd);
}
else if(CMP(argv[1], __H) || CMP(argv[1], __HELP))
{
printf(
"LUDTM(Linux User Dump Test Module)"
"\n"
"Make each dump:"
"\n"
" echo TYEP_OF_CRASH | ./ludtm"
"\n"
"Make various dump:"
"\n"
" echo segfault | ./ludtm"
"\n"
" echo null_dereference | ./ludtm"
"\n"
" echo write_weired_area | ./ludtm"
"\n"
" echo stack_corruption | ./ludtm"
"\n"
" echo stack_overflow_recursive | ./ludtm"
"\n"
" echo stack_overflow_oversize | ./ludtm"
"\n"
" echo heap_overflow | ./ludtm"
"\n"
" echo mem_leak | ./ludtm"
"\n"
" echo double_free | ./ludtm"
"\n"
" echo list_concurrency | ./ludtm"
"\n"
" echo wrong_funtion_pointer | ./ludtm"
"\n"
);
exit(EXIT_SUCCESS);
}
else
{
make_coredump(argv[1]);
}
pause(); /* Wait for signals */
_exit(atoi(argv[1]));
}
else /* Code executed by parent */
{
do
{
w = waitpid(cpid, &status, WUNTRACED);
if (w == -1)
{
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status))
{
printf("exited, status=%d\n", WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
{
char cmd[CMD_SIZE];
printf("killed by signal %d\n", WTERMSIG(status));
snprintf(cmd, sizeof(cmd), "ls -ahl %s/core*", CORE_PATH);
system(cmd);
}
else if (WIFSTOPPED(status))
{
printf("stopped by signal %d\n", WSTOPSIG(status));
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
} while (0);
exit(EXIT_SUCCESS);
}