Module Name:    src
Committed By:   jym
Date:           Mon Jul 18 23:16:11 UTC 2011

Modified Files:
        src/tests/lib/libc/sys: Makefile t_mprotect.c
Added Files:
        src/tests/lib/libc/arch: Makefile.exec_prot
        src/tests/lib/libc/arch/alpha: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/arm: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/hppa: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/i386: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/ia64: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/m68k: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/mips: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/powerpc: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/powerpc64: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/sh3: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/sparc: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/sparc64: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/vax: exec_prot_support.c return_one.S
        src/tests/lib/libc/arch/x86_64: exec_prot_support.c return_one.S
        src/tests/lib/libc/common: exec_prot.h

Log Message:
Add a simple test case to check executable mapping rights for mprotect(2).

- provide an exec_prot_support() routine so $ARCH can indicate whether
it supports execution protection or not, and skip test accordingly.

- have a trivial 'return_one' shellcode to copy anywhere in a page, and
call it. The decision to keep the assembly solution is mine, reasons are
twofold:

   - all pure-C implementations cannot be controlled easily:
     bounds detection (beginning/end) of return_one is unpredictable,
     or requires the use of overkill solutions like libelf. Using
     dlsym(3) was a good proposal, however I can't use it to know the
     end address of the payload. It makes copying of the shellcode a bit
     more difficult: using a constant may be too small (code has not been
     entirely copied, and can lead to errors that can be erroneously
     detected as "test passed"), or too big (depending on where it is mapped
     in memory, copying past the end of the function may trigger SIGSEGV).

   - I have to ensure that the resulting assembly is the most compact
     possible, especially as it will be reused to test other parts of
     memory (stack, data, rodata, etc.).

Only i386 and amd64 are implemented so far. Others will come in due time.
FWIW, writing the exec_prot_support() callback and the return_one payload
should be enough. Writing callback requires good knowledge of the platform,
depending on chip revision, CPU, board, MMU... the protection level may vary.

Current files are put under lib/libc/arch/* and lib/libc/common/. Feel free to
move them around the tests/ tree. Keep in mind that the common/ and arch/ code
will be used outside of libc, so please do not hide them too deep in the tree.

I checked a few architectures via build.sh cross-compile, and will keep an
eye on buildbot for potential build breakage. Feel free to contact me in
case you see any, of course.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/Makefile.exec_prot
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/alpha/exec_prot_support.c \
    src/tests/lib/libc/arch/alpha/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/arm/exec_prot_support.c \
    src/tests/lib/libc/arch/arm/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/hppa/exec_prot_support.c \
    src/tests/lib/libc/arch/hppa/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/i386/exec_prot_support.c \
    src/tests/lib/libc/arch/i386/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/ia64/exec_prot_support.c \
    src/tests/lib/libc/arch/ia64/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/m68k/exec_prot_support.c \
    src/tests/lib/libc/arch/m68k/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/mips/exec_prot_support.c \
    src/tests/lib/libc/arch/mips/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/powerpc/exec_prot_support.c \
    src/tests/lib/libc/arch/powerpc/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/powerpc64/exec_prot_support.c \
    src/tests/lib/libc/arch/powerpc64/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/sh3/exec_prot_support.c \
    src/tests/lib/libc/arch/sh3/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/sparc/exec_prot_support.c \
    src/tests/lib/libc/arch/sparc/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/sparc64/exec_prot_support.c \
    src/tests/lib/libc/arch/sparc64/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/vax/exec_prot_support.c \
    src/tests/lib/libc/arch/vax/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/arch/x86_64/exec_prot_support.c \
    src/tests/lib/libc/arch/x86_64/return_one.S
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/common/exec_prot.h
cvs rdiff -u -r1.6 -r1.7 src/tests/lib/libc/sys/Makefile
cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/sys/t_mprotect.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/sys/Makefile
diff -u src/tests/lib/libc/sys/Makefile:1.6 src/tests/lib/libc/sys/Makefile:1.7
--- src/tests/lib/libc/sys/Makefile:1.6	Thu Jul  7 19:29:58 2011
+++ src/tests/lib/libc/sys/Makefile	Mon Jul 18 23:16:11 2011
@@ -1,9 +1,11 @@
-# $NetBSD: Makefile,v 1.6 2011/07/07 19:29:58 jruoho Exp $
+# $NetBSD: Makefile,v 1.7 2011/07/18 23:16:11 jym Exp $
 
 MKMAN=	no
 
 .include <bsd.own.mk>
 
+.include "../arch/Makefile.exec_prot"
+
 TESTSDIR=		${TESTSBASE}/lib/libc/sys
 
 TESTS_C+=		t_access
@@ -41,6 +43,8 @@
 TESTS_C+=		t_umask
 TESTS_C+=		t_unlink
 
+SRCS.t_mprotect=	t_mprotect.c ${SRCS_EXEC_PROT}
+
 LDADD.t_getpid+=        -lpthread
 LDADD.t_timer_create+=  -lpthread
 

Index: src/tests/lib/libc/sys/t_mprotect.c
diff -u src/tests/lib/libc/sys/t_mprotect.c:1.1 src/tests/lib/libc/sys/t_mprotect.c:1.2
--- src/tests/lib/libc/sys/t_mprotect.c:1.1	Thu Jul  7 06:57:54 2011
+++ src/tests/lib/libc/sys/t_mprotect.c	Mon Jul 18 23:16:11 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: t_mprotect.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $ */
+/* $NetBSD: t_mprotect.c,v 1.2 2011/07/18 23:16:11 jym Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_mprotect.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $");
+__RCSID("$NetBSD: t_mprotect.c,v 1.2 2011/07/18 23:16:11 jym Exp $");
 
 #include <sys/param.h>
 #include <sys/mman.h>
@@ -44,6 +44,8 @@
 
 #include <atf-c.h>
 
+#include "../common/exec_prot.h"
+
 static long	page = 0;
 static int	pax_global = -1;
 static int	pax_enabled = -1;
@@ -158,10 +160,91 @@
 	ATF_REQUIRE(errno == EINVAL);
 }
 
+ATF_TC(mprotect_exec);
+ATF_TC_HEAD(mprotect_exec, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Test mprotect(2) executable space protections");
+}
+
+/*
+ * Trivial function -- should fit into a page
+ */
+ATF_TC_BODY(mprotect_exec, tc)
+{
+	pid_t pid;
+	void *map;
+	int sta, xp_support;
+
+	xp_support = exec_prot_support();
+
+	switch (xp_support) {
+	case NOTIMPL:
+		atf_tc_skip(
+		    "Execute protection callback check not implemented");
+		break;
+	case NO_XP:
+		atf_tc_skip(
+		    "Host does not support executable space protection");
+		break;
+	case PARTIAL_XP: case PERPAGE_XP: default:
+		break;
+	}
+
+	/*
+	 * Map a page read/write and copy a trivial assembly function inside.
+	 * We will then change the mapping rights:
+	 * - first by setting the execution right, and check that we can
+	 *   call the code found in the allocated page.
+	 * - second by removing the execution right. This should generate
+	 *   a SIGSEGV on architectures that can enforce --x permissions.
+	 */
+
+	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
+	ATF_REQUIRE(map != MAP_FAILED);
+
+	memcpy(map, (void *)return_one,
+	    (uintptr_t)return_one_end - (uintptr_t)return_one);
+ 
+	/* give r-x rights then call code in page */
+	ATF_REQUIRE(mprotect(map, page, PROT_EXEC|PROT_READ) == 0);
+	ATF_REQUIRE(((int (*)(void))map)() == 1);
+
+	/* remove --x right */
+	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
+		ATF_CHECK(((int (*)(void))map)() == 1);
+		_exit(0);
+	}
+
+	(void)wait(&sta);
+
+	ATF_REQUIRE(WIFEXITED(sta) != 0);
+
+	switch (xp_support) {
+	case PARTIAL_XP:
+		/* Partial protection might fail: indicate it */
+		ATF_CHECK_MSG(WEXITSTATUS(sta) == SIGSEGV,
+		    "Host only supports partial executable space protection");
+		break;
+	case PERPAGE_XP: default:
+		/* Per-page --x protection should not fail */
+		ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
+		break;
+	}
+
+	ATF_REQUIRE(munmap(map, page) == 0);
+}
+
 ATF_TC(mprotect_pax);
 ATF_TC_HEAD(mprotect_pax, tc)
 {
-	atf_tc_set_md_var(tc, "descr", "PaX restrictions and mprotect(2),");
+	atf_tc_set_md_var(tc, "descr", "PaX restrictions and mprotect(2)");
 	atf_tc_set_md_var(tc, "require.user", "root");
 }
 
@@ -224,7 +307,7 @@
 ATF_TC(mprotect_write);
 ATF_TC_HEAD(mprotect_write, tc)
 {
-	atf_tc_set_md_var(tc, "descr", "Test mprotect(2) protections");
+	atf_tc_set_md_var(tc, "descr", "Test mprotect(2) write protections");
 }
 
 ATF_TC_BODY(mprotect_write, tc)
@@ -266,6 +349,7 @@
 
 	ATF_TP_ADD_TC(tp, mprotect_access);
 	ATF_TP_ADD_TC(tp, mprotect_err);
+	ATF_TP_ADD_TC(tp, mprotect_exec);
 	ATF_TP_ADD_TC(tp, mprotect_pax);
 	ATF_TP_ADD_TC(tp, mprotect_write);
 

Added files:

Index: src/tests/lib/libc/arch/Makefile.exec_prot
diff -u /dev/null src/tests/lib/libc/arch/Makefile.exec_prot:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/Makefile.exec_prot	Mon Jul 18 23:16:08 2011
@@ -0,0 +1,8 @@
+# $NetBSD: Makefile.exec_prot,v 1.1 2011/07/18 23:16:08 jym Exp $
+
+.include <bsd.own.mk>
+
+EXEC_PROT_ARCHDIR=	${.PARSEDIR}/${MACHINE_CPU}
+.PATH: ${EXEC_PROT_ARCHDIR}
+
+SRCS_EXEC_PROT=		exec_prot_support.c return_one.S

Index: src/tests/lib/libc/arch/alpha/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/alpha/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/alpha/exec_prot_support.c	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/alpha/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/alpha/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/alpha/return_one.S	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/arm/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/arm/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/arm/exec_prot_support.c	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/arm/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/arm/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/arm/return_one.S	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/hppa/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/hppa/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/hppa/exec_prot_support.c	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/hppa/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/hppa/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/hppa/return_one.S	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/i386/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/i386/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/i386/exec_prot_support.c	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,65 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+#include <stdlib.h>
+#include <sys/sysctl.h>
+
+#include "../../common/exec_prot.h"
+
+/*
+ * Support for executable space protection has always been erratic under i386.
+ * Originally IA-32 can't do per-page execute permission, so it is
+ * implemented using different executable segments for %cs (code segment).
+ * This only allows coarse grained protection, especially when memory starts
+ * being fragmented.
+ * Later, PAE was introduced together with a NX/XD bit in the page table
+ * entry to offer per-page permission.
+ */
+int
+exec_prot_support(void)
+{
+	int pae;
+	size_t pae_len = sizeof(pae);
+
+	if (sysctlbyname("machdep.pae", &pae, &pae_len, NULL, 0) == -1)
+		return PARTIAL_XP;
+
+	if (pae == 1) {
+		if (system("cpuctl identify 0 | grep -q NOX") == 0 ||
+		    system("cpuctl identify 0 | grep -q XD") == 0)
+			return PERPAGE_XP;
+	}
+	
+	return PARTIAL_XP;
+}
Index: src/tests/lib/libc/arch/i386/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/i386/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/i386/return_one.S	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,10 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+#include <machine/asm.h>
+
+RCSID("$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+_ENTRY(return_one)
+	movl	$0x1,%eax
+	ret
+LABEL(return_one_end)

Index: src/tests/lib/libc/arch/ia64/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/ia64/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/ia64/exec_prot_support.c	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/ia64/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/ia64/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/ia64/return_one.S	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/m68k/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/m68k/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/m68k/exec_prot_support.c	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/m68k/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/m68k/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/m68k/return_one.S	Mon Jul 18 23:16:09 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:09 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/mips/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/mips/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/mips/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/mips/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/mips/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/mips/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/powerpc/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/powerpc/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/powerpc/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/powerpc/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/powerpc/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/powerpc/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/powerpc64/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/powerpc64/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/powerpc64/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/powerpc64/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/powerpc64/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/powerpc64/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/sh3/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/sh3/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/sh3/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/sh3/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/sh3/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/sh3/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/sparc/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/sparc/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/sparc/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/sparc/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/sparc/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/sparc/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/sparc64/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/sparc64/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/sparc64/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/sparc64/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/sparc64/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/sparc64/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/vax/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/vax/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/vax/exec_prot_support.c	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,41 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:10 jym Exp $");
+
+#include "../../common/exec_prot.h"
+
+int
+exec_prot_support(void)
+{
+	return NOTIMPL;
+}
Index: src/tests/lib/libc/arch/vax/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/vax/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/vax/return_one.S	Mon Jul 18 23:16:10 2011
@@ -0,0 +1,8 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:10 jym Exp $ */
+
+#include <machine/asm.h>
+
+.globl	return_one, return_one_end;
+
+return_one: return_one_end:
+	nop

Index: src/tests/lib/libc/arch/x86_64/exec_prot_support.c
diff -u /dev/null src/tests/lib/libc/arch/x86_64/exec_prot_support.c:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/x86_64/exec_prot_support.c	Mon Jul 18 23:16:11 2011
@@ -0,0 +1,50 @@
+/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:11 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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/cdefs.h>
+__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:11 jym Exp $");
+
+#include <stdlib.h>
+
+#include "../../common/exec_prot.h"
+
+/*
+ * When the NX/XD flag is present, the protection should be enabled.
+ */
+int
+exec_prot_support(void)
+{
+	if (system("cpuctl identify 0 | grep -q NOX") == 0 ||
+	    system("cpuctl identify 0 | grep -q XD") == 0)
+		return PERPAGE_XP;
+	
+	return NO_XP;
+}
Index: src/tests/lib/libc/arch/x86_64/return_one.S
diff -u /dev/null src/tests/lib/libc/arch/x86_64/return_one.S:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/arch/x86_64/return_one.S	Mon Jul 18 23:16:11 2011
@@ -0,0 +1,10 @@
+/*	$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:11 jym Exp $ */
+
+#include <machine/asm.h>
+
+RCSID("$NetBSD: return_one.S,v 1.1 2011/07/18 23:16:11 jym Exp $");
+
+_ENTRY(return_one)
+	movq	$0x1, %rax
+	retq
+LABEL(return_one_end)

Index: src/tests/lib/libc/common/exec_prot.h
diff -u /dev/null src/tests/lib/libc/common/exec_prot.h:1.1
--- /dev/null	Mon Jul 18 23:16:11 2011
+++ src/tests/lib/libc/common/exec_prot.h	Mon Jul 18 23:16:11 2011
@@ -0,0 +1,61 @@
+/* $NetBSD: exec_prot.h,v 1.1 2011/07/18 23:16:11 jym Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jean-Yves Migeon.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 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.
+ */
+
+#ifndef _TESTS_EXEC_PROT_H_
+#define _TESTS_EXEC_PROT_H_
+
+/*
+ * Prototype definitions of external helper functions for executable
+ * mapping tests.
+ */
+
+/*
+ * Trivial MD shellcode that justs returns 1.
+ */
+int return_one(void);     /* begin marker -- shellcode entry */
+int return_one_end(void); /* end marker */
+
+/*
+ * MD callback to verify whether host offers executable space protection.
+ * Returns execute protection level.
+ */
+int exec_prot_support(void);
+
+/* execute protection level */
+enum {
+	NOTIMPL = -1, /* callback not implemented */
+	NO_XP,        /* no execute protection */
+	PERPAGE_XP,   /* per-page execute protection */
+	PARTIAL_XP    /* partial execute protection. Depending on where the
+			 page is located in virtual memory, executable space
+			 protection may be enforced or not. */
+};
+#endif

Reply via email to