]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_priv.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / 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 <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/jail.h>
36 #include <sys/kernel.h>
37 #include <sys/priv.h>
38 #include <sys/proc.h>
39 #include <sys/sdt.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
42
43 #include <security/mac/mac_framework.h>
44
45 /*
46  * `suser_enabled' (which can be set by the security.bsd.suser_enabled
47  * sysctl) determines whether the system 'super-user' policy is in effect.  If
48  * it is nonzero, an effective uid of 0 connotes special privilege,
49  * overriding many mandatory and discretionary protections.  If it is zero,
50  * uid 0 is offered no special privilege in the kernel security policy.
51  * Setting it to zero may seriously impact the functionality of many existing
52  * userland programs, and should not be done without careful consideration of
53  * the consequences.
54  */
55 static int      suser_enabled = 1;
56 SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RWTUN,
57     &suser_enabled, 0, "processes with uid 0 have privilege");
58
59 static int      unprivileged_mlock = 1;
60 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_mlock, CTLFLAG_RWTUN,
61     &unprivileged_mlock, 0, "Allow non-root users to call mlock(2)");
62
63 static int      unprivileged_read_msgbuf = 1;
64 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
65     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
66     "Unprivileged processes may read the kernel message buffer");
67
68 SDT_PROVIDER_DEFINE(priv);
69 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv__ok, "int");
70 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv__err, "int");
71
72 /*
73  * Check a credential for privilege.  Lots of good reasons to deny privilege;
74  * only a few to grant it.
75  */
76 int
77 priv_check_cred(struct ucred *cred, int priv, int flags)
78 {
79         int error;
80
81         KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d",
82             priv));
83
84         /*
85          * We first evaluate policies that may deny the granting of
86          * privilege unilaterally.
87          */
88 #ifdef MAC
89         error = mac_priv_check(cred, priv);
90         if (error)
91                 goto out;
92 #endif
93
94         /*
95          * Jail policy will restrict certain privileges that may otherwise be
96          * be granted.
97          */
98         error = prison_priv_check(cred, priv);
99         if (error)
100                 goto out;
101
102         if (unprivileged_mlock) {
103                 /*
104                  * Allow unprivileged users to call mlock(2)/munlock(2) and
105                  * mlockall(2)/munlockall(2).
106                  */
107                 switch (priv) {
108                 case PRIV_VM_MLOCK:
109                 case PRIV_VM_MUNLOCK:
110                         error = 0;
111                         goto out;
112                 }
113         }
114
115         if (unprivileged_read_msgbuf) {
116                 /*
117                  * Allow an unprivileged user to read the kernel message
118                  * buffer.
119                  */
120                 if (priv == PRIV_MSGBUF) {
121                         error = 0;
122                         goto out;
123                 }
124         }
125
126         /*
127          * Having determined if privilege is restricted by various policies,
128          * now determine if privilege is granted.  At this point, any policy
129          * may grant privilege.  For now, we allow short-circuit boolean
130          * evaluation, so may not call all policies.  Perhaps we should.
131          *
132          * Superuser policy grants privilege based on the effective (or in
133          * the case of specific privileges, real) uid being 0.  We allow the
134          * superuser policy to be globally disabled, although this is
135          * currenty of limited utility.
136          */
137         if (suser_enabled) {
138                 switch (priv) {
139                 case PRIV_MAXFILES:
140                 case PRIV_MAXPROC:
141                 case PRIV_PROC_LIMIT:
142                         if (cred->cr_ruid == 0) {
143                                 error = 0;
144                                 goto out;
145                         }
146                         break;
147                 default:
148                         if (cred->cr_uid == 0) {
149                                 error = 0;
150                                 goto out;
151                         }
152                         break;
153                 }
154         }
155
156         /*
157          * Writes to kernel/physical memory are a typical root-only operation,
158          * but non-root users are expected to be able to read it (provided they
159          * have permission to access /dev/[k]mem).
160          */
161         if (priv == PRIV_KMEM_READ) {
162                 error = 0;
163                 goto out;
164         }
165
166         /*
167          * Now check with MAC, if enabled, to see if a policy module grants
168          * privilege.
169          */
170 #ifdef MAC
171         if (mac_priv_grant(cred, priv) == 0) {
172                 error = 0;
173                 goto out;
174         }
175 #endif
176
177         /*
178          * The default is deny, so if no policies have granted it, reject
179          * with a privilege error here.
180          */
181         error = EPERM;
182 out:
183         if (error)
184                 SDT_PROBE1(priv, kernel, priv_check, priv__err, priv);
185         else
186                 SDT_PROBE1(priv, kernel, priv_check, priv__ok, priv);
187         return (error);
188 }
189
190 int
191 priv_check(struct thread *td, int priv)
192 {
193
194         KASSERT(td == curthread, ("priv_check: td != curthread"));
195
196         return (priv_check_cred(td->td_ucred, priv, 0));
197 }