kern_pax.c revision 1.3.10.3 1 1.3.10.3 yamt /* $NetBSD: kern_pax.c,v 1.3.10.3 2006/12/30 20:50:05 yamt Exp $ */
2 1.3.10.2 yamt
3 1.3.10.2 yamt /*-
4 1.3.10.2 yamt * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
5 1.3.10.2 yamt * All rights reserved.
6 1.3.10.2 yamt *
7 1.3.10.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.3.10.2 yamt * modification, are permitted provided that the following conditions
9 1.3.10.2 yamt * are met:
10 1.3.10.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.3.10.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.3.10.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.10.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.3.10.2 yamt * documentation and/or other materials provided with the distribution.
15 1.3.10.2 yamt * 3. All advertising materials mentioning features or use of this software
16 1.3.10.2 yamt * must display the following acknowledgement:
17 1.3.10.2 yamt * This product includes software developed by Elad Efrat.
18 1.3.10.2 yamt * 4. The name of the author may not be used to endorse or promote products
19 1.3.10.2 yamt * derived from this software without specific prior written permission.
20 1.3.10.2 yamt *
21 1.3.10.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.3.10.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.3.10.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.3.10.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.3.10.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.3.10.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.3.10.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.3.10.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.3.10.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.3.10.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.3.10.2 yamt */
32 1.3.10.2 yamt
33 1.3.10.2 yamt #include "opt_pax.h"
34 1.3.10.2 yamt
35 1.3.10.2 yamt #include <sys/param.h>
36 1.3.10.2 yamt #include <sys/proc.h>
37 1.3.10.2 yamt #include <sys/exec_elf.h>
38 1.3.10.2 yamt #include <sys/pax.h>
39 1.3.10.2 yamt #include <sys/sysctl.h>
40 1.3.10.3 yamt #include <sys/malloc.h>
41 1.3.10.3 yamt #include <sys/fileassoc.h>
42 1.3.10.3 yamt #include <sys/syslog.h>
43 1.3.10.3 yamt #include <sys/vnode.h>
44 1.3.10.3 yamt #include <sys/queue.h>
45 1.3.10.3 yamt #include <sys/kauth.h>
46 1.3.10.3 yamt
47 1.3.10.3 yamt #ifdef PAX_MPROTECT
48 1.3.10.3 yamt static int pax_mprotect_enabled = 1;
49 1.3.10.3 yamt static int pax_mprotect_global = PAX_MPROTECT;
50 1.3.10.3 yamt
51 1.3.10.3 yamt specificdata_key_t pax_mprotect_key;
52 1.3.10.3 yamt #endif
53 1.3.10.3 yamt
54 1.3.10.3 yamt #ifdef PAX_SEGVGUARD
55 1.3.10.3 yamt #ifndef PAX_SEGVGUARD_EXPIRY
56 1.3.10.3 yamt #define PAX_SEGVGUARD_EXPIRY (2 * 60)
57 1.3.10.3 yamt #endif
58 1.3.10.3 yamt
59 1.3.10.3 yamt #ifndef PAX_SEGVGUARD_SUSPENSION
60 1.3.10.3 yamt #define PAX_SEGVGUARD_SUSPENSION (10 * 60)
61 1.3.10.3 yamt #endif
62 1.3.10.3 yamt
63 1.3.10.3 yamt #ifndef PAX_SEGVGUARD_MAXCRASHES
64 1.3.10.3 yamt #define PAX_SEGVGUARD_MAXCRASHES 5
65 1.3.10.3 yamt #endif
66 1.3.10.3 yamt
67 1.3.10.3 yamt static int pax_segvguard_enabled = 1;
68 1.3.10.3 yamt static int pax_segvguard_global = PAX_SEGVGUARD;
69 1.3.10.3 yamt static int pax_segvguard_expiry = PAX_SEGVGUARD_EXPIRY;
70 1.3.10.3 yamt static int pax_segvguard_suspension = PAX_SEGVGUARD_SUSPENSION;
71 1.3.10.3 yamt static int pax_segvguard_maxcrashes = PAX_SEGVGUARD_MAXCRASHES;
72 1.3.10.3 yamt
73 1.3.10.3 yamt static fileassoc_t segvguard_id;
74 1.3.10.3 yamt specificdata_key_t pax_segvguard_key;
75 1.3.10.3 yamt
76 1.3.10.3 yamt struct pax_segvguard_uid_entry {
77 1.3.10.3 yamt uid_t sue_uid;
78 1.3.10.3 yamt size_t sue_ncrashes;
79 1.3.10.3 yamt time_t sue_expiry;
80 1.3.10.3 yamt time_t sue_suspended;
81 1.3.10.3 yamt LIST_ENTRY(pax_segvguard_uid_entry) sue_list;
82 1.3.10.3 yamt };
83 1.3.10.3 yamt
84 1.3.10.3 yamt struct pax_segvguard_entry {
85 1.3.10.3 yamt LIST_HEAD(, pax_segvguard_uid_entry) segv_uids;
86 1.3.10.3 yamt };
87 1.3.10.3 yamt
88 1.3.10.3 yamt static void pax_segvguard_cb(void *);
89 1.3.10.3 yamt #endif /* PAX_SEGVGUARD */
90 1.3.10.3 yamt
91 1.3.10.3 yamt /* PaX internal setspecific flags */
92 1.3.10.3 yamt #define PAX_MPROTECT_EXPLICIT_ENABLE (void *)0x01
93 1.3.10.3 yamt #define PAX_MPROTECT_EXPLICIT_DISABLE (void *)0x02
94 1.3.10.3 yamt #define PAX_SEGVGUARD_EXPLICIT_ENABLE (void *)0x03
95 1.3.10.3 yamt #define PAX_SEGVGUARD_EXPLICIT_DISABLE (void *)0x04
96 1.3.10.2 yamt
97 1.3.10.2 yamt SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
98 1.3.10.2 yamt {
99 1.3.10.3 yamt const struct sysctlnode *rnode = NULL, *cnode;
100 1.3.10.2 yamt
101 1.3.10.2 yamt sysctl_createv(clog, 0, NULL, &rnode,
102 1.3.10.2 yamt CTLFLAG_PERMANENT,
103 1.3.10.2 yamt CTLTYPE_NODE, "security", NULL,
104 1.3.10.2 yamt NULL, 0, NULL, 0,
105 1.3.10.3 yamt CTL_SECURITY, CTL_EOL);
106 1.3.10.2 yamt
107 1.3.10.2 yamt sysctl_createv(clog, 0, &rnode, &rnode,
108 1.3.10.2 yamt CTLFLAG_PERMANENT,
109 1.3.10.2 yamt CTLTYPE_NODE, "pax",
110 1.3.10.2 yamt SYSCTL_DESCR("PaX (exploit mitigation) features."),
111 1.3.10.2 yamt NULL, 0, NULL, 0,
112 1.3.10.2 yamt CTL_CREATE, CTL_EOL);
113 1.3.10.2 yamt
114 1.3.10.3 yamt cnode = rnode;
115 1.3.10.3 yamt
116 1.3.10.3 yamt #ifdef PAX_MPROTECT
117 1.3.10.2 yamt sysctl_createv(clog, 0, &rnode, &rnode,
118 1.3.10.2 yamt CTLFLAG_PERMANENT,
119 1.3.10.2 yamt CTLTYPE_NODE, "mprotect",
120 1.3.10.2 yamt SYSCTL_DESCR("mprotect(2) W^X restrictions."),
121 1.3.10.2 yamt NULL, 0, NULL, 0,
122 1.3.10.2 yamt CTL_CREATE, CTL_EOL);
123 1.3.10.2 yamt sysctl_createv(clog, 0, &rnode, NULL,
124 1.3.10.2 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
125 1.3.10.2 yamt CTLTYPE_INT, "enabled",
126 1.3.10.2 yamt SYSCTL_DESCR("Restrictions enabled."),
127 1.3.10.2 yamt NULL, 0, &pax_mprotect_enabled, 0,
128 1.3.10.2 yamt CTL_CREATE, CTL_EOL);
129 1.3.10.2 yamt sysctl_createv(clog, 0, &rnode, NULL,
130 1.3.10.2 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
131 1.3.10.3 yamt CTLTYPE_INT, "global",
132 1.3.10.2 yamt SYSCTL_DESCR("When enabled, unless explicitly "
133 1.3.10.3 yamt "specified, apply restrictions to "
134 1.3.10.2 yamt "all processes."),
135 1.3.10.2 yamt NULL, 0, &pax_mprotect_global, 0,
136 1.3.10.2 yamt CTL_CREATE, CTL_EOL);
137 1.3.10.3 yamt #endif /* PAX_MPROTECT */
138 1.3.10.3 yamt
139 1.3.10.3 yamt rnode = cnode;
140 1.3.10.3 yamt
141 1.3.10.3 yamt #ifdef PAX_SEGVGUARD
142 1.3.10.3 yamt sysctl_createv(clog, 0, &rnode, &rnode,
143 1.3.10.3 yamt CTLFLAG_PERMANENT,
144 1.3.10.3 yamt CTLTYPE_NODE, "segvguard",
145 1.3.10.3 yamt SYSCTL_DESCR("PaX segvguard."),
146 1.3.10.3 yamt NULL, 0, NULL, 0,
147 1.3.10.3 yamt CTL_CREATE, CTL_EOL);
148 1.3.10.3 yamt sysctl_createv(clog, 0, &rnode, NULL,
149 1.3.10.3 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
150 1.3.10.3 yamt CTLTYPE_INT, "enabled",
151 1.3.10.3 yamt SYSCTL_DESCR("segvguard enabled."),
152 1.3.10.3 yamt NULL, 0, &pax_segvguard_enabled, 0,
153 1.3.10.3 yamt CTL_CREATE, CTL_EOL);
154 1.3.10.3 yamt sysctl_createv(clog, 0, &rnode, NULL,
155 1.3.10.3 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
156 1.3.10.3 yamt CTLTYPE_INT, "global",
157 1.3.10.3 yamt SYSCTL_DESCR("segvguard all programs."),
158 1.3.10.3 yamt NULL, 0, &pax_segvguard_global, 0,
159 1.3.10.3 yamt CTL_CREATE, CTL_EOL);
160 1.3.10.3 yamt sysctl_createv(clog, 0, &rnode, NULL,
161 1.3.10.3 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
162 1.3.10.3 yamt CTLTYPE_INT, "expiry_timeout",
163 1.3.10.3 yamt SYSCTL_DESCR("Entry expiry timeout (in seconds)."),
164 1.3.10.3 yamt NULL, 0, &pax_segvguard_expiry, 0,
165 1.3.10.3 yamt CTL_CREATE, CTL_EOL);
166 1.3.10.3 yamt sysctl_createv(clog, 0, &rnode, NULL,
167 1.3.10.3 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
168 1.3.10.3 yamt CTLTYPE_INT, "suspend_timeout",
169 1.3.10.3 yamt SYSCTL_DESCR("Entry suspension timeout (in seconds)."),
170 1.3.10.3 yamt NULL, 0, &pax_segvguard_suspension, 0,
171 1.3.10.3 yamt CTL_CREATE, CTL_EOL);
172 1.3.10.3 yamt sysctl_createv(clog, 0, &rnode, NULL,
173 1.3.10.3 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
174 1.3.10.3 yamt CTLTYPE_INT, "max_crashes",
175 1.3.10.3 yamt SYSCTL_DESCR("Max number of crashes before expiry."),
176 1.3.10.3 yamt NULL, 0, &pax_segvguard_maxcrashes, 0,
177 1.3.10.3 yamt CTL_CREATE, CTL_EOL);
178 1.3.10.3 yamt #endif /* PAX_SEGVGUARD */
179 1.3.10.2 yamt }
180 1.3.10.2 yamt
181 1.3.10.3 yamt /*
182 1.3.10.3 yamt * Initialize PaX.
183 1.3.10.3 yamt */
184 1.3.10.2 yamt void
185 1.3.10.3 yamt pax_init(void)
186 1.3.10.2 yamt {
187 1.3.10.3 yamt #ifdef PAX_SEGVGUARD
188 1.3.10.3 yamt int error;
189 1.3.10.3 yamt #endif /* PAX_SEGVGUARD */
190 1.3.10.3 yamt
191 1.3.10.3 yamt #ifdef PAX_MPROTECT
192 1.3.10.3 yamt proc_specific_key_create(&pax_mprotect_key, NULL);
193 1.3.10.3 yamt #endif /* PAX_MPROTECT */
194 1.3.10.3 yamt
195 1.3.10.3 yamt #ifdef PAX_SEGVGUARD
196 1.3.10.3 yamt error = fileassoc_register("segvguard", pax_segvguard_cb,
197 1.3.10.3 yamt &segvguard_id);
198 1.3.10.3 yamt if (error) {
199 1.3.10.3 yamt panic("pax_init: segvguard_id: error=%d\n", error);
200 1.3.10.3 yamt }
201 1.3.10.3 yamt proc_specific_key_create(&pax_segvguard_key, NULL);
202 1.3.10.3 yamt #endif /* PAX_SEGVGUARD */
203 1.3.10.3 yamt }
204 1.3.10.3 yamt
205 1.3.10.3 yamt void
206 1.3.10.3 yamt pax_adjust(struct lwp *l, int f)
207 1.3.10.3 yamt {
208 1.3.10.3 yamt #ifdef PAX_MPROTECT
209 1.3.10.3 yamt if (pax_mprotect_enabled) {
210 1.3.10.3 yamt if (f & PF_PAXMPROTECT)
211 1.3.10.3 yamt proc_setspecific(l->l_proc, pax_mprotect_key,
212 1.3.10.3 yamt PAX_MPROTECT_EXPLICIT_ENABLE);
213 1.3.10.3 yamt if (f & PF_PAXNOMPROTECT)
214 1.3.10.3 yamt proc_setspecific(l->l_proc, pax_mprotect_key,
215 1.3.10.3 yamt PAX_MPROTECT_EXPLICIT_DISABLE);
216 1.3.10.3 yamt }
217 1.3.10.3 yamt #endif /* PAX_MPROTECT */
218 1.3.10.2 yamt
219 1.3.10.3 yamt #ifdef PAX_SEGVGUARD
220 1.3.10.3 yamt if (pax_segvguard_enabled) {
221 1.3.10.3 yamt if (f & PF_PAXGUARD) {
222 1.3.10.3 yamt proc_setspecific(l->l_proc, pax_segvguard_key,
223 1.3.10.3 yamt PAX_SEGVGUARD_EXPLICIT_ENABLE);
224 1.3.10.3 yamt }
225 1.3.10.3 yamt if (f & PF_PAXNOGUARD)
226 1.3.10.3 yamt proc_setspecific(l->l_proc, pax_segvguard_key,
227 1.3.10.3 yamt PAX_SEGVGUARD_EXPLICIT_DISABLE);
228 1.3.10.3 yamt }
229 1.3.10.3 yamt #endif /* PAX_SEGVGUARD */
230 1.3.10.2 yamt }
231 1.3.10.2 yamt
232 1.3.10.3 yamt #ifdef PAX_MPROTECT
233 1.3.10.2 yamt void
234 1.3.10.2 yamt pax_mprotect(struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
235 1.3.10.2 yamt {
236 1.3.10.3 yamt void *t;
237 1.3.10.3 yamt
238 1.3.10.3 yamt if (!pax_mprotect_enabled)
239 1.3.10.3 yamt return;
240 1.3.10.3 yamt
241 1.3.10.3 yamt t = proc_getspecific(l->l_proc, pax_mprotect_key);
242 1.3.10.3 yamt if ((pax_mprotect_global && t == PAX_MPROTECT_EXPLICIT_DISABLE) ||
243 1.3.10.3 yamt (!pax_mprotect_global && t != PAX_MPROTECT_EXPLICIT_ENABLE))
244 1.3.10.2 yamt return;
245 1.3.10.2 yamt
246 1.3.10.2 yamt if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
247 1.3.10.2 yamt *prot &= ~VM_PROT_EXECUTE;
248 1.3.10.2 yamt *maxprot &= ~VM_PROT_EXECUTE;
249 1.3.10.2 yamt } else {
250 1.3.10.2 yamt *prot &= ~VM_PROT_WRITE;
251 1.3.10.2 yamt *maxprot &= ~VM_PROT_WRITE;
252 1.3.10.2 yamt }
253 1.3.10.2 yamt }
254 1.3.10.3 yamt #endif /* PAX_MPROTECT */
255 1.3.10.3 yamt
256 1.3.10.3 yamt #ifdef PAX_SEGVGUARD
257 1.3.10.3 yamt static void
258 1.3.10.3 yamt pax_segvguard_cb(void *v)
259 1.3.10.3 yamt {
260 1.3.10.3 yamt struct pax_segvguard_entry *p;
261 1.3.10.3 yamt struct pax_segvguard_uid_entry *up;
262 1.3.10.3 yamt
263 1.3.10.3 yamt if (v == NULL)
264 1.3.10.3 yamt return;
265 1.3.10.3 yamt
266 1.3.10.3 yamt p = v;
267 1.3.10.3 yamt while ((up = LIST_FIRST(&p->segv_uids)) != NULL) {
268 1.3.10.3 yamt LIST_REMOVE(up, sue_list);
269 1.3.10.3 yamt free(up, M_TEMP);
270 1.3.10.3 yamt }
271 1.3.10.3 yamt
272 1.3.10.3 yamt free(v, M_TEMP);
273 1.3.10.3 yamt }
274 1.3.10.3 yamt
275 1.3.10.3 yamt /*
276 1.3.10.3 yamt * Called when a process of image vp generated a segfault.
277 1.3.10.3 yamt */
278 1.3.10.3 yamt int
279 1.3.10.3 yamt pax_segvguard(struct lwp *l, struct vnode *vp, const char *name,
280 1.3.10.3 yamt boolean_t crashed)
281 1.3.10.3 yamt {
282 1.3.10.3 yamt struct pax_segvguard_entry *p;
283 1.3.10.3 yamt struct pax_segvguard_uid_entry *up;
284 1.3.10.3 yamt struct timeval tv;
285 1.3.10.3 yamt uid_t uid;
286 1.3.10.3 yamt void *t;
287 1.3.10.3 yamt boolean_t have_uid;
288 1.3.10.3 yamt
289 1.3.10.3 yamt if (!pax_segvguard_enabled)
290 1.3.10.3 yamt return (0);
291 1.3.10.3 yamt
292 1.3.10.3 yamt t = proc_getspecific(l->l_proc, pax_segvguard_key);
293 1.3.10.3 yamt if ((pax_segvguard_global && t == PAX_SEGVGUARD_EXPLICIT_DISABLE) ||
294 1.3.10.3 yamt (!pax_segvguard_global && t != PAX_SEGVGUARD_EXPLICIT_ENABLE))
295 1.3.10.3 yamt return (0);
296 1.3.10.3 yamt
297 1.3.10.3 yamt if (vp == NULL)
298 1.3.10.3 yamt return (EFAULT);
299 1.3.10.3 yamt
300 1.3.10.3 yamt /* Check if we already monitor the file. */
301 1.3.10.3 yamt p = fileassoc_lookup(vp, segvguard_id);
302 1.3.10.3 yamt
303 1.3.10.3 yamt /* Fast-path if starting a program we don't know. */
304 1.3.10.3 yamt if (p == NULL && !crashed)
305 1.3.10.3 yamt return (0);
306 1.3.10.3 yamt
307 1.3.10.3 yamt microtime(&tv);
308 1.3.10.3 yamt
309 1.3.10.3 yamt /*
310 1.3.10.3 yamt * If a program we don't know crashed, we need to create a new entry
311 1.3.10.3 yamt * for it.
312 1.3.10.3 yamt */
313 1.3.10.3 yamt if (p == NULL) {
314 1.3.10.3 yamt p = malloc(sizeof(*p), M_TEMP, M_WAITOK);
315 1.3.10.3 yamt if (fileassoc_add(vp, segvguard_id, p) != 0) {
316 1.3.10.3 yamt fileassoc_table_add(vp->v_mount, 16);
317 1.3.10.3 yamt fileassoc_add(vp, segvguard_id, p);
318 1.3.10.3 yamt }
319 1.3.10.3 yamt LIST_INIT(&p->segv_uids);
320 1.3.10.3 yamt
321 1.3.10.3 yamt /*
322 1.3.10.3 yamt * Initialize a new entry with "crashes so far" of 1.
323 1.3.10.3 yamt * The expiry time is when we purge the entry if it didn't
324 1.3.10.3 yamt * reach the limit.
325 1.3.10.3 yamt */
326 1.3.10.3 yamt up = malloc(sizeof(*up), M_TEMP, M_WAITOK);
327 1.3.10.3 yamt up->sue_uid = kauth_cred_getuid(l->l_cred);
328 1.3.10.3 yamt up->sue_ncrashes = 1;
329 1.3.10.3 yamt up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
330 1.3.10.3 yamt up->sue_suspended = 0;
331 1.3.10.3 yamt
332 1.3.10.3 yamt LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
333 1.3.10.3 yamt
334 1.3.10.3 yamt return (0);
335 1.3.10.3 yamt }
336 1.3.10.3 yamt
337 1.3.10.3 yamt /*
338 1.3.10.3 yamt * A program we "know" either executed or crashed again.
339 1.3.10.3 yamt * See if it's a culprit we're familiar with.
340 1.3.10.3 yamt */
341 1.3.10.3 yamt uid = kauth_cred_getuid(l->l_cred);
342 1.3.10.3 yamt have_uid = FALSE;
343 1.3.10.3 yamt LIST_FOREACH(up, &p->segv_uids, sue_list) {
344 1.3.10.3 yamt if (up->sue_uid == uid) {
345 1.3.10.3 yamt have_uid = TRUE;
346 1.3.10.3 yamt break;
347 1.3.10.3 yamt }
348 1.3.10.3 yamt }
349 1.3.10.3 yamt
350 1.3.10.3 yamt /*
351 1.3.10.3 yamt * It's someone else. Add an entry for him if we crashed.
352 1.3.10.3 yamt */
353 1.3.10.3 yamt if (!have_uid) {
354 1.3.10.3 yamt if (crashed) {
355 1.3.10.3 yamt up = malloc(sizeof(*up), M_TEMP, M_WAITOK);
356 1.3.10.3 yamt up->sue_uid = uid;
357 1.3.10.3 yamt up->sue_ncrashes = 1;
358 1.3.10.3 yamt up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
359 1.3.10.3 yamt up->sue_suspended = 0;
360 1.3.10.3 yamt
361 1.3.10.3 yamt LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
362 1.3.10.3 yamt }
363 1.3.10.3 yamt
364 1.3.10.3 yamt return (0);
365 1.3.10.3 yamt }
366 1.3.10.3 yamt
367 1.3.10.3 yamt if (crashed) {
368 1.3.10.3 yamt /* Check if timer on previous crashes expired first. */
369 1.3.10.3 yamt if (up->sue_expiry < tv.tv_sec) {
370 1.3.10.3 yamt log(LOG_INFO, "PaX Segvguard: [%s] Suspension"
371 1.3.10.3 yamt " expired.\n", name ? name : "unknown");
372 1.3.10.3 yamt
373 1.3.10.3 yamt up->sue_ncrashes = 1;
374 1.3.10.3 yamt up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
375 1.3.10.3 yamt up->sue_suspended = 0;
376 1.3.10.3 yamt
377 1.3.10.3 yamt return (0);
378 1.3.10.3 yamt }
379 1.3.10.3 yamt
380 1.3.10.3 yamt up->sue_ncrashes++;
381 1.3.10.3 yamt
382 1.3.10.3 yamt if (up->sue_ncrashes >= pax_segvguard_maxcrashes) {
383 1.3.10.3 yamt log(LOG_ALERT, "PaX Segvguard: [%s] Suspending "
384 1.3.10.3 yamt "execution for %d seconds after %zu crashes.\n",
385 1.3.10.3 yamt name ? name : "unknown", pax_segvguard_suspension,
386 1.3.10.3 yamt up->sue_ncrashes);
387 1.3.10.3 yamt
388 1.3.10.3 yamt /* Suspend this program for a while. */
389 1.3.10.3 yamt up->sue_suspended = tv.tv_sec + pax_segvguard_suspension;
390 1.3.10.3 yamt up->sue_ncrashes = 0;
391 1.3.10.3 yamt up->sue_expiry = 0;
392 1.3.10.3 yamt }
393 1.3.10.3 yamt } else {
394 1.3.10.3 yamt /* Are we supposed to be suspended? */
395 1.3.10.3 yamt if (up->sue_suspended > tv.tv_sec) {
396 1.3.10.3 yamt log(LOG_ALERT, "PaX Segvguard: [%s] Preventing "
397 1.3.10.3 yamt "execution due to repeated segfaults.\n", name ?
398 1.3.10.3 yamt name : "unknown");
399 1.3.10.3 yamt
400 1.3.10.3 yamt return (EPERM);
401 1.3.10.3 yamt }
402 1.3.10.3 yamt }
403 1.3.10.3 yamt
404 1.3.10.3 yamt return (0);
405 1.3.10.3 yamt }
406 1.3.10.3 yamt #endif /* PAX_SEGVGUARD */
407