/
test_exec_openbsd_c.in
71 lines (60 loc) · 1.12 KB
/
test_exec_openbsd_c.in
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
/*
Copyright (C) 2004-2009, Parrot Foundation.
$Id$
test for exec privs
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <limits.h>
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#ifndef PAGE_SIZE
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
#endif
/*
* c equiv:
int t() {
return 1;
}
*/
char code[] = {
0xB8, 0x01, 0, 0, 0, /* movl $1, %eax */
0xC3 /* ret */
};
typedef int (*pf)(void);
int
main(int argc, char *argv[])
{
pf t;
char *p;
int rc;
int prot = PROT_READ;
if (argc != 2) {
fprintf(stderr, "usage: test 0 | 1\n");
exit(1);
}
if (atoi(argv[1]))
prot |= PROT_EXEC;
p = malloc(PAGE_SIZE);
memcpy(p, code, sizeof (code));
t = (pf) p;
rc = mprotect(p, PAGE_SIZE, prot);
if (rc) {
fprintf(stderr, "p = %p PAGE_SIZE = %d (0x%x)\n", p,
PAGE_SIZE, PAGE_SIZE);
perror("failure");
}
if (t() == 1)
puts("ok");
else
return 1;
return 0;
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/