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/capsicum.h>
52 #include <sys/fcntl.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
59 #include <sys/systm.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/sysent.h>
63 #include <sys/vnode.h>
64 #include <sys/mount.h>
66 #include <sys/namei.h>
67 #include <sys/socket.h>
69 #include <sys/socketvar.h>
71 #include <security/mac/mac_framework.h>
72 #include <security/mac/mac_internal.h>
73 #include <security/mac/mac_policy.h>
77 FEATURE(security_mac, "Mandatory Access Control Framework support");
80 sys___mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
82 char *elements, *buffer;
88 error = copyin(uap->mac_p, &mac, sizeof(mac));
92 error = mac_check_structmac_consistent(&mac);
96 tproc = pfind(uap->pid);
100 tcred = NULL; /* Satisfy gcc. */
101 error = p_cansee(td, tproc);
103 tcred = crhold(tproc->p_ucred);
108 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
109 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
111 free(elements, M_MACTEMP);
116 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
117 error = mac_cred_externalize_label(tcred->cr_label, elements,
118 buffer, mac.m_buflen);
120 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
122 free(buffer, M_MACTEMP);
123 free(elements, M_MACTEMP);
129 sys___mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
131 char *elements, *buffer;
135 error = copyin(uap->mac_p, &mac, sizeof(mac));
139 error = mac_check_structmac_consistent(&mac);
143 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
144 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
146 free(elements, M_MACTEMP);
150 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
151 error = mac_cred_externalize_label(td->td_ucred->cr_label,
152 elements, buffer, mac.m_buflen);
154 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
156 free(buffer, M_MACTEMP);
157 free(elements, M_MACTEMP);
162 sys___mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
164 struct ucred *newcred, *oldcred;
165 struct label *intlabel;
171 if (!(mac_labeled & MPC_OBJECT_CRED))
174 error = copyin(uap->mac_p, &mac, sizeof(mac));
178 error = mac_check_structmac_consistent(&mac);
182 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
183 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
185 free(buffer, M_MACTEMP);
189 intlabel = mac_cred_label_alloc();
190 error = mac_cred_internalize_label(intlabel, buffer);
191 free(buffer, M_MACTEMP);
199 oldcred = p->p_ucred;
201 error = mac_cred_check_relabel(oldcred, intlabel);
209 crcopy(newcred, oldcred);
210 mac_cred_relabel(newcred, intlabel);
211 proc_set_cred(p, newcred);
215 mac_proc_vm_revoke(td);
218 mac_cred_label_free(intlabel);
223 sys___mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
225 char *elements, *buffer;
226 struct label *intlabel;
236 error = copyin(uap->mac_p, &mac, sizeof(mac));
240 error = mac_check_structmac_consistent(&mac);
244 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
245 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
247 free(elements, M_MACTEMP);
251 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
252 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_MAC_GET), &fp);
256 label_type = fp->f_type;
257 switch (fp->f_type) {
260 if (!(mac_labeled & MPC_OBJECT_VNODE)) {
265 intlabel = mac_vnode_label_alloc();
266 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
267 mac_vnode_copy_label(vp->v_label, intlabel);
269 error = mac_vnode_externalize_label(intlabel, elements,
270 buffer, mac.m_buflen);
271 mac_vnode_label_free(intlabel);
275 if (!(mac_labeled & MPC_OBJECT_PIPE)) {
280 intlabel = mac_pipe_label_alloc();
282 mac_pipe_copy_label(pipe->pipe_pair->pp_label, intlabel);
284 error = mac_pipe_externalize_label(intlabel, elements,
285 buffer, mac.m_buflen);
286 mac_pipe_label_free(intlabel);
290 if (!(mac_labeled & MPC_OBJECT_SOCKET)) {
295 intlabel = mac_socket_label_alloc(M_WAITOK);
297 mac_socket_copy_label(so->so_label, intlabel);
299 error = mac_socket_externalize_label(intlabel, elements,
300 buffer, mac.m_buflen);
301 mac_socket_label_free(intlabel);
308 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
312 free(buffer, M_MACTEMP);
313 free(elements, M_MACTEMP);
318 sys___mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
320 char *elements, *buffer;
322 struct label *intlabel;
326 if (!(mac_labeled & MPC_OBJECT_VNODE))
329 error = copyin(uap->mac_p, &mac, sizeof(mac));
333 error = mac_check_structmac_consistent(&mac);
337 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
338 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
340 free(elements, M_MACTEMP);
344 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
345 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
351 intlabel = mac_vnode_label_alloc();
352 mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
353 error = mac_vnode_externalize_label(intlabel, elements, buffer,
357 mac_vnode_label_free(intlabel);
359 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
362 free(buffer, M_MACTEMP);
363 free(elements, M_MACTEMP);
369 sys___mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
371 char *elements, *buffer;
373 struct label *intlabel;
377 if (!(mac_labeled & MPC_OBJECT_VNODE))
380 error = copyin(uap->mac_p, &mac, sizeof(mac));
384 error = mac_check_structmac_consistent(&mac);
388 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
389 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
391 free(elements, M_MACTEMP);
395 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
396 NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE,
402 intlabel = mac_vnode_label_alloc();
403 mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
404 error = mac_vnode_externalize_label(intlabel, elements, buffer,
407 mac_vnode_label_free(intlabel);
410 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
413 free(buffer, M_MACTEMP);
414 free(elements, M_MACTEMP);
420 sys___mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
422 struct label *intlabel;
433 error = copyin(uap->mac_p, &mac, sizeof(mac));
437 error = mac_check_structmac_consistent(&mac);
441 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
442 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
444 free(buffer, M_MACTEMP);
448 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_MAC_SET), &fp);
452 switch (fp->f_type) {
455 if (!(mac_labeled & MPC_OBJECT_VNODE)) {
459 intlabel = mac_vnode_label_alloc();
460 error = mac_vnode_internalize_label(intlabel, buffer);
462 mac_vnode_label_free(intlabel);
466 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
468 mac_vnode_label_free(intlabel);
471 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
472 error = vn_setlabel(vp, intlabel, td->td_ucred);
474 vn_finished_write(mp);
475 mac_vnode_label_free(intlabel);
479 if (!(mac_labeled & MPC_OBJECT_PIPE)) {
483 intlabel = mac_pipe_label_alloc();
484 error = mac_pipe_internalize_label(intlabel, buffer);
488 error = mac_pipe_label_set(td->td_ucred,
489 pipe->pipe_pair, intlabel);
492 mac_pipe_label_free(intlabel);
496 if (!(mac_labeled & MPC_OBJECT_SOCKET)) {
500 intlabel = mac_socket_label_alloc(M_WAITOK);
501 error = mac_socket_internalize_label(intlabel, buffer);
504 error = mac_socket_label_set(td->td_ucred, so,
507 mac_socket_label_free(intlabel);
516 free(buffer, M_MACTEMP);
521 sys___mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
523 struct label *intlabel;
530 if (!(mac_labeled & MPC_OBJECT_VNODE))
533 error = copyin(uap->mac_p, &mac, sizeof(mac));
537 error = mac_check_structmac_consistent(&mac);
541 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
542 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
544 free(buffer, M_MACTEMP);
548 intlabel = mac_vnode_label_alloc();
549 error = mac_vnode_internalize_label(intlabel, buffer);
550 free(buffer, M_MACTEMP);
554 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
558 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
560 error = vn_setlabel(nd.ni_vp, intlabel,
562 vn_finished_write(mp);
568 mac_vnode_label_free(intlabel);
573 sys___mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
575 struct label *intlabel;
582 if (!(mac_labeled & MPC_OBJECT_VNODE))
585 error = copyin(uap->mac_p, &mac, sizeof(mac));
589 error = mac_check_structmac_consistent(&mac);
593 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
594 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
596 free(buffer, M_MACTEMP);
600 intlabel = mac_vnode_label_alloc();
601 error = mac_vnode_internalize_label(intlabel, buffer);
602 free(buffer, M_MACTEMP);
606 NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE,
610 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
612 error = vn_setlabel(nd.ni_vp, intlabel,
614 vn_finished_write(mp);
620 mac_vnode_label_free(intlabel);
625 sys_mac_syscall(struct thread *td, struct mac_syscall_args *uap)
627 struct mac_policy_conf *mpc;
628 char target[MAC_MAX_POLICY_NAME];
631 error = copyinstr(uap->policy, target, sizeof(target), NULL);
636 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
637 if (strcmp(mpc->mpc_name, target) == 0 &&
638 mpc->mpc_ops->mpo_syscall != NULL) {
639 error = mpc->mpc_ops->mpo_syscall(td,
640 uap->call, uap->arg);
645 if (!LIST_EMPTY(&mac_policy_list)) {
646 mac_policy_slock_sleep();
647 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
648 if (strcmp(mpc->mpc_name, target) == 0 &&
649 mpc->mpc_ops->mpo_syscall != NULL) {
650 error = mpc->mpc_ops->mpo_syscall(td,
651 uap->call, uap->arg);
655 mac_policy_sunlock_sleep();
664 sys___mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
671 sys___mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
678 sys___mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
685 sys___mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
692 sys___mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
699 sys___mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
706 sys___mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
713 sys___mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
720 sys___mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
727 sys_mac_syscall(struct thread *td, struct mac_syscall_args *uap)