kern_pax.c revision 1.6 1 1.6 yamt /* $NetBSD: kern_pax.c,v 1.6 2006/11/01 09:36:28 yamt 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.6 yamt static int pax_mprotect_enabled = 1;
42 1.6 yamt static int pax_mprotect_global = PAX_MPROTECT;
43 1.1 elad
44 1.1 elad SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
45 1.1 elad {
46 1.1 elad const struct sysctlnode *rnode = NULL;
47 1.1 elad
48 1.1 elad sysctl_createv(clog, 0, NULL, &rnode,
49 1.1 elad CTLFLAG_PERMANENT,
50 1.1 elad CTLTYPE_NODE, "security", NULL,
51 1.1 elad NULL, 0, NULL, 0,
52 1.1 elad CTL_CREATE, CTL_EOL);
53 1.1 elad
54 1.1 elad sysctl_createv(clog, 0, &rnode, &rnode,
55 1.1 elad CTLFLAG_PERMANENT,
56 1.1 elad CTLTYPE_NODE, "pax",
57 1.1 elad SYSCTL_DESCR("PaX (exploit mitigation) features."),
58 1.1 elad NULL, 0, NULL, 0,
59 1.1 elad CTL_CREATE, CTL_EOL);
60 1.1 elad
61 1.1 elad sysctl_createv(clog, 0, &rnode, &rnode,
62 1.1 elad CTLFLAG_PERMANENT,
63 1.1 elad CTLTYPE_NODE, "mprotect",
64 1.1 elad SYSCTL_DESCR("mprotect(2) W^X restrictions."),
65 1.1 elad NULL, 0, NULL, 0,
66 1.1 elad CTL_CREATE, CTL_EOL);
67 1.1 elad sysctl_createv(clog, 0, &rnode, NULL,
68 1.2 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
69 1.1 elad CTLTYPE_INT, "enabled",
70 1.1 elad SYSCTL_DESCR("Restrictions enabled."),
71 1.2 elad NULL, 0, &pax_mprotect_enabled, 0,
72 1.1 elad CTL_CREATE, CTL_EOL);
73 1.1 elad sysctl_createv(clog, 0, &rnode, NULL,
74 1.2 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
75 1.4 elad CTLTYPE_INT, "global",
76 1.1 elad SYSCTL_DESCR("When enabled, unless explicitly "
77 1.5 cbiere "specified, apply restrictions to "
78 1.1 elad "all processes."),
79 1.2 elad NULL, 0, &pax_mprotect_global, 0,
80 1.1 elad CTL_CREATE, CTL_EOL);
81 1.1 elad }
82 1.1 elad
83 1.1 elad void
84 1.1 elad pax_mprotect_adjust(struct lwp *l, int f)
85 1.1 elad {
86 1.3 elad if (!pax_mprotect_enabled)
87 1.1 elad return;
88 1.1 elad
89 1.1 elad if (f & PF_PAXMPROTECT)
90 1.1 elad l->l_proc->p_flag |= P_PAXMPROTECT;
91 1.1 elad if (f & PF_PAXNOMPROTECT)
92 1.1 elad l->l_proc->p_flag |= P_PAXNOMPROTECT;
93 1.1 elad }
94 1.1 elad
95 1.1 elad void
96 1.3 elad pax_mprotect(struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
97 1.1 elad {
98 1.1 elad if (!pax_mprotect_enabled ||
99 1.1 elad (pax_mprotect_global && (l->l_proc->p_flag & P_PAXNOMPROTECT)) ||
100 1.1 elad (!pax_mprotect_global && !(l->l_proc->p_flag & P_PAXMPROTECT)))
101 1.1 elad return;
102 1.1 elad
103 1.3 elad if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
104 1.3 elad *prot &= ~VM_PROT_EXECUTE;
105 1.3 elad *maxprot &= ~VM_PROT_EXECUTE;
106 1.1 elad } else {
107 1.3 elad *prot &= ~VM_PROT_WRITE;
108 1.3 elad *maxprot &= ~VM_PROT_WRITE;
109 1.1 elad }
110 1.1 elad }
111