Deleted Added
full compact
mac_process.c (189503) mac_process.c (189529)
1/*-
2 * Copyright (c) 1999-2002, 2008-2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
1/*-
2 * Copyright (c) 1999-2002, 2008-2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
5 * Copyright (c) 2005 Samy Al Bahra
6 * Copyright (c) 2006 SPARTA, Inc.
7 * Copyright (c) 2008 Apple Inc.
8 * All rights reserved.
9 *
10 * This software was developed by Robert Watson and Ilmar Habibulin for the
11 * TrustedBSD Project.
12 *
13 * This software was developed for the FreeBSD Project in part by Network
14 * Associates Laboratories, the Security Research Division of Network
15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
16 * as part of the DARPA CHATS research program.
17 *
18 * This software was enhanced by SPARTA ISSO under SPAWAR contract
19 * N66001-04-C-6019 ("SEFOS").
20 *
21 * This software was developed at the University of Cambridge Computer
22 * Laboratory with support from a grant from Google, Inc.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
46#include <sys/cdefs.h>
5 * Copyright (c) 2006 SPARTA, Inc.
6 * Copyright (c) 2008 Apple Inc.
7 * All rights reserved.
8 *
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
10 * TrustedBSD Project.
11 *
12 * This software was developed for the FreeBSD Project in part by Network
13 * Associates Laboratories, the Security Research Division of Network
14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
15 * as part of the DARPA CHATS research program.
16 *
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
19 *
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 */
44
45#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/sys/security/mac/mac_process.c 189503 2009-03-08 00:50:37Z rwatson $");
46__FBSDID("$FreeBSD: head/sys/security/mac/mac_process.c 189529 2009-03-08 10:58:37Z rwatson $");
48
49#include "opt_kdtrace.h"
50#include "opt_mac.h"
51
52#include <sys/param.h>
53#include <sys/condvar.h>
54#include <sys/imgact.h>
55#include <sys/kernel.h>
56#include <sys/lock.h>
57#include <sys/malloc.h>
58#include <sys/mutex.h>
59#include <sys/mac.h>
60#include <sys/proc.h>
61#include <sys/sbuf.h>
62#include <sys/sdt.h>
63#include <sys/systm.h>
64#include <sys/vnode.h>
65#include <sys/mount.h>
66#include <sys/file.h>
67#include <sys/namei.h>
68#include <sys/sysctl.h>
69
70#include <vm/vm.h>
71#include <vm/pmap.h>
72#include <vm/vm_map.h>
73#include <vm/vm_object.h>
74
75#include <security/mac/mac_framework.h>
76#include <security/mac/mac_internal.h>
77#include <security/mac/mac_policy.h>
78
79static int mac_mmap_revocation = 1;
80SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
81 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
82 "relabel");
83
84static int mac_mmap_revocation_via_cow = 0;
85SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
86 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
87 "copy-on-write semantics, or by removing all write access");
88
89static void mac_proc_vm_revoke_recurse(struct thread *td,
90 struct ucred *cred, struct vm_map *map);
91
92static struct label *
93mac_proc_label_alloc(void)
94{
95 struct label *label;
96
97 label = mac_labelzone_alloc(M_WAITOK);
98 MAC_PERFORM(proc_init_label, label);
99 return (label);
100}
101
102void
103mac_proc_init(struct proc *p)
104{
105
106 if (mac_labeled & MPC_OBJECT_PROC)
107 p->p_label = mac_proc_label_alloc();
108 else
109 p->p_label = NULL;
110}
111
112static void
113mac_proc_label_free(struct label *label)
114{
115
116 MAC_PERFORM(proc_destroy_label, label);
117 mac_labelzone_free(label);
118}
119
120void
121mac_proc_destroy(struct proc *p)
122{
123
124 if (p->p_label != NULL) {
125 mac_proc_label_free(p->p_label);
126 p->p_label = NULL;
127 }
128}
129
130void
131mac_thread_userret(struct thread *td)
132{
133
134 MAC_PERFORM(thread_userret, td);
135}
136
137int
138mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
139{
140 struct label *label;
141 struct mac mac;
142 char *buffer;
143 int error;
144
145 if (mac_p == NULL)
146 return (0);
147
148 if (!(mac_labeled & MPC_OBJECT_CRED))
149 return (EINVAL);
150
151 error = copyin(mac_p, &mac, sizeof(mac));
152 if (error)
153 return (error);
154
155 error = mac_check_structmac_consistent(&mac);
156 if (error)
157 return (error);
158
159 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
160 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
161 if (error) {
162 free(buffer, M_MACTEMP);
163 return (error);
164 }
165
166 label = mac_cred_label_alloc();
167 error = mac_cred_internalize_label(label, buffer);
168 free(buffer, M_MACTEMP);
169 if (error) {
170 mac_cred_label_free(label);
171 return (error);
172 }
173 imgp->execlabel = label;
174 return (0);
175}
176
177void
178mac_execve_exit(struct image_params *imgp)
179{
180 if (imgp->execlabel != NULL) {
181 mac_cred_label_free(imgp->execlabel);
182 imgp->execlabel = NULL;
183 }
184}
185
186void
187mac_execve_interpreter_enter(struct vnode *interpvp,
188 struct label **interpvplabel)
189{
190
191 if (mac_labeled & MPC_OBJECT_VNODE) {
192 *interpvplabel = mac_vnode_label_alloc();
193 mac_vnode_copy_label(interpvp->v_label, *interpvplabel);
194 } else
195 *interpvplabel = NULL;
196}
197
198void
199mac_execve_interpreter_exit(struct label *interpvplabel)
200{
201
202 if (interpvplabel != NULL)
203 mac_vnode_label_free(interpvplabel);
204}
205
206/*
207 * When relabeling a process, call out to the policies for the maximum
208 * permission allowed for each object type we know about in its memory space,
209 * and revoke access (in the least surprising ways we know) when necessary.
210 * The process lock is not held here.
211 */
212void
213mac_proc_vm_revoke(struct thread *td)
214{
215 struct ucred *cred;
216
217 PROC_LOCK(td->td_proc);
218 cred = crhold(td->td_proc->p_ucred);
219 PROC_UNLOCK(td->td_proc);
220
221 /* XXX freeze all other threads */
222 mac_proc_vm_revoke_recurse(td, cred,
223 &td->td_proc->p_vmspace->vm_map);
224 /* XXX allow other threads to continue */
225
226 crfree(cred);
227}
228
229static __inline const char *
230prot2str(vm_prot_t prot)
231{
232
233 switch (prot & VM_PROT_ALL) {
234 case VM_PROT_READ:
235 return ("r--");
236 case VM_PROT_READ | VM_PROT_WRITE:
237 return ("rw-");
238 case VM_PROT_READ | VM_PROT_EXECUTE:
239 return ("r-x");
240 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
241 return ("rwx");
242 case VM_PROT_WRITE:
243 return ("-w-");
244 case VM_PROT_EXECUTE:
245 return ("--x");
246 case VM_PROT_WRITE | VM_PROT_EXECUTE:
247 return ("-wx");
248 default:
249 return ("---");
250 }
251}
252
253static void
254mac_proc_vm_revoke_recurse(struct thread *td, struct ucred *cred,
255 struct vm_map *map)
256{
257 vm_map_entry_t vme;
258 int vfslocked, result;
259 vm_prot_t revokeperms;
260 vm_object_t backing_object, object;
261 vm_ooffset_t offset;
262 struct vnode *vp;
263 struct mount *mp;
264
265 if (!mac_mmap_revocation)
266 return;
267
268 vm_map_lock(map);
269 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
270 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
271 mac_proc_vm_revoke_recurse(td, cred,
272 vme->object.sub_map);
273 continue;
274 }
275 /*
276 * Skip over entries that obviously are not shared.
277 */
278 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
279 !vme->max_protection)
280 continue;
281 /*
282 * Drill down to the deepest backing object.
283 */
284 offset = vme->offset;
285 object = vme->object.vm_object;
286 if (object == NULL)
287 continue;
288 VM_OBJECT_LOCK(object);
289 while ((backing_object = object->backing_object) != NULL) {
290 VM_OBJECT_LOCK(backing_object);
291 offset += object->backing_object_offset;
292 VM_OBJECT_UNLOCK(object);
293 object = backing_object;
294 }
295 VM_OBJECT_UNLOCK(object);
296 /*
297 * At the moment, vm_maps and objects aren't considered by
298 * the MAC system, so only things with backing by a normal
299 * object (read: vnodes) are checked.
300 */
301 if (object->type != OBJT_VNODE)
302 continue;
303 vp = (struct vnode *)object->handle;
304 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
305 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
306 result = vme->max_protection;
307 mac_vnode_check_mmap_downgrade(cred, vp, &result);
308 VOP_UNLOCK(vp, 0);
309 /*
310 * Find out what maximum protection we may be allowing now
311 * but a policy needs to get removed.
312 */
313 revokeperms = vme->max_protection & ~result;
314 if (!revokeperms) {
315 VFS_UNLOCK_GIANT(vfslocked);
316 continue;
317 }
318 printf("pid %ld: revoking %s perms from %#lx:%ld "
319 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
320 prot2str(revokeperms), (u_long)vme->start,
321 (long)(vme->end - vme->start),
322 prot2str(vme->max_protection), prot2str(vme->protection));
323 /*
324 * This is the really simple case: if a map has more
325 * max_protection than is allowed, but it's not being
326 * actually used (that is, the current protection is still
327 * allowed), we can just wipe it out and do nothing more.
328 */
329 if ((vme->protection & revokeperms) == 0) {
330 vme->max_protection -= revokeperms;
331 } else {
332 if (revokeperms & VM_PROT_WRITE) {
333 /*
334 * In the more complicated case, flush out all
335 * pending changes to the object then turn it
336 * copy-on-write.
337 */
338 vm_object_reference(object);
339 (void) vn_start_write(vp, &mp, V_WAIT);
340 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
341 VM_OBJECT_LOCK(object);
342 vm_object_page_clean(object,
343 OFF_TO_IDX(offset),
344 OFF_TO_IDX(offset + vme->end - vme->start +
345 PAGE_MASK),
346 OBJPC_SYNC);
347 VM_OBJECT_UNLOCK(object);
348 VOP_UNLOCK(vp, 0);
349 vn_finished_write(mp);
350 vm_object_deallocate(object);
351 /*
352 * Why bother if there's no read permissions
353 * anymore? For the rest, we need to leave
354 * the write permissions on for COW, or
355 * remove them entirely if configured to.
356 */
357 if (!mac_mmap_revocation_via_cow) {
358 vme->max_protection &= ~VM_PROT_WRITE;
359 vme->protection &= ~VM_PROT_WRITE;
360 } if ((revokeperms & VM_PROT_READ) == 0)
361 vme->eflags |= MAP_ENTRY_COW |
362 MAP_ENTRY_NEEDS_COPY;
363 }
364 if (revokeperms & VM_PROT_EXECUTE) {
365 vme->max_protection &= ~VM_PROT_EXECUTE;
366 vme->protection &= ~VM_PROT_EXECUTE;
367 }
368 if (revokeperms & VM_PROT_READ) {
369 vme->max_protection = 0;
370 vme->protection = 0;
371 }
372 pmap_protect(map->pmap, vme->start, vme->end,
373 vme->protection & ~revokeperms);
374 vm_map_simplify_entry(map, vme);
375 }
376 VFS_UNLOCK_GIANT(vfslocked);
377 }
378 vm_map_unlock(map);
379}
380
381MAC_CHECK_PROBE_DEFINE2(proc_check_debug, "struct ucred *", "struct proc *");
382
383int
384mac_proc_check_debug(struct ucred *cred, struct proc *p)
385{
386 int error;
387
388 PROC_LOCK_ASSERT(p, MA_OWNED);
389
390 MAC_CHECK(proc_check_debug, cred, p);
391 MAC_CHECK_PROBE2(proc_check_debug, error, cred, p);
392
393 return (error);
394}
395
396MAC_CHECK_PROBE_DEFINE2(proc_check_sched, "struct ucred *", "struct proc *");
397
398int
399mac_proc_check_sched(struct ucred *cred, struct proc *p)
400{
401 int error;
402
403 PROC_LOCK_ASSERT(p, MA_OWNED);
404
405 MAC_CHECK(proc_check_sched, cred, p);
406 MAC_CHECK_PROBE2(proc_check_sched, error, cred, p);
407
408 return (error);
409}
410
411MAC_CHECK_PROBE_DEFINE3(proc_check_signal, "struct ucred *", "struct proc *",
412 "int");
413
414int
415mac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
416{
417 int error;
418
419 PROC_LOCK_ASSERT(p, MA_OWNED);
420
421 MAC_CHECK(proc_check_signal, cred, p, signum);
422 MAC_CHECK_PROBE3(proc_check_signal, error, cred, p, signum);
423
424 return (error);
425}
426
47
48#include "opt_kdtrace.h"
49#include "opt_mac.h"
50
51#include <sys/param.h>
52#include <sys/condvar.h>
53#include <sys/imgact.h>
54#include <sys/kernel.h>
55#include <sys/lock.h>
56#include <sys/malloc.h>
57#include <sys/mutex.h>
58#include <sys/mac.h>
59#include <sys/proc.h>
60#include <sys/sbuf.h>
61#include <sys/sdt.h>
62#include <sys/systm.h>
63#include <sys/vnode.h>
64#include <sys/mount.h>
65#include <sys/file.h>
66#include <sys/namei.h>
67#include <sys/sysctl.h>
68
69#include <vm/vm.h>
70#include <vm/pmap.h>
71#include <vm/vm_map.h>
72#include <vm/vm_object.h>
73
74#include <security/mac/mac_framework.h>
75#include <security/mac/mac_internal.h>
76#include <security/mac/mac_policy.h>
77
78static int mac_mmap_revocation = 1;
79SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
80 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
81 "relabel");
82
83static int mac_mmap_revocation_via_cow = 0;
84SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
85 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
86 "copy-on-write semantics, or by removing all write access");
87
88static void mac_proc_vm_revoke_recurse(struct thread *td,
89 struct ucred *cred, struct vm_map *map);
90
91static struct label *
92mac_proc_label_alloc(void)
93{
94 struct label *label;
95
96 label = mac_labelzone_alloc(M_WAITOK);
97 MAC_PERFORM(proc_init_label, label);
98 return (label);
99}
100
101void
102mac_proc_init(struct proc *p)
103{
104
105 if (mac_labeled & MPC_OBJECT_PROC)
106 p->p_label = mac_proc_label_alloc();
107 else
108 p->p_label = NULL;
109}
110
111static void
112mac_proc_label_free(struct label *label)
113{
114
115 MAC_PERFORM(proc_destroy_label, label);
116 mac_labelzone_free(label);
117}
118
119void
120mac_proc_destroy(struct proc *p)
121{
122
123 if (p->p_label != NULL) {
124 mac_proc_label_free(p->p_label);
125 p->p_label = NULL;
126 }
127}
128
129void
130mac_thread_userret(struct thread *td)
131{
132
133 MAC_PERFORM(thread_userret, td);
134}
135
136int
137mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
138{
139 struct label *label;
140 struct mac mac;
141 char *buffer;
142 int error;
143
144 if (mac_p == NULL)
145 return (0);
146
147 if (!(mac_labeled & MPC_OBJECT_CRED))
148 return (EINVAL);
149
150 error = copyin(mac_p, &mac, sizeof(mac));
151 if (error)
152 return (error);
153
154 error = mac_check_structmac_consistent(&mac);
155 if (error)
156 return (error);
157
158 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
159 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
160 if (error) {
161 free(buffer, M_MACTEMP);
162 return (error);
163 }
164
165 label = mac_cred_label_alloc();
166 error = mac_cred_internalize_label(label, buffer);
167 free(buffer, M_MACTEMP);
168 if (error) {
169 mac_cred_label_free(label);
170 return (error);
171 }
172 imgp->execlabel = label;
173 return (0);
174}
175
176void
177mac_execve_exit(struct image_params *imgp)
178{
179 if (imgp->execlabel != NULL) {
180 mac_cred_label_free(imgp->execlabel);
181 imgp->execlabel = NULL;
182 }
183}
184
185void
186mac_execve_interpreter_enter(struct vnode *interpvp,
187 struct label **interpvplabel)
188{
189
190 if (mac_labeled & MPC_OBJECT_VNODE) {
191 *interpvplabel = mac_vnode_label_alloc();
192 mac_vnode_copy_label(interpvp->v_label, *interpvplabel);
193 } else
194 *interpvplabel = NULL;
195}
196
197void
198mac_execve_interpreter_exit(struct label *interpvplabel)
199{
200
201 if (interpvplabel != NULL)
202 mac_vnode_label_free(interpvplabel);
203}
204
205/*
206 * When relabeling a process, call out to the policies for the maximum
207 * permission allowed for each object type we know about in its memory space,
208 * and revoke access (in the least surprising ways we know) when necessary.
209 * The process lock is not held here.
210 */
211void
212mac_proc_vm_revoke(struct thread *td)
213{
214 struct ucred *cred;
215
216 PROC_LOCK(td->td_proc);
217 cred = crhold(td->td_proc->p_ucred);
218 PROC_UNLOCK(td->td_proc);
219
220 /* XXX freeze all other threads */
221 mac_proc_vm_revoke_recurse(td, cred,
222 &td->td_proc->p_vmspace->vm_map);
223 /* XXX allow other threads to continue */
224
225 crfree(cred);
226}
227
228static __inline const char *
229prot2str(vm_prot_t prot)
230{
231
232 switch (prot & VM_PROT_ALL) {
233 case VM_PROT_READ:
234 return ("r--");
235 case VM_PROT_READ | VM_PROT_WRITE:
236 return ("rw-");
237 case VM_PROT_READ | VM_PROT_EXECUTE:
238 return ("r-x");
239 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
240 return ("rwx");
241 case VM_PROT_WRITE:
242 return ("-w-");
243 case VM_PROT_EXECUTE:
244 return ("--x");
245 case VM_PROT_WRITE | VM_PROT_EXECUTE:
246 return ("-wx");
247 default:
248 return ("---");
249 }
250}
251
252static void
253mac_proc_vm_revoke_recurse(struct thread *td, struct ucred *cred,
254 struct vm_map *map)
255{
256 vm_map_entry_t vme;
257 int vfslocked, result;
258 vm_prot_t revokeperms;
259 vm_object_t backing_object, object;
260 vm_ooffset_t offset;
261 struct vnode *vp;
262 struct mount *mp;
263
264 if (!mac_mmap_revocation)
265 return;
266
267 vm_map_lock(map);
268 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
269 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
270 mac_proc_vm_revoke_recurse(td, cred,
271 vme->object.sub_map);
272 continue;
273 }
274 /*
275 * Skip over entries that obviously are not shared.
276 */
277 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
278 !vme->max_protection)
279 continue;
280 /*
281 * Drill down to the deepest backing object.
282 */
283 offset = vme->offset;
284 object = vme->object.vm_object;
285 if (object == NULL)
286 continue;
287 VM_OBJECT_LOCK(object);
288 while ((backing_object = object->backing_object) != NULL) {
289 VM_OBJECT_LOCK(backing_object);
290 offset += object->backing_object_offset;
291 VM_OBJECT_UNLOCK(object);
292 object = backing_object;
293 }
294 VM_OBJECT_UNLOCK(object);
295 /*
296 * At the moment, vm_maps and objects aren't considered by
297 * the MAC system, so only things with backing by a normal
298 * object (read: vnodes) are checked.
299 */
300 if (object->type != OBJT_VNODE)
301 continue;
302 vp = (struct vnode *)object->handle;
303 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
304 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
305 result = vme->max_protection;
306 mac_vnode_check_mmap_downgrade(cred, vp, &result);
307 VOP_UNLOCK(vp, 0);
308 /*
309 * Find out what maximum protection we may be allowing now
310 * but a policy needs to get removed.
311 */
312 revokeperms = vme->max_protection & ~result;
313 if (!revokeperms) {
314 VFS_UNLOCK_GIANT(vfslocked);
315 continue;
316 }
317 printf("pid %ld: revoking %s perms from %#lx:%ld "
318 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
319 prot2str(revokeperms), (u_long)vme->start,
320 (long)(vme->end - vme->start),
321 prot2str(vme->max_protection), prot2str(vme->protection));
322 /*
323 * This is the really simple case: if a map has more
324 * max_protection than is allowed, but it's not being
325 * actually used (that is, the current protection is still
326 * allowed), we can just wipe it out and do nothing more.
327 */
328 if ((vme->protection & revokeperms) == 0) {
329 vme->max_protection -= revokeperms;
330 } else {
331 if (revokeperms & VM_PROT_WRITE) {
332 /*
333 * In the more complicated case, flush out all
334 * pending changes to the object then turn it
335 * copy-on-write.
336 */
337 vm_object_reference(object);
338 (void) vn_start_write(vp, &mp, V_WAIT);
339 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
340 VM_OBJECT_LOCK(object);
341 vm_object_page_clean(object,
342 OFF_TO_IDX(offset),
343 OFF_TO_IDX(offset + vme->end - vme->start +
344 PAGE_MASK),
345 OBJPC_SYNC);
346 VM_OBJECT_UNLOCK(object);
347 VOP_UNLOCK(vp, 0);
348 vn_finished_write(mp);
349 vm_object_deallocate(object);
350 /*
351 * Why bother if there's no read permissions
352 * anymore? For the rest, we need to leave
353 * the write permissions on for COW, or
354 * remove them entirely if configured to.
355 */
356 if (!mac_mmap_revocation_via_cow) {
357 vme->max_protection &= ~VM_PROT_WRITE;
358 vme->protection &= ~VM_PROT_WRITE;
359 } if ((revokeperms & VM_PROT_READ) == 0)
360 vme->eflags |= MAP_ENTRY_COW |
361 MAP_ENTRY_NEEDS_COPY;
362 }
363 if (revokeperms & VM_PROT_EXECUTE) {
364 vme->max_protection &= ~VM_PROT_EXECUTE;
365 vme->protection &= ~VM_PROT_EXECUTE;
366 }
367 if (revokeperms & VM_PROT_READ) {
368 vme->max_protection = 0;
369 vme->protection = 0;
370 }
371 pmap_protect(map->pmap, vme->start, vme->end,
372 vme->protection & ~revokeperms);
373 vm_map_simplify_entry(map, vme);
374 }
375 VFS_UNLOCK_GIANT(vfslocked);
376 }
377 vm_map_unlock(map);
378}
379
380MAC_CHECK_PROBE_DEFINE2(proc_check_debug, "struct ucred *", "struct proc *");
381
382int
383mac_proc_check_debug(struct ucred *cred, struct proc *p)
384{
385 int error;
386
387 PROC_LOCK_ASSERT(p, MA_OWNED);
388
389 MAC_CHECK(proc_check_debug, cred, p);
390 MAC_CHECK_PROBE2(proc_check_debug, error, cred, p);
391
392 return (error);
393}
394
395MAC_CHECK_PROBE_DEFINE2(proc_check_sched, "struct ucred *", "struct proc *");
396
397int
398mac_proc_check_sched(struct ucred *cred, struct proc *p)
399{
400 int error;
401
402 PROC_LOCK_ASSERT(p, MA_OWNED);
403
404 MAC_CHECK(proc_check_sched, cred, p);
405 MAC_CHECK_PROBE2(proc_check_sched, error, cred, p);
406
407 return (error);
408}
409
410MAC_CHECK_PROBE_DEFINE3(proc_check_signal, "struct ucred *", "struct proc *",
411 "int");
412
413int
414mac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
415{
416 int error;
417
418 PROC_LOCK_ASSERT(p, MA_OWNED);
419
420 MAC_CHECK(proc_check_signal, cred, p, signum);
421 MAC_CHECK_PROBE3(proc_check_signal, error, cred, p, signum);
422
423 return (error);
424}
425
427MAC_CHECK_PROBE_DEFINE2(proc_check_setuid, "struct ucred *", "uid_t");
428
429int
430mac_proc_check_setuid(struct proc *p, struct ucred *cred, uid_t uid)
431{
432 int error;
433
434 PROC_LOCK_ASSERT(p, MA_OWNED);
435
436 MAC_CHECK(proc_check_setuid, cred, uid);
437 MAC_CHECK_PROBE2(proc_check_setuid, error, cred, uid);
438
439 return (error);
440}
441
442MAC_CHECK_PROBE_DEFINE2(proc_check_seteuid, "struct ucred *", "uid_t");
443
444int
445mac_proc_check_seteuid(struct proc *p, struct ucred *cred, uid_t euid)
446{
447 int error;
448
449 PROC_LOCK_ASSERT(p, MA_OWNED);
450
451 MAC_CHECK(proc_check_seteuid, cred, euid);
452 MAC_CHECK_PROBE2(proc_check_seteuid, error, cred, euid);
453
454 return (error);
455}
456
457MAC_CHECK_PROBE_DEFINE2(proc_check_setgid, "struct ucred *", "gid_t");
458
459int
460mac_proc_check_setgid(struct proc *p, struct ucred *cred, gid_t gid)
461{
462 int error;
463
464 PROC_LOCK_ASSERT(p, MA_OWNED);
465
466 MAC_CHECK(proc_check_setgid, cred, gid);
467 MAC_CHECK_PROBE2(proc_check_setgid, error, cred, gid);
468
469 return (error);
470}
471
472MAC_CHECK_PROBE_DEFINE2(proc_check_setegid, "struct ucred *", "gid_t");
473
474int
475mac_proc_check_setegid(struct proc *p, struct ucred *cred, gid_t egid)
476{
477 int error;
478
479 PROC_LOCK_ASSERT(p, MA_OWNED);
480
481 MAC_CHECK(proc_check_setegid, cred, egid);
482 MAC_CHECK_PROBE2(proc_check_setegid, error, cred, egid);
483
484 return (error);
485}
486
487MAC_CHECK_PROBE_DEFINE3(proc_check_setgroups, "struct ucred *", "int",
488 "gid_t *");
489
490int
491mac_proc_check_setgroups(struct proc *p, struct ucred *cred, int ngroups,
492 gid_t *gidset)
493{
494 int error;
495
496 PROC_LOCK_ASSERT(p, MA_OWNED);
497
498 MAC_CHECK(proc_check_setgroups, cred, ngroups, gidset);
499 MAC_CHECK_PROBE3(proc_check_setgroups, error, cred, ngroups, gidset);
500
501 return (error);
502}
503
504MAC_CHECK_PROBE_DEFINE3(proc_check_setreuid, "struct ucred *", "uid_t",
505 "uid_t");
506
507int
508mac_proc_check_setreuid(struct proc *p, struct ucred *cred, uid_t ruid,
509 uid_t euid)
510{
511 int error;
512
513 PROC_LOCK_ASSERT(p, MA_OWNED);
514
515 MAC_CHECK(proc_check_setreuid, cred, ruid, euid);
516 MAC_CHECK_PROBE3(proc_check_setreuid, error, cred, ruid, euid);
517
518 return (error);
519}
520
521MAC_CHECK_PROBE_DEFINE3(proc_check_setregid, "struct ucred *", "gid_t",
522 "gid_t");
523
524int
525mac_proc_check_setregid(struct proc *proc, struct ucred *cred, gid_t rgid,
526 gid_t egid)
527{
528 int error;
529
530 PROC_LOCK_ASSERT(proc, MA_OWNED);
531
532 MAC_CHECK(proc_check_setregid, cred, rgid, egid);
533 MAC_CHECK_PROBE3(proc_check_setregid, error, cred, rgid, egid);
534
535 return (error);
536}
537
538MAC_CHECK_PROBE_DEFINE4(proc_check_setresuid, "struct ucred *", "uid_t",
539 "uid_t", "uid_t");
540
541int
542mac_proc_check_setresuid(struct proc *p, struct ucred *cred, uid_t ruid,
543 uid_t euid, uid_t suid)
544{
545 int error;
546
547 PROC_LOCK_ASSERT(p, MA_OWNED);
548
549 MAC_CHECK(proc_check_setresuid, cred, ruid, euid, suid);
550 MAC_CHECK_PROBE4(proc_check_setresuid, error, cred, ruid, euid,
551 suid);
552
553 return (error);
554}
555
556MAC_CHECK_PROBE_DEFINE4(proc_check_setresgid, "struct ucred *", "gid_t",
557 "gid_t", "gid_t");
558
559int
560mac_proc_check_setresgid(struct proc *p, struct ucred *cred, gid_t rgid,
561 gid_t egid, gid_t sgid)
562{
563 int error;
564
565 PROC_LOCK_ASSERT(p, MA_OWNED);
566
567 MAC_CHECK(proc_check_setresgid, cred, rgid, egid, sgid);
568 MAC_CHECK_PROBE4(proc_check_setresgid, error, cred, rgid, egid,
569 sgid);
570
571 return (error);
572}
573
574MAC_CHECK_PROBE_DEFINE2(proc_check_wait, "struct ucred *", "struct proc *");
575
576int
577mac_proc_check_wait(struct ucred *cred, struct proc *p)
578{
579 int error;
580
581 PROC_LOCK_ASSERT(p, MA_OWNED);
582
583 MAC_CHECK(proc_check_wait, cred, p);
584 MAC_CHECK_PROBE2(proc_check_wait, error, cred, p);
585
586 return (error);
587}
426MAC_CHECK_PROBE_DEFINE2(proc_check_wait, "struct ucred *", "struct proc *");
427
428int
429mac_proc_check_wait(struct ucred *cred, struct proc *p)
430{
431 int error;
432
433 PROC_LOCK_ASSERT(p, MA_OWNED);
434
435 MAC_CHECK(proc_check_wait, cred, p);
436 MAC_CHECK_PROBE2(proc_check_wait, error, cred, p);
437
438 return (error);
439}