Loading...
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 | #include <stdio.h> #include <strings.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <setjmp.h> #include <sys/mman.h> int test_func(); void catch_segv(int); jmp_buf resume; #define func_len 256 #define ALT_STK_SIZE (MINSIGSTKSZ + pagesize) #if __i386__ typedef unsigned int psint_t; #endif #if __x86_64__ typedef unsigned long long psint_t; #endif int verbose = 0; #define msg(...) do { if (verbose) printf(__VA_ARGS__); } while (0); /* * Test whether the architecture allows execution from the stack and heap data areas. What's * allowed varies by architecture due to backwards compatibility. We also run a separate test * where we turn on PROT_EXEC explicitly which should always allow execution to take place. * * The "expected" array tells us what the result of each test should be based on the architecture. * The code assumes the test numbers in the macros below are consecutive starting from 0. */ #define HEAP_TEST 0 #define HEAP_PROT_EXEC 1 #define STACK_TEST 2 #define STACK_PROT_EXEC 3 #define SUCCEED 1 #define FAIL -1 /* can't use 0 since setjmp uses that */ int expected[4] = { #if NXDATA32TESTNONX SUCCEED, /* execute from heap */ SUCCEED, /* exeucte from heap with PROT_EXEC */ FAIL, /* execute from stack */ SUCCEED, /* exeucte from stack with PROT_EXEC */ #elif __i386__ FAIL, /* execute from heap */ SUCCEED, /* exeucte from heap with PROT_EXEC */ FAIL, /* execute from stack */ SUCCEED, /* exeucte from stack with PROT_EXEC */ #endif #if __x86_64__ FAIL, /* execute from heap */ SUCCEED, /* exeucte from heap with PROT_EXEC */ FAIL, /* execute from stack */ SUCCEED, /* exeucte from stack with PROT_EXEC */ #endif }; main(int argc, char *argv[]) { int (*func)(); int result, test; char buf[func_len + 4]; psint_t base; unsigned int len; psint_t pagesize; size_t count; stack_t sigstk; struct sigaction sigact; char *cmd_name; int c; cmd_name = argv[0]; while ((c = getopt(argc, argv, "v")) != -1) { switch (c) { case 'v': verbose = 1; break; case '?': default: fprintf(stderr, "usage: data_exec [-v]\n"); exit(1); } } pagesize = getpagesize(); sigstk.ss_sp = malloc(ALT_STK_SIZE); sigstk.ss_size = ALT_STK_SIZE; sigstk.ss_flags = 0; if (sigaltstack(&sigstk, NULL) < 0) { perror("sigaltstack"); exit(1); } sigact.sa_handler = catch_segv; sigact.sa_flags = SA_ONSTACK; sigemptyset(&sigact.sa_mask); if (sigaction(SIGSEGV, &sigact, NULL) == -1) { perror("sigaction SIGSEGV"); exit(1); } if (sigaction(SIGBUS, &sigact, NULL) == -1) { perror("sigaction SIGBUS"); exit(1); } test = HEAP_TEST; restart: if ((result = setjmp(resume)) != 0) { if (result != expected[test]) { printf("%s: test %d failed, expected %d, got %d\n", cmd_name, test, expected[test], result); exit(2); } test++; goto restart; } switch (test) { case HEAP_TEST: msg("attempting to execute from malloc'ed area..\n"); func = (void *)malloc(func_len); func = (void *)((char *)func + ((psint_t)test_func & 0x3)); bcopy(test_func, func, func_len); result = (*func)(); msg("execution suceeded, result is %d\n\n", result); longjmp(resume, SUCCEED); case HEAP_PROT_EXEC: msg("attempting to execute from malloc'ed area with PROT_EXEC..\n"); func = (void *)malloc(func_len); func = (void *)((char *)func + ((psint_t)test_func & 0x3)); bcopy(test_func, func, func_len); base = (psint_t)func & ~(pagesize - 1); len = func_len + (psint_t)func - base; if(mprotect((void *)base, len, PROT_READ|PROT_WRITE|PROT_EXEC) == -1) { perror("mprotect of stack"); exit(1); } result = (*func)(); msg("execution suceeded, result is %d\n\n", result); longjmp(resume, SUCCEED); case STACK_TEST: msg("attempting to execute from stack...\n"); func = (void *)(buf + ((psint_t)test_func & 0x3)); bcopy(test_func, func, func_len); result = (*func)(); msg("stack execution suceeded, result from stack exec is %d\n\n", result); longjmp(resume, SUCCEED); case STACK_PROT_EXEC: msg("attempting to execute from stack with PROT_EXEC...\n"); func = (void *)(buf + ((psint_t)test_func & 0x3)); bcopy(test_func, func, func_len); base = (psint_t)func & ~(pagesize - 1); len = func_len + (psint_t)func - base; if(mprotect((void *)base, len, PROT_READ|PROT_WRITE|PROT_EXEC) == -1) { perror("mprotect of stack"); exit(1); } result = (*func)(); msg("stack execution suceeded, result from stack exec is %d\n", result); longjmp(resume, SUCCEED); } msg("All tests passed.\n"); exit(0); } int test_func() { return 42; } void catch_segv(int sig) { msg("got sig %d\n\n", sig); longjmp(resume, FAIL); } |