tech-toolchain archive

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

MACHINE/MACHINE_ARCH combinations



Dear all,

Is there a way to get (or generate) all possible combinations of MACHINE
+ MACHINE_ARCH acceptable via build.sh, without reading through the
Makefiles?

I am currently writing tests for exec rights enforcement (either on a
page basis via mprotect(2), or for certain area of memory like
stack/heap/data).
I need to use specific payloads in certain parts of memory (see attach
for an example) and these are rather MD parts. I'd like to cover the
maximum number of arches, hence my need for MACHINE <> MACHINE_ARCH
relationship.

Please CC on reply on that one; thanks!

-- 
Jean-Yves Migeon
jeanyves.migeon%free.fr@localhost
? tests/lib/libc/arch
Index: tests/lib/libc/sys/Makefile
===================================================================
RCS file: /cvsroot/src/tests/lib/libc/sys/Makefile,v
retrieving revision 1.6
diff -u -p -r1.6 Makefile
--- tests/lib/libc/sys/Makefile 7 Jul 2011 19:29:58 -0000       1.6
+++ tests/lib/libc/sys/Makefile 10 Jul 2011 01:37:48 -0000
@@ -5,6 +5,7 @@ MKMAN=  no
 .include <bsd.own.mk>
 
 TESTSDIR=              ${TESTSBASE}/lib/libc/sys
+.PATH:                 ${.CURDIR}/../arch/${MACHINE_ARCH}
 
 TESTS_C+=              t_access
 TESTS_C+=              t_chroot
@@ -41,6 +42,8 @@ TESTS_C+=             t_truncate
 TESTS_C+=              t_umask
 TESTS_C+=              t_unlink
 
+SRCS.t_mprotect=       t_mprotect.c return_one.S
+
 LDADD.t_getpid+=        -lpthread
 LDADD.t_timer_create+=  -lpthread
 
Index: tests/lib/libc/sys/t_mprotect.c
===================================================================
RCS file: /cvsroot/src/tests/lib/libc/sys/t_mprotect.c,v
retrieving revision 1.1
diff -u -p -r1.1 t_mprotect.c
--- tests/lib/libc/sys/t_mprotect.c     7 Jul 2011 06:57:54 -0000       1.1
+++ tests/lib/libc/sys/t_mprotect.c     10 Jul 2011 01:37:48 -0000
@@ -44,6 +44,12 @@ __RCSID("$NetBSD: t_mprotect.c,v 1.1 201
 
 #include <atf-c.h>
 
+/*
+ * MD assembly function. No input and always returns 1.
+ */
+int return_one(void);
+int return_one_end(void);
+
 static long    page = 0;
 static int     pax_global = -1;
 static int     pax_enabled = -1;
@@ -158,10 +164,64 @@ ATF_TC_BODY(mprotect_err, tc)
        ATF_REQUIRE(errno == EINVAL);
 }
 
+ATF_TC(mprotect_exec);
+ATF_TC_HEAD(mprotect_exec, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test mprotect(2) execute protections");
+}
+
+/*
+ * Trivial function -- should fit into a page
+ */
+ATF_TC_BODY(mprotect_exec, tc)
+{
+       pid_t pid;
+       void *map;
+       int sta;
+
+       // XXX check exec_rights_support => if not, skip
+
+       /*
+        * 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 execute 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_REQUIRE(((int (*)(void))map)() != 1);
+       }
+
+       (void)wait(&sta);
+
+       ATF_REQUIRE(WIFEXITED(sta) != 0);
+       ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
+       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 +284,7 @@ out:
 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 +326,7 @@ ATF_TP_ADD_TCS(tp)
 
        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);
 
--- /dev/null   2011-07-10 03:32:42.000000000 +0200
+++ tests/lib/libc/arch/x86_64/return_one.S     2011-07-10 03:12:49.000000000 
+0200
@@ -0,0 +1,44 @@
+/*      $NetBSD$ */
+
+/*-
+ * 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$")
+
+#include <machine/asm.h>
+
+NENTRY(return_one)
+       pushq   %rbp
+       movq    %rsp,%rbp
+       movq    $1, %rax
+       popq    %rbp
+       retq
+       nop
+LABEL(return_one_end)


Home | Main Index | Thread Index | Old Index