Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc Add a simple test case to check executable ma...



details:   https://anonhg.NetBSD.org/src/rev/8f9dc112b470
branches:  trunk
changeset: 767457:8f9dc112b470
user:      jym <jym%NetBSD.org@localhost>
date:      Mon Jul 18 23:16:08 2011 +0000

description:
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.

diffstat:

 tests/lib/libc/arch/Makefile.exec_prot            |   8 ++
 tests/lib/libc/arch/alpha/exec_prot_support.c     |  41 ++++++++++
 tests/lib/libc/arch/alpha/return_one.S            |   8 ++
 tests/lib/libc/arch/arm/exec_prot_support.c       |  41 ++++++++++
 tests/lib/libc/arch/arm/return_one.S              |   8 ++
 tests/lib/libc/arch/hppa/exec_prot_support.c      |  41 ++++++++++
 tests/lib/libc/arch/hppa/return_one.S             |   8 ++
 tests/lib/libc/arch/i386/exec_prot_support.c      |  65 ++++++++++++++++
 tests/lib/libc/arch/i386/return_one.S             |  10 ++
 tests/lib/libc/arch/ia64/exec_prot_support.c      |  41 ++++++++++
 tests/lib/libc/arch/ia64/return_one.S             |   8 ++
 tests/lib/libc/arch/m68k/exec_prot_support.c      |  41 ++++++++++
 tests/lib/libc/arch/m68k/return_one.S             |   8 ++
 tests/lib/libc/arch/mips/exec_prot_support.c      |  41 ++++++++++
 tests/lib/libc/arch/mips/return_one.S             |   8 ++
 tests/lib/libc/arch/powerpc/exec_prot_support.c   |  41 ++++++++++
 tests/lib/libc/arch/powerpc/return_one.S          |   8 ++
 tests/lib/libc/arch/powerpc64/exec_prot_support.c |  41 ++++++++++
 tests/lib/libc/arch/powerpc64/return_one.S        |   8 ++
 tests/lib/libc/arch/sh3/exec_prot_support.c       |  41 ++++++++++
 tests/lib/libc/arch/sh3/return_one.S              |   8 ++
 tests/lib/libc/arch/sparc/exec_prot_support.c     |  41 ++++++++++
 tests/lib/libc/arch/sparc/return_one.S            |   8 ++
 tests/lib/libc/arch/sparc64/exec_prot_support.c   |  41 ++++++++++
 tests/lib/libc/arch/sparc64/return_one.S          |   8 ++
 tests/lib/libc/arch/vax/exec_prot_support.c       |  41 ++++++++++
 tests/lib/libc/arch/vax/return_one.S              |   8 ++
 tests/lib/libc/arch/x86_64/exec_prot_support.c    |  50 ++++++++++++
 tests/lib/libc/arch/x86_64/return_one.S           |  10 ++
 tests/lib/libc/common/exec_prot.h                 |  61 +++++++++++++++
 tests/lib/libc/sys/Makefile                       |   6 +-
 tests/lib/libc/sys/t_mprotect.c                   |  92 ++++++++++++++++++++++-
 32 files changed, 885 insertions(+), 5 deletions(-)

diffs (truncated from 1074 to 300 lines):

diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/Makefile.exec_prot
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/Makefile.exec_prot    Mon Jul 18 23:16:08 2011 +0000
@@ -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
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/alpha/exec_prot_support.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/alpha/exec_prot_support.c     Mon Jul 18 23:16:08 2011 +0000
@@ -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;
+}
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/alpha/return_one.S
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/alpha/return_one.S    Mon Jul 18 23:16:08 2011 +0000
@@ -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
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/arm/exec_prot_support.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/arm/exec_prot_support.c       Mon Jul 18 23:16:08 2011 +0000
@@ -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;
+}
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/arm/return_one.S
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/arm/return_one.S      Mon Jul 18 23:16:08 2011 +0000
@@ -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
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/hppa/exec_prot_support.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/hppa/exec_prot_support.c      Mon Jul 18 23:16:08 2011 +0000
@@ -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;
+}
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/hppa/return_one.S
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/hppa/return_one.S     Mon Jul 18 23:16:08 2011 +0000
@@ -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
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/i386/exec_prot_support.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/i386/exec_prot_support.c      Mon Jul 18 23:16:08 2011 +0000
@@ -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;
+}
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/i386/return_one.S
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/i386/return_one.S     Mon Jul 18 23:16:08 2011 +0000
@@ -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)
diff -r 01836a92895d -r 8f9dc112b470 tests/lib/libc/arch/ia64/exec_prot_support.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/arch/ia64/exec_prot_support.c      Mon Jul 18 23:16:08 2011 +0000
@@ -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.
+ */



Home | Main Index | Thread Index | Old Index