xref: /freebsd/sys/security/mac/mac_process.c (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
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) 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>
46 __FBSDID("$FreeBSD$");
47 
48 #include "opt_mac.h"
49 
50 #include <sys/param.h>
51 #include <sys/condvar.h>
52 #include <sys/imgact.h>
53 #include <sys/kernel.h>
54 #include <sys/lock.h>
55 #include <sys/malloc.h>
56 #include <sys/mac.h>
57 #include <sys/proc.h>
58 #include <sys/rwlock.h>
59 #include <sys/sbuf.h>
60 #include <sys/sdt.h>
61 #include <sys/systm.h>
62 #include <sys/vnode.h>
63 #include <sys/mount.h>
64 #include <sys/file.h>
65 #include <sys/namei.h>
66 #include <sys/sysctl.h>
67 
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 
73 #include <security/mac/mac_framework.h>
74 #include <security/mac/mac_internal.h>
75 #include <security/mac/mac_policy.h>
76 
77 static int	mac_mmap_revocation = 1;
78 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
79     &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
80     "relabel");
81 
82 static int	mac_mmap_revocation_via_cow = 0;
83 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
84     &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
85     "copy-on-write semantics, or by removing all write access");
86 
87 static void	mac_proc_vm_revoke_recurse(struct thread *td,
88 		    struct ucred *cred, struct vm_map *map);
89 
90 static struct label *
91 mac_proc_label_alloc(void)
92 {
93 	struct label *label;
94 
95 	label = mac_labelzone_alloc(M_WAITOK);
96 	MAC_POLICY_PERFORM(proc_init_label, label);
97 	return (label);
98 }
99 
100 void
101 mac_proc_init(struct proc *p)
102 {
103 
104 	if (mac_labeled & MPC_OBJECT_PROC)
105 		p->p_label = mac_proc_label_alloc();
106 	else
107 		p->p_label = NULL;
108 }
109 
110 static void
111 mac_proc_label_free(struct label *label)
112 {
113 
114 	MAC_POLICY_PERFORM_NOSLEEP(proc_destroy_label, label);
115 	mac_labelzone_free(label);
116 }
117 
118 void
119 mac_proc_destroy(struct proc *p)
120 {
121 
122 	if (p->p_label != NULL) {
123 		mac_proc_label_free(p->p_label);
124 		p->p_label = NULL;
125 	}
126 }
127 
128 void
129 mac_thread_userret(struct thread *td)
130 {
131 
132 	MAC_POLICY_PERFORM(thread_userret, td);
133 }
134 
135 int
136 mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
137 {
138 	struct label *label;
139 	struct mac mac;
140 	char *buffer;
141 	int error;
142 
143 	if (mac_p == NULL)
144 		return (0);
145 
146 	if (!(mac_labeled & MPC_OBJECT_CRED))
147 		return (EINVAL);
148 
149 	error = copyin(mac_p, &mac, sizeof(mac));
150 	if (error)
151 		return (error);
152 
153 	error = mac_check_structmac_consistent(&mac);
154 	if (error)
155 		return (error);
156 
157 	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
158 	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
159 	if (error) {
160 		free(buffer, M_MACTEMP);
161 		return (error);
162 	}
163 
164 	label = mac_cred_label_alloc();
165 	error = mac_cred_internalize_label(label, buffer);
166 	free(buffer, M_MACTEMP);
167 	if (error) {
168 		mac_cred_label_free(label);
169 		return (error);
170 	}
171 	imgp->execlabel = label;
172 	return (0);
173 }
174 
175 void
176 mac_execve_exit(struct image_params *imgp)
177 {
178 	if (imgp->execlabel != NULL) {
179 		mac_cred_label_free(imgp->execlabel);
180 		imgp->execlabel = NULL;
181 	}
182 }
183 
184 void
185 mac_execve_interpreter_enter(struct vnode *interpvp,
186     struct label **interpvplabel)
187 {
188 
189 	if (mac_labeled & MPC_OBJECT_VNODE) {
190 		*interpvplabel = mac_vnode_label_alloc();
191 		mac_vnode_copy_label(interpvp->v_label, *interpvplabel);
192 	} else
193 		*interpvplabel = NULL;
194 }
195 
196 void
197 mac_execve_interpreter_exit(struct label *interpvplabel)
198 {
199 
200 	if (interpvplabel != NULL)
201 		mac_vnode_label_free(interpvplabel);
202 }
203 
204 /*
205  * When relabeling a process, call out to the policies for the maximum
206  * permission allowed for each object type we know about in its memory space,
207  * and revoke access (in the least surprising ways we know) when necessary.
208  * The process lock is not held here.
209  */
210 void
211 mac_proc_vm_revoke(struct thread *td)
212 {
213 	struct ucred *cred;
214 
215 	PROC_LOCK(td->td_proc);
216 	cred = crhold(td->td_proc->p_ucred);
217 	PROC_UNLOCK(td->td_proc);
218 
219 	/* XXX freeze all other threads */
220 	mac_proc_vm_revoke_recurse(td, cred,
221 	    &td->td_proc->p_vmspace->vm_map);
222 	/* XXX allow other threads to continue */
223 
224 	crfree(cred);
225 }
226 
227 static __inline const char *
228 prot2str(vm_prot_t prot)
229 {
230 
231 	switch (prot & VM_PROT_ALL) {
232 	case VM_PROT_READ:
233 		return ("r--");
234 	case VM_PROT_READ | VM_PROT_WRITE:
235 		return ("rw-");
236 	case VM_PROT_READ | VM_PROT_EXECUTE:
237 		return ("r-x");
238 	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
239 		return ("rwx");
240 	case VM_PROT_WRITE:
241 		return ("-w-");
242 	case VM_PROT_EXECUTE:
243 		return ("--x");
244 	case VM_PROT_WRITE | VM_PROT_EXECUTE:
245 		return ("-wx");
246 	default:
247 		return ("---");
248 	}
249 }
250 
251 static void
252 mac_proc_vm_revoke_recurse(struct thread *td, struct ucred *cred,
253     struct vm_map *map)
254 {
255 	vm_map_entry_t prev, vme;
256 	int result;
257 	vm_prot_t revokeperms;
258 	vm_object_t backing_object, object;
259 	vm_ooffset_t offset;
260 	struct vnode *vp;
261 	struct mount *mp;
262 
263 	if (!mac_mmap_revocation)
264 		return;
265 
266 	prev = &map->header;
267 	vm_map_lock(map);
268 	for (vme = vm_map_entry_first(map); vme != &map->header;
269 	    prev = vme, vme = vm_map_entry_succ(prev)) {
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_RLOCK(object);
289 		while ((backing_object = object->backing_object) != NULL) {
290 			VM_OBJECT_RLOCK(backing_object);
291 			offset += object->backing_object_offset;
292 			VM_OBJECT_RUNLOCK(object);
293 			object = backing_object;
294 		}
295 		VM_OBJECT_RUNLOCK(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 		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);
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 			continue;
315 		printf("pid %ld: revoking %s perms from %#lx:%ld "
316 		    "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
317 		    prot2str(revokeperms), (u_long)vme->start,
318 		    (long)(vme->end - vme->start),
319 		    prot2str(vme->max_protection), prot2str(vme->protection));
320 		/*
321 		 * This is the really simple case: if a map has more
322 		 * max_protection than is allowed, but it's not being
323 		 * actually used (that is, the current protection is still
324 		 * allowed), we can just wipe it out and do nothing more.
325 		 */
326 		if ((vme->protection & revokeperms) == 0) {
327 			vme->max_protection -= revokeperms;
328 		} else {
329 			if (revokeperms & VM_PROT_WRITE) {
330 				/*
331 				 * In the more complicated case, flush out all
332 				 * pending changes to the object then turn it
333 				 * copy-on-write.
334 				 */
335 				vm_object_reference(object);
336 				(void) vn_start_write(vp, &mp, V_WAIT);
337 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
338 				VM_OBJECT_WLOCK(object);
339 				vm_object_page_clean(object, offset, offset +
340 				    vme->end - vme->start, OBJPC_SYNC);
341 				VM_OBJECT_WUNLOCK(object);
342 				VOP_UNLOCK(vp);
343 				vn_finished_write(mp);
344 				vm_object_deallocate(object);
345 				/*
346 				 * Why bother if there's no read permissions
347 				 * anymore?  For the rest, we need to leave
348 				 * the write permissions on for COW, or
349 				 * remove them entirely if configured to.
350 				 */
351 				if (!mac_mmap_revocation_via_cow) {
352 					vme->max_protection &= ~VM_PROT_WRITE;
353 					vme->protection &= ~VM_PROT_WRITE;
354 				} if ((revokeperms & VM_PROT_READ) == 0)
355 					vme->eflags |= MAP_ENTRY_COW |
356 					    MAP_ENTRY_NEEDS_COPY;
357 			}
358 			if (revokeperms & VM_PROT_EXECUTE) {
359 				vme->max_protection &= ~VM_PROT_EXECUTE;
360 				vme->protection &= ~VM_PROT_EXECUTE;
361 			}
362 			if (revokeperms & VM_PROT_READ) {
363 				vme->max_protection = 0;
364 				vme->protection = 0;
365 			}
366 			pmap_protect(map->pmap, vme->start, vme->end,
367 			    vme->protection & ~revokeperms);
368 			vm_map_try_merge_entries(map, prev, vme);
369 		}
370 	}
371 	vm_map_unlock(map);
372 }
373 
374 MAC_CHECK_PROBE_DEFINE2(proc_check_debug, "struct ucred *", "struct proc *");
375 
376 int
377 mac_proc_check_debug(struct ucred *cred, struct proc *p)
378 {
379 	int error;
380 
381 	PROC_LOCK_ASSERT(p, MA_OWNED);
382 
383 	MAC_POLICY_CHECK_NOSLEEP(proc_check_debug, cred, p);
384 	MAC_CHECK_PROBE2(proc_check_debug, error, cred, p);
385 
386 	return (error);
387 }
388 
389 MAC_CHECK_PROBE_DEFINE2(proc_check_sched, "struct ucred *", "struct proc *");
390 
391 int
392 mac_proc_check_sched(struct ucred *cred, struct proc *p)
393 {
394 	int error;
395 
396 	PROC_LOCK_ASSERT(p, MA_OWNED);
397 
398 	MAC_POLICY_CHECK_NOSLEEP(proc_check_sched, cred, p);
399 	MAC_CHECK_PROBE2(proc_check_sched, error, cred, p);
400 
401 	return (error);
402 }
403 
404 MAC_CHECK_PROBE_DEFINE3(proc_check_signal, "struct ucred *", "struct proc *",
405     "int");
406 
407 int
408 mac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
409 {
410 	int error;
411 
412 	PROC_LOCK_ASSERT(p, MA_OWNED);
413 
414 	MAC_POLICY_CHECK_NOSLEEP(proc_check_signal, cred, p, signum);
415 	MAC_CHECK_PROBE3(proc_check_signal, error, cred, p, signum);
416 
417 	return (error);
418 }
419 
420 MAC_CHECK_PROBE_DEFINE2(proc_check_wait, "struct ucred *", "struct proc *");
421 
422 int
423 mac_proc_check_wait(struct ucred *cred, struct proc *p)
424 {
425 	int error;
426 
427 	PROC_LOCK_ASSERT(p, MA_OWNED);
428 
429 	MAC_POLICY_CHECK_NOSLEEP(proc_check_wait, cred, p);
430 	MAC_CHECK_PROBE2(proc_check_wait, error, cred, p);
431 
432 	return (error);
433 }
434