2 * Copyright (c) 1999-2002, 2006, 2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
5 * Copyright (c) 2005-2006 SPARTA, Inc.
6 * Copyright (c) 2008 Apple Inc.
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
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.
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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.
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
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/fcntl.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/sysent.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
64 #include <sys/namei.h>
65 #include <sys/socket.h>
67 #include <sys/socketvar.h>
69 #include <security/mac/mac_framework.h>
70 #include <security/mac/mac_internal.h>
71 #include <security/mac/mac_policy.h>
76 __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
78 char *elements, *buffer;
84 error = copyin(uap->mac_p, &mac, sizeof(mac));
88 error = mac_check_structmac_consistent(&mac);
92 tproc = pfind(uap->pid);
96 tcred = NULL; /* Satisfy gcc. */
97 error = p_cansee(td, tproc);
99 tcred = crhold(tproc->p_ucred);
104 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
105 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
107 free(elements, M_MACTEMP);
112 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
113 error = mac_cred_externalize_label(tcred->cr_label, elements,
114 buffer, mac.m_buflen);
116 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
118 free(buffer, M_MACTEMP);
119 free(elements, M_MACTEMP);
125 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
127 char *elements, *buffer;
131 error = copyin(uap->mac_p, &mac, sizeof(mac));
135 error = mac_check_structmac_consistent(&mac);
139 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
140 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
142 free(elements, M_MACTEMP);
146 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
147 error = mac_cred_externalize_label(td->td_ucred->cr_label,
148 elements, buffer, mac.m_buflen);
150 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
152 free(buffer, M_MACTEMP);
153 free(elements, M_MACTEMP);
158 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
160 struct ucred *newcred, *oldcred;
161 struct label *intlabel;
167 if (!(mac_labeled & MPC_OBJECT_CRED))
170 error = copyin(uap->mac_p, &mac, sizeof(mac));
174 error = mac_check_structmac_consistent(&mac);
178 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
179 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
181 free(buffer, M_MACTEMP);
185 intlabel = mac_cred_label_alloc();
186 error = mac_cred_internalize_label(intlabel, buffer);
187 free(buffer, M_MACTEMP);
195 oldcred = p->p_ucred;
197 error = mac_cred_check_relabel(oldcred, intlabel);
205 crcopy(newcred, oldcred);
206 mac_cred_relabel(newcred, intlabel);
207 p->p_ucred = newcred;
211 mac_proc_vm_revoke(td);
214 mac_cred_label_free(intlabel);
219 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
221 char *elements, *buffer;
222 struct label *intlabel;
229 int vfslocked, error;
231 error = copyin(uap->mac_p, &mac, sizeof(mac));
235 error = mac_check_structmac_consistent(&mac);
239 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
240 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
242 free(elements, M_MACTEMP);
246 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
247 error = fget(td, uap->fd, &fp);
251 label_type = fp->f_type;
252 switch (fp->f_type) {
255 if (!(mac_labeled & MPC_OBJECT_VNODE))
258 intlabel = mac_vnode_label_alloc();
259 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
260 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
261 mac_vnode_copy_label(vp->v_label, intlabel);
263 VFS_UNLOCK_GIANT(vfslocked);
264 error = mac_vnode_externalize_label(intlabel, elements,
265 buffer, mac.m_buflen);
266 mac_vnode_label_free(intlabel);
270 if (!(mac_labeled & MPC_OBJECT_PIPE))
273 intlabel = mac_pipe_label_alloc();
275 mac_pipe_copy_label(pipe->pipe_pair->pp_label, intlabel);
277 error = mac_pipe_externalize_label(intlabel, elements,
278 buffer, mac.m_buflen);
279 mac_pipe_label_free(intlabel);
283 if (!(mac_labeled & MPC_OBJECT_SOCKET))
286 intlabel = mac_socket_label_alloc(M_WAITOK);
288 mac_socket_copy_label(so->so_label, intlabel);
290 error = mac_socket_externalize_label(intlabel, elements,
291 buffer, mac.m_buflen);
292 mac_socket_label_free(intlabel);
300 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
303 free(buffer, M_MACTEMP);
304 free(elements, M_MACTEMP);
309 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
311 char *elements, *buffer;
313 struct label *intlabel;
315 int vfslocked, error;
317 if (!(mac_labeled & MPC_OBJECT_VNODE))
320 error = copyin(uap->mac_p, &mac, sizeof(mac));
324 error = mac_check_structmac_consistent(&mac);
328 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
329 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
331 free(elements, M_MACTEMP);
335 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
336 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | FOLLOW, UIO_USERSPACE,
342 intlabel = mac_vnode_label_alloc();
343 vfslocked = NDHASGIANT(&nd);
344 mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
345 error = mac_vnode_externalize_label(intlabel, elements, buffer,
349 VFS_UNLOCK_GIANT(vfslocked);
350 mac_vnode_label_free(intlabel);
352 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
355 free(buffer, M_MACTEMP);
356 free(elements, M_MACTEMP);
362 __mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
364 char *elements, *buffer;
366 struct label *intlabel;
368 int vfslocked, error;
370 if (!(mac_labeled & MPC_OBJECT_VNODE))
373 error = copyin(uap->mac_p, &mac, sizeof(mac));
377 error = mac_check_structmac_consistent(&mac);
381 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
382 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
384 free(elements, M_MACTEMP);
388 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
389 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | NOFOLLOW, UIO_USERSPACE,
395 intlabel = mac_vnode_label_alloc();
396 vfslocked = NDHASGIANT(&nd);
397 mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
398 error = mac_vnode_externalize_label(intlabel, elements, buffer,
401 VFS_UNLOCK_GIANT(vfslocked);
402 mac_vnode_label_free(intlabel);
405 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
408 free(buffer, M_MACTEMP);
409 free(elements, M_MACTEMP);
415 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
417 struct label *intlabel;
425 int error, vfslocked;
427 error = copyin(uap->mac_p, &mac, sizeof(mac));
431 error = mac_check_structmac_consistent(&mac);
435 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
436 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
438 free(buffer, M_MACTEMP);
442 error = fget(td, uap->fd, &fp);
446 switch (fp->f_type) {
449 if (!(mac_labeled & MPC_OBJECT_VNODE))
451 intlabel = mac_vnode_label_alloc();
452 error = mac_vnode_internalize_label(intlabel, buffer);
454 mac_vnode_label_free(intlabel);
458 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
459 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
461 VFS_UNLOCK_GIANT(vfslocked);
462 mac_vnode_label_free(intlabel);
465 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
466 error = vn_setlabel(vp, intlabel, td->td_ucred);
468 vn_finished_write(mp);
469 VFS_UNLOCK_GIANT(vfslocked);
470 mac_vnode_label_free(intlabel);
474 if (!(mac_labeled & MPC_OBJECT_PIPE))
476 intlabel = mac_pipe_label_alloc();
477 error = mac_pipe_internalize_label(intlabel, buffer);
481 error = mac_pipe_label_set(td->td_ucred,
482 pipe->pipe_pair, intlabel);
485 mac_pipe_label_free(intlabel);
489 if (!(mac_labeled & MPC_OBJECT_SOCKET))
491 intlabel = mac_socket_label_alloc(M_WAITOK);
492 error = mac_socket_internalize_label(intlabel, buffer);
495 error = mac_socket_label_set(td->td_ucred, so,
498 mac_socket_label_free(intlabel);
506 free(buffer, M_MACTEMP);
511 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
513 struct label *intlabel;
518 int vfslocked, error;
520 if (!(mac_labeled & MPC_OBJECT_VNODE))
523 error = copyin(uap->mac_p, &mac, sizeof(mac));
527 error = mac_check_structmac_consistent(&mac);
531 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
532 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
534 free(buffer, M_MACTEMP);
538 intlabel = mac_vnode_label_alloc();
539 error = mac_vnode_internalize_label(intlabel, buffer);
540 free(buffer, M_MACTEMP);
544 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | FOLLOW, UIO_USERSPACE,
547 vfslocked = NDHASGIANT(&nd);
549 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
551 error = vn_setlabel(nd.ni_vp, intlabel,
553 vn_finished_write(mp);
558 VFS_UNLOCK_GIANT(vfslocked);
560 mac_vnode_label_free(intlabel);
565 __mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
567 struct label *intlabel;
572 int vfslocked, error;
574 if (!(mac_labeled & MPC_OBJECT_VNODE))
577 error = copyin(uap->mac_p, &mac, sizeof(mac));
581 error = mac_check_structmac_consistent(&mac);
585 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
586 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
588 free(buffer, M_MACTEMP);
592 intlabel = mac_vnode_label_alloc();
593 error = mac_vnode_internalize_label(intlabel, buffer);
594 free(buffer, M_MACTEMP);
598 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | NOFOLLOW, UIO_USERSPACE,
601 vfslocked = NDHASGIANT(&nd);
603 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
605 error = vn_setlabel(nd.ni_vp, intlabel,
607 vn_finished_write(mp);
612 VFS_UNLOCK_GIANT(vfslocked);
614 mac_vnode_label_free(intlabel);
619 mac_syscall(struct thread *td, struct mac_syscall_args *uap)
621 struct mac_policy_conf *mpc;
622 char target[MAC_MAX_POLICY_NAME];
625 error = copyinstr(uap->policy, target, sizeof(target), NULL);
630 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
631 if (strcmp(mpc->mpc_name, target) == 0 &&
632 mpc->mpc_ops->mpo_syscall != NULL) {
633 error = mpc->mpc_ops->mpo_syscall(td,
634 uap->call, uap->arg);
639 if (!LIST_EMPTY(&mac_policy_list)) {
640 mac_policy_slock_sleep();
641 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
642 if (strcmp(mpc->mpc_name, target) == 0 &&
643 mpc->mpc_ops->mpo_syscall != NULL) {
644 error = mpc->mpc_ops->mpo_syscall(td,
645 uap->call, uap->arg);
649 mac_policy_sunlock_sleep();
658 __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
665 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
672 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
679 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
686 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
693 __mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
700 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
707 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
714 __mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
721 mac_syscall(struct thread *td, struct mac_syscall_args *uap)