xref: /freebsd/sys/security/mac/mac_sysv_msg.c (revision d2b2128a286a00ee53d79cb88b4e59bf42525cf9)
1 /*-
2  * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3  * Copyright (c) 2006 SPARTA, Inc.
4  * Copyright (c) 2008 Apple Inc.
5  * Copyright (c) 2009 Robert N. M. Watson
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project in part by Network
9  * Associates Laboratories, the Security Research Division of Network
10  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11  * as part of the DARPA CHATS research program.
12  *
13  * This software was enhanced by SPARTA ISSO under SPAWAR contract
14  * N66001-04-C-6019 ("SEFOS").
15  *
16  * This software was developed at the University of Cambridge Computer
17  * Laboratory with support from a grant from Google, Inc.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_kdtrace.h"
45 #include "opt_mac.h"
46 
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
52 #include <sys/sbuf.h>
53 #include <sys/sdt.h>
54 #include <sys/systm.h>
55 #include <sys/vnode.h>
56 #include <sys/mount.h>
57 #include <sys/file.h>
58 #include <sys/namei.h>
59 #include <sys/sysctl.h>
60 #include <sys/msg.h>
61 
62 #include <security/mac/mac_framework.h>
63 #include <security/mac/mac_internal.h>
64 #include <security/mac/mac_policy.h>
65 
66 static struct label *
67 mac_sysv_msgmsg_label_alloc(void)
68 {
69 	struct label *label;
70 
71 	label = mac_labelzone_alloc(M_WAITOK);
72 	MAC_PERFORM(sysvmsg_init_label, label);
73 	return (label);
74 }
75 
76 void
77 mac_sysvmsg_init(struct msg *msgptr)
78 {
79 
80 	if (mac_labeled & MPC_OBJECT_SYSVMSG)
81 		msgptr->label = mac_sysv_msgmsg_label_alloc();
82 	else
83 		msgptr->label = NULL;
84 }
85 
86 static struct label *
87 mac_sysv_msgqueue_label_alloc(void)
88 {
89 	struct label *label;
90 
91 	label = mac_labelzone_alloc(M_WAITOK);
92 	MAC_PERFORM(sysvmsq_init_label, label);
93 	return (label);
94 }
95 
96 void
97 mac_sysvmsq_init(struct msqid_kernel *msqkptr)
98 {
99 
100 	if (mac_labeled & MPC_OBJECT_SYSVMSQ)
101 		msqkptr->label = mac_sysv_msgqueue_label_alloc();
102 	else
103 		msqkptr->label = NULL;
104 }
105 
106 static void
107 mac_sysv_msgmsg_label_free(struct label *label)
108 {
109 
110 	MAC_PERFORM_NOSLEEP(sysvmsg_destroy_label, label);
111 	mac_labelzone_free(label);
112 }
113 
114 void
115 mac_sysvmsg_destroy(struct msg *msgptr)
116 {
117 
118 	if (msgptr->label != NULL) {
119 		mac_sysv_msgmsg_label_free(msgptr->label);
120 		msgptr->label = NULL;
121 	}
122 }
123 
124 static void
125 mac_sysv_msgqueue_label_free(struct label *label)
126 {
127 
128 	MAC_PERFORM_NOSLEEP(sysvmsq_destroy_label, label);
129 	mac_labelzone_free(label);
130 }
131 
132 void
133 mac_sysvmsq_destroy(struct msqid_kernel *msqkptr)
134 {
135 
136 	if (msqkptr->label != NULL) {
137 		mac_sysv_msgqueue_label_free(msqkptr->label);
138 		msqkptr->label = NULL;
139 	}
140 }
141 
142 void
143 mac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
144     struct msg *msgptr)
145 {
146 
147 	MAC_PERFORM_NOSLEEP(sysvmsg_create, cred, msqkptr, msqkptr->label,
148 		msgptr, msgptr->label);
149 }
150 
151 void
152 mac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr)
153 {
154 
155 	MAC_PERFORM_NOSLEEP(sysvmsq_create, cred, msqkptr, msqkptr->label);
156 }
157 
158 void
159 mac_sysvmsg_cleanup(struct msg *msgptr)
160 {
161 
162 	MAC_PERFORM_NOSLEEP(sysvmsg_cleanup, msgptr->label);
163 }
164 
165 void
166 mac_sysvmsq_cleanup(struct msqid_kernel *msqkptr)
167 {
168 
169 	MAC_PERFORM_NOSLEEP(sysvmsq_cleanup, msqkptr->label);
170 }
171 
172 MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msgmsq, "struct ucred *",
173     "struct msg *", "struct msqid_kernel *");
174 
175 int
176 mac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr,
177 	struct msqid_kernel *msqkptr)
178 {
179 	int error;
180 
181 	MAC_CHECK_NOSLEEP(sysvmsq_check_msgmsq, cred, msgptr, msgptr->label,
182 	    msqkptr, msqkptr->label);
183 	MAC_CHECK_PROBE3(sysvmsq_check_msgmsq, error, cred, msgptr, msqkptr);
184 
185 	return (error);
186 }
187 
188 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrcv, "struct ucred *",
189     "struct msg *");
190 
191 int
192 mac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr)
193 {
194 	int error;
195 
196 	MAC_CHECK_NOSLEEP(sysvmsq_check_msgrcv, cred, msgptr, msgptr->label);
197 	MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr);
198 
199 	return (error);
200 }
201 
202 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *",
203     "struct msg *");
204 
205 int
206 mac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr)
207 {
208 	int error;
209 
210 	MAC_CHECK_NOSLEEP(sysvmsq_check_msgrmid, cred, msgptr,
211 	    msgptr->label);
212 	MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr);
213 
214 	return (error);
215 }
216 
217 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *",
218     "struct msqid_kernel *");
219 
220 int
221 mac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
222 {
223 	int error;
224 
225 	MAC_CHECK_NOSLEEP(sysvmsq_check_msqget, cred, msqkptr,
226 	    msqkptr->label);
227 	MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr);
228 
229 	return (error);
230 }
231 
232 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *",
233     "struct msqid_kernel *");
234 
235 int
236 mac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
237 {
238 	int error;
239 
240 	MAC_CHECK_NOSLEEP(sysvmsq_check_msqsnd, cred, msqkptr,
241 	    msqkptr->label);
242 	MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr);
243 
244 	return (error);
245 }
246 
247 MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *",
248     "struct msqid_kernel *");
249 
250 int
251 mac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
252 {
253 	int error;
254 
255 	MAC_CHECK_NOSLEEP(sysvmsq_check_msqrcv, cred, msqkptr,
256 	    msqkptr->label);
257 	MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr);
258 
259 	return (error);
260 }
261 
262 MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *",
263     "struct msqid_kernel *", "int");
264 
265 int
266 mac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
267     int cmd)
268 {
269 	int error;
270 
271 	MAC_CHECK_NOSLEEP(sysvmsq_check_msqctl, cred, msqkptr,
272 	    msqkptr->label, cmd);
273 	MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd);
274 
275 	return (error);
276 }
277