Home | History | Annotate | Line # | Download | only in kern
kern_pax.c revision 1.38
      1  1.38  christos /*	$NetBSD: kern_pax.c,v 1.38 2016/04/07 03:31:12 christos Exp $	*/
      2   1.1      elad 
      3  1.29      maxv /*
      4  1.29      maxv  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.29      maxv  * All rights reserved.
      6  1.29      maxv  *
      7  1.29      maxv  * This code is derived from software contributed to The NetBSD Foundation
      8  1.29      maxv  * by Maxime Villard.
      9  1.29      maxv  *
     10  1.29      maxv  * Redistribution and use in source and binary forms, with or without
     11  1.29      maxv  * modification, are permitted provided that the following conditions
     12  1.29      maxv  * are met:
     13  1.29      maxv  * 1. Redistributions of source code must retain the above copyright
     14  1.29      maxv  *    notice, this list of conditions and the following disclaimer.
     15  1.29      maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.29      maxv  *    notice, this list of conditions and the following disclaimer in the
     17  1.29      maxv  *    documentation and/or other materials provided with the distribution.
     18  1.29      maxv  *
     19  1.29      maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.29      maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.29      maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.29      maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.29      maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.29      maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.29      maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.29      maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.29      maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.29      maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.29      maxv  * POSSIBILITY OF SUCH DAMAGE.
     30  1.29      maxv  */
     31  1.29      maxv 
     32  1.29      maxv /*
     33   1.1      elad  * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
     34   1.1      elad  * All rights reserved.
     35   1.1      elad  *
     36   1.1      elad  * Redistribution and use in source and binary forms, with or without
     37   1.1      elad  * modification, are permitted provided that the following conditions
     38   1.1      elad  * are met:
     39   1.1      elad  * 1. Redistributions of source code must retain the above copyright
     40   1.1      elad  *    notice, this list of conditions and the following disclaimer.
     41   1.1      elad  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1      elad  *    notice, this list of conditions and the following disclaimer in the
     43   1.1      elad  *    documentation and/or other materials provided with the distribution.
     44  1.12      elad  * 3. The name of the author may not be used to endorse or promote products
     45   1.1      elad  *    derived from this software without specific prior written permission.
     46   1.1      elad  *
     47   1.1      elad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48   1.1      elad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49   1.1      elad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50   1.1      elad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51   1.1      elad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52   1.1      elad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53   1.1      elad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54   1.1      elad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55   1.1      elad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56   1.1      elad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57   1.1      elad  */
     58   1.1      elad 
     59  1.17       dsl #include <sys/cdefs.h>
     60  1.38  christos __KERNEL_RCSID(0, "$NetBSD: kern_pax.c,v 1.38 2016/04/07 03:31:12 christos Exp $");
     61  1.17       dsl 
     62   1.1      elad #include "opt_pax.h"
     63   1.1      elad 
     64   1.1      elad #include <sys/param.h>
     65   1.1      elad #include <sys/proc.h>
     66  1.33  christos #include <sys/exec.h>
     67   1.1      elad #include <sys/exec_elf.h>
     68   1.1      elad #include <sys/pax.h>
     69   1.1      elad #include <sys/sysctl.h>
     70  1.25     rmind #include <sys/kmem.h>
     71  1.34  christos #include <sys/mman.h>
     72   1.8      elad #include <sys/fileassoc.h>
     73   1.8      elad #include <sys/syslog.h>
     74   1.8      elad #include <sys/vnode.h>
     75   1.8      elad #include <sys/queue.h>
     76   1.8      elad #include <sys/kauth.h>
     77  1.26       tls #include <sys/cprng.h>
     78   1.1      elad 
     79  1.29      maxv #ifdef PAX_ASLR_DEBUG
     80  1.34  christos #define PAX_DPRINTF(_fmt, args...) \
     81  1.34  christos 	do if (pax_aslr_debug) uprintf("%s: " _fmt "\n", __func__, ##args); \
     82  1.34  christos 	while (/*CONSTCOND*/0)
     83  1.29      maxv #else
     84  1.29      maxv #define PAX_DPRINTF(_fmt, args...)	do {} while (/*CONSTCOND*/0)
     85  1.29      maxv #endif
     86  1.29      maxv 
     87  1.18  christos #ifdef PAX_ASLR
     88  1.18  christos #include <sys/mman.h>
     89  1.18  christos 
     90  1.18  christos int pax_aslr_enabled = 1;
     91  1.18  christos int pax_aslr_global = PAX_ASLR;
     92  1.18  christos 
     93  1.18  christos #ifndef PAX_ASLR_DELTA_MMAP_LSB
     94  1.18  christos #define PAX_ASLR_DELTA_MMAP_LSB		PGSHIFT
     95  1.18  christos #endif
     96  1.18  christos #ifndef PAX_ASLR_DELTA_MMAP_LEN
     97  1.18  christos #define PAX_ASLR_DELTA_MMAP_LEN		((sizeof(void *) * NBBY) / 2)
     98  1.18  christos #endif
     99  1.36  christos #ifndef PAX_ASLR_DELTA_MMAP_LEN32
    100  1.36  christos #define PAX_ASLR_DELTA_MMAP_LEN32	((sizeof(uint32_t) * NBBY) / 2)
    101  1.36  christos #endif
    102  1.18  christos #ifndef PAX_ASLR_DELTA_STACK_LSB
    103  1.18  christos #define PAX_ASLR_DELTA_STACK_LSB	PGSHIFT
    104  1.18  christos #endif
    105  1.18  christos #ifndef PAX_ASLR_DELTA_STACK_LEN
    106  1.18  christos #define PAX_ASLR_DELTA_STACK_LEN 	12
    107  1.18  christos #endif
    108  1.18  christos 
    109  1.29      maxv static bool pax_aslr_elf_flags_active(uint32_t);
    110  1.18  christos #endif /* PAX_ASLR */
    111  1.18  christos 
    112   1.7      elad #ifdef PAX_MPROTECT
    113   1.6      yamt static int pax_mprotect_enabled = 1;
    114   1.6      yamt static int pax_mprotect_global = PAX_MPROTECT;
    115  1.29      maxv static bool pax_mprotect_elf_flags_active(uint32_t);
    116  1.18  christos #endif /* PAX_MPROTECT */
    117  1.38  christos #ifdef PAX_MPROTECT_DEBUG
    118  1.38  christos int pax_mprotect_debug;
    119  1.38  christos #endif
    120   1.8      elad 
    121   1.8      elad #ifdef PAX_SEGVGUARD
    122   1.8      elad #ifndef PAX_SEGVGUARD_EXPIRY
    123   1.8      elad #define	PAX_SEGVGUARD_EXPIRY		(2 * 60)
    124   1.8      elad #endif
    125   1.8      elad #ifndef PAX_SEGVGUARD_SUSPENSION
    126   1.8      elad #define	PAX_SEGVGUARD_SUSPENSION	(10 * 60)
    127   1.8      elad #endif
    128   1.8      elad #ifndef	PAX_SEGVGUARD_MAXCRASHES
    129   1.8      elad #define	PAX_SEGVGUARD_MAXCRASHES	5
    130   1.8      elad #endif
    131   1.8      elad 
    132  1.34  christos #ifdef PAX_ASLR_DEBUG
    133  1.34  christos int pax_aslr_debug;
    134  1.34  christos #endif
    135  1.34  christos 
    136   1.8      elad static int pax_segvguard_enabled = 1;
    137   1.8      elad static int pax_segvguard_global = PAX_SEGVGUARD;
    138   1.8      elad static int pax_segvguard_expiry = PAX_SEGVGUARD_EXPIRY;
    139   1.8      elad static int pax_segvguard_suspension = PAX_SEGVGUARD_SUSPENSION;
    140   1.8      elad static int pax_segvguard_maxcrashes = PAX_SEGVGUARD_MAXCRASHES;
    141   1.8      elad 
    142   1.8      elad static fileassoc_t segvguard_id;
    143   1.8      elad 
    144   1.8      elad struct pax_segvguard_uid_entry {
    145   1.8      elad 	uid_t sue_uid;
    146   1.8      elad 	size_t sue_ncrashes;
    147   1.8      elad 	time_t sue_expiry;
    148   1.8      elad 	time_t sue_suspended;
    149   1.8      elad 	LIST_ENTRY(pax_segvguard_uid_entry) sue_list;
    150   1.8      elad };
    151   1.8      elad 
    152   1.8      elad struct pax_segvguard_entry {
    153   1.8      elad 	LIST_HEAD(, pax_segvguard_uid_entry) segv_uids;
    154   1.8      elad };
    155  1.10      yamt 
    156  1.29      maxv static bool pax_segvguard_elf_flags_active(uint32_t);
    157  1.31      maxv static void pax_segvguard_cleanup_cb(void *);
    158   1.8      elad #endif /* PAX_SEGVGUARD */
    159   1.7      elad 
    160   1.1      elad SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
    161   1.1      elad {
    162   1.8      elad 	const struct sysctlnode *rnode = NULL, *cnode;
    163   1.1      elad 
    164  1.27     pooka 	sysctl_createv(clog, 0, NULL, &rnode,
    165   1.1      elad 		       CTLFLAG_PERMANENT,
    166   1.1      elad 		       CTLTYPE_NODE, "pax",
    167   1.1      elad 		       SYSCTL_DESCR("PaX (exploit mitigation) features."),
    168   1.1      elad 		       NULL, 0, NULL, 0,
    169  1.27     pooka 		       CTL_SECURITY, CTL_CREATE, CTL_EOL);
    170   1.1      elad 
    171  1.19      elad 	cnode = rnode;
    172  1.19      elad 
    173  1.18  christos #ifdef PAX_MPROTECT
    174  1.19      elad 	rnode = cnode;
    175   1.1      elad 	sysctl_createv(clog, 0, &rnode, &rnode,
    176   1.1      elad 		       CTLFLAG_PERMANENT,
    177   1.1      elad 		       CTLTYPE_NODE, "mprotect",
    178   1.1      elad 		       SYSCTL_DESCR("mprotect(2) W^X restrictions."),
    179   1.1      elad 		       NULL, 0, NULL, 0,
    180   1.1      elad 		       CTL_CREATE, CTL_EOL);
    181   1.1      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    182   1.2      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    183   1.1      elad 		       CTLTYPE_INT, "enabled",
    184   1.1      elad 		       SYSCTL_DESCR("Restrictions enabled."),
    185   1.2      elad 		       NULL, 0, &pax_mprotect_enabled, 0,
    186   1.1      elad 		       CTL_CREATE, CTL_EOL);
    187   1.1      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    188   1.2      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    189   1.4      elad 		       CTLTYPE_INT, "global",
    190   1.1      elad 		       SYSCTL_DESCR("When enabled, unless explicitly "
    191   1.5    cbiere 				    "specified, apply restrictions to "
    192   1.1      elad 				    "all processes."),
    193   1.2      elad 		       NULL, 0, &pax_mprotect_global, 0,
    194   1.1      elad 		       CTL_CREATE, CTL_EOL);
    195  1.38  christos #ifdef PAX_MPROTECT_DEBUG
    196  1.38  christos 	sysctl_createv(clog, 0, &rnode, NULL,
    197  1.38  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    198  1.38  christos 		       CTLTYPE_INT, "debug",
    199  1.38  christos 		       SYSCTL_DESCR("print mprotect changes."),
    200  1.38  christos 		       NULL, 0, &pax_mprotect_debug, 0,
    201  1.38  christos 		       CTL_CREATE, CTL_EOL);
    202  1.38  christos #endif
    203   1.7      elad #endif /* PAX_MPROTECT */
    204   1.8      elad 
    205  1.18  christos #ifdef PAX_SEGVGUARD
    206   1.8      elad 	rnode = cnode;
    207   1.8      elad 	sysctl_createv(clog, 0, &rnode, &rnode,
    208   1.8      elad 		       CTLFLAG_PERMANENT,
    209   1.8      elad 		       CTLTYPE_NODE, "segvguard",
    210   1.8      elad 		       SYSCTL_DESCR("PaX segvguard."),
    211   1.8      elad 		       NULL, 0, NULL, 0,
    212   1.8      elad 		       CTL_CREATE, CTL_EOL);
    213   1.8      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    214   1.8      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    215   1.8      elad 		       CTLTYPE_INT, "enabled",
    216   1.8      elad 		       SYSCTL_DESCR("segvguard enabled."),
    217   1.8      elad 		       NULL, 0, &pax_segvguard_enabled, 0,
    218   1.8      elad 		       CTL_CREATE, CTL_EOL);
    219   1.8      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    220   1.8      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    221   1.8      elad 		       CTLTYPE_INT, "global",
    222   1.8      elad 		       SYSCTL_DESCR("segvguard all programs."),
    223   1.8      elad 		       NULL, 0, &pax_segvguard_global, 0,
    224   1.8      elad 		       CTL_CREATE, CTL_EOL);
    225   1.8      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    226   1.8      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    227   1.8      elad 		       CTLTYPE_INT, "expiry_timeout",
    228   1.8      elad 		       SYSCTL_DESCR("Entry expiry timeout (in seconds)."),
    229   1.8      elad 		       NULL, 0, &pax_segvguard_expiry, 0,
    230   1.8      elad 		       CTL_CREATE, CTL_EOL);
    231   1.8      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    232   1.8      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    233   1.8      elad 		       CTLTYPE_INT, "suspend_timeout",
    234   1.8      elad 		       SYSCTL_DESCR("Entry suspension timeout (in seconds)."),
    235   1.8      elad 		       NULL, 0, &pax_segvguard_suspension, 0,
    236   1.8      elad 		       CTL_CREATE, CTL_EOL);
    237   1.8      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    238   1.8      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    239   1.8      elad 		       CTLTYPE_INT, "max_crashes",
    240   1.8      elad 		       SYSCTL_DESCR("Max number of crashes before expiry."),
    241   1.8      elad 		       NULL, 0, &pax_segvguard_maxcrashes, 0,
    242   1.8      elad 		       CTL_CREATE, CTL_EOL);
    243   1.8      elad #endif /* PAX_SEGVGUARD */
    244  1.18  christos 
    245  1.18  christos #ifdef PAX_ASLR
    246  1.18  christos 	rnode = cnode;
    247  1.18  christos 	sysctl_createv(clog, 0, &rnode, &rnode,
    248  1.18  christos 		       CTLFLAG_PERMANENT,
    249  1.18  christos 		       CTLTYPE_NODE, "aslr",
    250  1.18  christos 		       SYSCTL_DESCR("Address Space Layout Randomization."),
    251  1.18  christos 		       NULL, 0, NULL, 0,
    252  1.18  christos 		       CTL_CREATE, CTL_EOL);
    253  1.18  christos 	sysctl_createv(clog, 0, &rnode, NULL,
    254  1.18  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    255  1.18  christos 		       CTLTYPE_INT, "enabled",
    256  1.18  christos 		       SYSCTL_DESCR("Restrictions enabled."),
    257  1.18  christos 		       NULL, 0, &pax_aslr_enabled, 0,
    258  1.18  christos 		       CTL_CREATE, CTL_EOL);
    259  1.18  christos 	sysctl_createv(clog, 0, &rnode, NULL,
    260  1.18  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    261  1.18  christos 		       CTLTYPE_INT, "global",
    262  1.18  christos 		       SYSCTL_DESCR("When enabled, unless explicitly "
    263  1.18  christos 				    "specified, apply to all processes."),
    264  1.18  christos 		       NULL, 0, &pax_aslr_global, 0,
    265  1.18  christos 		       CTL_CREATE, CTL_EOL);
    266  1.34  christos #ifdef PAX_ASLR_DEBUG
    267  1.34  christos 	sysctl_createv(clog, 0, &rnode, NULL,
    268  1.34  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    269  1.34  christos 		       CTLTYPE_INT, "debug",
    270  1.34  christos 		       SYSCTL_DESCR("Pring ASLR selected addresses."),
    271  1.34  christos 		       NULL, 0, &pax_aslr_debug, 0,
    272  1.34  christos 		       CTL_CREATE, CTL_EOL);
    273  1.34  christos #endif
    274  1.18  christos 	sysctl_createv(clog, 0, &rnode, NULL,
    275  1.18  christos 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    276  1.18  christos 		       CTLTYPE_INT, "mmap_len",
    277  1.18  christos 		       SYSCTL_DESCR("Number of bits randomized for "
    278  1.18  christos 				    "mmap(2) calls."),
    279  1.18  christos 		       NULL, PAX_ASLR_DELTA_MMAP_LEN, NULL, 0,
    280  1.18  christos 		       CTL_CREATE, CTL_EOL);
    281  1.19      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    282  1.19      elad 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    283  1.19      elad 		       CTLTYPE_INT, "stack_len",
    284  1.19      elad 		       SYSCTL_DESCR("Number of bits randomized for "
    285  1.19      elad 				    "the stack."),
    286  1.19      elad 		       NULL, PAX_ASLR_DELTA_STACK_LEN, NULL, 0,
    287  1.19      elad 		       CTL_CREATE, CTL_EOL);
    288  1.19      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    289  1.19      elad 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    290  1.19      elad 		       CTLTYPE_INT, "exec_len",
    291  1.19      elad 		       SYSCTL_DESCR("Number of bits randomized for "
    292  1.19      elad 				    "the PIE exec base."),
    293  1.19      elad 		       NULL, PAX_ASLR_DELTA_EXEC_LEN, NULL, 0,
    294  1.19      elad 		       CTL_CREATE, CTL_EOL);
    295  1.19      elad 
    296  1.18  christos #endif /* PAX_ASLR */
    297   1.1      elad }
    298   1.1      elad 
    299   1.7      elad /*
    300   1.7      elad  * Initialize PaX.
    301   1.7      elad  */
    302   1.1      elad void
    303   1.7      elad pax_init(void)
    304   1.1      elad {
    305   1.9      yamt #ifdef PAX_SEGVGUARD
    306   1.9      yamt 	int error;
    307   1.9      yamt 
    308  1.31      maxv 	error = fileassoc_register("segvguard", pax_segvguard_cleanup_cb,
    309   1.9      yamt 	    &segvguard_id);
    310   1.9      yamt 	if (error) {
    311   1.9      yamt 		panic("pax_init: segvguard_id: error=%d\n", error);
    312   1.9      yamt 	}
    313   1.8      elad #endif /* PAX_SEGVGUARD */
    314   1.1      elad }
    315   1.1      elad 
    316  1.29      maxv void
    317  1.32      maxv pax_setup_elf_flags(struct exec_package *epp, uint32_t elf_flags)
    318  1.29      maxv {
    319  1.29      maxv 	uint32_t flags = 0;
    320  1.29      maxv 
    321  1.29      maxv #ifdef PAX_ASLR
    322  1.29      maxv 	if (pax_aslr_elf_flags_active(elf_flags)) {
    323  1.29      maxv 		flags |= P_PAX_ASLR;
    324  1.29      maxv 	}
    325  1.29      maxv #endif
    326  1.29      maxv #ifdef PAX_MPROTECT
    327  1.29      maxv 	if (pax_mprotect_elf_flags_active(elf_flags)) {
    328  1.29      maxv 		flags |= P_PAX_MPROTECT;
    329  1.29      maxv 	}
    330  1.29      maxv #endif
    331  1.29      maxv #ifdef PAX_SEGVGUARD
    332  1.29      maxv 	if (pax_segvguard_elf_flags_active(elf_flags)) {
    333  1.29      maxv 		flags |= P_PAX_GUARD;
    334  1.29      maxv 	}
    335  1.29      maxv #endif
    336  1.29      maxv 
    337  1.32      maxv 	epp->ep_pax_flags = flags;
    338  1.29      maxv }
    339  1.29      maxv 
    340  1.31      maxv #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
    341  1.31      maxv static inline bool
    342  1.31      maxv pax_flags_active(uint32_t flags, uint32_t opt)
    343  1.31      maxv {
    344  1.31      maxv 	if (!(flags & opt))
    345  1.31      maxv 		return false;
    346  1.31      maxv 	return true;
    347  1.31      maxv }
    348  1.31      maxv #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
    349  1.29      maxv 
    350   1.7      elad #ifdef PAX_MPROTECT
    351  1.29      maxv static bool
    352  1.29      maxv pax_mprotect_elf_flags_active(uint32_t flags)
    353  1.29      maxv {
    354  1.29      maxv 	if (!pax_mprotect_enabled)
    355  1.29      maxv 		return false;
    356  1.29      maxv 	if (pax_mprotect_global && (flags & ELF_NOTE_PAX_NOMPROTECT) != 0) {
    357  1.29      maxv 		/* Mprotect explicitly disabled */
    358  1.29      maxv 		return false;
    359  1.29      maxv 	}
    360  1.29      maxv 	if (!pax_mprotect_global && (flags & ELF_NOTE_PAX_MPROTECT) == 0) {
    361  1.29      maxv 		/* Mprotect not requested */
    362  1.29      maxv 		return false;
    363  1.29      maxv 	}
    364  1.29      maxv 	return true;
    365  1.29      maxv }
    366  1.29      maxv 
    367   1.1      elad void
    368  1.38  christos pax_mprotect_adjust(
    369  1.38  christos #ifdef PAX_MPROTECT_DEBUG
    370  1.38  christos     const char *file, size_t line,
    371  1.38  christos #endif
    372  1.38  christos     struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
    373   1.1      elad {
    374  1.29      maxv 	uint32_t flags;
    375   1.7      elad 
    376  1.29      maxv 	flags = l->l_proc->p_pax;
    377  1.31      maxv 	if (!pax_flags_active(flags, P_PAX_MPROTECT))
    378   1.1      elad 		return;
    379   1.1      elad 
    380   1.3      elad 	if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
    381  1.38  christos #ifdef PAX_MPROTECT_DEBUG
    382  1.37  christos 		struct proc *p = l->l_proc;
    383  1.38  christos 		if (pax_mprotect_debug) {
    384  1.38  christos 			printf("%s: %s,%zu: %d.%d (%s): -x\n",
    385  1.38  christos 			    __func__, file, line,
    386  1.38  christos 			    p->p_pid, l->l_lid, p->p_comm);
    387  1.38  christos 		}
    388  1.37  christos #endif
    389   1.3      elad 		*prot &= ~VM_PROT_EXECUTE;
    390   1.3      elad 		*maxprot &= ~VM_PROT_EXECUTE;
    391   1.1      elad 	} else {
    392  1.38  christos #ifdef PAX_MPROTECT_DEBUG
    393  1.37  christos 		struct proc *p = l->l_proc;
    394  1.38  christos 		if (pax_mprotect_debug) {
    395  1.38  christos 			printf("%s: %s,%zu: %d.%d (%s): -w\n",
    396  1.38  christos 			    __func__, file, line,
    397  1.38  christos 			    p->p_pid, l->l_lid, p->p_comm);
    398  1.38  christos 		}
    399  1.37  christos #endif
    400   1.3      elad 		*prot &= ~VM_PROT_WRITE;
    401   1.3      elad 		*maxprot &= ~VM_PROT_WRITE;
    402   1.1      elad 	}
    403   1.1      elad }
    404   1.7      elad #endif /* PAX_MPROTECT */
    405   1.8      elad 
    406  1.18  christos #ifdef PAX_ASLR
    407  1.29      maxv static bool
    408  1.29      maxv pax_aslr_elf_flags_active(uint32_t flags)
    409  1.18  christos {
    410  1.18  christos 	if (!pax_aslr_enabled)
    411  1.18  christos 		return false;
    412  1.29      maxv 	if (pax_aslr_global && (flags & ELF_NOTE_PAX_NOASLR) != 0) {
    413  1.29      maxv 		/* ASLR explicitly disabled */
    414  1.29      maxv 		return false;
    415  1.29      maxv 	}
    416  1.29      maxv 	if (!pax_aslr_global && (flags & ELF_NOTE_PAX_ASLR) == 0) {
    417  1.29      maxv 		/* ASLR not requested */
    418  1.29      maxv 		return false;
    419  1.29      maxv 	}
    420  1.29      maxv 	return true;
    421  1.29      maxv }
    422  1.18  christos 
    423  1.29      maxv bool
    424  1.32      maxv pax_aslr_epp_active(struct exec_package *epp)
    425  1.32      maxv {
    426  1.32      maxv 	return pax_flags_active(epp->ep_pax_flags, P_PAX_ASLR);
    427  1.32      maxv }
    428  1.32      maxv 
    429  1.32      maxv bool
    430  1.29      maxv pax_aslr_active(struct lwp *l)
    431  1.29      maxv {
    432  1.31      maxv 	return pax_flags_active(l->l_proc->p_pax, P_PAX_ASLR);
    433  1.18  christos }
    434  1.18  christos 
    435  1.18  christos void
    436  1.35   khorben pax_aslr_init_vm(struct lwp *l, struct vmspace *vm, struct exec_package *ep)
    437  1.18  christos {
    438  1.18  christos 	if (!pax_aslr_active(l))
    439  1.18  christos 		return;
    440  1.18  christos 
    441  1.36  christos 	uint32_t len = (ep->ep_flags & EXEC_32) ?
    442  1.36  christos 	    PAX_ASLR_DELTA_MMAP_LEN32 : PAX_ASLR_DELTA_MMAP_LEN;
    443  1.36  christos 
    444  1.36  christos 	vm->vm_aslr_delta_mmap = PAX_ASLR_DELTA(cprng_fast32(),
    445  1.36  christos 	    PAX_ASLR_DELTA_MMAP_LSB, len);
    446  1.36  christos 
    447  1.36  christos 	PAX_DPRINTF("delta_mmap=%#jx/%u", vm->vm_aslr_delta_mmap, len);
    448  1.18  christos }
    449  1.18  christos 
    450  1.18  christos void
    451  1.31      maxv pax_aslr_mmap(struct lwp *l, vaddr_t *addr, vaddr_t orig_addr, int f)
    452  1.18  christos {
    453  1.18  christos 	if (!pax_aslr_active(l))
    454  1.18  christos 		return;
    455  1.34  christos #ifdef PAX_ASLR_DEBUG
    456  1.34  christos 	char buf[256];
    457  1.34  christos 	if (pax_aslr_debug)
    458  1.34  christos 		snprintb(buf, sizeof(buf), MAP_FMT, f);
    459  1.34  christos 	else
    460  1.34  christos 		buf[0] = '\0';
    461  1.34  christos #endif
    462  1.18  christos 
    463  1.18  christos 	if (!(f & MAP_FIXED) && ((orig_addr == 0) || !(f & MAP_ANON))) {
    464  1.34  christos 		PAX_DPRINTF("applying to %#jx orig_addr=%#jx f=%s",
    465  1.34  christos 		    (uintmax_t)*addr, (uintmax_t)orig_addr, buf);
    466  1.18  christos 		if (!(l->l_proc->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
    467  1.18  christos 			*addr += l->l_proc->p_vmspace->vm_aslr_delta_mmap;
    468  1.18  christos 		else
    469  1.18  christos 			*addr -= l->l_proc->p_vmspace->vm_aslr_delta_mmap;
    470  1.34  christos 		PAX_DPRINTF("result %#jx", (uintmax_t)*addr);
    471  1.29      maxv 	} else {
    472  1.34  christos 		PAX_DPRINTF("not applying to %#jx orig_addr=%#jx f=%s",
    473  1.34  christos 		    (uintmax_t)*addr, (uintmax_t)orig_addr, buf);
    474  1.18  christos 	}
    475  1.18  christos }
    476  1.18  christos 
    477  1.18  christos void
    478  1.32      maxv pax_aslr_stack(struct exec_package *epp, u_long *max_stack_size)
    479  1.18  christos {
    480  1.32      maxv 	if (!pax_aslr_epp_active(epp))
    481  1.31      maxv 		return;
    482  1.31      maxv 
    483  1.31      maxv 	u_long d = PAX_ASLR_DELTA(cprng_fast32(),
    484  1.31      maxv 	    PAX_ASLR_DELTA_STACK_LSB,
    485  1.31      maxv 	    PAX_ASLR_DELTA_STACK_LEN);
    486  1.34  christos 	PAX_DPRINTF("stack %#jx delta=%#lx diff=%lx",
    487  1.34  christos 	    (uintmax_t)epp->ep_minsaddr, d, epp->ep_minsaddr - d);
    488  1.31      maxv 	epp->ep_minsaddr -= d;
    489  1.31      maxv 	*max_stack_size -= d;
    490  1.31      maxv 	if (epp->ep_ssize > *max_stack_size)
    491  1.31      maxv 		epp->ep_ssize = *max_stack_size;
    492  1.18  christos }
    493  1.18  christos #endif /* PAX_ASLR */
    494  1.18  christos 
    495   1.8      elad #ifdef PAX_SEGVGUARD
    496  1.29      maxv static bool
    497  1.29      maxv pax_segvguard_elf_flags_active(uint32_t flags)
    498  1.29      maxv {
    499  1.29      maxv 	if (!pax_segvguard_enabled)
    500  1.29      maxv 		return false;
    501  1.29      maxv 	if (pax_segvguard_global && (flags & ELF_NOTE_PAX_NOGUARD) != 0) {
    502  1.29      maxv 		/* Segvguard explicitly disabled */
    503  1.29      maxv 		return false;
    504  1.29      maxv 	}
    505  1.29      maxv 	if (!pax_segvguard_global && (flags & ELF_NOTE_PAX_GUARD) == 0) {
    506  1.29      maxv 		/* Segvguard not requested */
    507  1.29      maxv 		return false;
    508  1.29      maxv 	}
    509  1.29      maxv 	return true;
    510  1.29      maxv }
    511  1.29      maxv 
    512  1.10      yamt static void
    513  1.31      maxv pax_segvguard_cleanup_cb(void *v)
    514   1.8      elad {
    515  1.25     rmind 	struct pax_segvguard_entry *p = v;
    516   1.8      elad 	struct pax_segvguard_uid_entry *up;
    517   1.8      elad 
    518  1.25     rmind 	if (p == NULL) {
    519   1.8      elad 		return;
    520  1.25     rmind 	}
    521  1.10      yamt 	while ((up = LIST_FIRST(&p->segv_uids)) != NULL) {
    522  1.10      yamt 		LIST_REMOVE(up, sue_list);
    523  1.25     rmind 		kmem_free(up, sizeof(*up));
    524   1.8      elad 	}
    525  1.25     rmind 	kmem_free(p, sizeof(*p));
    526   1.8      elad }
    527   1.8      elad 
    528   1.8      elad /*
    529   1.8      elad  * Called when a process of image vp generated a segfault.
    530   1.8      elad  */
    531   1.8      elad int
    532   1.8      elad pax_segvguard(struct lwp *l, struct vnode *vp, const char *name,
    533  1.14   thorpej     bool crashed)
    534   1.8      elad {
    535   1.8      elad 	struct pax_segvguard_entry *p;
    536   1.8      elad 	struct pax_segvguard_uid_entry *up;
    537   1.8      elad 	struct timeval tv;
    538   1.8      elad 	uid_t uid;
    539  1.29      maxv 	uint32_t flags;
    540  1.14   thorpej 	bool have_uid;
    541   1.8      elad 
    542  1.29      maxv 	flags = l->l_proc->p_pax;
    543  1.31      maxv 	if (!pax_flags_active(flags, P_PAX_GUARD))
    544  1.29      maxv 		return 0;
    545   1.8      elad 
    546   1.9      yamt 	if (vp == NULL)
    547  1.31      maxv 		return EFAULT;
    548   1.8      elad 
    549   1.8      elad 	/* Check if we already monitor the file. */
    550   1.8      elad 	p = fileassoc_lookup(vp, segvguard_id);
    551   1.8      elad 
    552   1.8      elad 	/* Fast-path if starting a program we don't know. */
    553   1.8      elad 	if (p == NULL && !crashed)
    554  1.31      maxv 		return 0;
    555   1.8      elad 
    556   1.8      elad 	microtime(&tv);
    557   1.8      elad 
    558   1.8      elad 	/*
    559   1.8      elad 	 * If a program we don't know crashed, we need to create a new entry
    560   1.8      elad 	 * for it.
    561   1.8      elad 	 */
    562   1.8      elad 	if (p == NULL) {
    563  1.25     rmind 		p = kmem_alloc(sizeof(*p), KM_SLEEP);
    564  1.13      elad 		fileassoc_add(vp, segvguard_id, p);
    565   1.8      elad 		LIST_INIT(&p->segv_uids);
    566   1.8      elad 
    567   1.8      elad 		/*
    568   1.8      elad 		 * Initialize a new entry with "crashes so far" of 1.
    569   1.8      elad 		 * The expiry time is when we purge the entry if it didn't
    570   1.8      elad 		 * reach the limit.
    571   1.8      elad 		 */
    572  1.25     rmind 		up = kmem_alloc(sizeof(*up), KM_SLEEP);
    573   1.8      elad 		up->sue_uid = kauth_cred_getuid(l->l_cred);
    574   1.8      elad 		up->sue_ncrashes = 1;
    575   1.8      elad 		up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
    576   1.8      elad 		up->sue_suspended = 0;
    577   1.8      elad 		LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
    578  1.31      maxv 		return 0;
    579   1.8      elad 	}
    580   1.8      elad 
    581   1.8      elad 	/*
    582   1.8      elad 	 * A program we "know" either executed or crashed again.
    583   1.8      elad 	 * See if it's a culprit we're familiar with.
    584   1.8      elad 	 */
    585   1.8      elad 	uid = kauth_cred_getuid(l->l_cred);
    586  1.15   thorpej 	have_uid = false;
    587   1.8      elad 	LIST_FOREACH(up, &p->segv_uids, sue_list) {
    588   1.8      elad 		if (up->sue_uid == uid) {
    589  1.15   thorpej 			have_uid = true;
    590   1.8      elad 			break;
    591   1.8      elad 		}
    592   1.8      elad 	}
    593   1.8      elad 
    594   1.8      elad 	/*
    595   1.8      elad 	 * It's someone else. Add an entry for him if we crashed.
    596   1.8      elad 	 */
    597   1.8      elad 	if (!have_uid) {
    598   1.8      elad 		if (crashed) {
    599  1.25     rmind 			up = kmem_alloc(sizeof(*up), KM_SLEEP);
    600   1.8      elad 			up->sue_uid = uid;
    601   1.8      elad 			up->sue_ncrashes = 1;
    602   1.8      elad 			up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
    603   1.8      elad 			up->sue_suspended = 0;
    604   1.8      elad 			LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
    605   1.8      elad 		}
    606  1.31      maxv 		return 0;
    607   1.8      elad 	}
    608   1.8      elad 
    609   1.8      elad 	if (crashed) {
    610   1.8      elad 		/* Check if timer on previous crashes expired first. */
    611   1.8      elad 		if (up->sue_expiry < tv.tv_sec) {
    612   1.8      elad 			log(LOG_INFO, "PaX Segvguard: [%s] Suspension"
    613   1.8      elad 			    " expired.\n", name ? name : "unknown");
    614   1.8      elad 			up->sue_ncrashes = 1;
    615   1.8      elad 			up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
    616   1.8      elad 			up->sue_suspended = 0;
    617  1.31      maxv 			return 0;
    618   1.8      elad 		}
    619   1.8      elad 
    620   1.8      elad 		up->sue_ncrashes++;
    621   1.8      elad 
    622   1.8      elad 		if (up->sue_ncrashes >= pax_segvguard_maxcrashes) {
    623   1.8      elad 			log(LOG_ALERT, "PaX Segvguard: [%s] Suspending "
    624   1.8      elad 			    "execution for %d seconds after %zu crashes.\n",
    625   1.8      elad 			    name ? name : "unknown", pax_segvguard_suspension,
    626   1.8      elad 			    up->sue_ncrashes);
    627   1.8      elad 
    628   1.8      elad 			/* Suspend this program for a while. */
    629   1.8      elad 			up->sue_suspended = tv.tv_sec + pax_segvguard_suspension;
    630   1.8      elad 			up->sue_ncrashes = 0;
    631   1.8      elad 			up->sue_expiry = 0;
    632   1.8      elad 		}
    633   1.8      elad 	} else {
    634   1.8      elad 		/* Are we supposed to be suspended? */
    635   1.8      elad 		if (up->sue_suspended > tv.tv_sec) {
    636   1.8      elad 			log(LOG_ALERT, "PaX Segvguard: [%s] Preventing "
    637   1.8      elad 			    "execution due to repeated segfaults.\n", name ?
    638   1.8      elad 			    name : "unknown");
    639  1.31      maxv 			return EPERM;
    640   1.8      elad 		}
    641   1.8      elad 	}
    642   1.8      elad 
    643  1.31      maxv 	return 0;
    644   1.8      elad }
    645   1.8      elad #endif /* PAX_SEGVGUARD */
    646