aboutsummaryrefslogtreecommitdiffstats
path: root/test/filters/memwrexe.c
blob: 797e7881d920bbd2829ec7da868c88a2a816d259 (plain) (blame)
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
// This file is part of Firejail project
// Copyright (C) 2014-2020 Firejail Authors
// License GPL v2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/syscall.h>

static void usage(void) {
	printf("memwrexe options\n");
	printf("where options is:\n");
	printf("\tmmap - mmap test\n");
	printf("\tmprotect - mprotect test\n");
	printf("\tmemfd_create - memfd_create test\n");
}

int main(int argc, char **argv) {
	if (argc != 2) {
		fprintf(stderr, "TESTING ERROR: memwrexe insufficient params\n");
		usage();
		return 1;
	}

	if (strcmp(argv[1], "mmap") == 0) {
		// open some file
		int fd = open("memwrexe.c", O_RDONLY);
		if (fd == -1) {
			fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
			return 1;
		}

		int size = lseek(fd, 0, SEEK_END);
		if (size == -1) {
			fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
			return 1;
		}

		void *p = mmap (0, size, PROT_WRITE|PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
		printf("mmap successful\n");

		// wait for expect to timeout
		sleep(100);

		return 0;
	}

	else if (strcmp(argv[1], "mprotect") == 0) {
		// open some file
		int fd = open("memwrexe.c", O_RDWR);
		if (fd == -1) {
			fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
			return 1;
		}

		int size = lseek(fd, 0, SEEK_END);
		if (size == -1) {
			fprintf(stderr, "TESTING ERROR: file not found, cannot run mmap test\n");
			return 1;
		}

		void *p = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0);
		if (!p) {
			fprintf(stderr, "TESTING ERROR: cannot map file for mprotect test\n");
			return 1;
		}

		mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC);
		printf("mprotect successful\n");

		// wait for expect to timeout
		sleep(100);

		return 0;
	}

	else if (strcmp(argv[1], "memfd_create") == 0) {
		int fd = syscall(SYS_memfd_create, "memfd_create", 0);
		if (fd == -1) {
			fprintf(stderr, "TESTING ERROR: cannot run memfd_create test\n");
			return 1;
		}
		printf("memfd_create successful\n");

		// wait for expect to timeout
		sleep(100);

		return 0;
	}
}