xref: /freebsd/sys/security/mac/mac_vfs.c (revision 0faa88f26c239b19ea543309f2c70384438eae73)
1 /*-
2  * Copyright (c) 1999-2002, 2009 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.
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 McAfee
13  * Research, the Security Research Division of McAfee, Inc. under
14  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
15  * 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 #include "opt_mac.h"
47 
48 #include <sys/param.h>
49 #include <sys/condvar.h>
50 #include <sys/extattr.h>
51 #include <sys/imgact.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/sbuf.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
61 #include <sys/file.h>
62 #include <sys/namei.h>
63 #include <sys/sdt.h>
64 #include <sys/sysctl.h>
65 
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 
71 #include <fs/devfs/devfs.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 /*
78  * Warn about EA transactions only the first time they happen.  No locking on
79  * this variable.
80  */
81 static int	ea_warn_once = 0;
82 
83 static int	mac_vnode_setlabel_extattr(struct ucred *cred,
84 		    struct vnode *vp, struct label *intlabel);
85 
86 static struct label *
mac_devfs_label_alloc(void)87 mac_devfs_label_alloc(void)
88 {
89 	struct label *label;
90 
91 	label = mac_labelzone_alloc(M_WAITOK);
92 	MAC_POLICY_PERFORM(devfs_init_label, label);
93 	return (label);
94 }
95 
96 void
mac_devfs_init(struct devfs_dirent * de)97 mac_devfs_init(struct devfs_dirent *de)
98 {
99 
100 	if (mac_labeled & MPC_OBJECT_DEVFS)
101 		de->de_label = mac_devfs_label_alloc();
102 	else
103 		de->de_label = NULL;
104 }
105 
106 static struct label *
mac_mount_label_alloc(void)107 mac_mount_label_alloc(void)
108 {
109 	struct label *label;
110 
111 	label = mac_labelzone_alloc(M_WAITOK);
112 	MAC_POLICY_PERFORM(mount_init_label, label);
113 	return (label);
114 }
115 
116 void
mac_mount_init(struct mount * mp)117 mac_mount_init(struct mount *mp)
118 {
119 
120 	if (mac_labeled & MPC_OBJECT_MOUNT)
121 		mp->mnt_label = mac_mount_label_alloc();
122 	else
123 		mp->mnt_label = NULL;
124 }
125 
126 /*
127  * SDT doesn't have a PROBE7 version anymore, so we just drop the label.
128  */
129 MAC_CHECK_PROBE_DEFINE5(mount_check_mount, "struct ucred *",
130     "struct vnode *", "struct vfsconf *",
131     "struct vfsoptlist **", "uint64_t");
132 int
mac_mount_check_mount(struct ucred * cred,struct vnode * vp,struct vfsconf * vfsp,struct vfsoptlist ** optlist,uint64_t fsflags)133 mac_mount_check_mount(struct ucred *cred, struct vnode *vp,
134      struct vfsconf *vfsp, struct vfsoptlist **optlist, uint64_t fsflags)
135 {
136 	int error;
137 
138 	MAC_POLICY_CHECK(mount_check_mount, cred, vp, vp->v_label, vfsp,
139 	    optlist, fsflags);
140 	MAC_CHECK_PROBE5(mount_check_mount, error, cred, vp, vfsp, optlist,
141 	    fsflags);
142 
143 	return (error);
144 }
145 
146 MAC_CHECK_PROBE_DEFINE5(mount_check_update, "struct ucred *",
147     "struct mount *", "struct label *", "struct vfsoptlist **", "uint64_t");
148 int
mac_mount_check_update(struct ucred * cred,struct mount * mp,struct vfsoptlist ** optlist,uint64_t fsflags)149 mac_mount_check_update(struct ucred *cred, struct mount *mp,
150 	    struct vfsoptlist **optlist, uint64_t fsflags)
151 {
152 	int error;
153 
154 	MAC_POLICY_CHECK(mount_check_update, cred, mp, mp->mnt_label,
155 	    optlist, fsflags);
156 	MAC_CHECK_PROBE5(mount_check_update, error, cred, mp, mp->mnt_label,
157 	    optlist, fsflags);
158 
159 	return (error);
160 }
161 
162 MAC_CHECK_PROBE_DEFINE4(mount_check_unmount, "struct ucred *",
163     "struct mount *", "struct label *", "uint64_t");
164 int
mac_mount_check_unmount(struct ucred * cred,struct mount * mp,uint64_t flags)165 mac_mount_check_unmount(struct ucred *cred, struct mount *mp, uint64_t flags)
166 {
167 	int error;
168 
169 	MAC_POLICY_CHECK(mount_check_unmount, cred, mp, mp->mnt_label, flags);
170 	MAC_CHECK_PROBE4(mount_check_unmount, error, cred, mp, mp->mnt_label,
171 	    flags);
172 
173 	return (error);
174 }
175 
176 struct label *
mac_vnode_label_alloc(void)177 mac_vnode_label_alloc(void)
178 {
179 	struct label *label;
180 
181 	label = mac_labelzone_alloc(M_WAITOK);
182 	MAC_POLICY_PERFORM(vnode_init_label, label);
183 	return (label);
184 }
185 
186 void
mac_vnode_init(struct vnode * vp)187 mac_vnode_init(struct vnode *vp)
188 {
189 
190 	if (mac_labeled & MPC_OBJECT_VNODE)
191 		vp->v_label = mac_vnode_label_alloc();
192 	else
193 		vp->v_label = NULL;
194 }
195 
196 static void
mac_devfs_label_free(struct label * label)197 mac_devfs_label_free(struct label *label)
198 {
199 
200 	MAC_POLICY_PERFORM_NOSLEEP(devfs_destroy_label, label);
201 	mac_labelzone_free(label);
202 }
203 
204 void
mac_devfs_destroy(struct devfs_dirent * de)205 mac_devfs_destroy(struct devfs_dirent *de)
206 {
207 
208 	if (de->de_label != NULL) {
209 		mac_devfs_label_free(de->de_label);
210 		de->de_label = NULL;
211 	}
212 }
213 
214 static void
mac_mount_label_free(struct label * label)215 mac_mount_label_free(struct label *label)
216 {
217 
218 	MAC_POLICY_PERFORM_NOSLEEP(mount_destroy_label, label);
219 	mac_labelzone_free(label);
220 }
221 
222 void
mac_mount_destroy(struct mount * mp)223 mac_mount_destroy(struct mount *mp)
224 {
225 
226 	if (mp->mnt_label != NULL) {
227 		mac_mount_label_free(mp->mnt_label);
228 		mp->mnt_label = NULL;
229 	}
230 }
231 
232 void
mac_vnode_label_free(struct label * label)233 mac_vnode_label_free(struct label *label)
234 {
235 
236 	MAC_POLICY_PERFORM_NOSLEEP(vnode_destroy_label, label);
237 	mac_labelzone_free(label);
238 }
239 
240 void
mac_vnode_destroy(struct vnode * vp)241 mac_vnode_destroy(struct vnode *vp)
242 {
243 
244 	if (vp->v_label != NULL) {
245 		mac_vnode_label_free(vp->v_label);
246 		vp->v_label = NULL;
247 	}
248 }
249 
250 void
mac_vnode_copy_label(struct label * src,struct label * dest)251 mac_vnode_copy_label(struct label *src, struct label *dest)
252 {
253 
254 	MAC_POLICY_PERFORM_NOSLEEP(vnode_copy_label, src, dest);
255 }
256 
257 int
mac_vnode_externalize_label(struct label * label,char * elements,char * outbuf,size_t outbuflen)258 mac_vnode_externalize_label(struct label *label, char *elements,
259     char *outbuf, size_t outbuflen)
260 {
261 	int error;
262 
263 	MAC_POLICY_EXTERNALIZE(vnode, label, elements, outbuf, outbuflen);
264 
265 	return (error);
266 }
267 
268 int
mac_vnode_internalize_label(struct label * label,char * string)269 mac_vnode_internalize_label(struct label *label, char *string)
270 {
271 	int error;
272 
273 	MAC_POLICY_INTERNALIZE(vnode, label, string);
274 
275 	return (error);
276 }
277 
278 void
mac_devfs_update(struct mount * mp,struct devfs_dirent * de,struct vnode * vp)279 mac_devfs_update(struct mount *mp, struct devfs_dirent *de, struct vnode *vp)
280 {
281 
282 	MAC_POLICY_PERFORM_NOSLEEP(devfs_update, mp, de, de->de_label, vp,
283 	    vp->v_label);
284 }
285 
286 void
mac_devfs_vnode_associate(struct mount * mp,struct devfs_dirent * de,struct vnode * vp)287 mac_devfs_vnode_associate(struct mount *mp, struct devfs_dirent *de,
288     struct vnode *vp)
289 {
290 
291 	MAC_POLICY_PERFORM_NOSLEEP(devfs_vnode_associate, mp, mp->mnt_label,
292 	    de, de->de_label, vp, vp->v_label);
293 }
294 
295 int
mac_vnode_associate_extattr(struct mount * mp,struct vnode * vp)296 mac_vnode_associate_extattr(struct mount *mp, struct vnode *vp)
297 {
298 	int error;
299 
300 	ASSERT_VOP_LOCKED(vp, "mac_vnode_associate_extattr");
301 
302 	MAC_POLICY_CHECK(vnode_associate_extattr, mp, mp->mnt_label, vp,
303 	    vp->v_label);
304 
305 	return (error);
306 }
307 
308 void
mac_vnode_associate_singlelabel(struct mount * mp,struct vnode * vp)309 mac_vnode_associate_singlelabel(struct mount *mp, struct vnode *vp)
310 {
311 
312 	MAC_POLICY_PERFORM_NOSLEEP(vnode_associate_singlelabel, mp,
313 	    mp->mnt_label, vp, vp->v_label);
314 }
315 
316 /*
317  * Functions implementing extended-attribute backed labels for file systems
318  * that support it.
319  *
320  * Where possible, we use EA transactions to make writes to multiple
321  * attributes across difference policies mutually atomic.  We allow work to
322  * continue on file systems not supporting EA transactions, but generate a
323  * printf warning.
324  */
325 int
mac_vnode_create_extattr(struct ucred * cred,struct mount * mp,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)326 mac_vnode_create_extattr(struct ucred *cred, struct mount *mp,
327     struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
328 {
329 	int error;
330 
331 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_create_extattr");
332 	ASSERT_VOP_LOCKED(vp, "mac_vnode_create_extattr");
333 
334 	error = VOP_OPENEXTATTR(vp, cred, curthread);
335 	if (error == EOPNOTSUPP) {
336 		if (ea_warn_once == 0) {
337 			printf("Warning: transactions not supported "
338 			    "in EA write.\n");
339 			ea_warn_once = 1;
340 		}
341 	} else if (error)
342 		return (error);
343 
344 	MAC_POLICY_CHECK(vnode_create_extattr, cred, mp, mp->mnt_label, dvp,
345 	    dvp->v_label, vp, vp->v_label, cnp);
346 
347 	if (error) {
348 		VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
349 		return (error);
350 	}
351 
352 	error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
353 	if (error == EOPNOTSUPP)
354 		error = 0;
355 
356 	return (error);
357 }
358 
359 static int
mac_vnode_setlabel_extattr(struct ucred * cred,struct vnode * vp,struct label * intlabel)360 mac_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
361     struct label *intlabel)
362 {
363 	int error;
364 
365 	ASSERT_VOP_LOCKED(vp, "mac_vnode_setlabel_extattr");
366 
367 	error = VOP_OPENEXTATTR(vp, cred, curthread);
368 	if (error == EOPNOTSUPP) {
369 		if (ea_warn_once == 0) {
370 			printf("Warning: transactions not supported "
371 			    "in EA write.\n");
372 			ea_warn_once = 1;
373 		}
374 	} else if (error)
375 		return (error);
376 
377 	MAC_POLICY_CHECK(vnode_setlabel_extattr, cred, vp, vp->v_label,
378 	    intlabel);
379 
380 	if (error) {
381 		VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
382 		return (error);
383 	}
384 
385 	error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
386 	if (error == EOPNOTSUPP)
387 		error = 0;
388 
389 	return (error);
390 }
391 
392 void
mac_vnode_execve_transition(struct ucred * old,struct ucred * new,struct vnode * vp,struct label * interpvplabel,struct image_params * imgp)393 mac_vnode_execve_transition(struct ucred *old, struct ucred *new,
394     struct vnode *vp, struct label *interpvplabel, struct image_params *imgp)
395 {
396 
397 	ASSERT_VOP_LOCKED(vp, "mac_vnode_execve_transition");
398 
399 	MAC_POLICY_PERFORM(vnode_execve_transition, old, new, vp,
400 	    vp->v_label, interpvplabel, imgp, imgp->execlabel);
401 }
402 
403 int
mac_vnode_execve_will_transition(struct ucred * old,struct vnode * vp,struct label * interpvplabel,struct image_params * imgp)404 mac_vnode_execve_will_transition(struct ucred *old, struct vnode *vp,
405     struct label *interpvplabel, struct image_params *imgp)
406 {
407 	int result;
408 
409 	ASSERT_VOP_LOCKED(vp, "mac_vnode_execve_will_transition");
410 
411 	result = 0;
412 	/* No sleeping since the process lock will be held by the caller. */
413 	MAC_POLICY_BOOLEAN_NOSLEEP(vnode_execve_will_transition, ||, old, vp,
414 	    vp->v_label, interpvplabel, imgp, imgp->execlabel);
415 
416 	return (result);
417 }
418 
419 MAC_CHECK_PROBE_DEFINE3(vnode_check_access, "struct ucred *",
420     "struct vnode *", "accmode_t");
421 
422 int
mac_vnode_check_access_impl(struct ucred * cred,struct vnode * vp,accmode_t accmode)423 mac_vnode_check_access_impl(struct ucred *cred, struct vnode *vp, accmode_t accmode)
424 {
425 	int error;
426 
427 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_access");
428 
429 	MAC_POLICY_CHECK(vnode_check_access, cred, vp, vp->v_label, accmode);
430 	MAC_CHECK_PROBE3(vnode_check_access, error, cred, vp, accmode);
431 
432 	return (error);
433 }
434 
435 MAC_CHECK_PROBE_DEFINE2(vnode_check_chdir, "struct ucred *",
436     "struct vnode *");
437 
438 int
mac_vnode_check_chdir(struct ucred * cred,struct vnode * dvp)439 mac_vnode_check_chdir(struct ucred *cred, struct vnode *dvp)
440 {
441 	int error;
442 
443 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_chdir");
444 
445 	MAC_POLICY_CHECK(vnode_check_chdir, cred, dvp, dvp->v_label);
446 	MAC_CHECK_PROBE2(vnode_check_chdir, error, cred, dvp);
447 
448 	return (error);
449 }
450 
451 MAC_CHECK_PROBE_DEFINE2(vnode_check_chroot, "struct ucred *",
452     "struct vnode *");
453 
454 int
mac_vnode_check_chroot(struct ucred * cred,struct vnode * dvp)455 mac_vnode_check_chroot(struct ucred *cred, struct vnode *dvp)
456 {
457 	int error;
458 
459 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_chroot");
460 
461 	MAC_POLICY_CHECK(vnode_check_chroot, cred, dvp, dvp->v_label);
462 	MAC_CHECK_PROBE2(vnode_check_chroot, error, cred, dvp);
463 
464 	return (error);
465 }
466 
467 MAC_CHECK_PROBE_DEFINE4(vnode_check_create, "struct ucred *",
468     "struct vnode *", "struct componentname *", "struct vattr *");
469 
470 int
mac_vnode_check_create(struct ucred * cred,struct vnode * dvp,struct componentname * cnp,struct vattr * vap)471 mac_vnode_check_create(struct ucred *cred, struct vnode *dvp,
472     struct componentname *cnp, struct vattr *vap)
473 {
474 	int error;
475 
476 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_create");
477 
478 	MAC_POLICY_CHECK(vnode_check_create, cred, dvp, dvp->v_label, cnp,
479 	    vap);
480 	MAC_CHECK_PROBE4(vnode_check_create, error, cred, dvp, cnp, vap);
481 
482 	return (error);
483 }
484 
485 MAC_CHECK_PROBE_DEFINE3(vnode_check_deleteacl, "struct ucred *",
486     "struct vnode *", "acl_type_t");
487 
488 int
mac_vnode_check_deleteacl(struct ucred * cred,struct vnode * vp,acl_type_t type)489 mac_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
490     acl_type_t type)
491 {
492 	int error;
493 
494 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_deleteacl");
495 
496 	MAC_POLICY_CHECK(vnode_check_deleteacl, cred, vp, vp->v_label, type);
497 	MAC_CHECK_PROBE3(vnode_check_deleteacl, error, cred, vp, type);
498 
499 	return (error);
500 }
501 
502 MAC_CHECK_PROBE_DEFINE4(vnode_check_deleteextattr, "struct ucred *",
503     "struct vnode *", "int", "const char *");
504 
505 int
mac_vnode_check_deleteextattr(struct ucred * cred,struct vnode * vp,int attrnamespace,const char * name)506 mac_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
507     int attrnamespace, const char *name)
508 {
509 	int error;
510 
511 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_deleteextattr");
512 
513 	MAC_POLICY_CHECK(vnode_check_deleteextattr, cred, vp, vp->v_label,
514 	    attrnamespace, name);
515 	MAC_CHECK_PROBE4(vnode_check_deleteextattr, error, cred, vp,
516 	    attrnamespace, name);
517 
518 	return (error);
519 }
520 
521 MAC_CHECK_PROBE_DEFINE3(vnode_check_exec, "struct ucred *", "struct vnode *",
522     "struct image_params *");
523 
524 int
mac_vnode_check_exec(struct ucred * cred,struct vnode * vp,struct image_params * imgp)525 mac_vnode_check_exec(struct ucred *cred, struct vnode *vp,
526     struct image_params *imgp)
527 {
528 	int error;
529 
530 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_exec");
531 
532 	MAC_POLICY_CHECK(vnode_check_exec, cred, vp, vp->v_label, imgp,
533 	    imgp->execlabel);
534 	MAC_CHECK_PROBE3(vnode_check_exec, error, cred, vp, imgp);
535 
536 	return (error);
537 }
538 
539 MAC_CHECK_PROBE_DEFINE3(vnode_check_getacl, "struct ucred *",
540     "struct vnode *", "acl_type_t");
541 
542 int
mac_vnode_check_getacl(struct ucred * cred,struct vnode * vp,acl_type_t type)543 mac_vnode_check_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
544 {
545 	int error;
546 
547 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_getacl");
548 
549 	MAC_POLICY_CHECK(vnode_check_getacl, cred, vp, vp->v_label, type);
550 	MAC_CHECK_PROBE3(vnode_check_getacl, error, cred, vp, type);
551 
552 	return (error);
553 }
554 
555 MAC_CHECK_PROBE_DEFINE4(vnode_check_getextattr, "struct ucred *",
556     "struct vnode *", "int", "const char *");
557 
558 int
mac_vnode_check_getextattr(struct ucred * cred,struct vnode * vp,int attrnamespace,const char * name)559 mac_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
560     int attrnamespace, const char *name)
561 {
562 	int error;
563 
564 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_getextattr");
565 
566 	MAC_POLICY_CHECK(vnode_check_getextattr, cred, vp, vp->v_label,
567 	    attrnamespace, name);
568 	MAC_CHECK_PROBE4(vnode_check_getextattr, error, cred, vp,
569 	    attrnamespace, name);
570 
571 	return (error);
572 }
573 
574 MAC_CHECK_PROBE_DEFINE4(vnode_check_link, "struct ucred *", "struct vnode *",
575     "struct vnode *", "struct componentname *");
576 
577 int
mac_vnode_check_link(struct ucred * cred,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)578 mac_vnode_check_link(struct ucred *cred, struct vnode *dvp,
579     struct vnode *vp, struct componentname *cnp)
580 {
581 	int error;
582 
583 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_link");
584 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_link");
585 
586 	MAC_POLICY_CHECK(vnode_check_link, cred, dvp, dvp->v_label, vp,
587 	    vp->v_label, cnp);
588 	MAC_CHECK_PROBE4(vnode_check_link, error, cred, dvp, vp, cnp);
589 
590 	return (error);
591 }
592 
593 MAC_CHECK_PROBE_DEFINE3(vnode_check_listextattr, "struct ucred *",
594     "struct vnode *", "int");
595 
596 int
mac_vnode_check_listextattr(struct ucred * cred,struct vnode * vp,int attrnamespace)597 mac_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
598     int attrnamespace)
599 {
600 	int error;
601 
602 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_listextattr");
603 
604 	MAC_POLICY_CHECK(vnode_check_listextattr, cred, vp, vp->v_label,
605 	    attrnamespace);
606 	MAC_CHECK_PROBE3(vnode_check_listextattr, error, cred, vp,
607 	    attrnamespace);
608 
609 	return (error);
610 }
611 
612 MAC_CHECK_PROBE_DEFINE3(vnode_check_lookup, "struct ucred *",
613     "struct vnode *", "struct componentname *");
614 
615 int
mac_vnode_check_lookup_impl(struct ucred * cred,struct vnode * dvp,struct componentname * cnp)616 mac_vnode_check_lookup_impl(struct ucred *cred, struct vnode *dvp,
617     struct componentname *cnp)
618 {
619 	int error;
620 
621 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_lookup");
622 
623 	if ((cnp->cn_flags & NOMACCHECK) != 0)
624 		return (0);
625 	MAC_POLICY_CHECK(vnode_check_lookup, cred, dvp, dvp->v_label, cnp);
626 	MAC_CHECK_PROBE3(vnode_check_lookup, error, cred, dvp, cnp);
627 
628 	return (error);
629 }
630 
631 MAC_CHECK_PROBE_DEFINE4(vnode_check_mmap, "struct ucred *", "struct vnode *",
632     "int", "int");
633 
634 int
mac_vnode_check_mmap_impl(struct ucred * cred,struct vnode * vp,int prot,int flags)635 mac_vnode_check_mmap_impl(struct ucred *cred, struct vnode *vp, int prot,
636     int flags)
637 {
638 	int error;
639 
640 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_mmap");
641 
642 	MAC_POLICY_CHECK(vnode_check_mmap, cred, vp, vp->v_label, prot, flags);
643 	MAC_CHECK_PROBE4(vnode_check_mmap, error, cred, vp, prot, flags);
644 
645 	return (error);
646 }
647 
648 void
mac_vnode_check_mmap_downgrade(struct ucred * cred,struct vnode * vp,int * prot)649 mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
650     int *prot)
651 {
652 	int result = *prot;
653 
654 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_mmap_downgrade");
655 
656 	MAC_POLICY_PERFORM(vnode_check_mmap_downgrade, cred, vp, vp->v_label,
657 	    &result);
658 
659 	*prot = result;
660 }
661 
662 MAC_CHECK_PROBE_DEFINE3(vnode_check_mprotect, "struct ucred *",
663     "struct vnode *", "int");
664 
665 int
mac_vnode_check_mprotect(struct ucred * cred,struct vnode * vp,int prot)666 mac_vnode_check_mprotect(struct ucred *cred, struct vnode *vp, int prot)
667 {
668 	int error;
669 
670 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_mprotect");
671 
672 	MAC_POLICY_CHECK(vnode_check_mprotect, cred, vp, vp->v_label, prot);
673 	MAC_CHECK_PROBE3(vnode_check_mprotect, error, cred, vp, prot);
674 
675 	return (error);
676 }
677 
678 MAC_CHECK_PROBE_DEFINE3(vnode_check_open, "struct ucred *", "struct vnode *",
679     "accmode_t");
680 
681 int
mac_vnode_check_open_impl(struct ucred * cred,struct vnode * vp,accmode_t accmode)682 mac_vnode_check_open_impl(struct ucred *cred, struct vnode *vp, accmode_t accmode)
683 {
684 	int error;
685 
686 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_open");
687 
688 	MAC_POLICY_CHECK(vnode_check_open, cred, vp, vp->v_label, accmode);
689 	MAC_CHECK_PROBE3(vnode_check_open, error, cred, vp, accmode);
690 
691 	return (error);
692 }
693 
694 MAC_CHECK_PROBE_DEFINE3(vnode_check_poll, "struct ucred *", "struct ucred *",
695     "struct vnode *");
696 
697 int
mac_vnode_check_poll(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp)698 mac_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred,
699     struct vnode *vp)
700 {
701 	int error;
702 
703 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_poll");
704 
705 	MAC_POLICY_CHECK(vnode_check_poll, active_cred, file_cred, vp,
706 	    vp->v_label);
707 	MAC_CHECK_PROBE3(vnode_check_poll, error, active_cred, file_cred,
708 	    vp);
709 
710 	return (error);
711 }
712 
713 MAC_CHECK_PROBE_DEFINE3(vnode_check_read, "struct ucred *", "struct ucred *",
714     "struct vnode *");
715 
716 int
mac_vnode_check_read_impl(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp)717 mac_vnode_check_read_impl(struct ucred *active_cred, struct ucred *file_cred,
718     struct vnode *vp)
719 {
720 	int error;
721 
722 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_read");
723 
724 	MAC_POLICY_CHECK(vnode_check_read, active_cred, file_cred, vp,
725 	    vp->v_label);
726 	MAC_CHECK_PROBE3(vnode_check_read, error, active_cred, file_cred,
727 	    vp);
728 
729 	return (error);
730 }
731 
732 MAC_CHECK_PROBE_DEFINE2(vnode_check_readdir, "struct ucred *",
733     "struct vnode *");
734 
735 int
mac_vnode_check_readdir(struct ucred * cred,struct vnode * dvp)736 mac_vnode_check_readdir(struct ucred *cred, struct vnode *dvp)
737 {
738 	int error;
739 
740 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_readdir");
741 
742 	MAC_POLICY_CHECK(vnode_check_readdir, cred, dvp, dvp->v_label);
743 	MAC_CHECK_PROBE2(vnode_check_readdir, error, cred, dvp);
744 
745 	return (error);
746 }
747 
748 MAC_CHECK_PROBE_DEFINE2(vnode_check_readlink, "struct ucred *",
749     "struct vnode *");
750 
751 int
mac_vnode_check_readlink_impl(struct ucred * cred,struct vnode * vp)752 mac_vnode_check_readlink_impl(struct ucred *cred, struct vnode *vp)
753 {
754 	int error;
755 
756 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_readlink");
757 
758 	MAC_POLICY_CHECK(vnode_check_readlink, cred, vp, vp->v_label);
759 	MAC_CHECK_PROBE2(vnode_check_readlink, error, cred, vp);
760 
761 	return (error);
762 }
763 
764 MAC_CHECK_PROBE_DEFINE3(vnode_check_relabel, "struct ucred *",
765     "struct vnode *", "struct label *");
766 
767 static int
mac_vnode_check_relabel(struct ucred * cred,struct vnode * vp,struct label * newlabel)768 mac_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
769     struct label *newlabel)
770 {
771 	int error;
772 
773 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_relabel");
774 
775 	MAC_POLICY_CHECK(vnode_check_relabel, cred, vp, vp->v_label, newlabel);
776 	MAC_CHECK_PROBE3(vnode_check_relabel, error, cred, vp, newlabel);
777 
778 	return (error);
779 }
780 
781 MAC_CHECK_PROBE_DEFINE4(vnode_check_rename_from, "struct ucred *",
782     "struct vnode *", "struct vnode *", "struct componentname *");
783 
784 int
mac_vnode_check_rename_from(struct ucred * cred,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)785 mac_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
786     struct vnode *vp, struct componentname *cnp)
787 {
788 	int error;
789 
790 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_rename_from");
791 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_rename_from");
792 
793 	MAC_POLICY_CHECK(vnode_check_rename_from, cred, dvp, dvp->v_label, vp,
794 	    vp->v_label, cnp);
795 	MAC_CHECK_PROBE4(vnode_check_rename_from, error, cred, dvp, vp, cnp);
796 
797 	return (error);
798 }
799 
800 MAC_CHECK_PROBE_DEFINE4(vnode_check_rename_to, "struct ucred *",
801     "struct vnode *", "struct vnode *", "struct componentname *");
802 
803 int
mac_vnode_check_rename_to(struct ucred * cred,struct vnode * dvp,struct vnode * vp,int samedir,struct componentname * cnp)804 mac_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
805     struct vnode *vp, int samedir, struct componentname *cnp)
806 {
807 	int error;
808 
809 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_rename_to");
810 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_rename_to");
811 
812 	MAC_POLICY_CHECK(vnode_check_rename_to, cred, dvp, dvp->v_label, vp,
813 	    vp != NULL ? vp->v_label : NULL, samedir, cnp);
814 	MAC_CHECK_PROBE4(vnode_check_rename_to, error, cred, dvp, vp, cnp);
815 	return (error);
816 }
817 
818 MAC_CHECK_PROBE_DEFINE2(vnode_check_revoke, "struct ucred *",
819     "struct vnode *");
820 
821 int
mac_vnode_check_revoke(struct ucred * cred,struct vnode * vp)822 mac_vnode_check_revoke(struct ucred *cred, struct vnode *vp)
823 {
824 	int error;
825 
826 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_revoke");
827 
828 	MAC_POLICY_CHECK(vnode_check_revoke, cred, vp, vp->v_label);
829 	MAC_CHECK_PROBE2(vnode_check_revoke, error, cred, vp);
830 
831 	return (error);
832 }
833 
834 MAC_CHECK_PROBE_DEFINE4(vnode_check_setacl, "struct ucred *",
835     "struct vnode *", "acl_type_t", "struct acl *");
836 
837 int
mac_vnode_check_setacl(struct ucred * cred,struct vnode * vp,acl_type_t type,struct acl * acl)838 mac_vnode_check_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
839     struct acl *acl)
840 {
841 	int error;
842 
843 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_setacl");
844 
845 	MAC_POLICY_CHECK(vnode_check_setacl, cred, vp, vp->v_label, type, acl);
846 	MAC_CHECK_PROBE4(vnode_check_setacl, error, cred, vp, type, acl);
847 
848 	return (error);
849 }
850 
851 MAC_CHECK_PROBE_DEFINE4(vnode_check_setextattr, "struct ucred *",
852     "struct vnode *", "int", "const char *");
853 
854 int
mac_vnode_check_setextattr(struct ucred * cred,struct vnode * vp,int attrnamespace,const char * name)855 mac_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
856     int attrnamespace, const char *name)
857 {
858 	int error;
859 
860 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_setextattr");
861 
862 	MAC_POLICY_CHECK(vnode_check_setextattr, cred, vp, vp->v_label,
863 	    attrnamespace, name);
864 	MAC_CHECK_PROBE4(vnode_check_setextattr, error, cred, vp,
865 	    attrnamespace, name);
866 
867 	return (error);
868 }
869 
870 MAC_CHECK_PROBE_DEFINE3(vnode_check_setflags, "struct ucred *",
871     "struct vnode *", "u_long");
872 
873 int
mac_vnode_check_setflags(struct ucred * cred,struct vnode * vp,u_long flags)874 mac_vnode_check_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
875 {
876 	int error;
877 
878 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_setflags");
879 
880 	MAC_POLICY_CHECK(vnode_check_setflags, cred, vp, vp->v_label, flags);
881 	MAC_CHECK_PROBE3(vnode_check_setflags, error, cred, vp, flags);
882 
883 	return (error);
884 }
885 
886 MAC_CHECK_PROBE_DEFINE3(vnode_check_setmode, "struct ucred *",
887     "struct vnode *", "mode_t");
888 
889 int
mac_vnode_check_setmode(struct ucred * cred,struct vnode * vp,mode_t mode)890 mac_vnode_check_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
891 {
892 	int error;
893 
894 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_setmode");
895 
896 	MAC_POLICY_CHECK(vnode_check_setmode, cred, vp, vp->v_label, mode);
897 	MAC_CHECK_PROBE3(vnode_check_setmode, error, cred, vp, mode);
898 
899 	return (error);
900 }
901 
902 MAC_CHECK_PROBE_DEFINE4(vnode_check_setowner, "struct ucred *",
903     "struct vnode *", "uid_t", "gid_t");
904 
905 int
mac_vnode_check_setowner(struct ucred * cred,struct vnode * vp,uid_t uid,gid_t gid)906 mac_vnode_check_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
907     gid_t gid)
908 {
909 	int error;
910 
911 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_setowner");
912 
913 	MAC_POLICY_CHECK(vnode_check_setowner, cred, vp, vp->v_label, uid, gid);
914 	MAC_CHECK_PROBE4(vnode_check_setowner, error, cred, vp, uid, gid);
915 
916 	return (error);
917 }
918 
919 MAC_CHECK_PROBE_DEFINE4(vnode_check_setutimes, "struct ucred *",
920     "struct vnode *", "struct timespec *", "struct timespec *");
921 
922 int
mac_vnode_check_setutimes(struct ucred * cred,struct vnode * vp,struct timespec atime,struct timespec mtime)923 mac_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
924     struct timespec atime, struct timespec mtime)
925 {
926 	int error;
927 
928 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_setutimes");
929 
930 	MAC_POLICY_CHECK(vnode_check_setutimes, cred, vp, vp->v_label, atime,
931 	    mtime);
932 	MAC_CHECK_PROBE4(vnode_check_setutimes, error, cred, vp, &atime,
933 	    &mtime);
934 
935 	return (error);
936 }
937 
938 MAC_CHECK_PROBE_DEFINE3(vnode_check_stat, "struct ucred *", "struct ucred *",
939     "struct vnode *");
940 
941 int
mac_vnode_check_stat_impl(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp)942 mac_vnode_check_stat_impl(struct ucred *active_cred, struct ucred *file_cred,
943     struct vnode *vp)
944 {
945 	int error;
946 
947 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_stat");
948 
949 	MAC_POLICY_CHECK(vnode_check_stat, active_cred, file_cred, vp,
950 	    vp->v_label);
951 	MAC_CHECK_PROBE3(vnode_check_stat, error, active_cred, file_cred,
952 	    vp);
953 
954 	return (error);
955 }
956 
957 MAC_CHECK_PROBE_DEFINE4(vnode_check_unlink, "struct ucred *",
958     "struct vnode *", "struct vnode *", "struct componentname *");
959 
960 int
mac_vnode_check_unlink(struct ucred * cred,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)961 mac_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
962     struct vnode *vp, struct componentname *cnp)
963 {
964 	int error;
965 
966 	ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_unlink");
967 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_unlink");
968 
969 	MAC_POLICY_CHECK(vnode_check_unlink, cred, dvp, dvp->v_label, vp,
970 	    vp->v_label, cnp);
971 	MAC_CHECK_PROBE4(vnode_check_unlink, error, cred, dvp, vp, cnp);
972 
973 	return (error);
974 }
975 
976 MAC_CHECK_PROBE_DEFINE3(vnode_check_write, "struct ucred *",
977     "struct ucred *", "struct vnode *");
978 
979 int
mac_vnode_check_write_impl(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp)980 mac_vnode_check_write_impl(struct ucred *active_cred, struct ucred *file_cred,
981     struct vnode *vp)
982 {
983 	int error;
984 
985 	ASSERT_VOP_LOCKED(vp, "mac_vnode_check_write");
986 
987 	MAC_POLICY_CHECK(vnode_check_write, active_cred, file_cred, vp,
988 	    vp->v_label);
989 	MAC_CHECK_PROBE3(vnode_check_write, error, active_cred, file_cred,
990 	    vp);
991 
992 	return (error);
993 }
994 
995 void
mac_vnode_relabel(struct ucred * cred,struct vnode * vp,struct label * newlabel)996 mac_vnode_relabel(struct ucred *cred, struct vnode *vp,
997     struct label *newlabel)
998 {
999 
1000 	MAC_POLICY_PERFORM(vnode_relabel, cred, vp, vp->v_label, newlabel);
1001 }
1002 
1003 void
mac_mount_create(struct ucred * cred,struct mount * mp)1004 mac_mount_create(struct ucred *cred, struct mount *mp)
1005 {
1006 
1007 	MAC_POLICY_PERFORM(mount_create, cred, mp, mp->mnt_label);
1008 }
1009 
1010 MAC_CHECK_PROBE_DEFINE2(mount_check_stat, "struct ucred *",
1011     "struct mount *");
1012 
1013 int
mac_mount_check_stat(struct ucred * cred,struct mount * mount)1014 mac_mount_check_stat(struct ucred *cred, struct mount *mount)
1015 {
1016 	int error;
1017 
1018 	MAC_POLICY_CHECK_NOSLEEP(mount_check_stat, cred, mount, mount->mnt_label);
1019 	MAC_CHECK_PROBE2(mount_check_stat, error, cred, mount);
1020 
1021 	return (error);
1022 }
1023 
1024 void
mac_devfs_create_device(struct ucred * cred,struct mount * mp,struct cdev * dev,struct devfs_dirent * de)1025 mac_devfs_create_device(struct ucred *cred, struct mount *mp,
1026     struct cdev *dev, struct devfs_dirent *de)
1027 {
1028 
1029 	MAC_POLICY_PERFORM_NOSLEEP(devfs_create_device, cred, mp, dev, de,
1030 	    de->de_label);
1031 }
1032 
1033 void
mac_devfs_create_symlink(struct ucred * cred,struct mount * mp,struct devfs_dirent * dd,struct devfs_dirent * de)1034 mac_devfs_create_symlink(struct ucred *cred, struct mount *mp,
1035     struct devfs_dirent *dd, struct devfs_dirent *de)
1036 {
1037 
1038 	MAC_POLICY_PERFORM_NOSLEEP(devfs_create_symlink, cred, mp, dd,
1039 	    dd->de_label, de, de->de_label);
1040 }
1041 
1042 void
mac_devfs_create_directory(struct mount * mp,char * dirname,int dirnamelen,struct devfs_dirent * de)1043 mac_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen,
1044     struct devfs_dirent *de)
1045 {
1046 
1047 	MAC_POLICY_PERFORM_NOSLEEP(devfs_create_directory, mp, dirname,
1048 	    dirnamelen, de, de->de_label);
1049 }
1050 
1051 /*
1052  * Implementation of VOP_SETLABEL() that relies on extended attributes to
1053  * store label data.  Can be referenced by filesystems supporting extended
1054  * attributes.
1055  */
1056 int
vop_stdsetlabel_ea(struct vop_setlabel_args * ap)1057 vop_stdsetlabel_ea(struct vop_setlabel_args *ap)
1058 {
1059 	struct vnode *vp = ap->a_vp;
1060 	struct label *intlabel = ap->a_label;
1061 	int error;
1062 
1063 	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
1064 
1065 	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
1066 		return (EOPNOTSUPP);
1067 
1068 	error = mac_vnode_setlabel_extattr(ap->a_cred, vp, intlabel);
1069 	if (error)
1070 		return (error);
1071 
1072 	/*
1073 	 * XXXRW: See the comment below in vn_setlabel() as to why this might
1074 	 * be the wrong place to call mac_vnode_relabel().
1075 	 */
1076 	mac_vnode_relabel(ap->a_cred, vp, intlabel);
1077 
1078 	return (0);
1079 }
1080 
1081 int
vn_setlabel(struct vnode * vp,struct label * intlabel,struct ucred * cred)1082 vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
1083 {
1084 	int error;
1085 
1086 	if (vp->v_mount == NULL) {
1087 		/* printf("vn_setlabel: null v_mount\n"); */
1088 		if (vp->v_type != VNON)
1089 			printf("vn_setlabel: null v_mount with non-VNON\n");
1090 		return (EBADF);
1091 	}
1092 
1093 	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
1094 		return (EOPNOTSUPP);
1095 
1096 	/*
1097 	 * Multi-phase commit.  First check the policies to confirm the
1098 	 * change is OK.  Then commit via the filesystem.  Finally, update
1099 	 * the actual vnode label.
1100 	 */
1101 	error = mac_vnode_check_relabel(cred, vp, intlabel);
1102 	if (error)
1103 		return (error);
1104 
1105 	/*
1106 	 * VADMIN provides the opportunity for the filesystem to make
1107 	 * decisions about who is and is not able to modify labels and
1108 	 * protections on files.  This might not be right.  We can't assume
1109 	 * VOP_SETLABEL() will do it, because we might implement that as part
1110 	 * of vop_stdsetlabel_ea().
1111 	 */
1112 	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
1113 	if (error)
1114 		return (error);
1115 
1116 	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
1117 	if (error)
1118 		return (error);
1119 
1120 	/*
1121 	 * It would be more symmetric if mac_vnode_relabel() was called here
1122 	 * rather than in VOP_SETLABEL(), but we don't for historical reasons.
1123 	 * We should think about moving it so that the filesystem is
1124 	 * responsible only for persistence in VOP_SETLABEL(), not the vnode
1125 	 * label update itself.
1126 	 */
1127 
1128 	return (0);
1129 }
1130 
1131 #ifdef INVARIANTS
1132 void
mac_vnode_assert_locked(struct vnode * vp,const char * func)1133 mac_vnode_assert_locked(struct vnode *vp, const char *func)
1134 {
1135 
1136 	ASSERT_VOP_LOCKED(vp, func);
1137 }
1138 #endif
1139