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: releng/11.0/sys/security/mac/mac_sysv_msg.c 258541 2013-11-25 07:38:45Z attilio $");
43137817Srwatson
44137817Srwatson#include "opt_mac.h"
45137817Srwatson
46137817Srwatson#include <sys/param.h>
47137817Srwatson#include <sys/kernel.h>
48137817Srwatson#include <sys/lock.h>
49137817Srwatson#include <sys/malloc.h>
50137817Srwatson#include <sys/mutex.h>
51137817Srwatson#include <sys/sbuf.h>
52189503Srwatson#include <sys/sdt.h>
53137817Srwatson#include <sys/systm.h>
54137817Srwatson#include <sys/vnode.h>
55137817Srwatson#include <sys/mount.h>
56137817Srwatson#include <sys/file.h>
57137817Srwatson#include <sys/namei.h>
58137817Srwatson#include <sys/sysctl.h>
59137817Srwatson#include <sys/msg.h>
60137817Srwatson
61163606Srwatson#include <security/mac/mac_framework.h>
62137817Srwatson#include <security/mac/mac_internal.h>
63165469Srwatson#include <security/mac/mac_policy.h>
64137817Srwatson
65137817Srwatsonstatic struct label *
66137817Srwatsonmac_sysv_msgmsg_label_alloc(void)
67137817Srwatson{
68137817Srwatson	struct label *label;
69137817Srwatson
70137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
71191731Srwatson	MAC_POLICY_PERFORM(sysvmsg_init_label, label);
72137817Srwatson	return (label);
73137817Srwatson}
74137817Srwatson
75137817Srwatsonvoid
76172930Srwatsonmac_sysvmsg_init(struct msg *msgptr)
77137817Srwatson{
78137817Srwatson
79182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSG)
80182063Srwatson		msgptr->label = mac_sysv_msgmsg_label_alloc();
81182063Srwatson	else
82182063Srwatson		msgptr->label = NULL;
83137817Srwatson}
84137817Srwatson
85137817Srwatsonstatic struct label *
86137817Srwatsonmac_sysv_msgqueue_label_alloc(void)
87137817Srwatson{
88137817Srwatson	struct label *label;
89137817Srwatson
90137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
91191731Srwatson	MAC_POLICY_PERFORM(sysvmsq_init_label, label);
92137817Srwatson	return (label);
93137817Srwatson}
94137817Srwatson
95137817Srwatsonvoid
96172930Srwatsonmac_sysvmsq_init(struct msqid_kernel *msqkptr)
97137817Srwatson{
98137817Srwatson
99182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSQ)
100182063Srwatson		msqkptr->label = mac_sysv_msgqueue_label_alloc();
101182063Srwatson	else
102182063Srwatson		msqkptr->label = NULL;
103137817Srwatson}
104137817Srwatson
105137817Srwatsonstatic void
106137817Srwatsonmac_sysv_msgmsg_label_free(struct label *label)
107137817Srwatson{
108137817Srwatson
109191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_destroy_label, label);
110137817Srwatson	mac_labelzone_free(label);
111137817Srwatson}
112137817Srwatson
113137817Srwatsonvoid
114172930Srwatsonmac_sysvmsg_destroy(struct msg *msgptr)
115137817Srwatson{
116137817Srwatson
117182063Srwatson	if (msgptr->label != NULL) {
118182063Srwatson		mac_sysv_msgmsg_label_free(msgptr->label);
119182063Srwatson		msgptr->label = NULL;
120182063Srwatson	}
121137817Srwatson}
122137817Srwatson
123137817Srwatsonstatic void
124137817Srwatsonmac_sysv_msgqueue_label_free(struct label *label)
125137817Srwatson{
126137817Srwatson
127191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_destroy_label, label);
128137817Srwatson	mac_labelzone_free(label);
129137817Srwatson}
130137817Srwatson
131137817Srwatsonvoid
132172930Srwatsonmac_sysvmsq_destroy(struct msqid_kernel *msqkptr)
133137817Srwatson{
134137817Srwatson
135182063Srwatson	if (msqkptr->label != NULL) {
136182063Srwatson		mac_sysv_msgqueue_label_free(msqkptr->label);
137182063Srwatson		msqkptr->label = NULL;
138182063Srwatson	}
139137817Srwatson}
140137817Srwatson
141137817Srwatsonvoid
142172930Srwatsonmac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
143137817Srwatson    struct msg *msgptr)
144137817Srwatson{
145165427Srwatson
146191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_create, cred, msqkptr,
147191731Srwatson	    msqkptr->label, msgptr, msgptr->label);
148137817Srwatson}
149137817Srwatson
150137817Srwatsonvoid
151172930Srwatsonmac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr)
152137817Srwatson{
153165427Srwatson
154191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsq_create, cred, msqkptr,
155191731Srwatson	    msqkptr->label);
156137817Srwatson}
157137817Srwatson
158137817Srwatsonvoid
159172930Srwatsonmac_sysvmsg_cleanup(struct msg *msgptr)
160137817Srwatson{
161137817Srwatson
162191731Srwatson	MAC_POLICY_PERFORM_NOSLEEP(sysvmsg_cleanup, msgptr->label);
163137817Srwatson}
164137817Srwatson
165137817Srwatsonvoid
166172930Srwatsonmac_sysvmsq_cleanup(struct msqid_kernel *msqkptr)
167137817Srwatson{
168165427Srwatson
169191731Srwatson	MAC_POLICY_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
181191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgmsq, cred, msgptr,
182191731Srwatson	    msgptr->label, 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
196191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrcv, cred, msgptr,
197191731Srwatson	    msgptr->label);
198189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msgrcv, error, cred, msgptr);
199137817Srwatson
200165434Srwatson	return (error);
201137817Srwatson}
202137817Srwatson
203189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msgrmid, "struct ucred *",
204189503Srwatson    "struct msg *");
205189503Srwatson
206137817Srwatsonint
207172930Srwatsonmac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr)
208137817Srwatson{
209137817Srwatson	int error;
210137817Srwatson
211191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msgrmid, cred, msgptr,
212189797Srwatson	    msgptr->label);
213189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msgrmid, error, cred, msgptr);
214137817Srwatson
215165434Srwatson	return (error);
216137817Srwatson}
217137817Srwatson
218189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqget, "struct ucred *",
219189503Srwatson    "struct msqid_kernel *");
220189503Srwatson
221137817Srwatsonint
222172930Srwatsonmac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
223137817Srwatson{
224137817Srwatson	int error;
225137817Srwatson
226191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqget, cred, msqkptr,
227189797Srwatson	    msqkptr->label);
228189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqget, error, cred, msqkptr);
229137817Srwatson
230165434Srwatson	return (error);
231137817Srwatson}
232137817Srwatson
233189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqsnd, "struct ucred *",
234189503Srwatson    "struct msqid_kernel *");
235189503Srwatson
236137817Srwatsonint
237172930Srwatsonmac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
238137817Srwatson{
239137817Srwatson	int error;
240137817Srwatson
241191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqsnd, cred, msqkptr,
242189797Srwatson	    msqkptr->label);
243189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqsnd, error, cred, msqkptr);
244137817Srwatson
245165434Srwatson	return (error);
246137817Srwatson}
247137817Srwatson
248189503SrwatsonMAC_CHECK_PROBE_DEFINE2(sysvmsq_check_msqrcv, "struct ucred *",
249189503Srwatson    "struct msqid_kernel *");
250189503Srwatson
251137817Srwatsonint
252172930Srwatsonmac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
253137817Srwatson{
254137817Srwatson	int error;
255137817Srwatson
256191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqrcv, cred, msqkptr,
257189797Srwatson	    msqkptr->label);
258189503Srwatson	MAC_CHECK_PROBE2(sysvmsq_check_msqrcv, error, cred, msqkptr);
259137817Srwatson
260165434Srwatson	return (error);
261137817Srwatson}
262137817Srwatson
263189503SrwatsonMAC_CHECK_PROBE_DEFINE3(sysvmsq_check_msqctl, "struct ucred *",
264189503Srwatson    "struct msqid_kernel *", "int");
265189503Srwatson
266137817Srwatsonint
267172930Srwatsonmac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
268137817Srwatson    int cmd)
269137817Srwatson{
270137817Srwatson	int error;
271137817Srwatson
272191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(sysvmsq_check_msqctl, cred, msqkptr,
273189797Srwatson	    msqkptr->label, cmd);
274189503Srwatson	MAC_CHECK_PROBE3(sysvmsq_check_msqctl, error, cred, msqkptr, cmd);
275137817Srwatson
276165434Srwatson	return (error);
277137817Srwatson}
278