Gentoo Archives: gentoo-commits

From: "Anthony G. Basile" <blueness@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/elfix:master commit in: /, poc/, tests/
Date: Thu, 05 May 2011 19:47:06
Message-Id: df7e9066d5f19264caa2b27cdae8a8e27c67433d.blueness@gentoo
1 commit: df7e9066d5f19264caa2b27cdae8a8e27c67433d
2 Author: Anthony G. Basile <basile <AT> opensource <DOT> dyc <DOT> edu>
3 AuthorDate: Thu May 5 15:10:36 2011 +0000
4 Commit: Anthony G. Basile <blueness <AT> gentoo <DOT> org>
5 CommitDate: Thu May 5 15:10:36 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=df7e9066
7
8 Revamped tests, added poc
9
10 ---
11 Makefile.am | 8 ++-
12 configure.ac | 19 ++++++++-
13 poc/Makefile.am | 4 ++
14 poc/bad-mmap.c | 20 ++++++++++
15 poc/mangle-paxflags.c | 72 +++++++++++++++++++++++++++++++++++
16 tests/Makefile.am | 33 ++++++----------
17 tests/{bad.c => bad-gnustack.c} | 0
18 tests/{test-bad32.asm => bad32.asm} | 0
19 tests/{test-bad64.asm => bad64.asm} | 0
20 tests/good.c | 23 -----------
21 10 files changed, 131 insertions(+), 48 deletions(-)
22
23 diff --git a/Makefile.am b/Makefile.am
24 index afff66d..fbec9a2 100644
25 --- a/Makefile.am
26 +++ b/Makefile.am
27 @@ -1,5 +1,7 @@
28 if TEST
29 -SUBDIRS = src doc tests
30 -else
31 -SUBDIRS = src doc
32 +SUBDIRS_TESTS = tests
33 endif
34 +if POC
35 +SUBDIRS_POC = poc
36 +endif
37 +SUBDIRS = src doc $(SUBDIRS_TESTS) $(SUBDIRS_POC)
38
39 diff --git a/configure.ac b/configure.ac
40 index fe326ec..039a269 100644
41 --- a/configure.ac
42 +++ b/configure.ac
43 @@ -7,8 +7,6 @@ AC_CONFIG_SRCDIR([src/fix-gnustack.c])
44 #AC_CONFIG_HEADERS([config.h])
45 AM_INIT_AUTOMAKE([1.11 foreign])
46
47 -# Checks for programs.
48 -AC_PROG_CC
49 AC_ARG_ENABLE(
50 [tests],
51 AS_HELP_STRING(
52 @@ -27,16 +25,32 @@ AC_ARG_ENABLE(
53 )
54 AM_CONDITIONAL([TEST],[test "x$has_yasm" = "xyes"])
55
56 +AC_ARG_ENABLE(
57 + [poc],
58 + AS_HELP_STRING(
59 + [--enable-poc],
60 + [build proof of concept code]
61 + )
62 +)
63 +AM_CONDITIONAL([POC],[test "x$enable_poc" = "xyes"])
64 +
65 +# Checks for programs.
66 +AC_PROG_CC
67 +AC_CHECK_FUNCS([strerror])
68 +AC_FUNC_MMAP
69 +
70 # Checks for libraries.
71 AC_CHECK_LIB([elf], [elf_begin])
72
73 # Checks for header files.
74 +AC_CHECK_HEADERS([errno.h])
75 AC_CHECK_HEADERS([error.h])
76 AC_CHECK_HEADERS([fcntl.h])
77 AC_CHECK_HEADERS([gelf.h])
78 AC_CHECK_HEADERS([stdio.h])
79 AC_CHECK_HEADERS([stdlib.h])
80 AC_CHECK_HEADERS([string.h])
81 +AC_CHECK_HEADERS([sys/mman.h])
82 AC_CHECK_HEADERS([sys/stat.h])
83 AC_CHECK_HEADERS([sys/types.h])
84 AC_CHECK_HEADERS([unistd.h])
85 @@ -50,6 +64,7 @@ AC_FUNC_ERROR_AT_LINE
86 AC_CONFIG_FILES([Makefile
87 src/Makefile
88 doc/Makefile
89 + poc/Makefile
90 tests/Makefile])
91
92 AC_OUTPUT
93
94 diff --git a/poc/Makefile.am b/poc/Makefile.am
95 new file mode 100644
96 index 0000000..b5d0ae7
97 --- /dev/null
98 +++ b/poc/Makefile.am
99 @@ -0,0 +1,4 @@
100 +noinst_PROGRAMS = mangle-paxflags bad-mmap
101 +mangle_paxflags_SOURCES = mangle-paxflags.c
102 +mangle_paxflags_LDADD = -lelf
103 +bad_mmap_SOURCES = bad-mmap.c
104
105 diff --git a/poc/bad-mmap.c b/poc/bad-mmap.c
106 new file mode 100644
107 index 0000000..c459abb
108 --- /dev/null
109 +++ b/poc/bad-mmap.c
110 @@ -0,0 +1,20 @@
111 +#include <stdio.h>
112 +#include <stdlib.h>
113 +#include <sys/mman.h>
114 +#include <errno.h>
115 +#include <string.h>
116 +
117 +int
118 +main()
119 +{
120 + if( mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) != MAP_FAILED )
121 + {
122 + printf("mmap(): succeeded\n");
123 + return 0;
124 + }
125 + else
126 + {
127 + printf("mmap(): %s\n", strerror(errno));
128 + return 1;
129 + }
130 +}
131
132 diff --git a/poc/mangle-paxflags.c b/poc/mangle-paxflags.c
133 new file mode 100644
134 index 0000000..9d59a96
135 --- /dev/null
136 +++ b/poc/mangle-paxflags.c
137 @@ -0,0 +1,72 @@
138 +/*
139 + fix-gnustack.c: check and optionally remove exec flag on Elf GNU_STACK
140 + Copyright (C) 2011 Anthony G. Basile
141 +
142 + This program is free software: you can redistribute it and/or modify
143 + it under the terms of the GNU General Public License as published by
144 + the Free Software Foundation, either version 3 of the License, or
145 + (at your option) any later version.
146 +
147 + This program is distributed in the hope that it will be useful,
148 + but WITHOUT ANY WARRANTY; without even the implied warranty of
149 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
150 + GNU General Public License for more details.
151 +
152 + You should have received a copy of the GNU General Public License
153 + along with this program. If not, see <http://www.gnu.org/licenses/>.
154 +*/
155 +
156 +#include <stdio.h>
157 +#include <stdlib.h>
158 +#include <string.h>
159 +#include <error.h>
160 +
161 +#include <gelf.h>
162 +
163 +#include <sys/types.h>
164 +#include <sys/stat.h>
165 +#include <fcntl.h>
166 +#include <unistd.h>
167 +
168 +
169 +
170 +int
171 +main( int argc, char *argv[])
172 +{
173 + int fd;
174 + char *f_name;
175 + size_t i, phnum;
176 +
177 + Elf *elf;
178 + GElf_Phdr phdr;
179 +
180 + f_name = argv[1];
181 +
182 + if(elf_version(EV_CURRENT) == EV_NONE)
183 + error(EXIT_FAILURE, 0, "Library out of date.");
184 +
185 + if((fd = open(f_name, O_RDWR)) < 0)
186 + error(EXIT_FAILURE, 0, "open() fail.");
187 + if((elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL)) == NULL)
188 + error(EXIT_FAILURE, 0, "elf_begin() fail: %s", elf_errmsg(-1));
189 + if(elf_kind(elf) != ELF_K_ELF)
190 + error(EXIT_FAILURE, 0, "elf_kind() fail: this is not an elf file.");
191 +
192 + elf_getphdrnum(elf, &phnum);
193 + for(i=0; i<phnum; ++i)
194 + {
195 + if(gelf_getphdr(elf, i, &phdr) != &phdr)
196 + error(EXIT_FAILURE, 0, "gelf_getphdr(): %s", elf_errmsg(-1));
197 +
198 + if(phdr.p_type == PT_PAX_FLAGS)
199 + {
200 + printf("Found PT_PAX_FLAGS\n");
201 + phdr.p_type = PT_NULL;
202 + if(!gelf_update_phdr(elf, i, &phdr))
203 + error(EXIT_FAILURE, 0, "gelf_update_phdr(): %s", elf_errmsg(-1));
204 + }
205 + }
206 +
207 + elf_end(elf);
208 + close(fd);
209 +}
210
211 diff --git a/tests/Makefile.am b/tests/Makefile.am
212 index a7fafde..ab23520 100644
213 --- a/tests/Makefile.am
214 +++ b/tests/Makefile.am
215 @@ -1,14 +1,13 @@
216 -noinst_PROGRAMS = good bad
217 -good_SOURCES = good.c
218 -bad_SOURCES = bad.c
219 -bad_DEPENDENCIES = test-bad.o
220 -bad_LDADD = test-bad.o
221 +noinst_PROGRAMS = bad-gnustack
222 +bad_gnustack_SOURCES = bad-gnustack.c
223 +bad_gnustack_DEPENDENCIES = bad-asm.o
224 +bad_gnustack_LDADD = bad-asm.o
225
226 ARCH = $(shell uname -m | sed -e 's/i./x/')
227
228 -test-bad.o: test-bad64.asm test-bad32.asm
229 - [[ "$(ARCH)" == "x86" ]] && yasm -f elf -m x86 test-bad32.asm && mv test-bad32.o test-bad.o || true
230 - [[ "$(ARCH)" == "x86_64" ]] && yasm -f elf -m amd64 test-bad64.asm && mv test-bad64.o test-bad.o || true
231 +bad-asm.o: bad64.asm bad32.asm
232 + [[ "$(ARCH)" == "x86" ]] && yasm -f elf -m x86 bad32.asm && mv bad32.o bad-asm.o || true
233 + [[ "$(ARCH)" == "x86_64" ]] && yasm -f elf -m amd64 bad64.asm && mv bad64.o bad-asm.o || true
234
235 check_SCRIPTS = test.sh
236 TEST = $(check_SCRIPTS)
237 @@ -16,19 +15,13 @@ TEST = $(check_SCRIPTS)
238 test.sh:
239 @echo "================================================================================"
240 @echo
241 - @echo "Good Elf"
242 - @../src/fix-gnustack good
243 + @echo "Fixing Bad GNU_STACK Elf"
244 + @../src/fix-gnustack -f bad-gnustack
245 @echo
246 - @echo "Bad Elf"
247 - @../src/fix-gnustack bad
248 - @echo
249 - @echo "Fixing Bad Elf"
250 - @../src/fix-gnustack -f bad
251 - @echo
252 - @echo "Fixed Bad Elf"
253 - @../src/fix-gnustack bad
254 - @rm -f good bad
255 + @echo "Fixed Bad GNU_STACK Elf"
256 + @../src/fix-gnustack bad-gnustack
257 + @rm -f good
258 @echo
259 @echo "================================================================================"
260
261 -CLEANFILES = test-bad.o test.sh
262 +CLEANFILES = bad-asm.o test.sh
263
264 diff --git a/tests/bad.c b/tests/bad-gnustack.c
265 similarity index 100%
266 rename from tests/bad.c
267 rename to tests/bad-gnustack.c
268
269 diff --git a/tests/test-bad32.asm b/tests/bad32.asm
270 similarity index 100%
271 rename from tests/test-bad32.asm
272 rename to tests/bad32.asm
273
274 diff --git a/tests/test-bad64.asm b/tests/bad64.asm
275 similarity index 100%
276 rename from tests/test-bad64.asm
277 rename to tests/bad64.asm
278
279 diff --git a/tests/good.c b/tests/good.c
280 deleted file mode 100644
281 index ff7700e..0000000
282 --- a/tests/good.c
283 +++ /dev/null
284 @@ -1,23 +0,0 @@
285 -/*
286 - good.c: C source for sample elf with no X on GNU_STACK
287 - Copyright (C) 2011 Anthony G. Basile
288 -
289 - This program is free software: you can redistribute it and/or modify
290 - it under the terms of the GNU General Public License as published by
291 - the Free Software Foundation, either version 3 of the License, or
292 - (at your option) any later version.
293 -
294 - This program is distributed in the hope that it will be useful,
295 - but WITHOUT ANY WARRANTY; without even the implied warranty of
296 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
297 - GNU General Public License for more details.
298 -
299 - You should have received a copy of the GNU General Public License
300 - along with this program. If not, see <http://www.gnu.org/licenses/>.
301 -*/
302 -
303 -int main()
304 -{
305 - ;
306 - return 0 ;
307 -}