xref: /freebsd/sys/security/mac/mac_system.c (revision b00ab754)
1 /*-
2  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3  * Copyright (c) 2006 SPARTA, Inc.
4  * Copyright (c) 2007, 2009 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project in part by Network
8  * Associates Laboratories, the Security Research Division of Network
9  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
10  * as part of the DARPA CHATS research program.
11  *
12  * Portions of this software were developed by Robert Watson for the
13  * TrustedBSD Project.
14  *
15  * This software was enhanced by SPARTA ISSO under SPAWAR contract
16  * N66001-04-C-6019 ("SEFOS").
17  *
18  * This software was developed at the University of Cambridge Computer
19  * Laboratory with support from a grant from Google, Inc.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 /*
44  * MAC Framework entry points relating to overall operation of system,
45  * including global services such as the kernel environment and loadable
46  * modules.
47  *
48  * System checks often align with existing privilege checks, but provide
49  * additional security context that may be relevant to policies, such as the
50  * specific object being operated on.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include "opt_mac.h"
57 
58 #include <sys/param.h>
59 #include <sys/kernel.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/module.h>
63 #include <sys/mutex.h>
64 #include <sys/sdt.h>
65 #include <sys/systm.h>
66 #include <sys/vnode.h>
67 #include <sys/sysctl.h>
68 
69 #include <security/mac/mac_framework.h>
70 #include <security/mac/mac_internal.h>
71 #include <security/mac/mac_policy.h>
72 
73 MAC_CHECK_PROBE_DEFINE1(kenv_check_dump, "struct ucred *");
74 
75 int
76 mac_kenv_check_dump(struct ucred *cred)
77 {
78 	int error;
79 
80 	MAC_POLICY_CHECK_NOSLEEP(kenv_check_dump, cred);
81 	MAC_CHECK_PROBE1(kenv_check_dump, error, cred);
82 
83 	return (error);
84 }
85 
86 MAC_CHECK_PROBE_DEFINE2(kenv_check_get, "struct ucred *", "char *");
87 
88 int
89 mac_kenv_check_get(struct ucred *cred, char *name)
90 {
91 	int error;
92 
93 	MAC_POLICY_CHECK_NOSLEEP(kenv_check_get, cred, name);
94 	MAC_CHECK_PROBE2(kenv_check_get, error, cred, name);
95 
96 	return (error);
97 }
98 
99 MAC_CHECK_PROBE_DEFINE3(kenv_check_set, "struct ucred *", "char *",
100     "char *");
101 
102 int
103 mac_kenv_check_set(struct ucred *cred, char *name, char *value)
104 {
105 	int error;
106 
107 	MAC_POLICY_CHECK_NOSLEEP(kenv_check_set, cred, name, value);
108 	MAC_CHECK_PROBE3(kenv_check_set, error, cred, name, value);
109 
110 	return (error);
111 }
112 
113 MAC_CHECK_PROBE_DEFINE2(kenv_check_unset, "struct ucred *", "char *");
114 
115 int
116 mac_kenv_check_unset(struct ucred *cred, char *name)
117 {
118 	int error;
119 
120 	MAC_POLICY_CHECK_NOSLEEP(kenv_check_unset, cred, name);
121 	MAC_CHECK_PROBE2(kenv_check_unset, error, cred, name);
122 
123 	return (error);
124 }
125 
126 MAC_CHECK_PROBE_DEFINE2(kld_check_load, "struct ucred *", "struct vnode *");
127 
128 int
129 mac_kld_check_load(struct ucred *cred, struct vnode *vp)
130 {
131 	int error;
132 
133 	ASSERT_VOP_LOCKED(vp, "mac_kld_check_load");
134 
135 	MAC_POLICY_CHECK(kld_check_load, cred, vp, vp->v_label);
136 	MAC_CHECK_PROBE2(kld_check_load, error, cred, vp);
137 
138 	return (error);
139 }
140 
141 MAC_CHECK_PROBE_DEFINE1(kld_check_stat, "struct ucred *");
142 
143 int
144 mac_kld_check_stat(struct ucred *cred)
145 {
146 	int error;
147 
148 	MAC_POLICY_CHECK_NOSLEEP(kld_check_stat, cred);
149 	MAC_CHECK_PROBE1(kld_check_stat, error, cred);
150 
151 	return (error);
152 }
153 
154 MAC_CHECK_PROBE_DEFINE2(system_check_acct, "struct ucred *",
155     "struct vnode *");
156 
157 int
158 mac_system_check_acct(struct ucred *cred, struct vnode *vp)
159 {
160 	int error;
161 
162 	if (vp != NULL) {
163 		ASSERT_VOP_LOCKED(vp, "mac_system_check_acct");
164 	}
165 
166 	MAC_POLICY_CHECK(system_check_acct, cred, vp,
167 	    vp != NULL ? vp->v_label : NULL);
168 	MAC_CHECK_PROBE2(system_check_acct, error, cred, vp);
169 
170 	return (error);
171 }
172 
173 MAC_CHECK_PROBE_DEFINE2(system_check_reboot, "struct ucred *", "int");
174 
175 int
176 mac_system_check_reboot(struct ucred *cred, int howto)
177 {
178 	int error;
179 
180 	MAC_POLICY_CHECK_NOSLEEP(system_check_reboot, cred, howto);
181 	MAC_CHECK_PROBE2(system_check_reboot, error, cred, howto);
182 
183 	return (error);
184 }
185 
186 MAC_CHECK_PROBE_DEFINE2(system_check_swapon, "struct ucred *",
187     "struct vnode *");
188 
189 int
190 mac_system_check_swapon(struct ucred *cred, struct vnode *vp)
191 {
192 	int error;
193 
194 	ASSERT_VOP_LOCKED(vp, "mac_system_check_swapon");
195 
196 	MAC_POLICY_CHECK(system_check_swapon, cred, vp, vp->v_label);
197 	MAC_CHECK_PROBE2(system_check_swapon, error, cred, vp);
198 
199 	return (error);
200 }
201 
202 MAC_CHECK_PROBE_DEFINE2(system_check_swapoff, "struct ucred *",
203     "struct vnode *");
204 
205 int
206 mac_system_check_swapoff(struct ucred *cred, struct vnode *vp)
207 {
208 	int error;
209 
210 	ASSERT_VOP_LOCKED(vp, "mac_system_check_swapoff");
211 
212 	MAC_POLICY_CHECK(system_check_swapoff, cred, vp, vp->v_label);
213 	MAC_CHECK_PROBE2(system_check_swapoff, error, cred, vp);
214 
215 	return (error);
216 }
217 
218 MAC_CHECK_PROBE_DEFINE3(system_check_sysctl, "struct ucred *",
219     "struct sysctl_oid *", "struct sysctl_req *");
220 
221 int
222 mac_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
223     void *arg1, int arg2, struct sysctl_req *req)
224 {
225 	int error;
226 
227 	/*
228 	 * XXXMAC: We would very much like to assert the SYSCTL_LOCK here,
229 	 * but since it's not exported from kern_sysctl.c, we can't.
230 	 */
231 	MAC_POLICY_CHECK_NOSLEEP(system_check_sysctl, cred, oidp, arg1, arg2,
232 	    req);
233 	MAC_CHECK_PROBE3(system_check_sysctl, error, cred, oidp, req);
234 
235 	return (error);
236 }
237