FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_priv.c
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/9.2/sys/kern/kern_priv.c 245420 2013-01-14 11:13:31Z zont $");
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_RW,
59 &suser_enabled, 0, "processes with uid 0 have privilege");
60 TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
61
62 static int unprivileged_mlock = 0;
63 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_mlock, CTLFLAG_RW|CTLFLAG_TUN,
64 &unprivileged_mlock, 0, "Allow non-root users to call mlock(2)");
65 TUNABLE_INT("security.bsd.unprivileged_mlock", &unprivileged_mlock);
66
67 SDT_PROVIDER_DEFINE(priv);
68 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv_ok, priv-ok, "int");
69 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv_err, priv-err, "int");
70
71 /*
72 * Check a credential for privilege. Lots of good reasons to deny privilege;
73 * only a few to grant it.
74 */
75 int
76 priv_check_cred(struct ucred *cred, int priv, int flags)
77 {
78 int error;
79
80 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d",
81 priv));
82
83 /*
84 * We first evaluate policies that may deny the granting of
85 * privilege unilaterally.
86 */
87 #ifdef MAC
88 error = mac_priv_check(cred, priv);
89 if (error)
90 goto out;
91 #endif
92
93 /*
94 * Jail policy will restrict certain privileges that may otherwise be
95 * be granted.
96 */
97 error = prison_priv_check(cred, priv);
98 if (error)
99 goto out;
100
101 if (unprivileged_mlock) {
102 /*
103 * Allow unprivileged users to call mlock(2)/munlock(2) and
104 * mlockall(2)/munlockall(2).
105 */
106 switch (priv) {
107 case PRIV_VM_MLOCK:
108 case PRIV_VM_MUNLOCK:
109 error = 0;
110 goto out;
111 }
112 }
113
114 /*
115 * Having determined if privilege is restricted by various policies,
116 * now determine if privilege is granted. At this point, any policy
117 * may grant privilege. For now, we allow short-circuit boolean
118 * evaluation, so may not call all policies. Perhaps we should.
119 *
120 * Superuser policy grants privilege based on the effective (or in
121 * the case of specific privileges, real) uid being 0. We allow the
122 * superuser policy to be globally disabled, although this is
123 * currenty of limited utility.
124 */
125 if (suser_enabled) {
126 switch (priv) {
127 case PRIV_MAXFILES:
128 case PRIV_MAXPROC:
129 case PRIV_PROC_LIMIT:
130 if (cred->cr_ruid == 0) {
131 error = 0;
132 goto out;
133 }
134 break;
135
136 default:
137 if (cred->cr_uid == 0) {
138 error = 0;
139 goto out;
140 }
141 break;
142 }
143 }
144
145 /*
146 * Now check with MAC, if enabled, to see if a policy module grants
147 * privilege.
148 */
149 #ifdef MAC
150 if (mac_priv_grant(cred, priv) == 0) {
151 error = 0;
152 goto out;
153 }
154 #endif
155
156 /*
157 * The default is deny, so if no policies have granted it, reject
158 * with a privilege error here.
159 */
160 error = EPERM;
161 out:
162 if (error) {
163 SDT_PROBE(priv, kernel, priv_check, priv_err, priv, 0, 0, 0,
164 0);
165 } else {
166 SDT_PROBE(priv, kernel, priv_check, priv_ok, priv, 0, 0, 0,
167 0);
168 }
169 return (error);
170 }
171
172 int
173 priv_check(struct thread *td, int priv)
174 {
175
176 KASSERT(td == curthread, ("priv_check: td != curthread"));
177
178 return (priv_check_cred(td->td_ucred, priv, 0));
179 }
Cache object: 1059b73c791cd5e08495aa35ae88fa21
|