1*f598570bSrin /* $NetBSD: paxctl.c,v 1.13 2023/06/23 01:56:21 rin Exp $ */
2a8c0f0c7Selad
3a8c0f0c7Selad /*-
4a8c0f0c7Selad * Copyright (c) 2006 Elad Efrat <elad@NetBSD.org>
54c20e2abSchristos * Copyright (c) 2008 Christos Zoulas <christos@NetBSD.org>
6a8c0f0c7Selad * All rights reserved.
7a8c0f0c7Selad *
8a8c0f0c7Selad * Redistribution and use in source and binary forms, with or without
9a8c0f0c7Selad * modification, are permitted provided that the following conditions
10a8c0f0c7Selad * are met:
11a8c0f0c7Selad * 1. Redistributions of source code must retain the above copyright
12a8c0f0c7Selad * notice, this list of conditions and the following disclaimer.
13a8c0f0c7Selad * 2. Redistributions in binary form must reproduce the above copyright
14a8c0f0c7Selad * notice, this list of conditions and the following disclaimer in the
15a8c0f0c7Selad * documentation and/or other materials provided with the distribution.
16a8c0f0c7Selad * 3. The name of the author may not be used to endorse or promote products
17a8c0f0c7Selad * derived from this software without specific prior written permission.
18a8c0f0c7Selad *
19a8c0f0c7Selad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20a8c0f0c7Selad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21a8c0f0c7Selad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22a8c0f0c7Selad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23a8c0f0c7Selad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24a8c0f0c7Selad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a8c0f0c7Selad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a8c0f0c7Selad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a8c0f0c7Selad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28a8c0f0c7Selad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a8c0f0c7Selad */
30a8c0f0c7Selad #if HAVE_NBTOOL_CONFIG_H
31a8c0f0c7Selad #include "nbtool_config.h"
32a8c0f0c7Selad #endif
33a8c0f0c7Selad
34a8c0f0c7Selad #include <sys/cdefs.h>
35a8c0f0c7Selad #ifndef lint
36a8c0f0c7Selad #ifdef __RCSID
37*f598570bSrin __RCSID("$NetBSD: paxctl.c,v 1.13 2023/06/23 01:56:21 rin Exp $");
38a8c0f0c7Selad #endif
39a8c0f0c7Selad #endif /* not lint */
40a8c0f0c7Selad
41a8c0f0c7Selad #include <sys/types.h>
42a8c0f0c7Selad #ifdef HAVE_NBTOOL_CONFIG_H
43a8c0f0c7Selad #include "../../sys/sys/exec_elf.h"
44a8c0f0c7Selad #else
45a8c0f0c7Selad #include <elf.h>
46a8c0f0c7Selad #endif
47a8c0f0c7Selad #include <stdio.h>
48a8c0f0c7Selad #include <err.h>
49a8c0f0c7Selad #include <fcntl.h>
50a8c0f0c7Selad #include <unistd.h>
51a8c0f0c7Selad #include <stdlib.h>
52a8c0f0c7Selad #include <string.h>
53a8c0f0c7Selad
548b0f9554Sperry static void usage(void) __dead;
554c20e2abSchristos static uint32_t pax_flag(char);
564c20e2abSchristos static int pax_flags_sane(uint32_t);
574c20e2abSchristos static int pax_haveflags(uint32_t);
584c20e2abSchristos static void pax_printflags(const char *, int, uint32_t);
59a8c0f0c7Selad
602ffe4b87Schristos #ifndef ELF_NOTE_TYPE_PAX_TAG
612ffe4b87Schristos /* NetBSD-specific note type: PaX. There should be 1 NOTE per executable.
622ffe4b87Schristos section. desc is a 32 bit bitmask */
632ffe4b87Schristos #define ELF_NOTE_TYPE_PAX_TAG 3
6432789733Selad #define ELF_NOTE_PAX_MPROTECT 0x01 /* Force enable MPROTECT */
6532789733Selad #define ELF_NOTE_PAX_NOMPROTECT 0x02 /* Force disable MPROTECT */
6632789733Selad #define ELF_NOTE_PAX_GUARD 0x04 /* Force enable Segvguard */
6732789733Selad #define ELF_NOTE_PAX_NOGUARD 0x08 /* Force disable Segvguard */
684e65b015Schristos #define ELF_NOTE_PAX_ASLR 0x10 /* Force enable ASLR */
694e65b015Schristos #define ELF_NOTE_PAX_NOASLR 0x20 /* Force disable ASLR */
702ffe4b87Schristos #define ELF_NOTE_PAX_NAMESZ 4
712ffe4b87Schristos #define ELF_NOTE_PAX_NAME "PaX\0"
722ffe4b87Schristos #define ELF_NOTE_PAX_DESCSZ 4
73a8c0f0c7Selad #endif
74a8c0f0c7Selad #ifndef __arraycount
75a8c0f0c7Selad #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
76a8c0f0c7Selad #endif
77a8c0f0c7Selad
78a8c0f0c7Selad
79a8c0f0c7Selad static const struct paxflag {
80a8c0f0c7Selad char mark;
81a8c0f0c7Selad const char *name;
824c20e2abSchristos uint32_t bits;
83a8c0f0c7Selad } flags[] = {
844e65b015Schristos { 'A', "ASLR, explicit enable",
854e65b015Schristos ELF_NOTE_PAX_ASLR },
864e65b015Schristos { 'a', "ASLR, explicit disable",
874e65b015Schristos ELF_NOTE_PAX_NOASLR },
882ffe4b87Schristos { 'G', "Segvguard, explicit enable",
892ffe4b87Schristos ELF_NOTE_PAX_GUARD },
902ffe4b87Schristos { 'g', "Segvguard, explicit disable",
912ffe4b87Schristos ELF_NOTE_PAX_NOGUARD },
922ffe4b87Schristos { 'M', "mprotect(2) restrictions, explicit enable",
932ffe4b87Schristos ELF_NOTE_PAX_MPROTECT },
942ffe4b87Schristos { 'm', "mprotect(2) restrictions, explicit disable",
952ffe4b87Schristos ELF_NOTE_PAX_NOMPROTECT },
96a8c0f0c7Selad };
97a8c0f0c7Selad
98a8c0f0c7Selad static void
usage(void)99a8c0f0c7Selad usage(void)
100a8c0f0c7Selad {
101*f598570bSrin (void)fprintf(stderr,
102*f598570bSrin "Usage: %s [ -0 | <-|+><A|a|G|g|M|m> ] <file> ...\n",
103a8c0f0c7Selad #if HAVE_NBTOOL_CONFIG_H
104a8c0f0c7Selad "paxctl"
105a8c0f0c7Selad #else
106a8c0f0c7Selad getprogname()
107a8c0f0c7Selad #endif
108a8c0f0c7Selad );
109a8c0f0c7Selad exit(1);
110a8c0f0c7Selad }
111a8c0f0c7Selad
1124c20e2abSchristos static uint32_t
pax_flag(char s)1134c20e2abSchristos pax_flag(char s)
114a8c0f0c7Selad {
115a8c0f0c7Selad size_t i;
116a8c0f0c7Selad
1174c20e2abSchristos if (s == '\0')
1184c20e2abSchristos return (uint32_t)-1;
119a8c0f0c7Selad
120a8c0f0c7Selad for (i = 0; i < __arraycount(flags); i++)
1214c20e2abSchristos if (s == flags[i].mark)
122a8c0f0c7Selad return flags[i].bits;
123a8c0f0c7Selad
1244c20e2abSchristos return (uint32_t)-1;
125a8c0f0c7Selad }
126a8c0f0c7Selad
127a8c0f0c7Selad static int
pax_flags_sane(uint32_t f)1284c20e2abSchristos pax_flags_sane(uint32_t f)
129a8c0f0c7Selad {
130a8c0f0c7Selad size_t i;
131a8c0f0c7Selad
132a8c0f0c7Selad for (i = 0; i < __arraycount(flags) - 1; i += 2) {
13329f2e1ceSlukem uint32_t g = flags[i].bits | flags[i+1].bits;
134a8c0f0c7Selad if ((f & g) == g)
135a8c0f0c7Selad return 0;
136a8c0f0c7Selad }
137a8c0f0c7Selad
138a8c0f0c7Selad return 1;
139a8c0f0c7Selad }
140a8c0f0c7Selad
141a8c0f0c7Selad static int
pax_haveflags(uint32_t f)1424c20e2abSchristos pax_haveflags(uint32_t f)
143a8c0f0c7Selad {
144a8c0f0c7Selad size_t i;
145a8c0f0c7Selad
146a8c0f0c7Selad for (i = 0; i < __arraycount(flags); i++)
147a8c0f0c7Selad if (f & flags[i].bits)
148a8c0f0c7Selad return (1);
149a8c0f0c7Selad
150a8c0f0c7Selad return (0);
151a8c0f0c7Selad }
152a8c0f0c7Selad
153a8c0f0c7Selad static void
pax_printflags(const char * name,int many,uint32_t f)1544c20e2abSchristos pax_printflags(const char *name, int many, uint32_t f)
155a8c0f0c7Selad {
156a8c0f0c7Selad size_t i;
157a8c0f0c7Selad
158a8c0f0c7Selad for (i = 0; i < __arraycount(flags); i++)
159f8082d9aSchristos if (f & flags[i].bits) {
160f8082d9aSchristos if (many)
161f8082d9aSchristos (void)printf("%s: ", name);
162a8c0f0c7Selad (void)printf(" %c: %s\n",
163a8c0f0c7Selad flags[i].mark, flags[i].name);
164a8c0f0c7Selad }
165f8082d9aSchristos }
166a8c0f0c7Selad
167f8082d9aSchristos static int
process_one(const char * name,uint32_t add_flags,uint32_t del_flags,int clear,int list,int many)1684c20e2abSchristos process_one(const char *name, uint32_t add_flags, uint32_t del_flags,
169*f598570bSrin int clear, int list, int many)
170a8c0f0c7Selad {
171a8c0f0c7Selad union {
172a8c0f0c7Selad Elf32_Ehdr h32;
173a8c0f0c7Selad Elf64_Ehdr h64;
174a8c0f0c7Selad } e;
175a8c0f0c7Selad union {
1766e36ff92Schristos Elf32_Shdr h32;
1776e36ff92Schristos Elf64_Shdr h64;
1786e36ff92Schristos } s;
1792ffe4b87Schristos union {
1802ffe4b87Schristos Elf32_Nhdr h32;
1812ffe4b87Schristos Elf64_Nhdr h64;
1822ffe4b87Schristos } n;
1834c20e2abSchristos #define SWAP(a) (swap == 0 ? (a) : \
1844c20e2abSchristos /*LINTED*/(sizeof(a) == 1 ? (a) : \
1854c20e2abSchristos /*LINTED*/(sizeof(a) == 2 ? bswap16(a) : \
1864c20e2abSchristos /*LINTED*/(sizeof(a) == 4 ? bswap32(a) : \
1874c20e2abSchristos /*LINTED*/(sizeof(a) == 8 ? bswap64(a) : (abort(), (a)))))))
1884c20e2abSchristos #define EH(field) (size == 32 ? SWAP(e.h32.field) : SWAP(e.h64.field))
1896e36ff92Schristos #define SH(field) (size == 32 ? SWAP(s.h32.field) : SWAP(s.h64.field))
1904c20e2abSchristos #define NH(field) (size == 32 ? SWAP(n.h32.field) : SWAP(n.h64.field))
1916e36ff92Schristos #define SHSIZE (size == 32 ? sizeof(s.h32) : sizeof(s.h64))
1922ffe4b87Schristos #define NHSIZE (size == 32 ? sizeof(n.h32) : sizeof(n.h64))
1932ffe4b87Schristos struct {
1942ffe4b87Schristos char name[ELF_NOTE_PAX_NAMESZ];
1952ffe4b87Schristos uint32_t flags;
1962ffe4b87Schristos } pax_tag;
19711a630f1Schristos int fd, size, ok = 0, flagged = 0, swap, error = 1;
19829f2e1ceSlukem size_t i;
199a8c0f0c7Selad
200f8082d9aSchristos fd = open(name, list ? O_RDONLY: O_RDWR, 0);
201f8082d9aSchristos if (fd == -1) {
202f8082d9aSchristos warn("Can't open `%s'", name);
20311a630f1Schristos return error;
204f8082d9aSchristos }
205f8082d9aSchristos
206f8082d9aSchristos if (read(fd, &e, sizeof(e)) != sizeof(e)) {
207f8082d9aSchristos warn("Can't read ELF header from `%s'", name);
20811a630f1Schristos goto out;
209f8082d9aSchristos }
210f8082d9aSchristos
211f8082d9aSchristos if (memcmp(e.h32.e_ident, ELFMAG, SELFMAG) != 0) {
2124c20e2abSchristos warnx("Bad ELF magic from `%s' (maybe it's not an ELF?)", name);
21311a630f1Schristos goto out;
214f8082d9aSchristos }
215f8082d9aSchristos
2164c20e2abSchristos if (e.h32.e_ehsize == sizeof(e.h32)) {
217f8082d9aSchristos size = 32;
2184c20e2abSchristos swap = 0;
2194c20e2abSchristos } else if (e.h64.e_ehsize == sizeof(e.h64)) {
220f8082d9aSchristos size = 64;
2214c20e2abSchristos swap = 0;
2224c20e2abSchristos } else if (bswap16(e.h32.e_ehsize) == sizeof(e.h32)) {
2234c20e2abSchristos size = 32;
2244c20e2abSchristos swap = 1;
2254c20e2abSchristos } else if (bswap16(e.h64.e_ehsize) == sizeof(e.h64)) {
2264c20e2abSchristos size = 64;
2274c20e2abSchristos swap = 1;
2284c20e2abSchristos } else {
2294c20e2abSchristos warnx("Bad ELF size %d from `%s' (maybe it's not an ELF?)",
230f8082d9aSchristos (int)e.h32.e_ehsize, name);
23111a630f1Schristos goto out;
232f8082d9aSchristos }
233f8082d9aSchristos
2346e36ff92Schristos for (i = 0; i < EH(e_shnum); i++) {
2356e36ff92Schristos if ((size_t)pread(fd, &s, SHSIZE,
2366e36ff92Schristos (off_t)EH(e_shoff) + i * SHSIZE) != SHSIZE) {
2376e36ff92Schristos warn("Can't read section header data from `%s'", name);
23811a630f1Schristos goto out;
239f8082d9aSchristos }
240f8082d9aSchristos
2416e36ff92Schristos if (SH(sh_type) != SHT_NOTE)
242f8082d9aSchristos continue;
243f8082d9aSchristos
2446e36ff92Schristos if (pread(fd, &n, NHSIZE, (off_t)SH(sh_offset)) != NHSIZE) {
245f8082d9aSchristos warn("Can't read note header from `%s'", name);
24611a630f1Schristos goto out;
247f8082d9aSchristos }
248f8082d9aSchristos if (NH(n_type) != ELF_NOTE_TYPE_PAX_TAG ||
249f8082d9aSchristos NH(n_descsz) != ELF_NOTE_PAX_DESCSZ ||
250f8082d9aSchristos NH(n_namesz) != ELF_NOTE_PAX_NAMESZ)
251f8082d9aSchristos continue;
2526e36ff92Schristos if (pread(fd, &pax_tag, sizeof(pax_tag), SH(sh_offset) + NHSIZE)
253f8082d9aSchristos != sizeof(pax_tag)) {
254f8082d9aSchristos warn("Can't read pax_tag from `%s'", name);
25511a630f1Schristos goto out;
256f8082d9aSchristos }
257f8082d9aSchristos if (memcmp(pax_tag.name, ELF_NOTE_PAX_NAME,
258f8082d9aSchristos sizeof(pax_tag.name)) != 0) {
259f8082d9aSchristos warn("Unknown pax_tag name `%*.*s' from `%s'",
260f8082d9aSchristos ELF_NOTE_PAX_NAMESZ, ELF_NOTE_PAX_NAMESZ,
261f8082d9aSchristos pax_tag.name, name);
26211a630f1Schristos goto out;
263f8082d9aSchristos }
264f8082d9aSchristos ok = 1;
265f8082d9aSchristos
266f8082d9aSchristos if (list) {
2674c20e2abSchristos if (!pax_haveflags(SWAP(pax_tag.flags)))
268f8082d9aSchristos break;
269f8082d9aSchristos
2704c20e2abSchristos if (!pax_flags_sane(SWAP(pax_tag.flags)))
2714c20e2abSchristos warnx("Current flags 0x%x don't make sense",
2724c20e2abSchristos (uint32_t)SWAP(pax_tag.flags));
273f8082d9aSchristos
274f8082d9aSchristos if (many)
275f8082d9aSchristos (void)printf("%s: ", name);
276f8082d9aSchristos (void)printf("PaX flags:\n");
277f8082d9aSchristos
2784c20e2abSchristos pax_printflags(name, many, SWAP(pax_tag.flags));
279f8082d9aSchristos flagged = 1;
280f8082d9aSchristos break;
281f8082d9aSchristos }
282f8082d9aSchristos
283*f598570bSrin if (clear) {
284*f598570bSrin pax_tag.flags = 0;
285*f598570bSrin } else {
2864c20e2abSchristos pax_tag.flags |= SWAP(add_flags);
2874c20e2abSchristos pax_tag.flags &= SWAP(~del_flags);
288*f598570bSrin }
289f8082d9aSchristos
2904c20e2abSchristos if (!pax_flags_sane(SWAP(pax_tag.flags))) {
2914c20e2abSchristos warnx("New flags 0x%x don't make sense",
2924c20e2abSchristos (uint32_t)SWAP(pax_tag.flags));
29311a630f1Schristos goto out;
294f8082d9aSchristos }
295f8082d9aSchristos
296f8082d9aSchristos if (pwrite(fd, &pax_tag, sizeof(pax_tag),
2976e36ff92Schristos (off_t)SH(sh_offset) + NHSIZE) != sizeof(pax_tag))
298f8082d9aSchristos warn("Can't modify flags on `%s'", name);
299f8082d9aSchristos break;
300f8082d9aSchristos }
301f8082d9aSchristos
302f8082d9aSchristos if (!ok) {
3036e36ff92Schristos warnx("Could not find an ELF PaX SHT_NOTE section in `%s'",
3044c20e2abSchristos name);
30511a630f1Schristos goto out;
306f8082d9aSchristos }
307f8082d9aSchristos
30811a630f1Schristos error = 0;
309f8082d9aSchristos if (list && !flagged) {
310f8082d9aSchristos if (many)
311f8082d9aSchristos (void)printf("%s: ", name);
312f8082d9aSchristos (void)printf("No PaX flags.\n");
313f8082d9aSchristos }
31411a630f1Schristos out:
31511a630f1Schristos (void)close(fd);
31611a630f1Schristos return error;
317f8082d9aSchristos }
318f8082d9aSchristos
319f8082d9aSchristos int
main(int argc,char ** argv)320f8082d9aSchristos main(int argc, char **argv)
321f8082d9aSchristos {
322f8082d9aSchristos char *opt;
323*f598570bSrin int i, clear = 0, list = 0, bad = 0, many, minus;
3244c20e2abSchristos uint32_t add_flags = 0, del_flags = 0;
3254c20e2abSchristos
3264c20e2abSchristos setprogname(argv[0]);
327a8c0f0c7Selad
328a8c0f0c7Selad if (argc < 2)
329a8c0f0c7Selad usage();
330a8c0f0c7Selad
331a8c0f0c7Selad for (i = 1; i < argc; i++) {
332a8c0f0c7Selad opt = argv[i];
333a8c0f0c7Selad
334*f598570bSrin if (strcmp(opt, "-0") == 0) {
335*f598570bSrin clear = 1;
336*f598570bSrin continue;
337*f598570bSrin }
338*f598570bSrin
339a8c0f0c7Selad if (*opt == '-' || *opt == '+') {
3404c20e2abSchristos uint32_t t;
3414c20e2abSchristos minus = 0;
342a8c0f0c7Selad
3434c20e2abSchristos while (*opt) {
3444c20e2abSchristos switch (*opt) {
3454c20e2abSchristos case '+':
3464c20e2abSchristos minus = 0;
3474c20e2abSchristos opt++;
3484c20e2abSchristos break;
3494c20e2abSchristos case '-':
3504c20e2abSchristos minus = 1;
3514c20e2abSchristos opt++;
3524c20e2abSchristos break;
3534c20e2abSchristos case ',':
3544c20e2abSchristos opt++;
3554c20e2abSchristos break;
3564c20e2abSchristos default:
3574c20e2abSchristos t = pax_flag(*opt++);
3584c20e2abSchristos if (t == (uint32_t)-1)
359a8c0f0c7Selad usage();
3604c20e2abSchristos if (minus)
361a8c0f0c7Selad del_flags |= t;
362a8c0f0c7Selad else
363a8c0f0c7Selad add_flags |= t;
3644c20e2abSchristos break;
3654c20e2abSchristos }
3664c20e2abSchristos }
367a8c0f0c7Selad } else
368a8c0f0c7Selad break;
369a8c0f0c7Selad }
370a8c0f0c7Selad
371f8082d9aSchristos if (i == argc)
372a8c0f0c7Selad usage();
373a8c0f0c7Selad
374*f598570bSrin switch ((add_flags != 0 || del_flags != 0) + clear) {
375*f598570bSrin case 0:
376a8c0f0c7Selad list = 1;
377*f598570bSrin break;
378*f598570bSrin case 1:
379*f598570bSrin break;
380*f598570bSrin default:
381*f598570bSrin usage();
382*f598570bSrin }
383a8c0f0c7Selad
384f8082d9aSchristos many = i != argc - 1;
385*f598570bSrin for (; i < argc; i++) {
386*f598570bSrin bad |= process_one(argv[i], add_flags, del_flags,
387*f598570bSrin clear, list, many);
388*f598570bSrin }
389a8c0f0c7Selad
390f8082d9aSchristos return bad ? EXIT_FAILURE : 0;
391a8c0f0c7Selad }
392