* [gentoo-commits] proj/elfix:master commit in: /, poc/, tests/
@ 2011-05-05 19:46 Anthony G. Basile
0 siblings, 0 replies; only message in thread
From: Anthony G. Basile @ 2011-05-05 19:46 UTC (permalink / raw
To: gentoo-commits
commit: df7e9066d5f19264caa2b27cdae8a8e27c67433d
Author: Anthony G. Basile <basile <AT> opensource <DOT> dyc <DOT> edu>
AuthorDate: Thu May 5 15:10:36 2011 +0000
Commit: Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Thu May 5 15:10:36 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=df7e9066
Revamped tests, added poc
---
Makefile.am | 8 ++-
configure.ac | 19 ++++++++-
poc/Makefile.am | 4 ++
poc/bad-mmap.c | 20 ++++++++++
poc/mangle-paxflags.c | 72 +++++++++++++++++++++++++++++++++++
tests/Makefile.am | 33 ++++++----------
tests/{bad.c => bad-gnustack.c} | 0
tests/{test-bad32.asm => bad32.asm} | 0
tests/{test-bad64.asm => bad64.asm} | 0
tests/good.c | 23 -----------
10 files changed, 131 insertions(+), 48 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index afff66d..fbec9a2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,7 @@
if TEST
-SUBDIRS = src doc tests
-else
-SUBDIRS = src doc
+SUBDIRS_TESTS = tests
endif
+if POC
+SUBDIRS_POC = poc
+endif
+SUBDIRS = src doc $(SUBDIRS_TESTS) $(SUBDIRS_POC)
diff --git a/configure.ac b/configure.ac
index fe326ec..039a269 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,8 +7,6 @@ AC_CONFIG_SRCDIR([src/fix-gnustack.c])
#AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([1.11 foreign])
-# Checks for programs.
-AC_PROG_CC
AC_ARG_ENABLE(
[tests],
AS_HELP_STRING(
@@ -27,16 +25,32 @@ AC_ARG_ENABLE(
)
AM_CONDITIONAL([TEST],[test "x$has_yasm" = "xyes"])
+AC_ARG_ENABLE(
+ [poc],
+ AS_HELP_STRING(
+ [--enable-poc],
+ [build proof of concept code]
+ )
+)
+AM_CONDITIONAL([POC],[test "x$enable_poc" = "xyes"])
+
+# Checks for programs.
+AC_PROG_CC
+AC_CHECK_FUNCS([strerror])
+AC_FUNC_MMAP
+
# Checks for libraries.
AC_CHECK_LIB([elf], [elf_begin])
# Checks for header files.
+AC_CHECK_HEADERS([errno.h])
AC_CHECK_HEADERS([error.h])
AC_CHECK_HEADERS([fcntl.h])
AC_CHECK_HEADERS([gelf.h])
AC_CHECK_HEADERS([stdio.h])
AC_CHECK_HEADERS([stdlib.h])
AC_CHECK_HEADERS([string.h])
+AC_CHECK_HEADERS([sys/mman.h])
AC_CHECK_HEADERS([sys/stat.h])
AC_CHECK_HEADERS([sys/types.h])
AC_CHECK_HEADERS([unistd.h])
@@ -50,6 +64,7 @@ AC_FUNC_ERROR_AT_LINE
AC_CONFIG_FILES([Makefile
src/Makefile
doc/Makefile
+ poc/Makefile
tests/Makefile])
AC_OUTPUT
diff --git a/poc/Makefile.am b/poc/Makefile.am
new file mode 100644
index 0000000..b5d0ae7
--- /dev/null
+++ b/poc/Makefile.am
@@ -0,0 +1,4 @@
+noinst_PROGRAMS = mangle-paxflags bad-mmap
+mangle_paxflags_SOURCES = mangle-paxflags.c
+mangle_paxflags_LDADD = -lelf
+bad_mmap_SOURCES = bad-mmap.c
diff --git a/poc/bad-mmap.c b/poc/bad-mmap.c
new file mode 100644
index 0000000..c459abb
--- /dev/null
+++ b/poc/bad-mmap.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include <string.h>
+
+int
+main()
+{
+ if( mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) != MAP_FAILED )
+ {
+ printf("mmap(): succeeded\n");
+ return 0;
+ }
+ else
+ {
+ printf("mmap(): %s\n", strerror(errno));
+ return 1;
+ }
+}
diff --git a/poc/mangle-paxflags.c b/poc/mangle-paxflags.c
new file mode 100644
index 0000000..9d59a96
--- /dev/null
+++ b/poc/mangle-paxflags.c
@@ -0,0 +1,72 @@
+/*
+ fix-gnustack.c: check and optionally remove exec flag on Elf GNU_STACK
+ Copyright (C) 2011 Anthony G. Basile
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <error.h>
+
+#include <gelf.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+
+
+int
+main( int argc, char *argv[])
+{
+ int fd;
+ char *f_name;
+ size_t i, phnum;
+
+ Elf *elf;
+ GElf_Phdr phdr;
+
+ f_name = argv[1];
+
+ if(elf_version(EV_CURRENT) == EV_NONE)
+ error(EXIT_FAILURE, 0, "Library out of date.");
+
+ if((fd = open(f_name, O_RDWR)) < 0)
+ error(EXIT_FAILURE, 0, "open() fail.");
+ if((elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL)) == NULL)
+ error(EXIT_FAILURE, 0, "elf_begin() fail: %s", elf_errmsg(-1));
+ if(elf_kind(elf) != ELF_K_ELF)
+ error(EXIT_FAILURE, 0, "elf_kind() fail: this is not an elf file.");
+
+ elf_getphdrnum(elf, &phnum);
+ for(i=0; i<phnum; ++i)
+ {
+ if(gelf_getphdr(elf, i, &phdr) != &phdr)
+ error(EXIT_FAILURE, 0, "gelf_getphdr(): %s", elf_errmsg(-1));
+
+ if(phdr.p_type == PT_PAX_FLAGS)
+ {
+ printf("Found PT_PAX_FLAGS\n");
+ phdr.p_type = PT_NULL;
+ if(!gelf_update_phdr(elf, i, &phdr))
+ error(EXIT_FAILURE, 0, "gelf_update_phdr(): %s", elf_errmsg(-1));
+ }
+ }
+
+ elf_end(elf);
+ close(fd);
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a7fafde..ab23520 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,14 +1,13 @@
-noinst_PROGRAMS = good bad
-good_SOURCES = good.c
-bad_SOURCES = bad.c
-bad_DEPENDENCIES = test-bad.o
-bad_LDADD = test-bad.o
+noinst_PROGRAMS = bad-gnustack
+bad_gnustack_SOURCES = bad-gnustack.c
+bad_gnustack_DEPENDENCIES = bad-asm.o
+bad_gnustack_LDADD = bad-asm.o
ARCH = $(shell uname -m | sed -e 's/i./x/')
-test-bad.o: test-bad64.asm test-bad32.asm
- [[ "$(ARCH)" == "x86" ]] && yasm -f elf -m x86 test-bad32.asm && mv test-bad32.o test-bad.o || true
- [[ "$(ARCH)" == "x86_64" ]] && yasm -f elf -m amd64 test-bad64.asm && mv test-bad64.o test-bad.o || true
+bad-asm.o: bad64.asm bad32.asm
+ [[ "$(ARCH)" == "x86" ]] && yasm -f elf -m x86 bad32.asm && mv bad32.o bad-asm.o || true
+ [[ "$(ARCH)" == "x86_64" ]] && yasm -f elf -m amd64 bad64.asm && mv bad64.o bad-asm.o || true
check_SCRIPTS = test.sh
TEST = $(check_SCRIPTS)
@@ -16,19 +15,13 @@ TEST = $(check_SCRIPTS)
test.sh:
@echo "================================================================================"
@echo
- @echo "Good Elf"
- @../src/fix-gnustack good
+ @echo "Fixing Bad GNU_STACK Elf"
+ @../src/fix-gnustack -f bad-gnustack
@echo
- @echo "Bad Elf"
- @../src/fix-gnustack bad
- @echo
- @echo "Fixing Bad Elf"
- @../src/fix-gnustack -f bad
- @echo
- @echo "Fixed Bad Elf"
- @../src/fix-gnustack bad
- @rm -f good bad
+ @echo "Fixed Bad GNU_STACK Elf"
+ @../src/fix-gnustack bad-gnustack
+ @rm -f good
@echo
@echo "================================================================================"
-CLEANFILES = test-bad.o test.sh
+CLEANFILES = bad-asm.o test.sh
diff --git a/tests/bad.c b/tests/bad-gnustack.c
similarity index 100%
rename from tests/bad.c
rename to tests/bad-gnustack.c
diff --git a/tests/test-bad32.asm b/tests/bad32.asm
similarity index 100%
rename from tests/test-bad32.asm
rename to tests/bad32.asm
diff --git a/tests/test-bad64.asm b/tests/bad64.asm
similarity index 100%
rename from tests/test-bad64.asm
rename to tests/bad64.asm
diff --git a/tests/good.c b/tests/good.c
deleted file mode 100644
index ff7700e..0000000
--- a/tests/good.c
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- good.c: C source for sample elf with no X on GNU_STACK
- Copyright (C) 2011 Anthony G. Basile
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-int main()
-{
- ;
- return 0 ;
-}
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2011-05-05 19:47 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-05 19:46 [gentoo-commits] proj/elfix:master commit in: /, poc/, tests/ Anthony G. Basile
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox