diff options
author | netblue30 <netblue30@yahoo.com> | 2017-08-18 08:09:38 -0400 |
---|---|---|
committer | netblue30 <netblue30@yahoo.com> | 2017-08-18 08:09:38 -0400 |
commit | ad262caef9f095e00ce51945020142838d93960e (patch) | |
tree | f592b6bdba5b159cfe7e09e79c1dce8b8535fd46 /test/filters/memwrexe.c | |
parent | private-lib (diff) | |
download | firejail-ad262caef9f095e00ce51945020142838d93960e.tar.gz firejail-ad262caef9f095e00ce51945020142838d93960e.tar.zst firejail-ad262caef9f095e00ce51945020142838d93960e.zip |
memory-deny-write-execute testing
Diffstat (limited to 'test/filters/memwrexe.c')
-rw-r--r-- | test/filters/memwrexe.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/test/filters/memwrexe.c b/test/filters/memwrexe.c new file mode 100644 index 000000000..7e14aa23d --- /dev/null +++ b/test/filters/memwrexe.c | |||
@@ -0,0 +1,76 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include <unistd.h> | ||
5 | #include <sys/types.h> | ||
6 | #include <sys/stat.h> | ||
7 | #include <fcntl.h> | ||
8 | #include <sys/mman.h> | ||
9 | |||
10 | static void usage(void) { | ||
11 | printf("memwrexe options\n"); | ||
12 | printf("where options is:\n"); | ||
13 | printf("\tmmap - mmap test\n"); | ||
14 | printf("\tmprotect - mprotect test\n"); | ||
15 | } | ||
16 | |||
17 | int main(int argc, char **argv) { | ||
18 | if (argc != 2) { | ||
19 | fprintf(stderr, "TESTING ERROR: memwrexe insufficient params\n"); | ||
20 | usage(); | ||
21 | return 1; | ||
22 | } | ||
23 | |||
24 | if (strcmp(argv[1], "mmap") == 0) { | ||
25 | // open some file | ||
26 | int fd = open("memwrexe.c", O_RDONLY); | ||
27 | if (fd == -1) { | ||
28 | fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n"); | ||
29 | return 1; | ||
30 | } | ||
31 | |||
32 | int size = lseek(fd, 0, SEEK_END); | ||
33 | if (size == -1) { | ||
34 | fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n"); | ||
35 | return 1; | ||
36 | } | ||
37 | |||
38 | void *p = mmap (0, size, PROT_WRITE|PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); | ||
39 | printf("mmap successful\n"); | ||
40 | |||
41 | // wait for expect to timeout | ||
42 | sleep(100); | ||
43 | |||
44 | return 0; | ||
45 | } | ||
46 | |||
47 | else if (strcmp(argv[1], "mprotect") == 0) { | ||
48 | // open some file | ||
49 | int fd = open("memwrexe.c", O_RDWR); | ||
50 | if (fd == -1) { | ||
51 | fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n"); | ||
52 | return 1; | ||
53 | } | ||
54 | |||
55 | int size = lseek(fd, 0, SEEK_END); | ||
56 | if (size == -1) { | ||
57 | fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n"); | ||
58 | return 1; | ||
59 | } | ||
60 | |||
61 | void *p = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0); | ||
62 | if (!p) { | ||
63 | fprintf(stderr, "TESTING ERROR: cannot map file for mprotect test\n"); | ||
64 | return 1; | ||
65 | } | ||
66 | |||
67 | mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC); | ||
68 | printf("mprotect successful\n"); | ||
69 | |||
70 | // wait for expect to timeout | ||
71 | sleep(100); | ||
72 | |||
73 | return 0; | ||
74 | } | ||
75 | } | ||
76 | \ No newline at end of file | ||