mac_sysv_msg.c revision 189797
1137817Srwatson/*-
2137817Srwatson * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3172930Srwatson * Copyright (c) 2006 SPARTA, Inc.
4182063Srwatson * Copyright (c) 2008 Apple Inc.
5189503Srwatson * Copyright (c) 2009 Robert N. M. Watson
6137817Srwatson * All rights reserved.
7137817Srwatson *
8137817Srwatson * This software was developed for the FreeBSD Project in part by Network
9137817Srwatson * Associates Laboratories, the Security Research Division of Network
10137817Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11137817Srwatson * as part of the DARPA CHATS research program.
12137817Srwatson *
13172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract
14172930Srwatson * N66001-04-C-6019 ("SEFOS").
15172930Srwatson *
16189503Srwatson * This software was developed at the University of Cambridge Computer
17189503Srwatson * Laboratory with support from a grant from Google, Inc.
18189503Srwatson *
19137817Srwatson * Redistribution and use in source and binary forms, with or without
20137817Srwatson * modification, are permitted provided that the following conditions
21137817Srwatson * are met:
22137817Srwatson * 1. Redistributions of source code must retain the above copyright
23137817Srwatson *    notice, this list of conditions and the following disclaimer.
24137817Srwatson * 2. Redistributions in binary form must reproduce the above copyright
25137817Srwatson *    notice, this list of conditions and the following disclaimer in the
26137817Srwatson *    documentation and/or other materials provided with the distribution.
27137817Srwatson *
28137817Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29137817Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30137817Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31137817Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32137817Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33137817Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34137817Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35137817Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36137817Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37137817Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38137817Srwatson * SUCH DAMAGE.
39137817Srwatson */
40137817Srwatson
41137817Srwatson#include <sys/cdefs.h>
42137817Srwatson__FBSDID("$FreeBSD: head/sys/security/mac/mac_sysv_msg.c 189797 2009-03-14 16:06:06Z rwatson $");
43137817Srwatson
44189503Srwatson#include "opt_kdtrace.h"
45137817Srwatson#include "opt_mac.h"
46137817Srwatson
47137817Srwatson#include <sys/param.h>
48137817Srwatson#include <sys/kernel.h>
49137817Srwatson#include <sys/lock.h>
50137817Srwatson#include <sys/malloc.h>
51137817Srwatson#include <sys/mutex.h>
52137817Srwatson#include <sys/sbuf.h>
53189503Srwatson#include <sys/sdt.h>
54137817Srwatson#include <sys/systm.h>
55137817Srwatson#include <sys/vnode.h>
56137817Srwatson#include <sys/mount.h>
57137817Srwatson#include <sys/file.h>
58137817Srwatson#include <sys/namei.h>
59137817Srwatson#include <sys/sysctl.h>
60137817Srwatson#include <sys/msg.h>
61137817Srwatson
62163606Srwatson#include <security/mac/mac_framework.h>
63137817Srwatson#include <security/mac/mac_internal.h>
64165469Srwatson#include <security/mac/mac_policy.h>
65137817Srwatson
66137817Srwatsonstatic struct label *
67137817Srwatsonmac_sysv_msgmsg_label_alloc(void)
68137817Srwatson{
69137817Srwatson	struct label *label;
70137817Srwatson
71137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
72172930Srwatson	MAC_PERFORM(sysvmsg_init_label, label);
73137817Srwatson	return (label);
74137817Srwatson}
75137817Srwatson
76137817Srwatsonvoid
77172930Srwatsonmac_sysvmsg_init(struct msg *msgptr)
78137817Srwatson{
79137817Srwatson
80182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSG)
81182063Srwatson		msgptr->label = mac_sysv_msgmsg_label_alloc();
82182063Srwatson	else
83182063Srwatson		msgptr->label = NULL;
84137817Srwatson}
85137817Srwatson
86137817Srwatsonstatic struct label *
87137817Srwatsonmac_sysv_msgqueue_label_alloc(void)
88137817Srwatson{
89137817Srwatson	struct label *label;
90137817Srwatson
91137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
92172930Srwatson	MAC_PERFORM(sysvmsq_init_label, label);
93137817Srwatson	return (label);
94137817Srwatson}
95137817Srwatson
96137817Srwatsonvoid
97172930Srwatsonmac_sysvmsq_init(struct msqid_kernel *msqkptr)
98137817Srwatson{
99137817Srwatson
100182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSQ)
101182063Srwatson		msqkptr->label = mac_sysv_msgqueue_label_alloc();
102182063Srwatson	else
103182063Srwatson		msqkptr->label = NULL;
104137817Srwatson}
105137817Srwatson
106137817Srwatsonstatic void
107137817Srwatsonmac_sysv_msgmsg_label_free(struct label *label)
108137817Srwatson{
109137817Srwatson
110189797Srwatson	MAC_PERFORM_NOSLEEP(sysvmsg_destroy_label, label);
111137817Srwatson	mac_labelzone_free(label);
112137817Srwatson}
113137817Srwatson
114137817Srwatsonvoid
115172930Srwatsonmac_sysvmsg_destroy(struct msg *msgptr)
116137817Srwatson{
117137817Srwatson
118182063Srwatson	if (msgptr->label != NULL) {
119182063Srwatson		mac_sysv_msgmsg_label_free(msgptr->label);
120182063Srwatson		msgptr->label = NULL;
121182063Srwatson	}
122137817Srwatson}
123137817Srwatson
124137817Srwatsonstatic void
125137817Srwatsonmac_sysv_msgqueue_label_free(struct label *label)
126137817Srwatson{
127137817Srwatson
128189797Srwatson	MAC_PERFORM_NOSLEEP(sysvmsq_destroy_label, label);
129137817Srwatson	mac_labelzone_free(label);
130137817Srwatson}
131137817Srwatson
132137817Srwatsonvoid
133172930Srwatsonmac_sysvmsq_destroy(struct msqid_kernel *msqkptr)
134137817Srwatson{
135137817Srwatson
136182063Srwatson	if (msqkptr->label != NULL) {
137182063Srwatson		mac_sysv_msgqueue_label_free(msqkptr->label);
138182063Srwatson		msqkptr->label = NULL;
139182063Srwatson	}
140137817Srwatson}
141137817Srwatson
142137817Srwatsonvoid
143172930Srwatsonmac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
144137817Srwatson    struct msg *msgptr)
145137817Srwatson{
146165427Srwatson
147189797Srwatson	MAC_PERFORM_NOSLEEP(sysvmsg_create, cred, msqkptr, msqkptr->label,
148137817Srwatson		msgptr, msgptr->label);
149137817Srwatson}
150137817Srwatson
151137817Srwatsonvoid
152172930Srwatsonmac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr)
153137817Srwatson{
154165427Srwatson
155189797Srwatson	MAC_PERFORM_NOSLEEP(sysvmsq_create, cred, msqkptr, msqkptr->label);
156137817Srwatson}
157137817Srwatson
158137817Srwatsonvoid
159172930Srwatsonmac_sysvmsg_cleanup(struct msg *msgptr)
160137817Srwatson{
161137817Srwatson
162189797Srwatson	MAC_PERFORM_NOSLEEP(sysvmsg_cleanup, msgptr->label);
163137817Srwatson}
164137817Srwatson
165137817Srwatsonvoid
166172930Srwatsonmac_sysvmsq_cleanup(struct msqid_kernel *msqkptr)
167137817Srwatson{
168165427Srwatson
169189797Srwatson	MAC_PERFORM_NOSLEEP(sysvmsq_cleanup, msqkptr->label);
170137817Srwatson}
171137817Srwatson
172189503SrwatsonMAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msgmsq, "struct ucred *",
173189503Srwatson    "struct msg *", "struct msqid_kernel *");
174189503Srwatson
175137817Srwatsonint
176172930Srwatsonmac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr,
177137817Srwatson	struct msqid_kernel *msqkptr)
178137817Srwatson{
179137817Srwatson	int error;
180137817Srwatson
181189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msgmsq, cred, msgptr, msgptr->label,
182172930Srwatson	    msqkptr, msqkptr->label);
183189503Srwatson	MAC_CHECK_PROBE3(sysvmsq_check_msgmsq, error, cred, msgptr, msqkptr);
184137817Srwatson
185165434Srwatson	return (error);
186137817Srwatson}
187137817Srwatson
188189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrcv, "struct ucred *",
189189503Srwatson    "struct msg *");
190189503Srwatson
191137817Srwatsonint
192172930Srwatsonmac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr)
193137817Srwatson{
194137817Srwatson	int error;
195137817Srwatson
196189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msgrcv, cred, msgptr, msgptr->label);
197189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr);
198137817Srwatson
199165434Srwatson	return (error);
200137817Srwatson}
201137817Srwatson
202189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *",
203189503Srwatson    "struct msg *");
204189503Srwatson
205137817Srwatsonint
206172930Srwatsonmac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr)
207137817Srwatson{
208137817Srwatson	int error;
209137817Srwatson
210189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msgrmid, cred, msgptr,
211189797Srwatson	    msgptr->label);
212189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr);
213137817Srwatson
214165434Srwatson	return (error);
215137817Srwatson}
216137817Srwatson
217189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *",
218189503Srwatson    "struct msqid_kernel *");
219189503Srwatson
220137817Srwatsonint
221172930Srwatsonmac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
222137817Srwatson{
223137817Srwatson	int error;
224137817Srwatson
225189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msqget, cred, msqkptr,
226189797Srwatson	    msqkptr->label);
227189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr);
228137817Srwatson
229165434Srwatson	return (error);
230137817Srwatson}
231137817Srwatson
232189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *",
233189503Srwatson    "struct msqid_kernel *");
234189503Srwatson
235137817Srwatsonint
236172930Srwatsonmac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
237137817Srwatson{
238137817Srwatson	int error;
239137817Srwatson
240189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msqsnd, cred, msqkptr,
241189797Srwatson	    msqkptr->label);
242189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr);
243137817Srwatson
244165434Srwatson	return (error);
245137817Srwatson}
246137817Srwatson
247189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *",
248189503Srwatson    "struct msqid_kernel *");
249189503Srwatson
250137817Srwatsonint
251172930Srwatsonmac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
252137817Srwatson{
253137817Srwatson	int error;
254137817Srwatson
255189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msqrcv, cred, msqkptr,
256189797Srwatson	    msqkptr->label);
257189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr);
258137817Srwatson
259165434Srwatson	return (error);
260137817Srwatson}
261137817Srwatson
262189503SrwatsonMAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *",
263189503Srwatson    "struct msqid_kernel *", "int");
264189503Srwatson
265137817Srwatsonint
266172930Srwatsonmac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
267137817Srwatson    int cmd)
268137817Srwatson{
269137817Srwatson	int error;
270137817Srwatson
271189797Srwatson	MAC_CHECK_NOSLEEP(sysvmsq_check_msqctl, cred, msqkptr,
272189797Srwatson	    msqkptr->label, cmd);
273189503Srwatson	MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd);
274137817Srwatson
275165434Srwatson	return (error);
276137817Srwatson}
277