selinux-testsuite: Add tests for mmap/mprotect
PATCH 1/1 selinux-testsuite: Add tests for mmap/mprotect
Add a set of tests for mmap and mprotect, covering mmap_zero, execmem, execstack, execheap, execmod, file execute for shared anonymous mappings (internal shmem /dev/zero file), and file execute for regular files.
Signed-off-by Stephen Smalley <[email protected]>
--- ToDo | 1 - policy/Makefile | 3 +- policy/test_mmap.te | 116 +++++++++++++++++++++++++++++ tests/Makefile | 2 +- tests/mmap/Makefile | 6 ++ tests/mmap/mmap_anon_private.c | 19 +++++ tests/mmap/mmap_anon_shared.c | 19 +++++ tests/mmap/mmap_file_private.c | 34 +++++++++ tests/mmap/mmap_file_shared.c | 34 +++++++++ tests/mmap/mmap_zero.c | 19 +++++ tests/mmap/mprotect_anon_private.c | 26 +++++++ tests/mmap/mprotect_anon_shared.c | 26 +++++++ tests/mmap/mprotect_file_private_execmod.c | 41 ++++++++++ tests/mmap/mprotect_file_private_rwx.c | 39 ++++++++++ tests/mmap/mprotect_file_private_rx.c | 39 ++++++++++ tests/mmap/mprotect_file_shared.c | 39 ++++++++++ tests/mmap/mprotect_heap.c | 26 +++++++ tests/mmap/mprotect_stack.c | 23 ++++++ tests/mmap/test | 96 ++++++++++++++++++++++++ 19 files changed, 605 insertions(+), 3 deletions(-) create mode 100644 policy/test_mmap.te create mode 100644 tests/mmap/Makefile create mode 100644 tests/mmap/mmap_anon_private.c create mode 100644 tests/mmap/mmap_anon_shared.c create mode 100644 tests/mmap/mmap_file_private.c create mode 100644 tests/mmap/mmap_file_shared.c create mode 100644 tests/mmap/mmap_zero.c create mode 100644 tests/mmap/mprotect_anon_private.c create mode 100644 tests/mmap/mprotect_anon_shared.c create mode 100644 tests/mmap/mprotect_file_private_execmod.c create mode 100644 tests/mmap/mprotect_file_private_rwx.c create mode 100644 tests/mmap/mprotect_file_private_rx.c create mode 100644 tests/mmap/mprotect_file_shared.c create mode 100644 tests/mmap/mprotect_heap.c create mode 100644 tests/mmap/mprotect_stack.c create mode 100755 tests/mmap/test
ToDo +0 -1
@@ -11,7 +11,6 @@ Add tests for netlink sockets, including nlmsg_read/write/... permissions. ioctl: Test new ioctl whitelisting feature. Generalize or duplicate nnp tests for nosuid mount case. Add tests for mount and umount, including permission checks and context-related mount options. -Add tests for mmap/mprotect for execmem, execstack, execheap, execmod, mmap_zero. Add tests for CAP_MAC_ADMIN/mac_admin and its effect on setting/getting security contexts unknown to the currently loaded policy. Add tests for new file labeling: type_transition, setfscreatecon, context mounts. Add tests for inheritance across domain-changing exec: noatsecure, siginh, rlimitinh.
policy/Makefile +2 -1
@@ -19,7 +19,8 @@ TARGETS = \ test_setnice.te test_sigkill.te test_stat.te test_sysctl.te \ test_task_create.te test_task_getpgid.te test_task_getsched.te \ test_task_getsid.te test_task_setpgid.te test_task_setsched.te \ - test_transition.te test_inet_socket.te test_unix_socket.te test_wait.te + test_transition.te test_inet_socket.te test_unix_socket.te \ + test_wait.te test_mmap.te ifeq ($(shell [ $(POL_VERS) -ge 24 ] && echo true),true) TARGETS += test_bounds.te
policy/test_mmap.te new file +116 -0
116 lines not on this page · raw message
tests/Makefile +1 -1
@@ -3,7 +3,7 @@ export CFLAGS+=-g -O0 -Wall -D_GNU_SOURCE DISTRO=$(shell ./os_detect) -SUBDIRS_COMMON:=domain_trans entrypoint execshare exectrace execute_no_trans fdreceive inherit link mkdir msg open ptrace readlink relabel rename rxdir sem setattr setnice shm sigkill stat sysctl task_create task_setnice task_setscheduler task_getscheduler task_getsid task_getpgid task_setpgid wait file ioctl capable_file capable_net capable_sys unix_socket +SUBDIRS_COMMON:=domain_trans entrypoint execshare exectrace execute_no_trans fdreceive inherit link mkdir msg open ptrace readlink relabel rename rxdir sem setattr setnice shm sigkill stat sysctl task_create task_setnice task_setscheduler task_getscheduler task_getsid task_getpgid task_setpgid wait file ioctl capable_file capable_net capable_sys unix_socket mmap SUBDIRS:= $(SUBDIRS_COMMON) dyntrans dyntrace bounds nnp inet_socket
tests/mmap/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +TARGETS=$(patsubst %.c,%,$(wildcard *.c)) + +all: $(TARGETS) + +clean: + rm -f $(TARGETS)
tests/mmap/mmap_anon_private.c new file +19 -0
@@ -0,0 +1,19 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +char *ptr; + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mmap_anon_shared.c new file +19 -0
@@ -0,0 +1,19 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +char *ptr; + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mmap_file_private.c new file +34 -0
@@ -0,0 +1,34 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> + +int main (int argc,char **argv) +{ +char *ptr; +int fd; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s file\n" , argv[0 ]); +exit (1 ); + } + + fd =open (argv[1 ],O_RDONLY ); +if (fd <0 ) { +perror (argv[1 ]); +exit (1 ); + } + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE , fd,0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +close (fd); +exit (1 ); + } +close (fd); +exit (0 ); +} +
tests/mmap/mmap_file_shared.c new file +34 -0
@@ -0,0 +1,34 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> + +int main (int argc,char **argv) +{ +char *ptr; +int fd; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s file\n" , argv[0 ]); +exit (1 ); + } + + fd =open (argv[1 ],O_RDWR ); +if (fd <0 ) { +perror (argv[1 ]); +exit (1 ); + } + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_SHARED , fd,0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +close (fd); +exit (1 ); + } +close (fd); +exit (0 ); +} +
tests/mmap/mmap_zero.c new file +19 -0
@@ -0,0 +1,19 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +char *ptr; + + ptr =mmap (0 ,4096 ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_anon_private.c new file +26 -0
@@ -0,0 +1,26 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +char *ptr; +int rc; + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 , +0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } + + rc =mprotect (ptr,4096 ,PROT_READ |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_anon_shared.c new file +26 -0
@@ -0,0 +1,26 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +char *ptr; +int rc; + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 , +0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } + + rc =mprotect (ptr,4096 ,PROT_READ |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_file_private_execmod.c new file +41 -0
41 lines not on this page · raw message
tests/mmap/mprotect_file_private_rwx.c new file +39 -0
@@ -0,0 +1,39 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> + +int main (int argc,char **argv) +{ +char *ptr; +int rc; +int fd; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s file\n" , argv[0 ]); +exit (1 ); + } + + fd =open (argv[1 ],O_RDONLY ); +if (fd <0 ) { +perror (argv[1 ]); +exit (1 ); + } + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd, +0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } + + rc =mprotect (ptr,4096 ,PROT_READ |PROT_WRITE |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_file_private_rx.c new file +39 -0
@@ -0,0 +1,39 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> + +int main (int argc,char **argv) +{ +char *ptr; +int rc; +int fd; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s file\n" , argv[0 ]); +exit (1 ); + } + + fd =open (argv[1 ],O_RDONLY ); +if (fd <0 ) { +perror (argv[1 ]); +exit (1 ); + } + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd, +0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } + + rc =mprotect (ptr,4096 ,PROT_READ |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_file_shared.c new file +39 -0
@@ -0,0 +1,39 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> + +int main (int argc,char **argv) +{ +char *ptr; +int rc; +int fd; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s file\n" , argv[0 ]); +exit (1 ); + } + + fd =open (argv[1 ],O_RDWR ); +if (fd <0 ) { +perror (argv[1 ]); +exit (1 ); + } + + ptr =mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_SHARED , fd, +0 ); +if (ptr ==MAP_FAILED ) { +perror ("mmap" ); +exit (1 ); + } + + rc =mprotect (ptr,4096 ,PROT_READ |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_heap.c new file +26 -0
@@ -0,0 +1,26 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +void *ptr; +int rc; +int pagesize =getpagesize (); + + rc =posix_memalign (&ptr, pagesize, pagesize); +if (rc) { +fprintf (stderr,"posix_memalign failed: %d\n" , rc); +exit (1 ); + } + + rc =mprotect (ptr,4096 ,PROT_READ |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/mprotect_stack.c new file +23 -0
@@ -0,0 +1,23 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/mman.h> + +int main (void ) +{ +char buf[4096 ]; +int rc; +void *ptr; +long pagesize =sysconf (_SC_PAGESIZE); + + ptr = (void *) (((unsigned long ) buf) & ~(pagesize -1 )); + + rc =mprotect (ptr, pagesize,PROT_READ |PROT_WRITE |PROT_EXEC ); +if (rc <0 ) { +perror ("mprotect" ); +exit (1 ); + } +exit (0 ); +} +
tests/mmap/test new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.1.0