kern_pax.c revision 1.27.6.1 1 1.27.6.1 skrll /* $NetBSD: kern_pax.c,v 1.27.6.1 2015/06/06 14:40:21 skrll 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.12 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
30 1.17 dsl #include <sys/cdefs.h>
31 1.27.6.1 skrll __KERNEL_RCSID(0, "$NetBSD: kern_pax.c,v 1.27.6.1 2015/06/06 14:40:21 skrll Exp $");
32 1.17 dsl
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.25 rmind #include <sys/kmem.h>
41 1.8 elad #include <sys/fileassoc.h>
42 1.8 elad #include <sys/syslog.h>
43 1.8 elad #include <sys/vnode.h>
44 1.8 elad #include <sys/queue.h>
45 1.8 elad #include <sys/kauth.h>
46 1.26 tls #include <sys/cprng.h>
47 1.1 elad
48 1.18 christos #ifdef PAX_ASLR
49 1.18 christos #include <sys/mman.h>
50 1.18 christos #include <sys/exec.h>
51 1.18 christos
52 1.18 christos int pax_aslr_enabled = 1;
53 1.18 christos int pax_aslr_global = PAX_ASLR;
54 1.18 christos
55 1.18 christos #ifndef PAX_ASLR_DELTA_MMAP_LSB
56 1.18 christos #define PAX_ASLR_DELTA_MMAP_LSB PGSHIFT
57 1.18 christos #endif
58 1.18 christos #ifndef PAX_ASLR_DELTA_MMAP_LEN
59 1.18 christos #define PAX_ASLR_DELTA_MMAP_LEN ((sizeof(void *) * NBBY) / 2)
60 1.18 christos #endif
61 1.18 christos #ifndef PAX_ASLR_DELTA_STACK_LSB
62 1.18 christos #define PAX_ASLR_DELTA_STACK_LSB PGSHIFT
63 1.18 christos #endif
64 1.18 christos #ifndef PAX_ASLR_DELTA_STACK_LEN
65 1.18 christos #define PAX_ASLR_DELTA_STACK_LEN 12
66 1.18 christos #endif
67 1.18 christos
68 1.18 christos #endif /* PAX_ASLR */
69 1.18 christos
70 1.7 elad #ifdef PAX_MPROTECT
71 1.6 yamt static int pax_mprotect_enabled = 1;
72 1.6 yamt static int pax_mprotect_global = PAX_MPROTECT;
73 1.18 christos #endif /* PAX_MPROTECT */
74 1.8 elad
75 1.8 elad #ifdef PAX_SEGVGUARD
76 1.8 elad #ifndef PAX_SEGVGUARD_EXPIRY
77 1.8 elad #define PAX_SEGVGUARD_EXPIRY (2 * 60)
78 1.8 elad #endif
79 1.8 elad
80 1.8 elad #ifndef PAX_SEGVGUARD_SUSPENSION
81 1.8 elad #define PAX_SEGVGUARD_SUSPENSION (10 * 60)
82 1.8 elad #endif
83 1.8 elad
84 1.8 elad #ifndef PAX_SEGVGUARD_MAXCRASHES
85 1.8 elad #define PAX_SEGVGUARD_MAXCRASHES 5
86 1.8 elad #endif
87 1.8 elad
88 1.8 elad static int pax_segvguard_enabled = 1;
89 1.8 elad static int pax_segvguard_global = PAX_SEGVGUARD;
90 1.8 elad static int pax_segvguard_expiry = PAX_SEGVGUARD_EXPIRY;
91 1.8 elad static int pax_segvguard_suspension = PAX_SEGVGUARD_SUSPENSION;
92 1.8 elad static int pax_segvguard_maxcrashes = PAX_SEGVGUARD_MAXCRASHES;
93 1.8 elad
94 1.8 elad static fileassoc_t segvguard_id;
95 1.8 elad
96 1.8 elad struct pax_segvguard_uid_entry {
97 1.8 elad uid_t sue_uid;
98 1.8 elad size_t sue_ncrashes;
99 1.8 elad time_t sue_expiry;
100 1.8 elad time_t sue_suspended;
101 1.8 elad LIST_ENTRY(pax_segvguard_uid_entry) sue_list;
102 1.8 elad };
103 1.8 elad
104 1.8 elad struct pax_segvguard_entry {
105 1.8 elad LIST_HEAD(, pax_segvguard_uid_entry) segv_uids;
106 1.8 elad };
107 1.10 yamt
108 1.10 yamt static void pax_segvguard_cb(void *);
109 1.8 elad #endif /* PAX_SEGVGUARD */
110 1.7 elad
111 1.7 elad /* PaX internal setspecific flags */
112 1.7 elad #define PAX_MPROTECT_EXPLICIT_ENABLE (void *)0x01
113 1.7 elad #define PAX_MPROTECT_EXPLICIT_DISABLE (void *)0x02
114 1.8 elad #define PAX_SEGVGUARD_EXPLICIT_ENABLE (void *)0x03
115 1.8 elad #define PAX_SEGVGUARD_EXPLICIT_DISABLE (void *)0x04
116 1.18 christos #define PAX_ASLR_EXPLICIT_ENABLE (void *)0x05
117 1.18 christos #define PAX_ASLR_EXPLICIT_DISABLE (void *)0x06
118 1.7 elad
119 1.1 elad SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
120 1.1 elad {
121 1.8 elad const struct sysctlnode *rnode = NULL, *cnode;
122 1.1 elad
123 1.27 pooka sysctl_createv(clog, 0, NULL, &rnode,
124 1.1 elad CTLFLAG_PERMANENT,
125 1.1 elad CTLTYPE_NODE, "pax",
126 1.1 elad SYSCTL_DESCR("PaX (exploit mitigation) features."),
127 1.1 elad NULL, 0, NULL, 0,
128 1.27 pooka CTL_SECURITY, CTL_CREATE, CTL_EOL);
129 1.1 elad
130 1.19 elad cnode = rnode;
131 1.19 elad
132 1.18 christos #ifdef PAX_MPROTECT
133 1.19 elad rnode = cnode;
134 1.1 elad sysctl_createv(clog, 0, &rnode, &rnode,
135 1.1 elad CTLFLAG_PERMANENT,
136 1.1 elad CTLTYPE_NODE, "mprotect",
137 1.1 elad SYSCTL_DESCR("mprotect(2) W^X restrictions."),
138 1.1 elad NULL, 0, NULL, 0,
139 1.1 elad CTL_CREATE, CTL_EOL);
140 1.1 elad sysctl_createv(clog, 0, &rnode, NULL,
141 1.2 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
142 1.1 elad CTLTYPE_INT, "enabled",
143 1.1 elad SYSCTL_DESCR("Restrictions enabled."),
144 1.2 elad NULL, 0, &pax_mprotect_enabled, 0,
145 1.1 elad CTL_CREATE, CTL_EOL);
146 1.1 elad sysctl_createv(clog, 0, &rnode, NULL,
147 1.2 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
148 1.4 elad CTLTYPE_INT, "global",
149 1.1 elad SYSCTL_DESCR("When enabled, unless explicitly "
150 1.5 cbiere "specified, apply restrictions to "
151 1.1 elad "all processes."),
152 1.2 elad NULL, 0, &pax_mprotect_global, 0,
153 1.1 elad CTL_CREATE, CTL_EOL);
154 1.7 elad #endif /* PAX_MPROTECT */
155 1.8 elad
156 1.18 christos #ifdef PAX_SEGVGUARD
157 1.8 elad rnode = cnode;
158 1.8 elad sysctl_createv(clog, 0, &rnode, &rnode,
159 1.8 elad CTLFLAG_PERMANENT,
160 1.8 elad CTLTYPE_NODE, "segvguard",
161 1.8 elad SYSCTL_DESCR("PaX segvguard."),
162 1.8 elad NULL, 0, NULL, 0,
163 1.8 elad CTL_CREATE, CTL_EOL);
164 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
165 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
166 1.8 elad CTLTYPE_INT, "enabled",
167 1.8 elad SYSCTL_DESCR("segvguard enabled."),
168 1.8 elad NULL, 0, &pax_segvguard_enabled, 0,
169 1.8 elad CTL_CREATE, CTL_EOL);
170 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
171 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
172 1.8 elad CTLTYPE_INT, "global",
173 1.8 elad SYSCTL_DESCR("segvguard all programs."),
174 1.8 elad NULL, 0, &pax_segvguard_global, 0,
175 1.8 elad CTL_CREATE, CTL_EOL);
176 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
177 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
178 1.8 elad CTLTYPE_INT, "expiry_timeout",
179 1.8 elad SYSCTL_DESCR("Entry expiry timeout (in seconds)."),
180 1.8 elad NULL, 0, &pax_segvguard_expiry, 0,
181 1.8 elad CTL_CREATE, CTL_EOL);
182 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
183 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
184 1.8 elad CTLTYPE_INT, "suspend_timeout",
185 1.8 elad SYSCTL_DESCR("Entry suspension timeout (in seconds)."),
186 1.8 elad NULL, 0, &pax_segvguard_suspension, 0,
187 1.8 elad CTL_CREATE, CTL_EOL);
188 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
189 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
190 1.8 elad CTLTYPE_INT, "max_crashes",
191 1.8 elad SYSCTL_DESCR("Max number of crashes before expiry."),
192 1.8 elad NULL, 0, &pax_segvguard_maxcrashes, 0,
193 1.8 elad CTL_CREATE, CTL_EOL);
194 1.8 elad #endif /* PAX_SEGVGUARD */
195 1.18 christos
196 1.18 christos #ifdef PAX_ASLR
197 1.18 christos rnode = cnode;
198 1.18 christos sysctl_createv(clog, 0, &rnode, &rnode,
199 1.18 christos CTLFLAG_PERMANENT,
200 1.18 christos CTLTYPE_NODE, "aslr",
201 1.18 christos SYSCTL_DESCR("Address Space Layout Randomization."),
202 1.18 christos NULL, 0, NULL, 0,
203 1.18 christos CTL_CREATE, CTL_EOL);
204 1.18 christos sysctl_createv(clog, 0, &rnode, NULL,
205 1.18 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
206 1.18 christos CTLTYPE_INT, "enabled",
207 1.18 christos SYSCTL_DESCR("Restrictions enabled."),
208 1.18 christos NULL, 0, &pax_aslr_enabled, 0,
209 1.18 christos CTL_CREATE, CTL_EOL);
210 1.18 christos sysctl_createv(clog, 0, &rnode, NULL,
211 1.18 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
212 1.18 christos CTLTYPE_INT, "global",
213 1.18 christos SYSCTL_DESCR("When enabled, unless explicitly "
214 1.18 christos "specified, apply to all processes."),
215 1.18 christos NULL, 0, &pax_aslr_global, 0,
216 1.18 christos CTL_CREATE, CTL_EOL);
217 1.18 christos sysctl_createv(clog, 0, &rnode, NULL,
218 1.18 christos CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
219 1.18 christos CTLTYPE_INT, "mmap_len",
220 1.18 christos SYSCTL_DESCR("Number of bits randomized for "
221 1.18 christos "mmap(2) calls."),
222 1.18 christos NULL, PAX_ASLR_DELTA_MMAP_LEN, NULL, 0,
223 1.18 christos CTL_CREATE, CTL_EOL);
224 1.19 elad sysctl_createv(clog, 0, &rnode, NULL,
225 1.19 elad CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
226 1.19 elad CTLTYPE_INT, "stack_len",
227 1.19 elad SYSCTL_DESCR("Number of bits randomized for "
228 1.19 elad "the stack."),
229 1.19 elad NULL, PAX_ASLR_DELTA_STACK_LEN, NULL, 0,
230 1.19 elad CTL_CREATE, CTL_EOL);
231 1.19 elad sysctl_createv(clog, 0, &rnode, NULL,
232 1.19 elad CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
233 1.19 elad CTLTYPE_INT, "exec_len",
234 1.19 elad SYSCTL_DESCR("Number of bits randomized for "
235 1.19 elad "the PIE exec base."),
236 1.19 elad NULL, PAX_ASLR_DELTA_EXEC_LEN, NULL, 0,
237 1.19 elad CTL_CREATE, CTL_EOL);
238 1.19 elad
239 1.18 christos #endif /* PAX_ASLR */
240 1.1 elad }
241 1.1 elad
242 1.7 elad /*
243 1.7 elad * Initialize PaX.
244 1.7 elad */
245 1.1 elad void
246 1.7 elad pax_init(void)
247 1.1 elad {
248 1.9 yamt #ifdef PAX_SEGVGUARD
249 1.9 yamt int error;
250 1.9 yamt #endif /* PAX_SEGVGUARD */
251 1.9 yamt
252 1.8 elad #ifdef PAX_SEGVGUARD
253 1.9 yamt error = fileassoc_register("segvguard", pax_segvguard_cb,
254 1.9 yamt &segvguard_id);
255 1.9 yamt if (error) {
256 1.9 yamt panic("pax_init: segvguard_id: error=%d\n", error);
257 1.9 yamt }
258 1.8 elad #endif /* PAX_SEGVGUARD */
259 1.1 elad }
260 1.1 elad
261 1.7 elad #ifdef PAX_MPROTECT
262 1.1 elad void
263 1.3 elad pax_mprotect(struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
264 1.1 elad {
265 1.21 ad uint32_t f;
266 1.7 elad
267 1.7 elad if (!pax_mprotect_enabled)
268 1.7 elad return;
269 1.7 elad
270 1.21 ad f = l->l_proc->p_pax;
271 1.22 ad if ((pax_mprotect_global && (f & ELF_NOTE_PAX_NOMPROTECT) != 0) ||
272 1.22 ad (!pax_mprotect_global && (f & ELF_NOTE_PAX_MPROTECT) == 0))
273 1.1 elad return;
274 1.1 elad
275 1.3 elad if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
276 1.3 elad *prot &= ~VM_PROT_EXECUTE;
277 1.3 elad *maxprot &= ~VM_PROT_EXECUTE;
278 1.1 elad } else {
279 1.3 elad *prot &= ~VM_PROT_WRITE;
280 1.3 elad *maxprot &= ~VM_PROT_WRITE;
281 1.1 elad }
282 1.1 elad }
283 1.7 elad #endif /* PAX_MPROTECT */
284 1.8 elad
285 1.18 christos #ifdef PAX_ASLR
286 1.18 christos bool
287 1.18 christos pax_aslr_active(struct lwp *l)
288 1.18 christos {
289 1.21 ad uint32_t f;
290 1.21 ad
291 1.18 christos if (!pax_aslr_enabled)
292 1.18 christos return false;
293 1.18 christos
294 1.21 ad f = l->l_proc->p_pax;
295 1.22 ad if ((pax_aslr_global && (f & ELF_NOTE_PAX_NOASLR) != 0) ||
296 1.22 ad (!pax_aslr_global && (f & ELF_NOTE_PAX_ASLR) == 0))
297 1.18 christos return false;
298 1.18 christos return true;
299 1.18 christos }
300 1.18 christos
301 1.18 christos void
302 1.18 christos pax_aslr_init(struct lwp *l, struct vmspace *vm)
303 1.18 christos {
304 1.18 christos if (!pax_aslr_active(l))
305 1.18 christos return;
306 1.18 christos
307 1.26 tls vm->vm_aslr_delta_mmap = PAX_ASLR_DELTA(cprng_fast32(),
308 1.18 christos PAX_ASLR_DELTA_MMAP_LSB, PAX_ASLR_DELTA_MMAP_LEN);
309 1.18 christos }
310 1.18 christos
311 1.18 christos void
312 1.18 christos pax_aslr(struct lwp *l, vaddr_t *addr, vaddr_t orig_addr, int f)
313 1.18 christos {
314 1.18 christos if (!pax_aslr_active(l))
315 1.18 christos return;
316 1.18 christos
317 1.18 christos if (!(f & MAP_FIXED) && ((orig_addr == 0) || !(f & MAP_ANON))) {
318 1.23 christos #ifdef PAX_ASLR_DEBUG
319 1.18 christos uprintf("applying to 0x%lx orig_addr=0x%lx f=%x\n",
320 1.18 christos (unsigned long)*addr, (unsigned long)orig_addr, f);
321 1.18 christos #endif
322 1.18 christos if (!(l->l_proc->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
323 1.18 christos *addr += l->l_proc->p_vmspace->vm_aslr_delta_mmap;
324 1.18 christos else
325 1.18 christos *addr -= l->l_proc->p_vmspace->vm_aslr_delta_mmap;
326 1.23 christos #ifdef PAX_ASLR_DEBUG
327 1.18 christos uprintf("result 0x%lx\n", *addr);
328 1.18 christos #endif
329 1.18 christos }
330 1.23 christos #ifdef PAX_ASLR_DEBUG
331 1.18 christos else
332 1.18 christos uprintf("not applying to 0x%lx orig_addr=0x%lx f=%x\n",
333 1.18 christos (unsigned long)*addr, (unsigned long)orig_addr, f);
334 1.18 christos #endif
335 1.18 christos }
336 1.18 christos
337 1.18 christos void
338 1.18 christos pax_aslr_stack(struct lwp *l, struct exec_package *epp, u_long *max_stack_size)
339 1.18 christos {
340 1.18 christos if (pax_aslr_active(l)) {
341 1.26 tls u_long d = PAX_ASLR_DELTA(cprng_fast32(),
342 1.18 christos PAX_ASLR_DELTA_STACK_LSB,
343 1.18 christos PAX_ASLR_DELTA_STACK_LEN);
344 1.23 christos #ifdef PAX_ASLR_DEBUG
345 1.18 christos uprintf("stack 0x%lx d=0x%lx 0x%lx\n",
346 1.18 christos epp->ep_minsaddr, d, epp->ep_minsaddr - d);
347 1.18 christos #endif
348 1.18 christos epp->ep_minsaddr -= d;
349 1.18 christos *max_stack_size -= d;
350 1.24 christos if (epp->ep_ssize > *max_stack_size)
351 1.24 christos epp->ep_ssize = *max_stack_size;
352 1.18 christos }
353 1.18 christos }
354 1.18 christos #endif /* PAX_ASLR */
355 1.18 christos
356 1.8 elad #ifdef PAX_SEGVGUARD
357 1.10 yamt static void
358 1.10 yamt pax_segvguard_cb(void *v)
359 1.8 elad {
360 1.25 rmind struct pax_segvguard_entry *p = v;
361 1.8 elad struct pax_segvguard_uid_entry *up;
362 1.8 elad
363 1.25 rmind if (p == NULL) {
364 1.8 elad return;
365 1.25 rmind }
366 1.10 yamt while ((up = LIST_FIRST(&p->segv_uids)) != NULL) {
367 1.10 yamt LIST_REMOVE(up, sue_list);
368 1.25 rmind kmem_free(up, sizeof(*up));
369 1.8 elad }
370 1.25 rmind kmem_free(p, sizeof(*p));
371 1.8 elad }
372 1.8 elad
373 1.8 elad /*
374 1.8 elad * Called when a process of image vp generated a segfault.
375 1.8 elad */
376 1.8 elad int
377 1.8 elad pax_segvguard(struct lwp *l, struct vnode *vp, const char *name,
378 1.14 thorpej bool crashed)
379 1.8 elad {
380 1.8 elad struct pax_segvguard_entry *p;
381 1.8 elad struct pax_segvguard_uid_entry *up;
382 1.8 elad struct timeval tv;
383 1.8 elad uid_t uid;
384 1.21 ad uint32_t f;
385 1.14 thorpej bool have_uid;
386 1.8 elad
387 1.8 elad if (!pax_segvguard_enabled)
388 1.8 elad return (0);
389 1.8 elad
390 1.21 ad f = l->l_proc->p_pax;
391 1.22 ad if ((pax_segvguard_global && (f & ELF_NOTE_PAX_NOGUARD) != 0) ||
392 1.22 ad (!pax_segvguard_global && (f & ELF_NOTE_PAX_GUARD) == 0))
393 1.8 elad return (0);
394 1.8 elad
395 1.9 yamt if (vp == NULL)
396 1.8 elad return (EFAULT);
397 1.8 elad
398 1.8 elad /* Check if we already monitor the file. */
399 1.8 elad p = fileassoc_lookup(vp, segvguard_id);
400 1.8 elad
401 1.8 elad /* Fast-path if starting a program we don't know. */
402 1.8 elad if (p == NULL && !crashed)
403 1.8 elad return (0);
404 1.8 elad
405 1.8 elad microtime(&tv);
406 1.8 elad
407 1.8 elad /*
408 1.8 elad * If a program we don't know crashed, we need to create a new entry
409 1.8 elad * for it.
410 1.8 elad */
411 1.8 elad if (p == NULL) {
412 1.25 rmind p = kmem_alloc(sizeof(*p), KM_SLEEP);
413 1.13 elad fileassoc_add(vp, segvguard_id, p);
414 1.8 elad LIST_INIT(&p->segv_uids);
415 1.8 elad
416 1.8 elad /*
417 1.8 elad * Initialize a new entry with "crashes so far" of 1.
418 1.8 elad * The expiry time is when we purge the entry if it didn't
419 1.8 elad * reach the limit.
420 1.8 elad */
421 1.25 rmind up = kmem_alloc(sizeof(*up), KM_SLEEP);
422 1.8 elad up->sue_uid = kauth_cred_getuid(l->l_cred);
423 1.8 elad up->sue_ncrashes = 1;
424 1.8 elad up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
425 1.8 elad up->sue_suspended = 0;
426 1.8 elad
427 1.8 elad LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
428 1.8 elad
429 1.8 elad return (0);
430 1.8 elad }
431 1.8 elad
432 1.8 elad /*
433 1.8 elad * A program we "know" either executed or crashed again.
434 1.8 elad * See if it's a culprit we're familiar with.
435 1.8 elad */
436 1.8 elad uid = kauth_cred_getuid(l->l_cred);
437 1.15 thorpej have_uid = false;
438 1.8 elad LIST_FOREACH(up, &p->segv_uids, sue_list) {
439 1.8 elad if (up->sue_uid == uid) {
440 1.15 thorpej have_uid = true;
441 1.8 elad break;
442 1.8 elad }
443 1.8 elad }
444 1.8 elad
445 1.8 elad /*
446 1.8 elad * It's someone else. Add an entry for him if we crashed.
447 1.8 elad */
448 1.8 elad if (!have_uid) {
449 1.8 elad if (crashed) {
450 1.25 rmind up = kmem_alloc(sizeof(*up), KM_SLEEP);
451 1.8 elad up->sue_uid = uid;
452 1.8 elad up->sue_ncrashes = 1;
453 1.8 elad up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
454 1.8 elad up->sue_suspended = 0;
455 1.8 elad
456 1.8 elad LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
457 1.8 elad }
458 1.8 elad return (0);
459 1.8 elad }
460 1.8 elad
461 1.8 elad if (crashed) {
462 1.8 elad /* Check if timer on previous crashes expired first. */
463 1.8 elad if (up->sue_expiry < tv.tv_sec) {
464 1.8 elad log(LOG_INFO, "PaX Segvguard: [%s] Suspension"
465 1.8 elad " expired.\n", name ? name : "unknown");
466 1.8 elad
467 1.8 elad up->sue_ncrashes = 1;
468 1.8 elad up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
469 1.8 elad up->sue_suspended = 0;
470 1.8 elad
471 1.8 elad return (0);
472 1.8 elad }
473 1.8 elad
474 1.8 elad up->sue_ncrashes++;
475 1.8 elad
476 1.8 elad if (up->sue_ncrashes >= pax_segvguard_maxcrashes) {
477 1.8 elad log(LOG_ALERT, "PaX Segvguard: [%s] Suspending "
478 1.8 elad "execution for %d seconds after %zu crashes.\n",
479 1.8 elad name ? name : "unknown", pax_segvguard_suspension,
480 1.8 elad up->sue_ncrashes);
481 1.8 elad
482 1.8 elad /* Suspend this program for a while. */
483 1.8 elad up->sue_suspended = tv.tv_sec + pax_segvguard_suspension;
484 1.8 elad up->sue_ncrashes = 0;
485 1.8 elad up->sue_expiry = 0;
486 1.8 elad }
487 1.8 elad } else {
488 1.8 elad /* Are we supposed to be suspended? */
489 1.8 elad if (up->sue_suspended > tv.tv_sec) {
490 1.8 elad log(LOG_ALERT, "PaX Segvguard: [%s] Preventing "
491 1.8 elad "execution due to repeated segfaults.\n", name ?
492 1.8 elad name : "unknown");
493 1.8 elad
494 1.8 elad return (EPERM);
495 1.8 elad }
496 1.8 elad }
497 1.8 elad
498 1.8 elad return (0);
499 1.8 elad }
500 1.8 elad #endif /* PAX_SEGVGUARD */
501