xref: /freebsd/sys/security/mac_bsdextended/mac_bsdextended.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * Copyright (c) 1999-2002, 2007-2008 Robert N. M. Watson
3  * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
4  * Copyright (c) 2005 Tom Rhodes
5  * Copyright (c) 2006 SPARTA, Inc.
6  * All rights reserved.
7  *
8  * This software was developed by Robert Watson for the TrustedBSD Project.
9  * It was later enhanced by Tom Rhodes for the TrustedBSD Project.
10  *
11  * This software was developed for the FreeBSD Project in part by Network
12  * Associates Laboratories, the Security Research Division of Network
13  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
14  * as part of the DARPA CHATS research program.
15  *
16  * This software was enhanced by SPARTA ISSO under SPAWAR contract
17  * N66001-04-C-6019 ("SEFOS").
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
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.
27  *
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
38  * SUCH DAMAGE.
39  */
40 
41 /*
42  * Developed by the TrustedBSD Project.
43  *
44  * "BSD Extended" MAC policy, allowing the administrator to impose mandatory
45  * firewall-like rules regarding users and file system objects.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/acl.h>
50 #include <sys/kernel.h>
51 #include <sys/jail.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/priv.h>
58 #include <sys/proc.h>
59 #include <sys/systm.h>
60 #include <sys/vnode.h>
61 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
63 #include <sys/stat.h>
64 
65 #include <security/mac/mac_policy.h>
66 #include <security/mac_bsdextended/mac_bsdextended.h>
67 #include <security/mac_bsdextended/ugidfw_internal.h>
68 
69 static struct mtx ugidfw_mtx;
70 
71 SYSCTL_DECL(_security_mac);
72 
73 static SYSCTL_NODE(_security_mac, OID_AUTO, bsdextended,
74     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
75     "TrustedBSD extended BSD MAC policy controls");
76 
77 static int	ugidfw_enabled = 1;
78 SYSCTL_INT(_security_mac_bsdextended, OID_AUTO, enabled, CTLFLAG_RWTUN,
79     &ugidfw_enabled, 0, "Enforce extended BSD policy");
80 
81 static MALLOC_DEFINE(M_MACBSDEXTENDED, "mac_bsdextended",
82     "BSD Extended MAC rule");
83 
84 #define	MAC_BSDEXTENDED_MAXRULES	250
85 static struct mac_bsdextended_rule *rules[MAC_BSDEXTENDED_MAXRULES];
86 static int rule_count = 0;
87 static int rule_slots = 0;
88 static int rule_version = MB_VERSION;
89 
90 SYSCTL_INT(_security_mac_bsdextended, OID_AUTO, rule_count, CTLFLAG_RD,
91     &rule_count, 0, "Number of defined rules\n");
92 SYSCTL_INT(_security_mac_bsdextended, OID_AUTO, rule_slots, CTLFLAG_RD,
93     &rule_slots, 0, "Number of used rule slots\n");
94 SYSCTL_INT(_security_mac_bsdextended, OID_AUTO, rule_version, CTLFLAG_RD,
95     &rule_version, 0, "Version number for API\n");
96 
97 /*
98  * This is just used for logging purposes, eventually we would like to log
99  * much more then failed requests.
100  */
101 static int ugidfw_logging;
102 SYSCTL_INT(_security_mac_bsdextended, OID_AUTO, logging, CTLFLAG_RW,
103     &ugidfw_logging, 0, "Log failed authorization requests");
104 
105 /*
106  * This tunable is here for compatibility.  It will allow the user to switch
107  * between the new mode (first rule matches) and the old functionality (all
108  * rules match).
109  */
110 static int ugidfw_firstmatch_enabled;
111 SYSCTL_INT(_security_mac_bsdextended, OID_AUTO, firstmatch_enabled,
112     CTLFLAG_RW, &ugidfw_firstmatch_enabled, 1,
113     "Disable/enable match first rule functionality");
114 
115 static int
116 ugidfw_rule_valid(struct mac_bsdextended_rule *rule)
117 {
118 
119 	if ((rule->mbr_subject.mbs_flags | MBS_ALL_FLAGS) != MBS_ALL_FLAGS)
120 		return (EINVAL);
121 	if ((rule->mbr_subject.mbs_neg | MBS_ALL_FLAGS) != MBS_ALL_FLAGS)
122 		return (EINVAL);
123 	if ((rule->mbr_object.mbo_flags | MBO_ALL_FLAGS) != MBO_ALL_FLAGS)
124 		return (EINVAL);
125 	if ((rule->mbr_object.mbo_neg | MBO_ALL_FLAGS) != MBO_ALL_FLAGS)
126 		return (EINVAL);
127 	if (((rule->mbr_object.mbo_flags & MBO_TYPE_DEFINED) != 0) &&
128 	    (rule->mbr_object.mbo_type | MBO_ALL_TYPE) != MBO_ALL_TYPE)
129 		return (EINVAL);
130 	if ((rule->mbr_mode | MBI_ALLPERM) != MBI_ALLPERM)
131 		return (EINVAL);
132 	return (0);
133 }
134 
135 static int
136 sysctl_rule(SYSCTL_HANDLER_ARGS)
137 {
138 	struct mac_bsdextended_rule temprule, *ruleptr;
139 	u_int namelen;
140 	int error, index, *name;
141 
142 	error = 0;
143 	name = (int *)arg1;
144 	namelen = arg2;
145 	if (namelen != 1)
146 		return (EINVAL);
147 	index = name[0];
148         if (index >= MAC_BSDEXTENDED_MAXRULES)
149 		return (ENOENT);
150 
151 	ruleptr = NULL;
152 	if (req->newptr && req->newlen != 0) {
153 		error = SYSCTL_IN(req, &temprule, sizeof(temprule));
154 		if (error)
155 			return (error);
156 		ruleptr = malloc(sizeof(*ruleptr), M_MACBSDEXTENDED,
157 		    M_WAITOK | M_ZERO);
158 	}
159 
160 	mtx_lock(&ugidfw_mtx);
161 	if (req->oldptr) {
162 		if (index < 0 || index > rule_slots + 1) {
163 			error = ENOENT;
164 			goto out;
165 		}
166 		if (rules[index] == NULL) {
167 			error = ENOENT;
168 			goto out;
169 		}
170 		temprule = *rules[index];
171 	}
172 	if (req->newptr && req->newlen == 0) {
173 		KASSERT(ruleptr == NULL, ("sysctl_rule: ruleptr != NULL"));
174 		ruleptr = rules[index];
175 		if (ruleptr == NULL) {
176 			error = ENOENT;
177 			goto out;
178 		}
179 		rule_count--;
180 		rules[index] = NULL;
181 	} else if (req->newptr) {
182 		error = ugidfw_rule_valid(&temprule);
183 		if (error)
184 			goto out;
185 		if (rules[index] == NULL) {
186 			*ruleptr = temprule;
187 			rules[index] = ruleptr;
188 			ruleptr = NULL;
189 			if (index + 1 > rule_slots)
190 				rule_slots = index + 1;
191 			rule_count++;
192 		} else
193 			*rules[index] = temprule;
194 	}
195 out:
196 	mtx_unlock(&ugidfw_mtx);
197 	if (ruleptr != NULL)
198 		free(ruleptr, M_MACBSDEXTENDED);
199 	if (req->oldptr && error == 0)
200 		error = SYSCTL_OUT(req, &temprule, sizeof(temprule));
201 	return (error);
202 }
203 
204 static SYSCTL_NODE(_security_mac_bsdextended, OID_AUTO, rules,
205     CTLFLAG_MPSAFE | CTLFLAG_RW, sysctl_rule, "BSD extended MAC rules");
206 
207 static void
208 ugidfw_init(struct mac_policy_conf *mpc)
209 {
210 
211 	mtx_init(&ugidfw_mtx, "mac_bsdextended lock", NULL, MTX_DEF);
212 }
213 
214 static void
215 ugidfw_destroy(struct mac_policy_conf *mpc)
216 {
217 	int i;
218 
219 	for (i = 0; i < MAC_BSDEXTENDED_MAXRULES; i++) {
220 		if (rules[i] != NULL)
221 			free(rules[i], M_MACBSDEXTENDED);
222 	}
223 	mtx_destroy(&ugidfw_mtx);
224 }
225 
226 static int
227 ugidfw_rulecheck(struct mac_bsdextended_rule *rule,
228     struct ucred *cred, struct vnode *vp, struct vattr *vap, int acc_mode)
229 {
230 	int mac_granted, match, priv_granted;
231 	int i;
232 
233 	/*
234 	 * Is there a subject match?
235 	 */
236 	mtx_assert(&ugidfw_mtx, MA_OWNED);
237 	if (rule->mbr_subject.mbs_flags & MBS_UID_DEFINED) {
238 		match =  ((cred->cr_uid <= rule->mbr_subject.mbs_uid_max &&
239 		    cred->cr_uid >= rule->mbr_subject.mbs_uid_min) ||
240 		    (cred->cr_ruid <= rule->mbr_subject.mbs_uid_max &&
241 		    cred->cr_ruid >= rule->mbr_subject.mbs_uid_min) ||
242 		    (cred->cr_svuid <= rule->mbr_subject.mbs_uid_max &&
243 		    cred->cr_svuid >= rule->mbr_subject.mbs_uid_min));
244 		if (rule->mbr_subject.mbs_neg & MBS_UID_DEFINED)
245 			match = !match;
246 		if (!match)
247 			return (0);
248 	}
249 
250 	if (rule->mbr_subject.mbs_flags & MBS_GID_DEFINED) {
251 		match = ((cred->cr_rgid <= rule->mbr_subject.mbs_gid_max &&
252 		    cred->cr_rgid >= rule->mbr_subject.mbs_gid_min) ||
253 		    (cred->cr_svgid <= rule->mbr_subject.mbs_gid_max &&
254 		    cred->cr_svgid >= rule->mbr_subject.mbs_gid_min));
255 		if (!match) {
256 			for (i = 0; i < cred->cr_ngroups; i++) {
257 				if (cred->cr_groups[i]
258 				    <= rule->mbr_subject.mbs_gid_max &&
259 				    cred->cr_groups[i]
260 				    >= rule->mbr_subject.mbs_gid_min) {
261 					match = 1;
262 					break;
263 				}
264 			}
265 		}
266 		if (rule->mbr_subject.mbs_neg & MBS_GID_DEFINED)
267 			match = !match;
268 		if (!match)
269 			return (0);
270 	}
271 
272 	if (rule->mbr_subject.mbs_flags & MBS_PRISON_DEFINED) {
273 		match =
274 		    (cred->cr_prison->pr_id == rule->mbr_subject.mbs_prison);
275 		if (rule->mbr_subject.mbs_neg & MBS_PRISON_DEFINED)
276 			match = !match;
277 		if (!match)
278 			return (0);
279 	}
280 
281 	/*
282 	 * Is there an object match?
283 	 */
284 	if (rule->mbr_object.mbo_flags & MBO_UID_DEFINED) {
285 		match = (vap->va_uid <= rule->mbr_object.mbo_uid_max &&
286 		    vap->va_uid >= rule->mbr_object.mbo_uid_min);
287 		if (rule->mbr_object.mbo_neg & MBO_UID_DEFINED)
288 			match = !match;
289 		if (!match)
290 			return (0);
291 	}
292 
293 	if (rule->mbr_object.mbo_flags & MBO_GID_DEFINED) {
294 		match = (vap->va_gid <= rule->mbr_object.mbo_gid_max &&
295 		    vap->va_gid >= rule->mbr_object.mbo_gid_min);
296 		if (rule->mbr_object.mbo_neg & MBO_GID_DEFINED)
297 			match = !match;
298 		if (!match)
299 			return (0);
300 	}
301 
302 	if (rule->mbr_object.mbo_flags & MBO_FSID_DEFINED) {
303 		match = (fsidcmp(&vp->v_mount->mnt_stat.f_fsid,
304 		    &rule->mbr_object.mbo_fsid) == 0);
305 		if (rule->mbr_object.mbo_neg & MBO_FSID_DEFINED)
306 			match = !match;
307 		if (!match)
308 			return (0);
309 	}
310 
311 	if (rule->mbr_object.mbo_flags & MBO_SUID) {
312 		match = (vap->va_mode & S_ISUID);
313 		if (rule->mbr_object.mbo_neg & MBO_SUID)
314 			match = !match;
315 		if (!match)
316 			return (0);
317 	}
318 
319 	if (rule->mbr_object.mbo_flags & MBO_SGID) {
320 		match = (vap->va_mode & S_ISGID);
321 		if (rule->mbr_object.mbo_neg & MBO_SGID)
322 			match = !match;
323 		if (!match)
324 			return (0);
325 	}
326 
327 	if (rule->mbr_object.mbo_flags & MBO_UID_SUBJECT) {
328 		match = (vap->va_uid == cred->cr_uid ||
329 		    vap->va_uid == cred->cr_ruid ||
330 		    vap->va_uid == cred->cr_svuid);
331 		if (rule->mbr_object.mbo_neg & MBO_UID_SUBJECT)
332 			match = !match;
333 		if (!match)
334 			return (0);
335 	}
336 
337 	if (rule->mbr_object.mbo_flags & MBO_GID_SUBJECT) {
338 		match = (groupmember(vap->va_gid, cred) ||
339 		    vap->va_gid == cred->cr_rgid ||
340 		    vap->va_gid == cred->cr_svgid);
341 		if (rule->mbr_object.mbo_neg & MBO_GID_SUBJECT)
342 			match = !match;
343 		if (!match)
344 			return (0);
345 	}
346 
347 	if (rule->mbr_object.mbo_flags & MBO_TYPE_DEFINED) {
348 		switch (vap->va_type) {
349 		case VREG:
350 			match = (rule->mbr_object.mbo_type & MBO_TYPE_REG);
351 			break;
352 		case VDIR:
353 			match = (rule->mbr_object.mbo_type & MBO_TYPE_DIR);
354 			break;
355 		case VBLK:
356 			match = (rule->mbr_object.mbo_type & MBO_TYPE_BLK);
357 			break;
358 		case VCHR:
359 			match = (rule->mbr_object.mbo_type & MBO_TYPE_CHR);
360 			break;
361 		case VLNK:
362 			match = (rule->mbr_object.mbo_type & MBO_TYPE_LNK);
363 			break;
364 		case VSOCK:
365 			match = (rule->mbr_object.mbo_type & MBO_TYPE_SOCK);
366 			break;
367 		case VFIFO:
368 			match = (rule->mbr_object.mbo_type & MBO_TYPE_FIFO);
369 			break;
370 		default:
371 			match = 0;
372 		}
373 		if (rule->mbr_object.mbo_neg & MBO_TYPE_DEFINED)
374 			match = !match;
375 		if (!match)
376 			return (0);
377 	}
378 
379 	/*
380 	 * MBI_APPEND should not be here as it should get converted to
381 	 * MBI_WRITE.
382 	 */
383 	priv_granted = 0;
384 	mac_granted = rule->mbr_mode;
385 	if ((acc_mode & MBI_ADMIN) && (mac_granted & MBI_ADMIN) == 0 &&
386 	    priv_check_cred(cred, PRIV_VFS_ADMIN) == 0)
387 		priv_granted |= MBI_ADMIN;
388 	if ((acc_mode & MBI_EXEC) && (mac_granted & MBI_EXEC) == 0 &&
389 	    priv_check_cred(cred, (vap->va_type == VDIR) ? PRIV_VFS_LOOKUP : PRIV_VFS_EXEC) == 0)
390 		priv_granted |= MBI_EXEC;
391 	if ((acc_mode & MBI_READ) && (mac_granted & MBI_READ) == 0 &&
392 	    priv_check_cred(cred, PRIV_VFS_READ) == 0)
393 		priv_granted |= MBI_READ;
394 	if ((acc_mode & MBI_STAT) && (mac_granted & MBI_STAT) == 0 &&
395 	    priv_check_cred(cred, PRIV_VFS_STAT) == 0)
396 		priv_granted |= MBI_STAT;
397 	if ((acc_mode & MBI_WRITE) && (mac_granted & MBI_WRITE) == 0 &&
398 	    priv_check_cred(cred, PRIV_VFS_WRITE) == 0)
399 		priv_granted |= MBI_WRITE;
400 	/*
401 	 * Is the access permitted?
402 	 */
403 	if (((mac_granted | priv_granted) & acc_mode) != acc_mode) {
404 		if (ugidfw_logging)
405 			log(LOG_AUTHPRIV, "mac_bsdextended: %d:%d request %d"
406 			    " on %d:%d failed. \n", cred->cr_ruid,
407 			    cred->cr_rgid, acc_mode, vap->va_uid,
408 			    vap->va_gid);
409 		return (EACCES);
410 	}
411 
412 	/*
413 	 * If the rule matched, permits access, and first match is enabled,
414 	 * return success.
415 	 */
416 	if (ugidfw_firstmatch_enabled)
417 		return (EJUSTRETURN);
418 	else
419 		return (0);
420 }
421 
422 int
423 ugidfw_check(struct ucred *cred, struct vnode *vp, struct vattr *vap,
424     int acc_mode)
425 {
426 	int error, i;
427 
428 	/*
429 	 * Since we do not separately handle append, map append to write.
430 	 */
431 	if (acc_mode & MBI_APPEND) {
432 		acc_mode &= ~MBI_APPEND;
433 		acc_mode |= MBI_WRITE;
434 	}
435 	mtx_lock(&ugidfw_mtx);
436 	for (i = 0; i < rule_slots; i++) {
437 		if (rules[i] == NULL)
438 			continue;
439 		error = ugidfw_rulecheck(rules[i], cred,
440 		    vp, vap, acc_mode);
441 		if (error == EJUSTRETURN)
442 			break;
443 		if (error) {
444 			mtx_unlock(&ugidfw_mtx);
445 			return (error);
446 		}
447 	}
448 	mtx_unlock(&ugidfw_mtx);
449 	return (0);
450 }
451 
452 int
453 ugidfw_check_vp(struct ucred *cred, struct vnode *vp, int acc_mode)
454 {
455 	int error;
456 	struct vattr vap;
457 
458 	if (!ugidfw_enabled)
459 		return (0);
460 	error = VOP_GETATTR(vp, &vap, cred);
461 	if (error)
462 		return (error);
463 	return (ugidfw_check(cred, vp, &vap, acc_mode));
464 }
465 
466 int
467 ugidfw_accmode2mbi(accmode_t accmode)
468 {
469 	int mbi;
470 
471 	mbi = 0;
472 	if (accmode & VEXEC)
473 		mbi |= MBI_EXEC;
474 	if (accmode & VWRITE)
475 		mbi |= MBI_WRITE;
476 	if (accmode & VREAD)
477 		mbi |= MBI_READ;
478 	if (accmode & VADMIN_PERMS)
479 		mbi |= MBI_ADMIN;
480 	if (accmode & VSTAT_PERMS)
481 		mbi |= MBI_STAT;
482 	if (accmode & VAPPEND)
483 		mbi |= MBI_APPEND;
484 	return (mbi);
485 }
486 
487 static struct mac_policy_ops ugidfw_ops =
488 {
489 	.mpo_destroy = ugidfw_destroy,
490 	.mpo_init = ugidfw_init,
491 	.mpo_system_check_acct = ugidfw_system_check_acct,
492 	.mpo_system_check_auditctl = ugidfw_system_check_auditctl,
493 	.mpo_system_check_swapon = ugidfw_system_check_swapon,
494 	.mpo_vnode_check_access = ugidfw_vnode_check_access,
495 	.mpo_vnode_check_chdir = ugidfw_vnode_check_chdir,
496 	.mpo_vnode_check_chroot = ugidfw_vnode_check_chroot,
497 	.mpo_vnode_check_create = ugidfw_check_create_vnode,
498 	.mpo_vnode_check_deleteacl = ugidfw_vnode_check_deleteacl,
499 	.mpo_vnode_check_deleteextattr = ugidfw_vnode_check_deleteextattr,
500 	.mpo_vnode_check_exec = ugidfw_vnode_check_exec,
501 	.mpo_vnode_check_getacl = ugidfw_vnode_check_getacl,
502 	.mpo_vnode_check_getextattr = ugidfw_vnode_check_getextattr,
503 	.mpo_vnode_check_link = ugidfw_vnode_check_link,
504 	.mpo_vnode_check_listextattr = ugidfw_vnode_check_listextattr,
505 	.mpo_vnode_check_lookup = ugidfw_vnode_check_lookup,
506 	.mpo_vnode_check_open = ugidfw_vnode_check_open,
507 	.mpo_vnode_check_readdir = ugidfw_vnode_check_readdir,
508 	.mpo_vnode_check_readlink = ugidfw_vnode_check_readdlink,
509 	.mpo_vnode_check_rename_from = ugidfw_vnode_check_rename_from,
510 	.mpo_vnode_check_rename_to = ugidfw_vnode_check_rename_to,
511 	.mpo_vnode_check_revoke = ugidfw_vnode_check_revoke,
512 	.mpo_vnode_check_setacl = ugidfw_check_setacl_vnode,
513 	.mpo_vnode_check_setextattr = ugidfw_vnode_check_setextattr,
514 	.mpo_vnode_check_setflags = ugidfw_vnode_check_setflags,
515 	.mpo_vnode_check_setmode = ugidfw_vnode_check_setmode,
516 	.mpo_vnode_check_setowner = ugidfw_vnode_check_setowner,
517 	.mpo_vnode_check_setutimes = ugidfw_vnode_check_setutimes,
518 	.mpo_vnode_check_stat = ugidfw_vnode_check_stat,
519 	.mpo_vnode_check_unlink = ugidfw_vnode_check_unlink,
520 };
521 
522 MAC_POLICY_SET(&ugidfw_ops, mac_bsdextended, "TrustedBSD MAC/BSD Extended",
523     MPC_LOADTIME_FLAG_UNLOADOK, NULL);
524