diff options
author | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2002-08-27 16:37:43 +0000 |
---|---|---|
committer | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2002-08-27 16:37:43 +0000 |
commit | c18809045992e855f33f5d025e76f2f3079dc485 (patch) | |
tree | 1eedfd1d942d18b35422d35ee285d942fba8fe4f /regress/sys | |
parent | 1ec002835ef6a2eea49aca2991ad3326a128e7f7 (diff) |
A test for non executable heap / mprotect. Ok art@
Diffstat (limited to 'regress/sys')
-rw-r--r-- | regress/sys/kern/Makefile | 4 | ||||
-rw-r--r-- | regress/sys/kern/nxheap-mprotect/Makefile | 15 | ||||
-rw-r--r-- | regress/sys/kern/nxheap-mprotect/nxheap-mprotect.c | 76 |
3 files changed, 93 insertions, 2 deletions
diff --git a/regress/sys/kern/Makefile b/regress/sys/kern/Makefile index a9b6964d4a3..e4ca7d97d17 100644 --- a/regress/sys/kern/Makefile +++ b/regress/sys/kern/Makefile @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile,v 1.36 2002/08/21 12:54:39 espie Exp $ +# $OpenBSD: Makefile,v 1.37 2002/08/27 16:37:42 matthieu Exp $ SUBDIR+= execve getrusage kqueue mmap mmap2 mmap3 dup2 minherit rlimit-file SUBDIR+= fcntl_dup dup2_self pread preadv exit pwrite pwritev SUBDIR+= syscall __syscall unfdpass accept nanosleep sysvmsg sysvsem SUBDIR+= sysvshm rfork gettimeofday signal nxstack nxbss -SUBDIR+= exec_self +SUBDIR+= exec_self nxheap-mprotect install: diff --git a/regress/sys/kern/nxheap-mprotect/Makefile b/regress/sys/kern/nxheap-mprotect/Makefile new file mode 100644 index 00000000000..48aa0547e73 --- /dev/null +++ b/regress/sys/kern/nxheap-mprotect/Makefile @@ -0,0 +1,15 @@ +# $OpenBSD: Makefile,v 1.1 2002/08/27 16:37:42 matthieu Exp $ + +.if ${MACHINE} == "sparc64" || ${MACHINE} == "sparc" || \ + ${MACHINE} == "alpha" || ${MACHINE} == "macppc" || \ + ${MACHINE} == "mvmeppc" || ${MACHINE} == "i386" || \ + ${MACHINE} == "hppa" + +PROG= nxheap-mprotect +LDSTATIC= ${STATIC} + +.else +REGRESSTARGETS= # this architecture has no test for this +.endif + +.include <bsd.regress.mk> diff --git a/regress/sys/kern/nxheap-mprotect/nxheap-mprotect.c b/regress/sys/kern/nxheap-mprotect/nxheap-mprotect.c new file mode 100644 index 00000000000..17d08c2fd1f --- /dev/null +++ b/regress/sys/kern/nxheap-mprotect/nxheap-mprotect.c @@ -0,0 +1,76 @@ +/* $OpenBSD: nxheap-mprotect.c,v 1.1 2002/08/27 16:37:42 matthieu Exp $ */ + +/* + * Copyright (c) 2002 Michael Shalayeff + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/mman.h> +#include <stdio.h> +#include <signal.h> +#include <string.h> + +#define SIZE 256 /* assuming the testfly() will fit */ + + +volatile sig_atomic_t fail; + +void +testfly() +{ +} + +void +sigsegv(int sig) +{ + _exit(fail); +} + +int +main(void) +{ + u_int64_t *buf; + + buf = (u_int64_t *)malloc(SIZE); + + signal(SIGSEGV, sigsegv); + memcpy(buf, &testfly, SIZE); + + printf("making it execute\n"); + /* here we must fail on segv since we said it gets executable */ + fail = 1; + mprotect(buf, sizeof(buf), PROT_READ|PROT_WRITE|PROT_EXEC); + ((void (*)(void))buf)(); + + printf("making it catch a signal\n"); + /* here we are successfull on segv and fail if it still executes */ + fail = 0; + mprotect(buf, sizeof(buf), PROT_READ|PROT_WRITE); + ((void (*)(void))&buf)(); + + exit(1); +} |