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