Home | History | Annotate | Line # | Download | only in kern
kern_pax.c revision 1.7
      1  1.7    elad /* $NetBSD: kern_pax.c,v 1.7 2006/11/22 00:41:38 elad 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. All advertising materials mentioning features or use of this software
     16  1.1    elad  *    must display the following acknowledgement:
     17  1.1    elad  *      This product includes software developed by Elad Efrat.
     18  1.1    elad  * 4. The name of the author may not be used to endorse or promote products
     19  1.1    elad  *    derived from this software without specific prior written permission.
     20  1.1    elad  *
     21  1.1    elad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1    elad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1    elad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1    elad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1    elad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1    elad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1    elad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1    elad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1    elad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1    elad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1    elad  */
     32  1.1    elad 
     33  1.1    elad #include "opt_pax.h"
     34  1.1    elad 
     35  1.1    elad #include <sys/param.h>
     36  1.1    elad #include <sys/proc.h>
     37  1.1    elad #include <sys/exec_elf.h>
     38  1.1    elad #include <sys/pax.h>
     39  1.1    elad #include <sys/sysctl.h>
     40  1.1    elad 
     41  1.7    elad #ifdef PAX_MPROTECT
     42  1.6    yamt static int pax_mprotect_enabled = 1;
     43  1.6    yamt static int pax_mprotect_global = PAX_MPROTECT;
     44  1.1    elad 
     45  1.7    elad specificdata_key_t pax_mprotect_key;
     46  1.7    elad #endif /* PAX_MPROTECT */
     47  1.7    elad 
     48  1.7    elad /* PaX internal setspecific flags */
     49  1.7    elad #define	PAX_MPROTECT_EXPLICIT_ENABLE	(void *)0x01
     50  1.7    elad #define	PAX_MPROTECT_EXPLICIT_DISABLE	(void *)0x02
     51  1.7    elad 
     52  1.1    elad SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
     53  1.1    elad {
     54  1.1    elad 	const struct sysctlnode *rnode = NULL;
     55  1.1    elad 
     56  1.1    elad         sysctl_createv(clog, 0, NULL, &rnode,
     57  1.1    elad                        CTLFLAG_PERMANENT,
     58  1.1    elad                        CTLTYPE_NODE, "security", NULL,
     59  1.1    elad                        NULL, 0, NULL, 0,
     60  1.1    elad                        CTL_CREATE, CTL_EOL);
     61  1.1    elad 
     62  1.1    elad 	sysctl_createv(clog, 0, &rnode, &rnode,
     63  1.1    elad 		       CTLFLAG_PERMANENT,
     64  1.1    elad 		       CTLTYPE_NODE, "pax",
     65  1.1    elad 		       SYSCTL_DESCR("PaX (exploit mitigation) features."),
     66  1.1    elad 		       NULL, 0, NULL, 0,
     67  1.1    elad 		       CTL_CREATE, CTL_EOL);
     68  1.1    elad 
     69  1.7    elad #ifdef PAX_MPROTECT
     70  1.1    elad 	sysctl_createv(clog, 0, &rnode, &rnode,
     71  1.1    elad 		       CTLFLAG_PERMANENT,
     72  1.1    elad 		       CTLTYPE_NODE, "mprotect",
     73  1.1    elad 		       SYSCTL_DESCR("mprotect(2) W^X restrictions."),
     74  1.1    elad 		       NULL, 0, NULL, 0,
     75  1.1    elad 		       CTL_CREATE, CTL_EOL);
     76  1.1    elad 	sysctl_createv(clog, 0, &rnode, NULL,
     77  1.2    elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
     78  1.1    elad 		       CTLTYPE_INT, "enabled",
     79  1.1    elad 		       SYSCTL_DESCR("Restrictions enabled."),
     80  1.2    elad 		       NULL, 0, &pax_mprotect_enabled, 0,
     81  1.1    elad 		       CTL_CREATE, CTL_EOL);
     82  1.1    elad 	sysctl_createv(clog, 0, &rnode, NULL,
     83  1.2    elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
     84  1.4    elad 		       CTLTYPE_INT, "global",
     85  1.1    elad 		       SYSCTL_DESCR("When enabled, unless explicitly "
     86  1.5  cbiere 				    "specified, apply restrictions to "
     87  1.1    elad 				    "all processes."),
     88  1.2    elad 		       NULL, 0, &pax_mprotect_global, 0,
     89  1.1    elad 		       CTL_CREATE, CTL_EOL);
     90  1.7    elad #endif /* PAX_MPROTECT */
     91  1.1    elad }
     92  1.1    elad 
     93  1.7    elad /*
     94  1.7    elad  * Initialize PaX.
     95  1.7    elad  */
     96  1.1    elad void
     97  1.7    elad pax_init(void)
     98  1.1    elad {
     99  1.7    elad #ifdef PAX_MPROTECT
    100  1.7    elad 	proc_specific_key_create(&pax_mprotect_key, NULL);
    101  1.7    elad #endif /* PAX_MPROTECT */
    102  1.7    elad }
    103  1.1    elad 
    104  1.7    elad void
    105  1.7    elad pax_adjust(struct lwp *l, int f)
    106  1.7    elad {
    107  1.7    elad #ifdef PAX_MPROTECT
    108  1.7    elad 	if (pax_mprotect_enabled) {
    109  1.7    elad 		if (f & PF_PAXMPROTECT)
    110  1.7    elad 			proc_setspecific(l->l_proc, pax_mprotect_key,
    111  1.7    elad 			    PAX_MPROTECT_EXPLICIT_ENABLE);
    112  1.7    elad 		if (f & PF_PAXNOMPROTECT)
    113  1.7    elad 			proc_setspecific(l->l_proc, pax_mprotect_key,
    114  1.7    elad 			    PAX_MPROTECT_EXPLICIT_DISABLE);
    115  1.7    elad 	}
    116  1.7    elad #endif /* PAX_MPROTECT */
    117  1.1    elad }
    118  1.1    elad 
    119  1.7    elad #ifdef PAX_MPROTECT
    120  1.1    elad void
    121  1.3    elad pax_mprotect(struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
    122  1.1    elad {
    123  1.7    elad 	void *t;
    124  1.7    elad 
    125  1.7    elad 	if (!pax_mprotect_enabled)
    126  1.7    elad 		return;
    127  1.7    elad 
    128  1.7    elad 	t = proc_getspecific(l->l_proc, pax_mprotect_key);
    129  1.7    elad 	if ((pax_mprotect_global && t == PAX_MPROTECT_EXPLICIT_DISABLE) ||
    130  1.7    elad 	    (!pax_mprotect_global && t != PAX_MPROTECT_EXPLICIT_ENABLE))
    131  1.1    elad 		return;
    132  1.1    elad 
    133  1.3    elad 	if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
    134  1.3    elad 		*prot &= ~VM_PROT_EXECUTE;
    135  1.3    elad 		*maxprot &= ~VM_PROT_EXECUTE;
    136  1.1    elad 	} else {
    137  1.3    elad 		*prot &= ~VM_PROT_WRITE;
    138  1.3    elad 		*maxprot &= ~VM_PROT_WRITE;
    139  1.1    elad 	}
    140  1.1    elad }
    141  1.7    elad #endif /* PAX_MPROTECT */
    142