about summary refs log tree commit diff stats
path: root/tests/test21_alt.c
blob: 748650231738d14f9ea7a9517a8588b962cba3c7 (plain) (blame)
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
#define __USE_GNU
#include <signal.h>
#include <setjmp.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/ucontext.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>

static jmp_buf context_buf;

static void segv_handler(int sig)
{
	printf("sig = %d\n", sig);
	siglongjmp(context_buf, 1);
}

void test()
{
	if(!setjmp(context_buf)) {
		int *bad_ptr = (int*)0xffffffffdeadbeef;
		printf("*bad_ptr = %d\n", *bad_ptr);
	} else {
		printf("got bad_ptr\n");
	}
}

void* exec_p = NULL;
typedef void(*vFv_t)(void);
#define X_IP		16
#define X_TRAPNO 	20
#define X_ERR		19
static void segv_action(int sig, siginfo_t* info, ucontext_t* ucntx)
{
	if(!exec_p) {
		segv_handler(sig);
		return;
	}
	printf("sig = %d\n", sig);
	uintptr_t rip = (intptr_t)ucntx->uc_mcontext.gregs[X_IP];
	if(info->si_addr>=exec_p && info->si_addr<(exec_p+10))
		printf("si_addr: exec_p+%zx, ", (uintptr_t)info->si_addr-(uintptr_t)exec_p);
	else
		printf("si_addr: %zx, ", info->si_addr);
	printf("si_code: %d, si_errno: %d, ", info->si_code, info->si_errno);
	if(rip>=((intptr_t)exec_p) && rip<((intptr_t)exec_p+5))
		printf("RIP offset: %zd, ", rip-((intptr_t)exec_p));
	printf("TRAPERR=0x%x TRAPNO=%d\n", 
		ucntx->uc_mcontext.gregs[X_ERR],
		ucntx->uc_mcontext.gregs[X_TRAPNO]
	);
	siglongjmp(context_buf, 1);
}

static unsigned char buff_cc[] = { 0xcc, 0xc3 };
static unsigned char buff_cd03[] = { 0xcd, 0x03, 0xc3 };
static unsigned char buff_cd2d[] = { 0xcd, 0x2d, 0xc3 };
static uint8_t buff_simplef[] = { 0xb8, 1, 0, 0, 0, 0xc3 };
void test_cc()
{
	memcpy(exec_p, buff_cc, sizeof(buff_cc));
	if(!setjmp(context_buf)) {
		vFv_t f = (vFv_t)exec_p;
		f();
	}
	memcpy(exec_p, buff_cd03, sizeof(buff_cd03));
	if(!setjmp(context_buf)) {
		vFv_t f = (vFv_t)exec_p;
		f();
	}
	memcpy(exec_p, buff_cd2d, sizeof(buff_cd2d));
	if(!setjmp(context_buf)) {
		vFv_t f = (vFv_t)exec_p;
		f();
	}
}

void test_segfault()
{
	printf("from non-existant memory\n");
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		int *bad_ptr = (int*)0xffffffffdeadbeef;
		*(uint8_t*)bad_ptr = 0xc3;
	} else {
		printf("segfault, good\n");
	}
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		int *bad_ptr = (int*)0xffffffffdeadbeef;
		if(*(uint8_t*)bad_ptr == 0xc3)
			printf("should not be readable or writeable!\n");
		else
			printf("should not be readable!\n");
		printf("aborting test\n");
		return;
	} else {
		printf("segfault, good\n");
	}
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		void* bad_ptr = (int*)0xffffffffdeadbeef;
		void(*f)() = bad_ptr;
		f();
		printf("should not work!!! aboting test\n");
		return;
	} else {
		printf("segfault, good\n");
	}
	printf("from NULL memory\n");
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		int *bad_ptr = (int*)NULL;
		*(uint8_t*)bad_ptr = 0xc3;
	} else {
		printf("segfault, good\n");
	}
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		int *bad_ptr = (int*)NULL;
		if(*(uint8_t*)bad_ptr == 0xc3)
			printf("should not be readable or writeable!\n");
		else
			printf("should not be readable!\n");
		printf("aborting test\n");
		return;
	} else {
		printf("segfault, good\n");
	}
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		void* bad_ptr = (int*)NULL;
		void(*f)() = bad_ptr;
		f();
		printf("should not work!!! aboting test\n");
		return;
	} else {
		printf("segfault, good\n");
	}
	printf("from existant memory\n");
	printf("exec_p prot = 0\n");
	mprotect(exec_p, 65536, 0);
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		*(uint8_t*)exec_p = 0xc3;
	} else {
		printf("segfault, good\n");
	}
	// reading for exising protected memory
	if(!setjmp(context_buf)) {
		if(*(uint8_t*)exec_p == 0xc3)
			printf("Error, this value should not be 0xc3\n");
	} else {
		printf("segfault, good\n");
	}
	printf("exec_p prot = PROT_READ\n");
	mprotect(exec_p, 65536, PROT_READ);
	// writing to existing protected memory
	if(!setjmp(context_buf)) {
		*(uint8_t*)exec_p = 0xc3;
	} else {
		printf("segfault, good\n");
	}
	// reading should work
	if(!setjmp(context_buf)) {
		if(*(uint8_t*)exec_p == 0xc3)
			printf("Error, this value should not be 0xc3\n");
	} else {
		printf("segfault, not good....\n");
	}
	// reading should work
	if(!setjmp(context_buf)) {
		if(*(uint8_t*)exec_p == 0xc3)
			printf("Error, this value should not be 0xc3\n");
	} else {
		printf("segfault, good\n");
	}
	printf("exec_p prot = PROT_READ|PROT_WRITE\n");
	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE);
	// writing should
	if(!setjmp(context_buf)) {
		*(uint8_t*)exec_p = 0xc3;
	} else {
		printf("segfault, not good, aborting test\n");
		return;
	}
	// reading should work
	if(!setjmp(context_buf)) {
		if(*(uint8_t*)exec_p != 0xc3) {
			printf("Error, this value should be 0xc3, aborting test\n");
			return;
		}
	} else {
		printf("segfault, not good....\n");
	}
	// should not be able to run
	if(!setjmp(context_buf)) {
		vFv_t f = (vFv_t)exec_p;
		f();
	} else {
		printf("Cannot run, good\n");
	}
	printf("exec_p prot = PROT_READ|PROT_WRITE|PROT_EXEC\n");
	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE|PROT_EXEC);
	if(!setjmp(context_buf)) {
		vFv_t f = (vFv_t)exec_p;
		f();
	} else {
		printf("Cannot run, not good!\n");
	}
	printf("exec_p prot = PROT_READ|PROT_WRITE\n");
	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE);
	memcpy(exec_p, buff_simplef, sizeof(buff_simplef));
	if(!setjmp(context_buf)) {
		int(*f)() = exec_p;
		if(f()!=1) {
			printf("function return should be 1\n");
		}
	} else {
		printf("Cannot run, good!\n");
	}
	printf("exec_p prot = PROT_READ|PROT_WRITE|PROT_EXEC\n");
	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE|PROT_EXEC);
	if(!setjmp(context_buf)) {
		int(*f)() = exec_p;
		if(f()!=1) {
			printf("function return should be 1\n");
		}
		((uint8_t*)exec_p)[1] = 2;
		if(f()!=2) {
			printf("function return should be 2\n");
		}
	} else {
		printf("Cannot run, not good, aborting test!\n");
	}
}

int main()
{
    struct sigaction action = {0};
    action.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
    action.sa_sigaction = (void*)segv_action;
    if(sigaction(SIGSEGV, &action, NULL)) {
		printf("sigaction: Err = %d\n", errno);
		return -2;
	}
    if(sigaction(SIGTRAP, &action, NULL)) {
		printf("sigaction 2: Err = %d\n", errno);
		return -2;
	}
	exec_p = NULL;
	test();
	exec_p = mmap(NULL, 65536, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
	if(exec_p==MAP_FAILED) {
		printf("mmap: Err = %d\n", errno);
		return -3;
	}
	test_cc();
	test_segfault();
	return 0;
}