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/capability.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 p->p_ucred = 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;
233 int vfslocked, error;
235 error = copyin(uap->mac_p, &mac, sizeof(mac));
239 error = mac_check_structmac_consistent(&mac);
243 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
244 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
246 free(elements, M_MACTEMP);
250 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
251 error = fget(td, uap->fd, CAP_MAC_GET, &fp);
255 label_type = fp->f_type;
256 switch (fp->f_type) {
259 if (!(mac_labeled & MPC_OBJECT_VNODE))
262 intlabel = mac_vnode_label_alloc();
263 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
264 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
265 mac_vnode_copy_label(vp->v_label, intlabel);
267 VFS_UNLOCK_GIANT(vfslocked);
268 error = mac_vnode_externalize_label(intlabel, elements,
269 buffer, mac.m_buflen);
270 mac_vnode_label_free(intlabel);
274 if (!(mac_labeled & MPC_OBJECT_PIPE))
277 intlabel = mac_pipe_label_alloc();
279 mac_pipe_copy_label(pipe->pipe_pair->pp_label, intlabel);
281 error = mac_pipe_externalize_label(intlabel, elements,
282 buffer, mac.m_buflen);
283 mac_pipe_label_free(intlabel);
287 if (!(mac_labeled & MPC_OBJECT_SOCKET))
290 intlabel = mac_socket_label_alloc(M_WAITOK);
292 mac_socket_copy_label(so->so_label, intlabel);
294 error = mac_socket_externalize_label(intlabel, elements,
295 buffer, mac.m_buflen);
296 mac_socket_label_free(intlabel);
304 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
307 free(buffer, M_MACTEMP);
308 free(elements, M_MACTEMP);
313 sys___mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
315 char *elements, *buffer;
317 struct label *intlabel;
319 int vfslocked, error;
321 if (!(mac_labeled & MPC_OBJECT_VNODE))
324 error = copyin(uap->mac_p, &mac, sizeof(mac));
328 error = mac_check_structmac_consistent(&mac);
332 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
333 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
335 free(elements, M_MACTEMP);
339 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
340 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | FOLLOW, UIO_USERSPACE,
346 intlabel = mac_vnode_label_alloc();
347 vfslocked = NDHASGIANT(&nd);
348 mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
349 error = mac_vnode_externalize_label(intlabel, elements, buffer,
353 VFS_UNLOCK_GIANT(vfslocked);
354 mac_vnode_label_free(intlabel);
356 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
359 free(buffer, M_MACTEMP);
360 free(elements, M_MACTEMP);
366 sys___mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
368 char *elements, *buffer;
370 struct label *intlabel;
372 int vfslocked, error;
374 if (!(mac_labeled & MPC_OBJECT_VNODE))
377 error = copyin(uap->mac_p, &mac, sizeof(mac));
381 error = mac_check_structmac_consistent(&mac);
385 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
386 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
388 free(elements, M_MACTEMP);
392 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
393 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | NOFOLLOW, UIO_USERSPACE,
399 intlabel = mac_vnode_label_alloc();
400 vfslocked = NDHASGIANT(&nd);
401 mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
402 error = mac_vnode_externalize_label(intlabel, elements, buffer,
405 VFS_UNLOCK_GIANT(vfslocked);
406 mac_vnode_label_free(intlabel);
409 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
412 free(buffer, M_MACTEMP);
413 free(elements, M_MACTEMP);
419 sys___mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
421 struct label *intlabel;
429 int error, vfslocked;
431 error = copyin(uap->mac_p, &mac, sizeof(mac));
435 error = mac_check_structmac_consistent(&mac);
439 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
440 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
442 free(buffer, M_MACTEMP);
446 error = fget(td, uap->fd, CAP_MAC_SET, &fp);
450 switch (fp->f_type) {
453 if (!(mac_labeled & MPC_OBJECT_VNODE))
455 intlabel = mac_vnode_label_alloc();
456 error = mac_vnode_internalize_label(intlabel, buffer);
458 mac_vnode_label_free(intlabel);
462 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
463 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
465 VFS_UNLOCK_GIANT(vfslocked);
466 mac_vnode_label_free(intlabel);
469 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
470 error = vn_setlabel(vp, intlabel, td->td_ucred);
472 vn_finished_write(mp);
473 VFS_UNLOCK_GIANT(vfslocked);
474 mac_vnode_label_free(intlabel);
478 if (!(mac_labeled & MPC_OBJECT_PIPE))
480 intlabel = mac_pipe_label_alloc();
481 error = mac_pipe_internalize_label(intlabel, buffer);
485 error = mac_pipe_label_set(td->td_ucred,
486 pipe->pipe_pair, intlabel);
489 mac_pipe_label_free(intlabel);
493 if (!(mac_labeled & MPC_OBJECT_SOCKET))
495 intlabel = mac_socket_label_alloc(M_WAITOK);
496 error = mac_socket_internalize_label(intlabel, buffer);
499 error = mac_socket_label_set(td->td_ucred, so,
502 mac_socket_label_free(intlabel);
510 free(buffer, M_MACTEMP);
515 sys___mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
517 struct label *intlabel;
522 int vfslocked, error;
524 if (!(mac_labeled & MPC_OBJECT_VNODE))
527 error = copyin(uap->mac_p, &mac, sizeof(mac));
531 error = mac_check_structmac_consistent(&mac);
535 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
536 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
538 free(buffer, M_MACTEMP);
542 intlabel = mac_vnode_label_alloc();
543 error = mac_vnode_internalize_label(intlabel, buffer);
544 free(buffer, M_MACTEMP);
548 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | FOLLOW, UIO_USERSPACE,
551 vfslocked = NDHASGIANT(&nd);
553 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
555 error = vn_setlabel(nd.ni_vp, intlabel,
557 vn_finished_write(mp);
562 VFS_UNLOCK_GIANT(vfslocked);
564 mac_vnode_label_free(intlabel);
569 sys___mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
571 struct label *intlabel;
576 int vfslocked, error;
578 if (!(mac_labeled & MPC_OBJECT_VNODE))
581 error = copyin(uap->mac_p, &mac, sizeof(mac));
585 error = mac_check_structmac_consistent(&mac);
589 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
590 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
592 free(buffer, M_MACTEMP);
596 intlabel = mac_vnode_label_alloc();
597 error = mac_vnode_internalize_label(intlabel, buffer);
598 free(buffer, M_MACTEMP);
602 NDINIT(&nd, LOOKUP, MPSAFE | LOCKLEAF | NOFOLLOW, UIO_USERSPACE,
605 vfslocked = NDHASGIANT(&nd);
607 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
609 error = vn_setlabel(nd.ni_vp, intlabel,
611 vn_finished_write(mp);
616 VFS_UNLOCK_GIANT(vfslocked);
618 mac_vnode_label_free(intlabel);
623 sys_mac_syscall(struct thread *td, struct mac_syscall_args *uap)
625 struct mac_policy_conf *mpc;
626 char target[MAC_MAX_POLICY_NAME];
629 error = copyinstr(uap->policy, target, sizeof(target), NULL);
634 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
635 if (strcmp(mpc->mpc_name, target) == 0 &&
636 mpc->mpc_ops->mpo_syscall != NULL) {
637 error = mpc->mpc_ops->mpo_syscall(td,
638 uap->call, uap->arg);
643 if (!LIST_EMPTY(&mac_policy_list)) {
644 mac_policy_slock_sleep();
645 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
646 if (strcmp(mpc->mpc_name, target) == 0 &&
647 mpc->mpc_ops->mpo_syscall != NULL) {
648 error = mpc->mpc_ops->mpo_syscall(td,
649 uap->call, uap->arg);
653 mac_policy_sunlock_sleep();
662 sys___mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
669 sys___mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
676 sys___mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
683 sys___mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
690 sys___mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
697 sys___mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
704 sys___mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
711 sys___mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
718 sys___mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
725 sys_mac_syscall(struct thread *td, struct mac_syscall_args *uap)