Home | History | Annotate | Line # | Download | only in paxctl
paxctl.c revision 1.7
      1  1.7  christos /* $NetBSD: paxctl.c,v 1.7 2007/12/26 22:16:31 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.7  christos __RCSID("$NetBSD: paxctl.c,v 1.7 2007/12/26 22:16:31 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.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.7  christos #define	ELF_NOTE_PAX_ASLR		0x10	/* Force enable ASLR */
     68  1.7  christos #define	ELF_NOTE_PAX_NOASLR		0x20	/* Force disable ASLR */
     69  1.2  christos #define ELF_NOTE_PAX_NAMESZ		4
     70  1.2  christos #define ELF_NOTE_PAX_NAME		"PaX\0"
     71  1.2  christos #define ELF_NOTE_PAX_DESCSZ		4
     72  1.1      elad #endif
     73  1.1      elad #ifndef __arraycount
     74  1.1      elad #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
     75  1.1      elad #endif
     76  1.1      elad 
     77  1.1      elad 
     78  1.1      elad static const struct paxflag {
     79  1.1      elad 	char mark;
     80  1.1      elad 	const char *name;
     81  1.1      elad 	int bits;
     82  1.1      elad } flags[] = {
     83  1.7  christos 	{ 'A', "ASLR, explicit enable",
     84  1.7  christos 	  ELF_NOTE_PAX_ASLR },
     85  1.7  christos 	{ 'a', "ASLR, explicit disable",
     86  1.7  christos 	  ELF_NOTE_PAX_NOASLR },
     87  1.2  christos 	{ 'G', "Segvguard, explicit enable",
     88  1.2  christos 	  ELF_NOTE_PAX_GUARD },
     89  1.2  christos 	{ 'g', "Segvguard, explicit disable",
     90  1.2  christos 	  ELF_NOTE_PAX_NOGUARD },
     91  1.2  christos 	{ 'M', "mprotect(2) restrictions, explicit enable",
     92  1.2  christos 	  ELF_NOTE_PAX_MPROTECT },
     93  1.2  christos 	{ 'm', "mprotect(2) restrictions, explicit disable",
     94  1.2  christos 	  ELF_NOTE_PAX_NOMPROTECT },
     95  1.1      elad };
     96  1.1      elad 
     97  1.1      elad static void
     98  1.1      elad usage(void)
     99  1.1      elad {
    100  1.7  christos 	(void)fprintf(stderr, "Usage: %s [ <-|+><A|a|G|g|M|m> ] <file> ...\n",
    101  1.1      elad #if HAVE_NBTOOL_CONFIG_H
    102  1.1      elad 	    "paxctl"
    103  1.1      elad #else
    104  1.1      elad 	    getprogname()
    105  1.1      elad #endif
    106  1.1      elad 	);
    107  1.1      elad 	exit(1);
    108  1.1      elad }
    109  1.1      elad 
    110  1.1      elad static int
    111  1.1      elad pax_flag(const char *s)
    112  1.1      elad {
    113  1.1      elad 	size_t i;
    114  1.1      elad 
    115  1.1      elad 	if (s[0] == '\0' || s[1] != '\0')
    116  1.1      elad 		return -1;
    117  1.1      elad 
    118  1.1      elad 	for (i = 0; i < __arraycount(flags); i++)
    119  1.1      elad 		if (*s == flags[i].mark)
    120  1.1      elad 			return flags[i].bits;
    121  1.1      elad 
    122  1.1      elad 	return -1;
    123  1.1      elad }
    124  1.1      elad 
    125  1.1      elad static int
    126  1.1      elad pax_flags_sane(u_long f)
    127  1.1      elad {
    128  1.1      elad 	size_t i;
    129  1.1      elad 
    130  1.1      elad 	for (i = 0; i < __arraycount(flags) - 1; i += 2) {
    131  1.1      elad 		int g = flags[i].bits | flags[i+1].bits;
    132  1.1      elad 		if ((f & g) == g)
    133  1.1      elad 			return 0;
    134  1.1      elad 	}
    135  1.1      elad 
    136  1.1      elad 	return 1;
    137  1.1      elad }
    138  1.1      elad 
    139  1.1      elad static int
    140  1.1      elad pax_haveflags(u_long f)
    141  1.1      elad {
    142  1.1      elad 	size_t i;
    143  1.1      elad 
    144  1.1      elad 	for (i = 0; i < __arraycount(flags); i++)
    145  1.1      elad 		if (f & flags[i].bits)
    146  1.1      elad 			return (1);
    147  1.1      elad 
    148  1.1      elad 	return (0);
    149  1.1      elad }
    150  1.1      elad 
    151  1.1      elad static void
    152  1.3  christos pax_printflags(const char *name, int many, u_long f)
    153  1.1      elad {
    154  1.1      elad 	size_t i;
    155  1.1      elad 
    156  1.1      elad 	for (i = 0; i < __arraycount(flags); i++)
    157  1.3  christos 		if (f & flags[i].bits) {
    158  1.3  christos 			if (many)
    159  1.3  christos 				(void)printf("%s: ", name);
    160  1.1      elad 			(void)printf("  %c: %s\n",
    161  1.1      elad 			    flags[i].mark, flags[i].name);
    162  1.3  christos 		}
    163  1.1      elad }
    164  1.1      elad 
    165  1.3  christos static int
    166  1.3  christos process_one(const char *name, int add_flags, int del_flags, int list, int many)
    167  1.1      elad {
    168  1.1      elad 	union {
    169  1.1      elad 	    Elf32_Ehdr h32;
    170  1.1      elad 	    Elf64_Ehdr h64;
    171  1.1      elad 	} e;
    172  1.1      elad 	union {
    173  1.1      elad 	    Elf32_Phdr h32;
    174  1.1      elad 	    Elf64_Phdr h64;
    175  1.1      elad 	} p;
    176  1.2  christos 	union {
    177  1.2  christos 	    Elf32_Nhdr h32;
    178  1.2  christos 	    Elf64_Nhdr h64;
    179  1.2  christos 	} n;
    180  1.1      elad #define EH(field)	(size == 32 ? e.h32.field : e.h64.field)
    181  1.1      elad #define PH(field)	(size == 32 ? p.h32.field : p.h64.field)
    182  1.2  christos #define NH(field)	(size == 32 ? n.h32.field : n.h64.field)
    183  1.1      elad #define SPH(field, val)	do { \
    184  1.1      elad     if (size == 32) \
    185  1.1      elad 	    p.h32.field val; \
    186  1.1      elad     else \
    187  1.1      elad 	    p.h64.field val; \
    188  1.1      elad } while (/*CONSTCOND*/0)
    189  1.1      elad #define PHSIZE		(size == 32 ? sizeof(p.h32) : sizeof(p.h64))
    190  1.2  christos #define NHSIZE		(size == 32 ? sizeof(n.h32) : sizeof(n.h64))
    191  1.2  christos 	struct {
    192  1.2  christos 		char name[ELF_NOTE_PAX_NAMESZ];
    193  1.2  christos 		uint32_t flags;
    194  1.2  christos 	} pax_tag;
    195  1.3  christos 	int i, fd, size, ok = 0, flagged = 0;
    196  1.1      elad 
    197  1.3  christos 	fd = open(name, list ? O_RDONLY: O_RDWR, 0);
    198  1.3  christos 	if (fd == -1) {
    199  1.3  christos 		warn("Can't open `%s'", name);
    200  1.3  christos 		return 1;
    201  1.3  christos 	}
    202  1.1      elad 
    203  1.3  christos 	if (read(fd, &e, sizeof(e)) != sizeof(e)) {
    204  1.3  christos 		warn("Can't read ELF header from `%s'", name);
    205  1.3  christos 		return 1;
    206  1.1      elad 	}
    207  1.1      elad 
    208  1.3  christos 	if (memcmp(e.h32.e_ident, ELFMAG, SELFMAG) != 0) {
    209  1.3  christos 		warn("Bad ELF magic from `%s' (maybe it's not an ELF?)", name);
    210  1.3  christos 		return 1;
    211  1.1      elad 	}
    212  1.1      elad 
    213  1.1      elad 	if (e.h32.e_ehsize == sizeof(e.h32))
    214  1.1      elad 		size = 32;
    215  1.1      elad 	else if (e.h64.e_ehsize == sizeof(e.h64))
    216  1.1      elad 		size = 64;
    217  1.3  christos 	else {
    218  1.3  christos 		warn("Bad ELF size %d from `%s' (maybe it's not an ELF?)",
    219  1.3  christos 		    (int)e.h32.e_ehsize, name);
    220  1.3  christos 		return 1;
    221  1.3  christos 	}
    222  1.1      elad 
    223  1.1      elad 	for (i = 0; i < EH(e_phnum); i++) {
    224  1.3  christos 		if (pread(fd, &p, PHSIZE, (off_t)EH(e_phoff) + i * PHSIZE) !=
    225  1.3  christos 		    PHSIZE) {
    226  1.3  christos 			warn("Can't read program header data from `%s'", name);
    227  1.3  christos 			return 1;
    228  1.3  christos 		}
    229  1.1      elad 
    230  1.1      elad 		if (PH(p_type) != PT_NOTE)
    231  1.1      elad 			continue;
    232  1.1      elad 
    233  1.3  christos 		if (pread(fd, &n, NHSIZE, (off_t)PH(p_offset)) != NHSIZE) {
    234  1.3  christos 			warn("Can't read note header from `%s'", name);
    235  1.3  christos 			return 1;
    236  1.3  christos 		}
    237  1.2  christos 		if (NH(n_type) != ELF_NOTE_TYPE_PAX_TAG ||
    238  1.2  christos 		    NH(n_descsz) != ELF_NOTE_PAX_DESCSZ ||
    239  1.2  christos 		    NH(n_namesz) != ELF_NOTE_PAX_NAMESZ)
    240  1.2  christos 			continue;
    241  1.2  christos 		if (pread(fd, &pax_tag, sizeof(pax_tag), PH(p_offset) + NHSIZE)
    242  1.3  christos 		    != sizeof(pax_tag)) {
    243  1.3  christos 			warn("Can't read pax_tag from `%s'", name);
    244  1.3  christos 			return 1;
    245  1.3  christos 		}
    246  1.2  christos 		if (memcmp(pax_tag.name, ELF_NOTE_PAX_NAME,
    247  1.3  christos 		    sizeof(pax_tag.name)) != 0) {
    248  1.3  christos 			warn("Unknown pax_tag name `%*.*s' from `%s'",
    249  1.3  christos 			    ELF_NOTE_PAX_NAMESZ, ELF_NOTE_PAX_NAMESZ,
    250  1.3  christos 			    pax_tag.name, name);
    251  1.3  christos 			return 1;
    252  1.3  christos 		}
    253  1.1      elad 		ok = 1;
    254  1.1      elad 
    255  1.1      elad 		if (list) {
    256  1.2  christos 			if (!pax_haveflags(pax_tag.flags))
    257  1.1      elad 				break;
    258  1.1      elad 
    259  1.2  christos 			if (!pax_flags_sane(pax_tag.flags))
    260  1.2  christos 				warnx("Current flags %x don't make sense",
    261  1.2  christos 				    pax_tag.flags);
    262  1.1      elad 
    263  1.3  christos 			if (many)
    264  1.3  christos 				(void)printf("%s: ", name);
    265  1.1      elad 			(void)printf("PaX flags:\n");
    266  1.1      elad 
    267  1.3  christos 			pax_printflags(name, many, pax_tag.flags);
    268  1.1      elad 
    269  1.1      elad 			flagged = 1;
    270  1.1      elad 
    271  1.1      elad 			break;
    272  1.1      elad 		}
    273  1.1      elad 
    274  1.2  christos 		pax_tag.flags |= add_flags;
    275  1.2  christos 		pax_tag.flags &= ~del_flags;
    276  1.1      elad 
    277  1.3  christos 		if (!pax_flags_sane(pax_tag.flags)) {
    278  1.3  christos 			warn("New flags %x don't make sense", pax_tag.flags);
    279  1.3  christos 			return 1;
    280  1.3  christos 		}
    281  1.1      elad 
    282  1.2  christos 		if (pwrite(fd, &pax_tag, sizeof(pax_tag),
    283  1.2  christos 		    (off_t)PH(p_offset) + NHSIZE) != sizeof(pax_tag))
    284  1.3  christos 			warn("Can't modify flags on `%s'", name);
    285  1.1      elad 		break;
    286  1.1      elad 	}
    287  1.1      elad 
    288  1.1      elad 	(void)close(fd);
    289  1.1      elad 
    290  1.3  christos 	if (!ok) {
    291  1.3  christos 		warn("Could not find an ELF PaX PT_NOTE section in `%s'", name);
    292  1.3  christos 		return 1;
    293  1.3  christos 	}
    294  1.1      elad 
    295  1.3  christos 	if (list && !flagged) {
    296  1.3  christos 		if (many)
    297  1.3  christos 			(void)printf("%s: ", name);
    298  1.1      elad 		(void)printf("No PaX flags.\n");
    299  1.3  christos 	}
    300  1.1      elad 	return 0;
    301  1.1      elad }
    302  1.3  christos 
    303  1.3  christos int
    304  1.3  christos main(int argc, char **argv)
    305  1.3  christos {
    306  1.3  christos 	char *opt;
    307  1.3  christos 	int i, add_flags = 0, del_flags = 0, list = 0, bad = 0, many;
    308  1.3  christos 
    309  1.3  christos 	if (argc < 2)
    310  1.3  christos 		usage();
    311  1.3  christos 
    312  1.3  christos 	for (i = 1; i < argc; i++) {
    313  1.3  christos 		opt = argv[i];
    314  1.3  christos 
    315  1.3  christos 		if (*opt == '-' || *opt == '+') {
    316  1.3  christos 			int t;
    317  1.3  christos 
    318  1.3  christos 			t = pax_flag(opt + 1);
    319  1.3  christos 			if (t == -1)
    320  1.3  christos 				usage();
    321  1.3  christos 
    322  1.3  christos 			if (*opt == '-')
    323  1.3  christos 				del_flags |= t;
    324  1.3  christos 			else
    325  1.3  christos 				add_flags |= t;
    326  1.3  christos 
    327  1.3  christos 			opt = NULL;
    328  1.3  christos 		} else
    329  1.3  christos 			break;
    330  1.3  christos 	}
    331  1.3  christos 
    332  1.3  christos 	if (i == argc)
    333  1.3  christos 		usage();
    334  1.3  christos 
    335  1.3  christos 	if (add_flags || del_flags) {
    336  1.3  christos 		if (list)
    337  1.3  christos 			usage();
    338  1.3  christos 	} else
    339  1.3  christos 		list = 1;
    340  1.3  christos 
    341  1.3  christos 	many = i != argc - 1;
    342  1.3  christos 	for (; i < argc; i++)
    343  1.3  christos 		bad |= process_one(argv[i], add_flags, del_flags, list, many);
    344  1.3  christos 
    345  1.3  christos 	return bad ? EXIT_FAILURE : 0;
    346  1.3  christos }
    347