-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonality.c
90 lines (80 loc) · 2.46 KB
/
personality.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/personality.h>
#include <string.h>
#include <unistd.h>
char executable_code[128];
typedef void(function_call)();
void change_personality () {
int pers = personality(0xffffffffUL);
if (pers < 0L) {
perror("Error getting personality:");
}
printf("Initial Personality == %x ASR %s, READ_IMPLIES_EXEC %s\n", pers,
((pers & ADDR_NO_RANDOMIZE) ? "yes" : "no"),
((pers & READ_IMPLIES_EXEC) ? "yes" : "no")
);
int new_pers = personality(pers | ADDR_NO_RANDOMIZE | READ_IMPLIES_EXEC);
// int new_pers = personality(pers | READ_IMPLIES_EXEC);
if (new_pers < 0L) {
perror("Error adding ADDR_NO_RANDOMIZE and READ_IMPLIES_EXEC");
}
pers = personality(0xffffffffUL);
if (pers < 0L) {
perror("Error getting personality:");
}
printf("New Personality == %x ASR %s, READ_IMPLIES_EXEC %s\n", pers,
((pers & ADDR_NO_RANDOMIZE) ? "yes" : "no"),
((pers & READ_IMPLIES_EXEC) ? "yes" : "no")
);
}
int main(int argc, char *argv[]) {
printf("Exec Memory Tests using personality\n");
change_personality ();
int do_malloc=0;
int do_static=0;
int do_stack=0;
int do_brk=0;
if (argc==1) {
printf ("Running all tests\n");
do_malloc=1;
do_static=1;
do_stack=1;
do_brk=1;
} else {
for (int i=1;i<argc;i++) {
if (!strcmp (argv[i], "malloc")) do_malloc=1;
if (!strcmp (argv[i], "static")) do_static=1;
if (!strcmp (argv[i], "stack")) do_stack=1;
if (!strcmp (argv[i], "brk")) do_brk=1;
}
}
if(do_static) {
printf("Exec code in static memory\n");
executable_code [0] = 0xC3; // flat mode near return
function_call *f_static = (function_call *)&executable_code[0];
(*f_static) ();
}
if (do_malloc) {
printf("Exec code in malloc memory\n");
char * allocated = (char*) malloc (128);
allocated [0] = 0xC3; // flat mode near return
function_call *f_malloc = (function_call *)&allocated[0];
(*f_malloc) ();
}
if (do_brk) {
printf("Exec code allocated by brk and sbrk memory\n");
char * allocated = (char*) sbrk(4096);
allocated [0] = 0xC3; // flat mode near return
function_call *f_malloc = (function_call *)&allocated[0];
(*f_malloc) ();
}
if (do_stack) {
printf("Exec code in stack memory\n");
char stack[128];
stack [0] = 0xC3; // flat mode near return
function_call *f_stack = (function_call *)&stack[0];
(*f_stack) ();
}
return 0;
}