xref: /freebsd/sys/kern/kern_priv.c (revision c0020399a650364d0134f79f3fa319f84064372d)
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * Copyright (c) 2009 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed by Robert N. M. Watson for the TrustedBSD
7  * Project under contract to nCircle Network Security, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "opt_kdtrace.h"
32 #include "opt_mac.h"
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/jail.h>
39 #include <sys/kernel.h>
40 #include <sys/priv.h>
41 #include <sys/proc.h>
42 #include <sys/sdt.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 
46 #include <security/mac/mac_framework.h>
47 
48 /*
49  * `suser_enabled' (which can be set by the security.bsd.suser_enabled
50  * sysctl) determines whether the system 'super-user' policy is in effect.  If
51  * it is nonzero, an effective uid of 0 connotes special privilege,
52  * overriding many mandatory and discretionary protections.  If it is zero,
53  * uid 0 is offered no special privilege in the kernel security policy.
54  * Setting it to zero may seriously impact the functionality of many existing
55  * userland programs, and should not be done without careful consideration of
56  * the consequences.
57  */
58 static int	suser_enabled = 1;
59 SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW,
60     &suser_enabled, 0, "processes with uid 0 have privilege");
61 TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
62 
63 SDT_PROVIDER_DEFINE(priv);
64 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv_ok, "int");
65 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv_err, "int");
66 
67 /*
68  * Check a credential for privilege.  Lots of good reasons to deny privilege;
69  * only a few to grant it.
70  */
71 int
72 priv_check_cred(struct ucred *cred, int priv, int flags)
73 {
74 	int error;
75 
76 	KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d",
77 	    priv));
78 
79 	/*
80 	 * We first evaluate policies that may deny the granting of
81 	 * privilege unilaterally.
82 	 */
83 #ifdef MAC
84 	error = mac_priv_check(cred, priv);
85 	if (error)
86 		goto out;
87 #endif
88 
89 	/*
90 	 * Jail policy will restrict certain privileges that may otherwise be
91 	 * be granted.
92 	 */
93 	error = prison_priv_check(cred, priv);
94 	if (error)
95 		goto out;
96 
97 	/*
98 	 * Having determined if privilege is restricted by various policies,
99 	 * now determine if privilege is granted.  At this point, any policy
100 	 * may grant privilege.  For now, we allow short-circuit boolean
101 	 * evaluation, so may not call all policies.  Perhaps we should.
102 	 *
103 	 * Superuser policy grants privilege based on the effective (or in
104 	 * the case of specific privileges, real) uid being 0.  We allow the
105 	 * superuser policy to be globally disabled, although this is
106 	 * currenty of limited utility.
107 	 */
108 	if (suser_enabled) {
109 		switch (priv) {
110 		case PRIV_MAXFILES:
111 		case PRIV_MAXPROC:
112 		case PRIV_PROC_LIMIT:
113 			if (cred->cr_ruid == 0) {
114 				error = 0;
115 				goto out;
116 			}
117 			break;
118 
119 		default:
120 			if (cred->cr_uid == 0) {
121 				error = 0;
122 				goto out;
123 			}
124 			break;
125 		}
126 	}
127 
128 	/*
129 	 * Now check with MAC, if enabled, to see if a policy module grants
130 	 * privilege.
131 	 */
132 #ifdef MAC
133 	if (mac_priv_grant(cred, priv) == 0) {
134 		error = 0;
135 		goto out;
136 	}
137 #endif
138 
139 	/*
140 	 * The default is deny, so if no policies have granted it, reject
141 	 * with a privilege error here.
142 	 */
143 	error = EPERM;
144 out:
145 	if (error) {
146 		SDT_PROBE(priv, kernel, priv_check, priv_err, priv, 0, 0, 0,
147 		    0);
148 	} else {
149 		SDT_PROBE(priv, kernel, priv_check, priv_ok, priv, 0, 0, 0,
150 		    0);
151 	}
152 	return (error);
153 }
154 
155 int
156 priv_check(struct thread *td, int priv)
157 {
158 
159 	KASSERT(td == curthread, ("priv_check: td != curthread"));
160 
161 	return (priv_check_cred(td->td_ucred, priv, 0));
162 }
163