Home | History | Annotate | Line # | Download | only in paxctl
paxctl.c revision 1.11
      1  1.11  christos /* $NetBSD: paxctl.c,v 1.11 2009/05/02 16:19: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.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.11  christos __RCSID("$NetBSD: paxctl.c,v 1.11 2009/05/02 16:19:36 christos 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.7  christos 	(void)fprintf(stderr, "Usage: %s [ <-|+><A|a|G|g|M|m> ] <file> ...\n",
    102   1.1      elad #if HAVE_NBTOOL_CONFIG_H
    103   1.1      elad 	    "paxctl"
    104   1.1      elad #else
    105   1.1      elad 	    getprogname()
    106   1.1      elad #endif
    107   1.1      elad 	);
    108   1.1      elad 	exit(1);
    109   1.1      elad }
    110   1.1      elad 
    111   1.8  christos static uint32_t
    112   1.8  christos pax_flag(char s)
    113   1.1      elad {
    114   1.1      elad 	size_t i;
    115   1.1      elad 
    116   1.8  christos 	if (s == '\0')
    117   1.8  christos 		return (uint32_t)-1;
    118   1.1      elad 
    119   1.1      elad 	for (i = 0; i < __arraycount(flags); i++)
    120   1.8  christos 		if (s == flags[i].mark)
    121   1.1      elad 			return flags[i].bits;
    122   1.1      elad 
    123   1.8  christos 	return (uint32_t)-1;
    124   1.1      elad }
    125   1.1      elad 
    126   1.1      elad static int
    127   1.8  christos pax_flags_sane(uint32_t f)
    128   1.1      elad {
    129   1.1      elad 	size_t i;
    130   1.1      elad 
    131   1.1      elad 	for (i = 0; i < __arraycount(flags) - 1; i += 2) {
    132   1.9     lukem 		uint32_t g = flags[i].bits | flags[i+1].bits;
    133   1.1      elad 		if ((f & g) == g)
    134   1.1      elad 			return 0;
    135   1.1      elad 	}
    136   1.1      elad 
    137   1.1      elad 	return 1;
    138   1.1      elad }
    139   1.1      elad 
    140   1.1      elad static int
    141   1.8  christos pax_haveflags(uint32_t f)
    142   1.1      elad {
    143   1.1      elad 	size_t i;
    144   1.1      elad 
    145   1.1      elad 	for (i = 0; i < __arraycount(flags); i++)
    146   1.1      elad 		if (f & flags[i].bits)
    147   1.1      elad 			return (1);
    148   1.1      elad 
    149   1.1      elad 	return (0);
    150   1.1      elad }
    151   1.1      elad 
    152   1.1      elad static void
    153   1.8  christos pax_printflags(const char *name, int many, uint32_t f)
    154   1.1      elad {
    155   1.1      elad 	size_t i;
    156   1.1      elad 
    157   1.1      elad 	for (i = 0; i < __arraycount(flags); i++)
    158   1.3  christos 		if (f & flags[i].bits) {
    159   1.3  christos 			if (many)
    160   1.3  christos 				(void)printf("%s: ", name);
    161   1.1      elad 			(void)printf("  %c: %s\n",
    162   1.1      elad 			    flags[i].mark, flags[i].name);
    163   1.3  christos 		}
    164   1.1      elad }
    165   1.1      elad 
    166   1.3  christos static int
    167   1.8  christos process_one(const char *name, uint32_t add_flags, uint32_t del_flags,
    168   1.8  christos     int list, int many)
    169   1.1      elad {
    170   1.1      elad 	union {
    171   1.1      elad 	    Elf32_Ehdr h32;
    172   1.1      elad 	    Elf64_Ehdr h64;
    173   1.1      elad 	} e;
    174   1.1      elad 	union {
    175   1.1      elad 	    Elf32_Phdr h32;
    176   1.1      elad 	    Elf64_Phdr h64;
    177   1.1      elad 	} p;
    178   1.2  christos 	union {
    179   1.2  christos 	    Elf32_Nhdr h32;
    180   1.2  christos 	    Elf64_Nhdr h64;
    181   1.2  christos 	} n;
    182   1.8  christos #define SWAP(a)	(swap == 0 ? (a) : \
    183   1.8  christos     /*LINTED*/(sizeof(a) == 1 ? (a) : \
    184   1.8  christos     /*LINTED*/(sizeof(a) == 2 ? bswap16(a) : \
    185   1.8  christos     /*LINTED*/(sizeof(a) == 4 ? bswap32(a) : \
    186   1.8  christos     /*LINTED*/(sizeof(a) == 8 ? bswap64(a) : (abort(), (a)))))))
    187   1.8  christos #define EH(field)	(size == 32 ? SWAP(e.h32.field) : SWAP(e.h64.field))
    188   1.8  christos #define PH(field)	(size == 32 ? SWAP(p.h32.field) : SWAP(p.h64.field))
    189   1.8  christos #define NH(field)	(size == 32 ? SWAP(n.h32.field) : SWAP(n.h64.field))
    190   1.1      elad #define PHSIZE		(size == 32 ? sizeof(p.h32) : sizeof(p.h64))
    191   1.2  christos #define NHSIZE		(size == 32 ? sizeof(n.h32) : sizeof(n.h64))
    192   1.2  christos 	struct {
    193   1.2  christos 		char name[ELF_NOTE_PAX_NAMESZ];
    194   1.2  christos 		uint32_t flags;
    195   1.2  christos 	} pax_tag;
    196  1.11  christos 	int fd, size, ok = 0, flagged = 0, swap, error = 1;
    197   1.9     lukem 	size_t i;
    198   1.1      elad 
    199   1.3  christos 	fd = open(name, list ? O_RDONLY: O_RDWR, 0);
    200   1.3  christos 	if (fd == -1) {
    201   1.3  christos 		warn("Can't open `%s'", name);
    202  1.11  christos 		return error;
    203   1.3  christos 	}
    204   1.1      elad 
    205   1.3  christos 	if (read(fd, &e, sizeof(e)) != sizeof(e)) {
    206   1.3  christos 		warn("Can't read ELF header from `%s'", name);
    207  1.11  christos 		goto out;
    208   1.1      elad 	}
    209   1.1      elad 
    210   1.3  christos 	if (memcmp(e.h32.e_ident, ELFMAG, SELFMAG) != 0) {
    211   1.8  christos 		warnx("Bad ELF magic from `%s' (maybe it's not an ELF?)", name);
    212  1.11  christos 		goto out;
    213   1.1      elad 	}
    214   1.1      elad 
    215   1.8  christos 	if (e.h32.e_ehsize == sizeof(e.h32)) {
    216   1.8  christos 		size = 32;
    217   1.8  christos 		swap = 0;
    218   1.8  christos 	} else if (e.h64.e_ehsize == sizeof(e.h64)) {
    219   1.8  christos 		size = 64;
    220   1.8  christos 		swap = 0;
    221   1.8  christos 	} else if (bswap16(e.h32.e_ehsize) == sizeof(e.h32)) {
    222   1.1      elad 		size = 32;
    223   1.8  christos 		swap = 1;
    224   1.8  christos 	} else if (bswap16(e.h64.e_ehsize) == sizeof(e.h64)) {
    225   1.1      elad 		size = 64;
    226   1.8  christos 		swap = 1;
    227   1.8  christos 	} else {
    228   1.8  christos 		warnx("Bad ELF size %d from `%s' (maybe it's not an ELF?)",
    229   1.3  christos 		    (int)e.h32.e_ehsize, name);
    230  1.11  christos 		goto out;
    231   1.3  christos 	}
    232   1.1      elad 
    233   1.1      elad 	for (i = 0; i < EH(e_phnum); i++) {
    234  1.11  christos 		if ((size_t)pread(fd, &p, PHSIZE,
    235  1.11  christos 		    (off_t)EH(e_phoff) + i * PHSIZE) != PHSIZE) {
    236   1.3  christos 			warn("Can't read program header data from `%s'", name);
    237  1.11  christos 			goto out;
    238   1.3  christos 		}
    239   1.1      elad 
    240   1.1      elad 		if (PH(p_type) != PT_NOTE)
    241   1.1      elad 			continue;
    242   1.1      elad 
    243   1.3  christos 		if (pread(fd, &n, NHSIZE, (off_t)PH(p_offset)) != NHSIZE) {
    244   1.3  christos 			warn("Can't read note header from `%s'", name);
    245  1.11  christos 			goto out;
    246   1.3  christos 		}
    247   1.2  christos 		if (NH(n_type) != ELF_NOTE_TYPE_PAX_TAG ||
    248   1.2  christos 		    NH(n_descsz) != ELF_NOTE_PAX_DESCSZ ||
    249   1.2  christos 		    NH(n_namesz) != ELF_NOTE_PAX_NAMESZ)
    250   1.2  christos 			continue;
    251   1.2  christos 		if (pread(fd, &pax_tag, sizeof(pax_tag), PH(p_offset) + NHSIZE)
    252   1.3  christos 		    != sizeof(pax_tag)) {
    253   1.3  christos 			warn("Can't read pax_tag from `%s'", name);
    254  1.11  christos 			goto out;
    255   1.3  christos 		}
    256   1.2  christos 		if (memcmp(pax_tag.name, ELF_NOTE_PAX_NAME,
    257   1.3  christos 		    sizeof(pax_tag.name)) != 0) {
    258   1.3  christos 			warn("Unknown pax_tag name `%*.*s' from `%s'",
    259   1.3  christos 			    ELF_NOTE_PAX_NAMESZ, ELF_NOTE_PAX_NAMESZ,
    260   1.3  christos 			    pax_tag.name, name);
    261  1.11  christos 			goto out;
    262   1.3  christos 		}
    263   1.1      elad 		ok = 1;
    264   1.1      elad 
    265   1.1      elad 		if (list) {
    266   1.8  christos 			if (!pax_haveflags(SWAP(pax_tag.flags)))
    267   1.1      elad 				break;
    268   1.1      elad 
    269   1.8  christos 			if (!pax_flags_sane(SWAP(pax_tag.flags)))
    270   1.8  christos 				warnx("Current flags 0x%x don't make sense",
    271   1.8  christos 				    (uint32_t)SWAP(pax_tag.flags));
    272   1.1      elad 
    273   1.3  christos 			if (many)
    274   1.3  christos 				(void)printf("%s: ", name);
    275   1.1      elad 			(void)printf("PaX flags:\n");
    276   1.1      elad 
    277   1.8  christos 			pax_printflags(name, many, SWAP(pax_tag.flags));
    278   1.1      elad 			flagged = 1;
    279   1.1      elad 			break;
    280   1.1      elad 		}
    281   1.1      elad 
    282   1.8  christos 		pax_tag.flags |= SWAP(add_flags);
    283   1.8  christos 		pax_tag.flags &= SWAP(~del_flags);
    284   1.1      elad 
    285   1.8  christos 		if (!pax_flags_sane(SWAP(pax_tag.flags))) {
    286   1.8  christos 			warnx("New flags 0x%x don't make sense",
    287   1.8  christos 			    (uint32_t)SWAP(pax_tag.flags));
    288  1.11  christos 			goto out;
    289   1.3  christos 		}
    290   1.1      elad 
    291   1.2  christos 		if (pwrite(fd, &pax_tag, sizeof(pax_tag),
    292   1.2  christos 		    (off_t)PH(p_offset) + NHSIZE) != sizeof(pax_tag))
    293   1.3  christos 			warn("Can't modify flags on `%s'", name);
    294   1.1      elad 		break;
    295   1.1      elad 	}
    296   1.1      elad 
    297   1.3  christos 	if (!ok) {
    298   1.8  christos 		warnx("Could not find an ELF PaX PT_NOTE section in `%s'",
    299   1.8  christos 		    name);
    300  1.11  christos 		goto out;
    301   1.3  christos 	}
    302   1.1      elad 
    303  1.11  christos 	error = 0;
    304   1.3  christos 	if (list && !flagged) {
    305   1.3  christos 		if (many)
    306   1.3  christos 			(void)printf("%s: ", name);
    307   1.1      elad 		(void)printf("No PaX flags.\n");
    308   1.3  christos 	}
    309  1.11  christos out:
    310  1.11  christos 	(void)close(fd);
    311  1.11  christos 	return error;
    312   1.1      elad }
    313   1.3  christos 
    314   1.3  christos int
    315   1.3  christos main(int argc, char **argv)
    316   1.3  christos {
    317   1.3  christos 	char *opt;
    318   1.8  christos 	int i, list = 0, bad = 0, many, minus;
    319   1.8  christos 	uint32_t add_flags = 0, del_flags = 0;
    320   1.8  christos 
    321   1.8  christos 	setprogname(argv[0]);
    322   1.3  christos 
    323   1.3  christos 	if (argc < 2)
    324   1.3  christos 		usage();
    325   1.3  christos 
    326   1.3  christos 	for (i = 1; i < argc; i++) {
    327   1.3  christos 		opt = argv[i];
    328   1.3  christos 
    329   1.3  christos 		if (*opt == '-' || *opt == '+') {
    330   1.8  christos 			uint32_t t;
    331   1.8  christos 			minus = 0;
    332   1.3  christos 
    333   1.8  christos 			while (*opt) {
    334   1.8  christos 				switch (*opt) {
    335   1.8  christos 				case '+':
    336   1.8  christos 					minus = 0;
    337   1.8  christos 					opt++;
    338   1.8  christos 					break;
    339   1.8  christos 				case '-':
    340   1.8  christos 					minus = 1;
    341   1.8  christos 					opt++;
    342   1.8  christos 					break;
    343   1.8  christos 				case ',':
    344   1.8  christos 					opt++;
    345   1.8  christos 					break;
    346   1.8  christos 				default:
    347   1.8  christos 					t = pax_flag(*opt++);
    348   1.8  christos 					if (t == (uint32_t)-1)
    349   1.8  christos 						usage();
    350   1.8  christos 					if (minus)
    351   1.8  christos 						del_flags |= t;
    352   1.8  christos 					else
    353   1.8  christos 						add_flags |= t;
    354   1.8  christos 					break;
    355   1.8  christos 				}
    356   1.8  christos 			}
    357   1.3  christos 		} else
    358   1.3  christos 			break;
    359   1.3  christos 	}
    360   1.3  christos 
    361   1.3  christos 	if (i == argc)
    362   1.3  christos 		usage();
    363   1.3  christos 
    364   1.3  christos 	if (add_flags || del_flags) {
    365   1.3  christos 		if (list)
    366   1.3  christos 			usage();
    367   1.3  christos 	} else
    368   1.3  christos 		list = 1;
    369   1.3  christos 
    370   1.3  christos 	many = i != argc - 1;
    371   1.3  christos 	for (; i < argc; i++)
    372   1.3  christos 		bad |= process_one(argv[i], add_flags, del_flags, list, many);
    373   1.3  christos 
    374   1.3  christos 	return bad ? EXIT_FAILURE : 0;
    375   1.3  christos }
    376