paxctl.c revision 1.6 1 1.6 elad /* $NetBSD: paxctl.c,v 1.6 2007/12/24 20:05:24 elad Exp $ */
2 1.1 elad
3 1.1 elad /*-
4 1.1 elad * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
5 1.1 elad * All rights reserved.
6 1.1 elad *
7 1.1 elad * Redistribution and use in source and binary forms, with or without
8 1.1 elad * modification, are permitted provided that the following conditions
9 1.1 elad * are met:
10 1.1 elad * 1. Redistributions of source code must retain the above copyright
11 1.1 elad * notice, this list of conditions and the following disclaimer.
12 1.1 elad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 elad * notice, this list of conditions and the following disclaimer in the
14 1.1 elad * documentation and/or other materials provided with the distribution.
15 1.1 elad * 3. The name of the author may not be used to endorse or promote products
16 1.1 elad * derived from this software without specific prior written permission.
17 1.1 elad *
18 1.1 elad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 elad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 elad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 elad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 elad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 elad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 elad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 elad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 elad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 elad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 elad */
29 1.1 elad #if HAVE_NBTOOL_CONFIG_H
30 1.1 elad #include "nbtool_config.h"
31 1.1 elad #endif
32 1.1 elad
33 1.1 elad #include <sys/cdefs.h>
34 1.1 elad #ifndef lint
35 1.1 elad #ifdef __RCSID
36 1.6 elad __RCSID("$NetBSD: paxctl.c,v 1.6 2007/12/24 20:05:24 elad Exp $");
37 1.1 elad #endif
38 1.1 elad #endif /* not lint */
39 1.1 elad
40 1.1 elad #include <sys/types.h>
41 1.1 elad #ifdef HAVE_NBTOOL_CONFIG_H
42 1.1 elad #include "../../sys/sys/exec_elf.h"
43 1.1 elad #else
44 1.1 elad #include <elf.h>
45 1.1 elad #endif
46 1.1 elad #include <stdio.h>
47 1.1 elad #include <err.h>
48 1.1 elad #include <fcntl.h>
49 1.1 elad #include <unistd.h>
50 1.1 elad #include <stdlib.h>
51 1.1 elad #include <string.h>
52 1.1 elad
53 1.5 perry static void usage(void) __dead;
54 1.1 elad static int pax_flag(const char *);
55 1.1 elad static int pax_flags_sane(u_long);
56 1.1 elad static int pax_haveflags(u_long);
57 1.3 christos static void pax_printflags(const char *, int, u_long);
58 1.1 elad
59 1.2 christos #ifndef ELF_NOTE_TYPE_PAX_TAG
60 1.2 christos /* NetBSD-specific note type: PaX. There should be 1 NOTE per executable.
61 1.2 christos section. desc is a 32 bit bitmask */
62 1.2 christos #define ELF_NOTE_TYPE_PAX_TAG 3
63 1.6 elad #define ELF_NOTE_PAX_MPROTECT 0x01 /* Force enable MPROTECT */
64 1.6 elad #define ELF_NOTE_PAX_NOMPROTECT 0x02 /* Force disable MPROTECT */
65 1.6 elad #define ELF_NOTE_PAX_GUARD 0x04 /* Force enable Segvguard */
66 1.6 elad #define ELF_NOTE_PAX_NOGUARD 0x08 /* Force disable Segvguard */
67 1.2 christos #define ELF_NOTE_PAX_NAMESZ 4
68 1.2 christos #define ELF_NOTE_PAX_NAME "PaX\0"
69 1.2 christos #define ELF_NOTE_PAX_DESCSZ 4
70 1.1 elad #endif
71 1.1 elad #ifndef __arraycount
72 1.1 elad #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
73 1.1 elad #endif
74 1.1 elad
75 1.1 elad
76 1.1 elad static const struct paxflag {
77 1.1 elad char mark;
78 1.1 elad const char *name;
79 1.1 elad int bits;
80 1.1 elad } flags[] = {
81 1.2 christos { 'G', "Segvguard, explicit enable",
82 1.2 christos ELF_NOTE_PAX_GUARD },
83 1.2 christos { 'g', "Segvguard, explicit disable",
84 1.2 christos ELF_NOTE_PAX_NOGUARD },
85 1.2 christos { 'M', "mprotect(2) restrictions, explicit enable",
86 1.2 christos ELF_NOTE_PAX_MPROTECT },
87 1.2 christos { 'm', "mprotect(2) restrictions, explicit disable",
88 1.2 christos ELF_NOTE_PAX_NOMPROTECT },
89 1.1 elad };
90 1.1 elad
91 1.1 elad static void
92 1.1 elad usage(void)
93 1.1 elad {
94 1.4 christos (void)fprintf(stderr, "Usage: %s [ <-|+><G|g|M|m> ] <file> ...\n",
95 1.1 elad #if HAVE_NBTOOL_CONFIG_H
96 1.1 elad "paxctl"
97 1.1 elad #else
98 1.1 elad getprogname()
99 1.1 elad #endif
100 1.1 elad );
101 1.1 elad exit(1);
102 1.1 elad }
103 1.1 elad
104 1.1 elad static int
105 1.1 elad pax_flag(const char *s)
106 1.1 elad {
107 1.1 elad size_t i;
108 1.1 elad
109 1.1 elad if (s[0] == '\0' || s[1] != '\0')
110 1.1 elad return -1;
111 1.1 elad
112 1.1 elad for (i = 0; i < __arraycount(flags); i++)
113 1.1 elad if (*s == flags[i].mark)
114 1.1 elad return flags[i].bits;
115 1.1 elad
116 1.1 elad return -1;
117 1.1 elad }
118 1.1 elad
119 1.1 elad static int
120 1.1 elad pax_flags_sane(u_long f)
121 1.1 elad {
122 1.1 elad size_t i;
123 1.1 elad
124 1.1 elad for (i = 0; i < __arraycount(flags) - 1; i += 2) {
125 1.1 elad int g = flags[i].bits | flags[i+1].bits;
126 1.1 elad if ((f & g) == g)
127 1.1 elad return 0;
128 1.1 elad }
129 1.1 elad
130 1.1 elad return 1;
131 1.1 elad }
132 1.1 elad
133 1.1 elad static int
134 1.1 elad pax_haveflags(u_long f)
135 1.1 elad {
136 1.1 elad size_t i;
137 1.1 elad
138 1.1 elad for (i = 0; i < __arraycount(flags); i++)
139 1.1 elad if (f & flags[i].bits)
140 1.1 elad return (1);
141 1.1 elad
142 1.1 elad return (0);
143 1.1 elad }
144 1.1 elad
145 1.1 elad static void
146 1.3 christos pax_printflags(const char *name, int many, u_long f)
147 1.1 elad {
148 1.1 elad size_t i;
149 1.1 elad
150 1.1 elad for (i = 0; i < __arraycount(flags); i++)
151 1.3 christos if (f & flags[i].bits) {
152 1.3 christos if (many)
153 1.3 christos (void)printf("%s: ", name);
154 1.1 elad (void)printf(" %c: %s\n",
155 1.1 elad flags[i].mark, flags[i].name);
156 1.3 christos }
157 1.1 elad }
158 1.1 elad
159 1.3 christos static int
160 1.3 christos process_one(const char *name, int add_flags, int del_flags, int list, int many)
161 1.1 elad {
162 1.1 elad union {
163 1.1 elad Elf32_Ehdr h32;
164 1.1 elad Elf64_Ehdr h64;
165 1.1 elad } e;
166 1.1 elad union {
167 1.1 elad Elf32_Phdr h32;
168 1.1 elad Elf64_Phdr h64;
169 1.1 elad } p;
170 1.2 christos union {
171 1.2 christos Elf32_Nhdr h32;
172 1.2 christos Elf64_Nhdr h64;
173 1.2 christos } n;
174 1.1 elad #define EH(field) (size == 32 ? e.h32.field : e.h64.field)
175 1.1 elad #define PH(field) (size == 32 ? p.h32.field : p.h64.field)
176 1.2 christos #define NH(field) (size == 32 ? n.h32.field : n.h64.field)
177 1.1 elad #define SPH(field, val) do { \
178 1.1 elad if (size == 32) \
179 1.1 elad p.h32.field val; \
180 1.1 elad else \
181 1.1 elad p.h64.field val; \
182 1.1 elad } while (/*CONSTCOND*/0)
183 1.1 elad #define PHSIZE (size == 32 ? sizeof(p.h32) : sizeof(p.h64))
184 1.2 christos #define NHSIZE (size == 32 ? sizeof(n.h32) : sizeof(n.h64))
185 1.2 christos struct {
186 1.2 christos char name[ELF_NOTE_PAX_NAMESZ];
187 1.2 christos uint32_t flags;
188 1.2 christos } pax_tag;
189 1.3 christos int i, fd, size, ok = 0, flagged = 0;
190 1.1 elad
191 1.3 christos fd = open(name, list ? O_RDONLY: O_RDWR, 0);
192 1.3 christos if (fd == -1) {
193 1.3 christos warn("Can't open `%s'", name);
194 1.3 christos return 1;
195 1.3 christos }
196 1.1 elad
197 1.3 christos if (read(fd, &e, sizeof(e)) != sizeof(e)) {
198 1.3 christos warn("Can't read ELF header from `%s'", name);
199 1.3 christos return 1;
200 1.1 elad }
201 1.1 elad
202 1.3 christos if (memcmp(e.h32.e_ident, ELFMAG, SELFMAG) != 0) {
203 1.3 christos warn("Bad ELF magic from `%s' (maybe it's not an ELF?)", name);
204 1.3 christos return 1;
205 1.1 elad }
206 1.1 elad
207 1.1 elad if (e.h32.e_ehsize == sizeof(e.h32))
208 1.1 elad size = 32;
209 1.1 elad else if (e.h64.e_ehsize == sizeof(e.h64))
210 1.1 elad size = 64;
211 1.3 christos else {
212 1.3 christos warn("Bad ELF size %d from `%s' (maybe it's not an ELF?)",
213 1.3 christos (int)e.h32.e_ehsize, name);
214 1.3 christos return 1;
215 1.3 christos }
216 1.1 elad
217 1.1 elad for (i = 0; i < EH(e_phnum); i++) {
218 1.3 christos if (pread(fd, &p, PHSIZE, (off_t)EH(e_phoff) + i * PHSIZE) !=
219 1.3 christos PHSIZE) {
220 1.3 christos warn("Can't read program header data from `%s'", name);
221 1.3 christos return 1;
222 1.3 christos }
223 1.1 elad
224 1.1 elad if (PH(p_type) != PT_NOTE)
225 1.1 elad continue;
226 1.1 elad
227 1.3 christos if (pread(fd, &n, NHSIZE, (off_t)PH(p_offset)) != NHSIZE) {
228 1.3 christos warn("Can't read note header from `%s'", name);
229 1.3 christos return 1;
230 1.3 christos }
231 1.2 christos if (NH(n_type) != ELF_NOTE_TYPE_PAX_TAG ||
232 1.2 christos NH(n_descsz) != ELF_NOTE_PAX_DESCSZ ||
233 1.2 christos NH(n_namesz) != ELF_NOTE_PAX_NAMESZ)
234 1.2 christos continue;
235 1.2 christos if (pread(fd, &pax_tag, sizeof(pax_tag), PH(p_offset) + NHSIZE)
236 1.3 christos != sizeof(pax_tag)) {
237 1.3 christos warn("Can't read pax_tag from `%s'", name);
238 1.3 christos return 1;
239 1.3 christos }
240 1.2 christos if (memcmp(pax_tag.name, ELF_NOTE_PAX_NAME,
241 1.3 christos sizeof(pax_tag.name)) != 0) {
242 1.3 christos warn("Unknown pax_tag name `%*.*s' from `%s'",
243 1.3 christos ELF_NOTE_PAX_NAMESZ, ELF_NOTE_PAX_NAMESZ,
244 1.3 christos pax_tag.name, name);
245 1.3 christos return 1;
246 1.3 christos }
247 1.1 elad ok = 1;
248 1.1 elad
249 1.1 elad if (list) {
250 1.2 christos if (!pax_haveflags(pax_tag.flags))
251 1.1 elad break;
252 1.1 elad
253 1.2 christos if (!pax_flags_sane(pax_tag.flags))
254 1.2 christos warnx("Current flags %x don't make sense",
255 1.2 christos pax_tag.flags);
256 1.1 elad
257 1.3 christos if (many)
258 1.3 christos (void)printf("%s: ", name);
259 1.1 elad (void)printf("PaX flags:\n");
260 1.1 elad
261 1.3 christos pax_printflags(name, many, pax_tag.flags);
262 1.1 elad
263 1.1 elad flagged = 1;
264 1.1 elad
265 1.1 elad break;
266 1.1 elad }
267 1.1 elad
268 1.2 christos pax_tag.flags |= add_flags;
269 1.2 christos pax_tag.flags &= ~del_flags;
270 1.1 elad
271 1.3 christos if (!pax_flags_sane(pax_tag.flags)) {
272 1.3 christos warn("New flags %x don't make sense", pax_tag.flags);
273 1.3 christos return 1;
274 1.3 christos }
275 1.1 elad
276 1.2 christos if (pwrite(fd, &pax_tag, sizeof(pax_tag),
277 1.2 christos (off_t)PH(p_offset) + NHSIZE) != sizeof(pax_tag))
278 1.3 christos warn("Can't modify flags on `%s'", name);
279 1.1 elad break;
280 1.1 elad }
281 1.1 elad
282 1.1 elad (void)close(fd);
283 1.1 elad
284 1.3 christos if (!ok) {
285 1.3 christos warn("Could not find an ELF PaX PT_NOTE section in `%s'", name);
286 1.3 christos return 1;
287 1.3 christos }
288 1.1 elad
289 1.3 christos if (list && !flagged) {
290 1.3 christos if (many)
291 1.3 christos (void)printf("%s: ", name);
292 1.1 elad (void)printf("No PaX flags.\n");
293 1.3 christos }
294 1.1 elad return 0;
295 1.1 elad }
296 1.3 christos
297 1.3 christos int
298 1.3 christos main(int argc, char **argv)
299 1.3 christos {
300 1.3 christos char *opt;
301 1.3 christos int i, add_flags = 0, del_flags = 0, list = 0, bad = 0, many;
302 1.3 christos
303 1.3 christos if (argc < 2)
304 1.3 christos usage();
305 1.3 christos
306 1.3 christos for (i = 1; i < argc; i++) {
307 1.3 christos opt = argv[i];
308 1.3 christos
309 1.3 christos if (*opt == '-' || *opt == '+') {
310 1.3 christos int t;
311 1.3 christos
312 1.3 christos t = pax_flag(opt + 1);
313 1.3 christos if (t == -1)
314 1.3 christos usage();
315 1.3 christos
316 1.3 christos if (*opt == '-')
317 1.3 christos del_flags |= t;
318 1.3 christos else
319 1.3 christos add_flags |= t;
320 1.3 christos
321 1.3 christos opt = NULL;
322 1.3 christos } else
323 1.3 christos break;
324 1.3 christos }
325 1.3 christos
326 1.3 christos if (i == argc)
327 1.3 christos usage();
328 1.3 christos
329 1.3 christos if (add_flags || del_flags) {
330 1.3 christos if (list)
331 1.3 christos usage();
332 1.3 christos } else
333 1.3 christos list = 1;
334 1.3 christos
335 1.3 christos many = i != argc - 1;
336 1.3 christos for (; i < argc; i++)
337 1.3 christos bad |= process_one(argv[i], add_flags, del_flags, list, many);
338 1.3 christos
339 1.3 christos return bad ? EXIT_FAILURE : 0;
340 1.3 christos }
341