paxctl.c revision 1.2 1 1.2 christos /* $NetBSD: paxctl.c,v 1.2 2007/06/24 20:35:36 christos 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.2 christos __RCSID("$NetBSD: paxctl.c,v 1.2 2007/06/24 20:35:36 christos 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.1 elad static void usage(void) __attribute__((__noreturn__));
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.1 elad static void pax_printflags(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.2 christos #define ELF_NOTE_PAX_MPROTECT 0x1 /* Force enable Mprotect */
64 1.2 christos #define ELF_NOTE_PAX_NOMPROTECT 0x2 /* Force disable Mprotect */
65 1.2 christos #define ELF_NOTE_PAX_GUARD 0x4 /* Force enable Segvguard */
66 1.2 christos #define ELF_NOTE_PAX_NOGUARD 0x8 /* Force disable Servguard */
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.1 elad (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.1 elad pax_printflags(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.1 elad if (f & flags[i].bits)
152 1.1 elad (void)printf(" %c: %s\n",
153 1.1 elad flags[i].mark, flags[i].name);
154 1.1 elad }
155 1.1 elad
156 1.1 elad int
157 1.1 elad main(int argc, char **argv)
158 1.1 elad {
159 1.1 elad union {
160 1.1 elad Elf32_Ehdr h32;
161 1.1 elad Elf64_Ehdr h64;
162 1.1 elad } e;
163 1.1 elad union {
164 1.1 elad Elf32_Phdr h32;
165 1.1 elad Elf64_Phdr h64;
166 1.1 elad } p;
167 1.2 christos union {
168 1.2 christos Elf32_Nhdr h32;
169 1.2 christos Elf64_Nhdr h64;
170 1.2 christos } n;
171 1.1 elad #define EH(field) (size == 32 ? e.h32.field : e.h64.field)
172 1.1 elad #define PH(field) (size == 32 ? p.h32.field : p.h64.field)
173 1.2 christos #define NH(field) (size == 32 ? n.h32.field : n.h64.field)
174 1.1 elad #define SPH(field, val) do { \
175 1.1 elad if (size == 32) \
176 1.1 elad p.h32.field val; \
177 1.1 elad else \
178 1.1 elad p.h64.field val; \
179 1.1 elad } while (/*CONSTCOND*/0)
180 1.1 elad #define PHSIZE (size == 32 ? sizeof(p.h32) : sizeof(p.h64))
181 1.2 christos #define NHSIZE (size == 32 ? sizeof(n.h32) : sizeof(n.h64))
182 1.2 christos struct {
183 1.2 christos char name[ELF_NOTE_PAX_NAMESZ];
184 1.2 christos uint32_t flags;
185 1.2 christos } pax_tag;
186 1.1 elad
187 1.1 elad int size;
188 1.1 elad char *opt = NULL;
189 1.1 elad int fd, i, add_flags = 0, del_flags = 0, list = 0, ok = 0, flagged = 0;
190 1.1 elad
191 1.1 elad if (argc < 2)
192 1.1 elad usage();
193 1.1 elad
194 1.1 elad for (i = 1; i < argc; i++) {
195 1.1 elad opt = argv[i];
196 1.1 elad
197 1.1 elad if (*opt == '-' || *opt == '+') {
198 1.1 elad int t;
199 1.1 elad
200 1.1 elad t = pax_flag(opt + 1);
201 1.1 elad if (t == -1)
202 1.1 elad usage();
203 1.1 elad
204 1.1 elad if (*opt == '-')
205 1.1 elad del_flags |= t;
206 1.1 elad else
207 1.1 elad add_flags |= t;
208 1.1 elad
209 1.1 elad opt = NULL;
210 1.1 elad } else
211 1.1 elad break;
212 1.1 elad }
213 1.1 elad
214 1.1 elad if (opt == NULL)
215 1.1 elad usage();
216 1.1 elad
217 1.1 elad if (add_flags || del_flags) {
218 1.1 elad if (list)
219 1.1 elad usage();
220 1.1 elad } else
221 1.1 elad list = 1;
222 1.1 elad
223 1.1 elad fd = open(opt, O_RDWR, 0);
224 1.1 elad if (fd == -1) {
225 1.1 elad if (!list || (fd = open(opt, O_RDONLY, 0)) == -1)
226 1.1 elad err(EXIT_FAILURE, "Can't open `%s'", opt);
227 1.1 elad }
228 1.1 elad
229 1.1 elad if (read(fd, &e, sizeof(e)) != sizeof(e))
230 1.1 elad err(EXIT_FAILURE, "Can't read ELF header from `%s'", opt);
231 1.1 elad
232 1.1 elad if (memcmp(e.h32.e_ident, ELFMAG, SELFMAG) != 0)
233 1.1 elad errx(EXIT_FAILURE,
234 1.1 elad "Bad ELF magic from `%s' (maybe it's not an ELF?)", opt);
235 1.1 elad
236 1.1 elad if (e.h32.e_ehsize == sizeof(e.h32))
237 1.1 elad size = 32;
238 1.1 elad else if (e.h64.e_ehsize == sizeof(e.h64))
239 1.1 elad size = 64;
240 1.1 elad else
241 1.1 elad errx(EXIT_FAILURE,
242 1.1 elad "Bad ELF size %d from `%s' (maybe it's not an ELF?)",
243 1.1 elad (int)e.h32.e_ehsize, opt);
244 1.1 elad
245 1.1 elad for (i = 0; i < EH(e_phnum); i++) {
246 1.1 elad if (pread(fd, &p, PHSIZE,
247 1.1 elad (off_t)EH(e_phoff) + i * PHSIZE) != PHSIZE)
248 1.2 christos err(EXIT_FAILURE, "Can't read program header data"
249 1.2 christos " from `%s'", opt);
250 1.1 elad
251 1.1 elad if (PH(p_type) != PT_NOTE)
252 1.1 elad continue;
253 1.1 elad
254 1.2 christos if (pread(fd, &n, NHSIZE, (off_t)PH(p_offset)) != NHSIZE)
255 1.2 christos err(EXIT_FAILURE, "Can't read note header from `%s'",
256 1.2 christos opt);
257 1.2 christos if (NH(n_type) != ELF_NOTE_TYPE_PAX_TAG ||
258 1.2 christos NH(n_descsz) != ELF_NOTE_PAX_DESCSZ ||
259 1.2 christos NH(n_namesz) != ELF_NOTE_PAX_NAMESZ)
260 1.2 christos continue;
261 1.2 christos if (pread(fd, &pax_tag, sizeof(pax_tag), PH(p_offset) + NHSIZE)
262 1.2 christos != sizeof(pax_tag))
263 1.2 christos err(EXIT_FAILURE, "Can't read pax_tag from `%s'",
264 1.2 christos opt);
265 1.2 christos if (memcmp(pax_tag.name, ELF_NOTE_PAX_NAME,
266 1.2 christos sizeof(pax_tag.name)) != 0)
267 1.2 christos err(EXIT_FAILURE, "Unknown pax_tag name `%*.*s' from"
268 1.2 christos " `%s'", ELF_NOTE_PAX_NAMESZ, ELF_NOTE_PAX_NAMESZ,
269 1.2 christos pax_tag.name, opt);
270 1.1 elad ok = 1;
271 1.1 elad
272 1.1 elad if (list) {
273 1.2 christos if (!pax_haveflags(pax_tag.flags))
274 1.1 elad break;
275 1.1 elad
276 1.2 christos if (!pax_flags_sane(pax_tag.flags))
277 1.2 christos warnx("Current flags %x don't make sense",
278 1.2 christos pax_tag.flags);
279 1.1 elad
280 1.1 elad (void)printf("PaX flags:\n");
281 1.1 elad
282 1.2 christos pax_printflags(pax_tag.flags);
283 1.1 elad
284 1.1 elad flagged = 1;
285 1.1 elad
286 1.1 elad break;
287 1.1 elad }
288 1.1 elad
289 1.2 christos pax_tag.flags |= add_flags;
290 1.2 christos pax_tag.flags &= ~del_flags;
291 1.1 elad
292 1.2 christos if (!pax_flags_sane(pax_tag.flags))
293 1.2 christos errx(EXIT_FAILURE, "New flags %x don't make sense",
294 1.2 christos pax_tag.flags);
295 1.1 elad
296 1.2 christos if (pwrite(fd, &pax_tag, sizeof(pax_tag),
297 1.2 christos (off_t)PH(p_offset) + NHSIZE) != sizeof(pax_tag))
298 1.1 elad err(EXIT_FAILURE, "Can't modify flags on `%s'", opt);
299 1.1 elad break;
300 1.1 elad }
301 1.1 elad
302 1.1 elad (void)close(fd);
303 1.1 elad
304 1.1 elad if (!ok)
305 1.1 elad errx(EXIT_FAILURE,
306 1.2 christos "Could not find an ELF PaX PT_NOTE section in `%s'", opt);
307 1.1 elad
308 1.1 elad if (list && !flagged)
309 1.1 elad (void)printf("No PaX flags.\n");
310 1.1 elad return 0;
311 1.1 elad }
312