mac_sysv_msg.c revision 189503
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: head/sys/security/mac/mac_sysv_msg.c 189503 2009-03-08 00:50:37Z rwatson $");
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
66static struct label *
67mac_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
76void
77mac_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
86static struct label *
87mac_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
96void
97mac_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
106static void
107mac_sysv_msgmsg_label_free(struct label *label)
108{
109
110	MAC_PERFORM(sysvmsg_destroy_label, label);
111	mac_labelzone_free(label);
112}
113
114void
115mac_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
124static void
125mac_sysv_msgqueue_label_free(struct label *label)
126{
127
128	MAC_PERFORM(sysvmsq_destroy_label, label);
129	mac_labelzone_free(label);
130}
131
132void
133mac_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
142void
143mac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
144    struct msg *msgptr)
145{
146
147	MAC_PERFORM(sysvmsg_create, cred, msqkptr, msqkptr->label,
148		msgptr, msgptr->label);
149}
150
151void
152mac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr)
153{
154
155	MAC_PERFORM(sysvmsq_create, cred, msqkptr, msqkptr->label);
156}
157
158void
159mac_sysvmsg_cleanup(struct msg *msgptr)
160{
161
162	MAC_PERFORM(sysvmsg_cleanup, msgptr->label);
163}
164
165void
166mac_sysvmsq_cleanup(struct msqid_kernel *msqkptr)
167{
168
169	MAC_PERFORM(sysvmsq_cleanup, msqkptr->label);
170}
171
172MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msgmsq, "struct ucred *",
173    "struct msg *", "struct msqid_kernel *");
174
175int
176mac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr,
177	struct msqid_kernel *msqkptr)
178{
179	int error;
180
181	MAC_CHECK(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
188MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrcv, "struct ucred *",
189    "struct msg *");
190
191int
192mac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr)
193{
194	int error;
195
196	MAC_CHECK(sysvmsq_check_msgrcv, cred, msgptr, msgptr->label);
197	MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr);
198
199	return (error);
200}
201
202MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *",
203    "struct msg *");
204
205int
206mac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr)
207{
208	int error;
209
210	MAC_CHECK(sysvmsq_check_msgrmid, cred, msgptr, msgptr->label);
211	MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr);
212
213	return (error);
214}
215
216MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *",
217    "struct msqid_kernel *");
218
219int
220mac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
221{
222	int error;
223
224	MAC_CHECK(sysvmsq_check_msqget, cred, msqkptr, msqkptr->label);
225	MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr);
226
227	return (error);
228}
229
230MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *",
231    "struct msqid_kernel *");
232
233int
234mac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
235{
236	int error;
237
238	MAC_CHECK(sysvmsq_check_msqsnd, cred, msqkptr, msqkptr->label);
239	MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr);
240
241	return (error);
242}
243
244MAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *",
245    "struct msqid_kernel *");
246
247int
248mac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
249{
250	int error;
251
252	MAC_CHECK(sysvmsq_check_msqrcv, cred, msqkptr, msqkptr->label);
253	MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr);
254
255	return (error);
256}
257
258MAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *",
259    "struct msqid_kernel *", "int");
260
261int
262mac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
263    int cmd)
264{
265	int error;
266
267	MAC_CHECK(sysvmsq_check_msqctl, cred, msqkptr, msqkptr->label, cmd);
268	MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd);
269
270	return (error);
271}
272