aboutsummaryrefslogtreecommitdiffstats
path: root/test/seccomp-extra/memwrexe.c
diff options
context:
space:
mode:
authorLibravatar netblue30 <netblue30@protonmail.com>2023-03-09 08:39:25 -0500
committerLibravatar netblue30 <netblue30@protonmail.com>2023-03-09 08:39:25 -0500
commitc79aa14295f907ffac0cf5555515602b7393b8b6 (patch)
tree87a114af4e12388e09e2d16d518b50be9ddbe0a6 /test/seccomp-extra/memwrexe.c
parenttesting (diff)
downloadfirejail-c79aa14295f907ffac0cf5555515602b7393b8b6.tar.gz
firejail-c79aa14295f907ffac0cf5555515602b7393b8b6.tar.zst
firejail-c79aa14295f907ffac0cf5555515602b7393b8b6.zip
testing
Diffstat (limited to 'test/seccomp-extra/memwrexe.c')
-rw-r--r--test/seccomp-extra/memwrexe.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/test/seccomp-extra/memwrexe.c b/test/seccomp-extra/memwrexe.c
new file mode 100644
index 000000000..548320df9
--- /dev/null
+++ b/test/seccomp-extra/memwrexe.c
@@ -0,0 +1,105 @@
1// This file is part of Firejail project
2// Copyright (C) 2014-2023 Firejail Authors
3// License GPL v2
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <fcntl.h>
12#include <sys/mman.h>
13#include <sys/syscall.h>
14
15static void usage(void) {
16 printf("memwrexe options\n");
17 printf("where options is:\n");
18 printf("\tmmap - mmap test\n");
19 printf("\tmprotect - mprotect test\n");
20 printf("\tmemfd_create - memfd_create test\n");
21}
22
23int main(int argc, char **argv) {
24 if (argc != 2) {
25 fprintf(stderr, "TESTING ERROR: memwrexe insufficient params\n");
26 usage();
27 return 1;
28 }
29
30 if (strcmp(argv[1], "mmap") == 0) {
31 // open some file
32 int fd = open("memwrexe.c", O_RDONLY);
33 if (fd == -1) {
34 fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
35 return 1;
36 }
37
38 int size = lseek(fd, 0, SEEK_END);
39 if (size == -1) {
40 fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
41 return 1;
42 }
43
44 void *p = mmap (0, size, PROT_WRITE|PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
45 if (p == MAP_FAILED) {
46 printf("mmap failed\n");
47 return 0;
48 }
49
50 printf("mmap successful\n");
51
52 // wait for expect to timeout
53 sleep(100);
54
55 return 0;
56 }
57
58 else if (strcmp(argv[1], "mprotect") == 0) {
59 // open some file
60 int fd = open("memwrexe.c", O_RDWR);
61 if (fd == -1) {
62 fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
63 return 1;
64 }
65
66 int size = lseek(fd, 0, SEEK_END);
67 if (size == -1) {
68 fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
69 return 1;
70 }
71
72 void *p = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0);
73 if (p == MAP_FAILED) {
74 fprintf(stderr, "TESTING ERROR: cannot map file for mprotect test\n");
75 return 1;
76 }
77
78 int rv = mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC);
79 if (rv) {
80 printf("mprotect failed\n");
81 return 1;
82 }
83
84 printf("mprotect successful\n");
85
86 // wait for expect to timeout
87 sleep(100);
88
89 return 0;
90 }
91
92 else if (strcmp(argv[1], "memfd_create") == 0) {
93 int fd = syscall(SYS_memfd_create, "memfd_create", 0);
94 if (fd == -1) {
95 printf("memfd_create failed\n");
96 return 1;
97 }
98 printf("memfd_create successful\n");
99
100 // wait for expect to timeout
101 sleep(100);
102
103 return 0;
104 }
105}