xref: /freebsd/sys/security/mac/mac_posix_shm.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * Copyright (c) 2003-2006 SPARTA, Inc.
3  * Copyright (c) 2009-2011 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project in part by Network
7  * Associates Laboratories, the Security Research Division of Network
8  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9  * as part of the DARPA CHATS research program.
10  *
11  * This software was enhanced by SPARTA ISSO under SPAWAR contract
12  * N66001-04-C-6019 ("SEFOS"). *
13  *
14  * This software was developed at the University of Cambridge Computer
15  * Laboratory with support from a grant from Google, Inc.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #include "opt_mac.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/mman.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/sdt.h>
48 #include <sys/systm.h>
49 #include <sys/sysctl.h>
50 
51 #include <security/mac/mac_framework.h>
52 #include <security/mac/mac_internal.h>
53 #include <security/mac/mac_policy.h>
54 
55 static struct label *
56 mac_posixshm_label_alloc(void)
57 {
58 	struct label *label;
59 
60 	label = mac_labelzone_alloc(M_WAITOK);
61 	MAC_POLICY_PERFORM(posixshm_init_label, label);
62 	return (label);
63 }
64 
65 void
66 mac_posixshm_init(struct shmfd *shmfd)
67 {
68 
69 	if (mac_labeled & MPC_OBJECT_POSIXSHM)
70 		shmfd->shm_label = mac_posixshm_label_alloc();
71 	else
72 		shmfd->shm_label = NULL;
73 }
74 
75 static void
76 mac_posixshm_label_free(struct label *label)
77 {
78 
79 	MAC_POLICY_PERFORM_NOSLEEP(posixshm_destroy_label, label);
80 	mac_labelzone_free(label);
81 }
82 
83 void
84 mac_posixshm_destroy(struct shmfd *shmfd)
85 {
86 
87 	if (shmfd->shm_label != NULL) {
88 		mac_posixshm_label_free(shmfd->shm_label);
89 		shmfd->shm_label = NULL;
90 	}
91 }
92 
93 void
94 mac_posixshm_create(struct ucred *cred, struct shmfd *shmfd)
95 {
96 
97 	MAC_POLICY_PERFORM_NOSLEEP(posixshm_create, cred, shmfd,
98 	    shmfd->shm_label);
99 }
100 
101 MAC_CHECK_PROBE_DEFINE2(posixshm_check_create, "struct ucred *",
102     "const char *");
103 
104 int
105 mac_posixshm_check_create(struct ucred *cred, const char *path)
106 {
107 	int error;
108 
109 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_create, cred, path);
110 	MAC_CHECK_PROBE2(posixshm_check_create, error, cred, path);
111 
112 	return (error);
113 }
114 
115 MAC_CHECK_PROBE_DEFINE4(posixshm_check_mmap, "struct ucred *",
116     "struct shmfd *", "int", "int");
117 
118 int
119 mac_posixshm_check_mmap(struct ucred *cred, struct shmfd *shmfd, int prot,
120     int flags)
121 {
122 	int error;
123 
124 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_mmap, cred, shmfd,
125 	    shmfd->shm_label, prot, flags);
126 	MAC_CHECK_PROBE4(posixshm_check_mmap, error, cred, shmfd, prot,
127 	    flags);
128 
129 	return (error);
130 }
131 
132 MAC_CHECK_PROBE_DEFINE3(posixshm_check_open, "struct ucred *",
133     "struct shmfd *", "accmode_t");
134 
135 int
136 mac_posixshm_check_open(struct ucred *cred, struct shmfd *shmfd,
137     accmode_t accmode)
138 {
139 	int error;
140 
141 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_open, cred, shmfd,
142 	    shmfd->shm_label, accmode);
143 	MAC_CHECK_PROBE3(posixshm_check_open, error, cred, shmfd, accmode);
144 
145 	return (error);
146 }
147 
148 MAC_CHECK_PROBE_DEFINE3(posixshm_check_stat, "struct ucred *",
149     "struct ucred *", "struct shmfd *");
150 
151 int
152 mac_posixshm_check_stat(struct ucred *active_cred, struct ucred *file_cred,
153     struct shmfd *shmfd)
154 {
155 	int error;
156 
157 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_stat, active_cred, file_cred,
158 	    shmfd, shmfd->shm_label);
159 	MAC_CHECK_PROBE3(posixshm_check_stat, error, active_cred, file_cred,
160 	    shmfd);
161 
162 	return (error);
163 }
164 
165 MAC_CHECK_PROBE_DEFINE3(posixshm_check_truncate, "struct ucred *",
166     "struct ucred *", "struct shmfd *");
167 
168 int
169 mac_posixshm_check_truncate(struct ucred *active_cred, struct ucred *file_cred,
170     struct shmfd *shmfd)
171 {
172 	int error;
173 
174 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_truncate, active_cred,
175 	    file_cred, shmfd, shmfd->shm_label);
176 	MAC_CHECK_PROBE3(posixshm_check_truncate, error, active_cred,
177 	    file_cred, shmfd);
178 
179 	return (error);
180 }
181 
182 MAC_CHECK_PROBE_DEFINE2(posixshm_check_unlink, "struct ucred *",
183     "struct shmfd *");
184 
185 int
186 mac_posixshm_check_unlink(struct ucred *cred, struct shmfd *shmfd)
187 {
188 	int error;
189 
190 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_unlink, cred, shmfd,
191 	    shmfd->shm_label);
192 	MAC_CHECK_PROBE2(posixshm_check_unlink, error, cred, shmfd);
193 
194 	return (error);
195 }
196 
197 MAC_CHECK_PROBE_DEFINE3(posixshm_check_setmode, "struct ucred *",
198     "struct shmfd *", "mode_t");
199 
200 int
201 mac_posixshm_check_setmode(struct ucred *cred, struct shmfd *shmfd, mode_t mode)
202 {
203 	int error;
204 
205 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_setmode, cred, shmfd,
206 	    shmfd->shm_label, mode);
207 	MAC_CHECK_PROBE3(posixshm_check_setmode, error, cred, shmfd, mode);
208 
209 	return (error);
210 }
211 
212 MAC_CHECK_PROBE_DEFINE4(posixshm_check_setowner, "struct ucred *",
213     "struct shmfd *", "uid_t", "gid_t");
214 
215 int
216 mac_posixshm_check_setowner(struct ucred *cred, struct shmfd *shmfd, uid_t uid,
217     gid_t gid)
218 {
219 	int error;
220 
221 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_setowner, cred, shmfd,
222 	    shmfd->shm_label, uid, gid);
223 	MAC_CHECK_PROBE4(posixshm_check_setowner, error, cred, shmfd,
224 	    uid, gid);
225 
226 	return (error);
227 }
228 
229 MAC_CHECK_PROBE_DEFINE3(posixshm_check_read, "struct ucred *",
230     "struct ucred *", "struct shmfd *");
231 
232 int
233 mac_posixshm_check_read(struct ucred *active_cred, struct ucred *file_cred,
234     struct shmfd *shmfd)
235 {
236 	int error;
237 
238 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_read, active_cred,
239 	    file_cred, shmfd, shmfd->shm_label);
240 	MAC_CHECK_PROBE3(posixshm_check_read, error, active_cred,
241 	    file_cred, shmfd);
242 
243 	return (error);
244 }
245 
246 MAC_CHECK_PROBE_DEFINE3(posixshm_check_write, "struct ucred *",
247     "struct ucred *", "struct shmfd *");
248 
249 int
250 mac_posixshm_check_write(struct ucred *active_cred, struct ucred *file_cred,
251     struct shmfd *shmfd)
252 {
253 	int error;
254 
255 	MAC_POLICY_CHECK_NOSLEEP(posixshm_check_write, active_cred,
256 	    file_cred, shmfd, shmfd->shm_label);
257 	MAC_CHECK_PROBE3(posixshm_check_write, error, active_cred,
258 	    file_cred, shmfd);
259 
260 	return (error);
261 }
262