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