From be7e339d8a2fbc132262e4a0b94b7c5c54aa89a3 Mon Sep 17 00:00:00 2001 From: Henry Kroll III Date: Tue, 20 Apr 2010 23:40:33 -0700 Subject: [PATCH] Use mmap instead of exec mem for Selinux machines. Fixes crash on Fedora. --- configure | 9 ++++++++- tccrun.c | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/configure b/configure index f80eb40..f9b91a7 100755 --- a/configure +++ b/configure @@ -130,6 +130,8 @@ for opt do ;; --with-libgcc) use_libgcc="yes" ;; + --with-selinux) have_selinux="yes" + ;; --help|-h) show_help="yes" ;; esac @@ -229,7 +231,8 @@ echo " --cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix]" echo " --sysroot=PREFIX prepend PREFIX to library/include paths []" echo " --cc=CC use C compiler CC [$cc]" echo " --with-libgcc use /lib/libgcc_s.so.1 instead of libtcc1.a" -echo "" +echo " --with-selinux use mmap instead of exec mem" +echo " [requires write access to /tmp]"echo "" #echo "NOTE: The object files are build at the place where configure is launched" exit 1 fi @@ -357,6 +360,10 @@ if test "$use_libgcc" = "yes" ; then echo "#define CONFIG_USE_LIBGCC" >> $TMPH echo "CONFIG_USE_LIBGCC=yes" >> config.mak fi +if test "$have_selinux" = "yes" ; then + echo "#define HAVE_SELINUX" >> $TMPH + echo "HAVE_SELINUX=yes" >> config.mak +fi version=`head $source_path/VERSION` echo "VERSION=$version" >>config.mak echo "#define TCC_VERSION \"$version\"" >> $TMPH diff --git a/tccrun.c b/tccrun.c index 25f2477..f2946a0 100644 --- a/tccrun.c +++ b/tccrun.c @@ -50,10 +50,34 @@ int tcc_relocate(TCCState *s1) int tcc_run(TCCState *s1, int argc, char **argv) { int (*prog_main)(int, char **); - + int ret; +#ifdef HAVE_SELINUX + int rret; + void *ptr,*writep; + char tmpfname[] = "/tmp/.tccrunXXXXXX"; + int fd = mkstemp (tmpfname); + unlink (tmpfname); + ftruncate (fd, 1000); + if ((rret= tcc_relocate_ex(s1,NULL)) < 0) + return -1; + /* Use mmap instead of malloc for Selinux */ + writep = mmap (NULL, rret, PROT_READ|PROT_WRITE, + MAP_SHARED, fd, 0); + if(writep == MAP_FAILED){ + error("/tmp not writeable"); + return -1; + } + ptr = mmap (NULL, rret, PROT_READ|PROT_EXEC, + MAP_SHARED, fd, 0); + if(ptr == MAP_FAILED){ + error("/tmp not executable"); + return -1; + } + tcc_relocate_ex(s1, writep); +#else if (tcc_relocate(s1) < 0) return -1; - +#endif prog_main = tcc_get_symbol_err(s1, "main"); #ifdef CONFIG_TCC_BACKTRACE @@ -65,7 +89,6 @@ int tcc_run(TCCState *s1, int argc, char **argv) if (s1->do_bounds_check) { void (*bound_init)(void); void (*bound_exit)(void); - int ret; /* set error function */ rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); rt_prog_main = prog_main; @@ -85,7 +108,13 @@ int tcc_run(TCCState *s1, int argc, char **argv) if (p) *p = 0; } #endif - return (*prog_main)(argc, argv); + ret=(*prog_main)(argc, argv); +#ifdef HAVE_SELINUX + munmap (writep, rret); + munmap (ptr, rret); + +#endif + return ret; }