From c79aa14295f907ffac0cf5555515602b7393b8b6 Mon Sep 17 00:00:00 2001 From: netblue30 Date: Thu, 9 Mar 2023 08:39:25 -0500 Subject: testing --- test/seccomp-extra/memwrexe.c | 105 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 test/seccomp-extra/memwrexe.c (limited to 'test/seccomp-extra/memwrexe.c') 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 @@ +// This file is part of Firejail project +// Copyright (C) 2014-2023 Firejail Authors +// License GPL v2 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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); + if (p == MAP_FAILED) { + printf("mmap failed\n"); + return 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 == MAP_FAILED) { + fprintf(stderr, "TESTING ERROR: cannot map file for mprotect test\n"); + return 1; + } + + int rv = mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC); + if (rv) { + printf("mprotect failed\n"); + return 1; + } + + 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) { + printf("memfd_create failed\n"); + return 1; + } + printf("memfd_create successful\n"); + + // wait for expect to timeout + sleep(100); + + return 0; + } +} -- cgit v1.2.3-70-g09d2