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