2 * Copyright (c) 1999-2002 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2005 McAfee, Inc.
5 * Copyright (c) 2005-2006 SPARTA, Inc.
8 * This software was developed by Robert Watson and Ilmar Habibulin for the
11 * This software was developed for the FreeBSD Project in part by McAfee
12 * Research, the Security Research Division of McAfee, Inc. under
13 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
14 * CHATS research program.
16 * This software was enhanced by SPARTA ISSO under SPAWAR contract
17 * N66001-04-C-6019 ("SEFOS").
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/condvar.h>
48 #include <sys/extattr.h>
49 #include <sys/imgact.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
56 #include <sys/systm.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/sysctl.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_object.h>
68 #include <fs/devfs/devfs.h>
70 #include <security/mac/mac_framework.h>
71 #include <security/mac/mac_internal.h>
72 #include <security/mac/mac_policy.h>
75 * Warn about EA transactions only the first time they happen. No locking on
78 static int ea_warn_once = 0;
80 static int mac_setlabel_vnode_extattr(struct ucred *cred,
81 struct vnode *vp, struct label *intlabel);
84 mac_devfs_label_alloc(void)
88 label = mac_labelzone_alloc(M_WAITOK);
89 MAC_PERFORM(init_devfs_label, label);
94 mac_init_devfs(struct devfs_dirent *de)
97 de->de_label = mac_devfs_label_alloc();
100 static struct label *
101 mac_mount_label_alloc(void)
105 label = mac_labelzone_alloc(M_WAITOK);
106 MAC_PERFORM(init_mount_label, label);
111 mac_init_mount(struct mount *mp)
114 mp->mnt_label = mac_mount_label_alloc();
118 mac_vnode_label_alloc(void)
122 label = mac_labelzone_alloc(M_WAITOK);
123 MAC_PERFORM(init_vnode_label, label);
128 mac_init_vnode(struct vnode *vp)
131 vp->v_label = mac_vnode_label_alloc();
135 mac_devfs_label_free(struct label *label)
138 MAC_PERFORM(destroy_devfs_label, label);
139 mac_labelzone_free(label);
143 mac_destroy_devfs(struct devfs_dirent *de)
146 mac_devfs_label_free(de->de_label);
151 mac_mount_label_free(struct label *label)
154 MAC_PERFORM(destroy_mount_label, label);
155 mac_labelzone_free(label);
159 mac_destroy_mount(struct mount *mp)
162 mac_mount_label_free(mp->mnt_label);
163 mp->mnt_label = NULL;
167 mac_vnode_label_free(struct label *label)
170 MAC_PERFORM(destroy_vnode_label, label);
171 mac_labelzone_free(label);
175 mac_destroy_vnode(struct vnode *vp)
178 mac_vnode_label_free(vp->v_label);
183 mac_copy_vnode_label(struct label *src, struct label *dest)
186 MAC_PERFORM(copy_vnode_label, src, dest);
190 mac_externalize_vnode_label(struct label *label, char *elements,
191 char *outbuf, size_t outbuflen)
195 MAC_EXTERNALIZE(vnode, label, elements, outbuf, outbuflen);
201 mac_internalize_vnode_label(struct label *label, char *string)
205 MAC_INTERNALIZE(vnode, label, string);
211 mac_update_devfs(struct mount *mp, struct devfs_dirent *de, struct vnode *vp)
214 MAC_PERFORM(update_devfs, mp, de, de->de_label, vp, vp->v_label);
218 mac_associate_vnode_devfs(struct mount *mp, struct devfs_dirent *de,
222 MAC_PERFORM(associate_vnode_devfs, mp, mp->mnt_label, de,
223 de->de_label, vp, vp->v_label);
227 mac_associate_vnode_extattr(struct mount *mp, struct vnode *vp)
231 ASSERT_VOP_LOCKED(vp, "mac_associate_vnode_extattr");
233 MAC_CHECK(associate_vnode_extattr, mp, mp->mnt_label, vp,
240 mac_associate_vnode_singlelabel(struct mount *mp, struct vnode *vp)
243 MAC_PERFORM(associate_vnode_singlelabel, mp, mp->mnt_label, vp,
248 * Functions implementing extended-attribute backed labels for file systems
251 * Where possible, we use EA transactions to make writes to multiple
252 * attributes across difference policies mutually atomic. We allow work to
253 * continue on file systems not supporting EA transactions, but generate a
257 mac_create_vnode_extattr(struct ucred *cred, struct mount *mp,
258 struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
262 ASSERT_VOP_LOCKED(dvp, "mac_create_vnode_extattr");
263 ASSERT_VOP_LOCKED(vp, "mac_create_vnode_extattr");
265 error = VOP_OPENEXTATTR(vp, cred, curthread);
266 if (error == EOPNOTSUPP) {
267 if (ea_warn_once == 0) {
268 printf("Warning: transactions not supported "
275 MAC_CHECK(create_vnode_extattr, cred, mp, mp->mnt_label, dvp,
276 dvp->v_label, vp, vp->v_label, cnp);
279 VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
283 error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
284 if (error == EOPNOTSUPP)
291 mac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp,
292 struct label *intlabel)
296 ASSERT_VOP_LOCKED(vp, "mac_setlabel_vnode_extattr");
298 error = VOP_OPENEXTATTR(vp, cred, curthread);
299 if (error == EOPNOTSUPP) {
300 if (ea_warn_once == 0) {
301 printf("Warning: transactions not supported "
308 MAC_CHECK(setlabel_vnode_extattr, cred, vp, vp->v_label, intlabel);
311 VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
315 error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
316 if (error == EOPNOTSUPP)
323 mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp,
324 struct label *interpvnodelabel, struct image_params *imgp)
327 ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
329 MAC_PERFORM(execve_transition, old, new, vp, vp->v_label,
330 interpvnodelabel, imgp, imgp->execlabel);
334 mac_execve_will_transition(struct ucred *old, struct vnode *vp,
335 struct label *interpvnodelabel, struct image_params *imgp)
339 ASSERT_VOP_LOCKED(vp, "mac_execve_will_transition");
342 MAC_BOOLEAN(execve_will_transition, ||, old, vp, vp->v_label,
343 interpvnodelabel, imgp, imgp->execlabel);
349 mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int acc_mode)
353 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
355 MAC_CHECK(check_vnode_access, cred, vp, vp->v_label, acc_mode);
360 mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
364 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
366 MAC_CHECK(check_vnode_chdir, cred, dvp, dvp->v_label);
371 mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
375 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
377 MAC_CHECK(check_vnode_chroot, cred, dvp, dvp->v_label);
382 mac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
383 struct componentname *cnp, struct vattr *vap)
387 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
389 MAC_CHECK(check_vnode_create, cred, dvp, dvp->v_label, cnp, vap);
394 mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
399 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
401 MAC_CHECK(check_vnode_deleteacl, cred, vp, vp->v_label, type);
406 mac_check_vnode_deleteextattr(struct ucred *cred, struct vnode *vp,
407 int attrnamespace, const char *name)
411 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteextattr");
413 MAC_CHECK(check_vnode_deleteextattr, cred, vp, vp->v_label,
414 attrnamespace, name);
419 mac_check_vnode_exec(struct ucred *cred, struct vnode *vp,
420 struct image_params *imgp)
424 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
426 MAC_CHECK(check_vnode_exec, cred, vp, vp->v_label, imgp,
433 mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
437 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
439 MAC_CHECK(check_vnode_getacl, cred, vp, vp->v_label, type);
444 mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
445 int attrnamespace, const char *name, struct uio *uio)
449 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
451 MAC_CHECK(check_vnode_getextattr, cred, vp, vp->v_label,
452 attrnamespace, name, uio);
457 mac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
458 struct vnode *vp, struct componentname *cnp)
462 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
463 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
465 MAC_CHECK(check_vnode_link, cred, dvp, dvp->v_label, vp,
471 mac_check_vnode_listextattr(struct ucred *cred, struct vnode *vp,
476 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_listextattr");
478 MAC_CHECK(check_vnode_listextattr, cred, vp, vp->v_label,
484 mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
485 struct componentname *cnp)
489 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
491 MAC_CHECK(check_vnode_lookup, cred, dvp, dvp->v_label, cnp);
496 mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp,
501 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap");
503 MAC_CHECK(check_vnode_mmap, cred, vp, vp->v_label, prot, flags);
508 mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot)
512 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade");
514 MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, vp->v_label,
521 mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot)
525 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect");
527 MAC_CHECK(check_vnode_mprotect, cred, vp, vp->v_label, prot);
532 mac_check_vnode_open(struct ucred *cred, struct vnode *vp, int acc_mode)
536 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
538 MAC_CHECK(check_vnode_open, cred, vp, vp->v_label, acc_mode);
543 mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
548 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
550 MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
557 mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
562 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
564 MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
571 mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
575 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
577 MAC_CHECK(check_vnode_readdir, cred, dvp, dvp->v_label);
582 mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
586 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
588 MAC_CHECK(check_vnode_readlink, cred, vp, vp->v_label);
593 mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
594 struct label *newlabel)
598 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
600 MAC_CHECK(check_vnode_relabel, cred, vp, vp->v_label, newlabel);
606 mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
607 struct vnode *vp, struct componentname *cnp)
611 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
612 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
614 MAC_CHECK(check_vnode_rename_from, cred, dvp, dvp->v_label, vp,
620 mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
621 struct vnode *vp, int samedir, struct componentname *cnp)
625 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
626 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
628 MAC_CHECK(check_vnode_rename_to, cred, dvp, dvp->v_label, vp,
629 vp != NULL ? vp->v_label : NULL, samedir, cnp);
634 mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
638 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
640 MAC_CHECK(check_vnode_revoke, cred, vp, vp->v_label);
645 mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
650 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
652 MAC_CHECK(check_vnode_setacl, cred, vp, vp->v_label, type, acl);
657 mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
658 int attrnamespace, const char *name, struct uio *uio)
662 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
664 MAC_CHECK(check_vnode_setextattr, cred, vp, vp->v_label,
665 attrnamespace, name, uio);
670 mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
674 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
676 MAC_CHECK(check_vnode_setflags, cred, vp, vp->v_label, flags);
681 mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
685 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
687 MAC_CHECK(check_vnode_setmode, cred, vp, vp->v_label, mode);
692 mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
697 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
699 MAC_CHECK(check_vnode_setowner, cred, vp, vp->v_label, uid, gid);
704 mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
705 struct timespec atime, struct timespec mtime)
709 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
711 MAC_CHECK(check_vnode_setutimes, cred, vp, vp->v_label, atime,
717 mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
722 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
724 MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
730 mac_check_vnode_unlink(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
731 struct componentname *cnp)
735 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_unlink");
736 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_unlink");
738 MAC_CHECK(check_vnode_unlink, cred, dvp, dvp->v_label, vp,
744 mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
749 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
751 MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
758 mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
761 MAC_PERFORM(relabel_vnode, cred, vp, vp->v_label, newlabel);
765 mac_create_mount(struct ucred *cred, struct mount *mp)
768 MAC_PERFORM(create_mount, cred, mp, mp->mnt_label);
772 mac_check_mount_stat(struct ucred *cred, struct mount *mount)
776 MAC_CHECK(check_mount_stat, cred, mount, mount->mnt_label);
782 mac_create_devfs_device(struct ucred *cred, struct mount *mp,
783 struct cdev *dev, struct devfs_dirent *de)
786 MAC_PERFORM(create_devfs_device, cred, mp, dev, de, de->de_label);
790 mac_create_devfs_symlink(struct ucred *cred, struct mount *mp,
791 struct devfs_dirent *dd, struct devfs_dirent *de)
794 MAC_PERFORM(create_devfs_symlink, cred, mp, dd, dd->de_label, de,
799 mac_create_devfs_directory(struct mount *mp, char *dirname, int dirnamelen,
800 struct devfs_dirent *de)
803 MAC_PERFORM(create_devfs_directory, mp, dirname, dirnamelen, de,
808 * Implementation of VOP_SETLABEL() that relies on extended attributes
809 * to store label data. Can be referenced by filesystems supporting
810 * extended attributes.
813 vop_stdsetlabel_ea(struct vop_setlabel_args *ap)
815 struct vnode *vp = ap->a_vp;
816 struct label *intlabel = ap->a_label;
819 ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
821 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
824 error = mac_setlabel_vnode_extattr(ap->a_cred, vp, intlabel);
828 mac_relabel_vnode(ap->a_cred, vp, intlabel);
834 vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
838 if (vp->v_mount == NULL) {
839 /* printf("vn_setlabel: null v_mount\n"); */
840 if (vp->v_type != VNON)
841 printf("vn_setlabel: null v_mount with non-VNON\n");
845 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
849 * Multi-phase commit. First check the policies to confirm the
850 * change is OK. Then commit via the filesystem. Finally, update
851 * the actual vnode label.
853 * Question: maybe the filesystem should update the vnode at the end
854 * as part of VOP_SETLABEL()?
856 error = mac_check_vnode_relabel(cred, vp, intlabel);
861 * VADMIN provides the opportunity for the filesystem to make
862 * decisions about who is and is not able to modify labels and
863 * protections on files. This might not be right. We can't assume
864 * VOP_SETLABEL() will do it, because we might implement that as
865 * part of vop_stdsetlabel_ea().
867 error = VOP_ACCESS(vp, VADMIN, cred, curthread);
871 error = VOP_SETLABEL(vp, intlabel, cred, curthread);
879 * When a thread becomes an NFS server daemon, its credential may need to be
880 * updated to reflect this so that policies can recognize when file system
881 * operations originate from the network.
883 * At some point, it would be desirable if the credential used for each NFS
884 * RPC could be set based on the RPC context (i.e., source system, etc) to
885 * provide more fine-grained access control.
888 mac_associate_nfsd_label(struct ucred *cred)
891 MAC_PERFORM(associate_nfsd_label, cred);