File: stacktest.c

package info (click to toggle)
kernel-patch-2.2.18-openwall 1.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 116 kB
  • ctags: 23
  • sloc: ansic: 194; sh: 55; makefile: 32
file content (76 lines) | stat: -rw-r--r-- 1,171 bytes parent folder | download
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>

void caller(void (*trampoline)())
{
	puts("Attempting to call a trampoline...");

	trampoline();
}

void do_trampoline()
{
	void nested()
	{
		puts("Succeeded.");
	}

	caller(nested);
}

void do_exploit()
{
	puts("Attempting to simulate a buffer overflow exploit...");

#ifdef __i386__
	__asm__ __volatile__(
		"movl $1f,%%eax\n\t"
		".byte 0x68; popl %%ecx; jmp *%%eax; nop\n\t"
		"pushl %%esp\n\t"
		"ret\n\t"
		"1:"
	: : : "ax", "cx");
#else
#error Wrong architecture
#endif

	puts("Succeeded.");
}

#define USAGE \
"Usage: %s OPTION\n" \
"Non-executable user stack area tests\n\n" \
"  -t\tcall a GCC trampoline\n" \
"  -e\tsimulate a buffer overflow exploit\n" \
"  -b\tsimulate an exploit after a trampoline call\n"

void usage(char *name)
{
	printf(USAGE, name ? name : "stacktest");
	exit(1);
}

int main(int argc, char **argv)
{
	if (argc != 2) usage(argv[0]);
	if (argv[1][0] != '-' || strlen(argv[1]) != 2) usage(argv[0]);

	switch (argv[1][1]) {
	case 't':
		do_trampoline();
		break;

	case 'b':
		do_trampoline();

	case 'e':
		do_exploit();
		break;

	default:
		usage(argv[0]);
	}

	return 0;
}