mac_sysv_msg.c revision 182063
1137817Srwatson/*-
2137817Srwatson * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3172930Srwatson * Copyright (c) 2006 SPARTA, Inc.
4182063Srwatson * Copyright (c) 2008 Apple Inc.
5137817Srwatson * All rights reserved.
6137817Srwatson *
7137817Srwatson * This software was developed for the FreeBSD Project in part by Network
8137817Srwatson * Associates Laboratories, the Security Research Division of Network
9137817Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
10137817Srwatson * as part of the DARPA CHATS research program.
11137817Srwatson *
12172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract
13172930Srwatson * N66001-04-C-6019 ("SEFOS").
14172930Srwatson *
15137817Srwatson * Redistribution and use in source and binary forms, with or without
16137817Srwatson * modification, are permitted provided that the following conditions
17137817Srwatson * are met:
18137817Srwatson * 1. Redistributions of source code must retain the above copyright
19137817Srwatson *    notice, this list of conditions and the following disclaimer.
20137817Srwatson * 2. Redistributions in binary form must reproduce the above copyright
21137817Srwatson *    notice, this list of conditions and the following disclaimer in the
22137817Srwatson *    documentation and/or other materials provided with the distribution.
23137817Srwatson *
24137817Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25137817Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26137817Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27137817Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28137817Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29137817Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30137817Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31137817Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32137817Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33137817Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34137817Srwatson * SUCH DAMAGE.
35137817Srwatson */
36137817Srwatson
37137817Srwatson#include <sys/cdefs.h>
38137817Srwatson__FBSDID("$FreeBSD: head/sys/security/mac/mac_sysv_msg.c 182063 2008-08-23 15:26:36Z rwatson $");
39137817Srwatson
40137817Srwatson#include "opt_mac.h"
41137817Srwatson
42137817Srwatson#include <sys/param.h>
43137817Srwatson#include <sys/kernel.h>
44137817Srwatson#include <sys/lock.h>
45137817Srwatson#include <sys/malloc.h>
46137817Srwatson#include <sys/mutex.h>
47137817Srwatson#include <sys/sbuf.h>
48137817Srwatson#include <sys/systm.h>
49137817Srwatson#include <sys/vnode.h>
50137817Srwatson#include <sys/mount.h>
51137817Srwatson#include <sys/file.h>
52137817Srwatson#include <sys/namei.h>
53137817Srwatson#include <sys/sysctl.h>
54137817Srwatson#include <sys/msg.h>
55137817Srwatson
56163606Srwatson#include <security/mac/mac_framework.h>
57137817Srwatson#include <security/mac/mac_internal.h>
58165469Srwatson#include <security/mac/mac_policy.h>
59137817Srwatson
60137817Srwatsonstatic struct label *
61137817Srwatsonmac_sysv_msgmsg_label_alloc(void)
62137817Srwatson{
63137817Srwatson	struct label *label;
64137817Srwatson
65137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
66172930Srwatson	MAC_PERFORM(sysvmsg_init_label, label);
67137817Srwatson	return (label);
68137817Srwatson}
69137817Srwatson
70137817Srwatsonvoid
71172930Srwatsonmac_sysvmsg_init(struct msg *msgptr)
72137817Srwatson{
73137817Srwatson
74182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSG)
75182063Srwatson		msgptr->label = mac_sysv_msgmsg_label_alloc();
76182063Srwatson	else
77182063Srwatson		msgptr->label = NULL;
78137817Srwatson}
79137817Srwatson
80137817Srwatsonstatic struct label *
81137817Srwatsonmac_sysv_msgqueue_label_alloc(void)
82137817Srwatson{
83137817Srwatson	struct label *label;
84137817Srwatson
85137817Srwatson	label = mac_labelzone_alloc(M_WAITOK);
86172930Srwatson	MAC_PERFORM(sysvmsq_init_label, label);
87137817Srwatson	return (label);
88137817Srwatson}
89137817Srwatson
90137817Srwatsonvoid
91172930Srwatsonmac_sysvmsq_init(struct msqid_kernel *msqkptr)
92137817Srwatson{
93137817Srwatson
94182063Srwatson	if (mac_labeled & MPC_OBJECT_SYSVMSQ)
95182063Srwatson		msqkptr->label = mac_sysv_msgqueue_label_alloc();
96182063Srwatson	else
97182063Srwatson		msqkptr->label = NULL;
98137817Srwatson}
99137817Srwatson
100137817Srwatsonstatic void
101137817Srwatsonmac_sysv_msgmsg_label_free(struct label *label)
102137817Srwatson{
103137817Srwatson
104172930Srwatson	MAC_PERFORM(sysvmsg_destroy_label, label);
105137817Srwatson	mac_labelzone_free(label);
106137817Srwatson}
107137817Srwatson
108137817Srwatsonvoid
109172930Srwatsonmac_sysvmsg_destroy(struct msg *msgptr)
110137817Srwatson{
111137817Srwatson
112182063Srwatson	if (msgptr->label != NULL) {
113182063Srwatson		mac_sysv_msgmsg_label_free(msgptr->label);
114182063Srwatson		msgptr->label = NULL;
115182063Srwatson	}
116137817Srwatson}
117137817Srwatson
118137817Srwatsonstatic void
119137817Srwatsonmac_sysv_msgqueue_label_free(struct label *label)
120137817Srwatson{
121137817Srwatson
122172930Srwatson	MAC_PERFORM(sysvmsq_destroy_label, label);
123137817Srwatson	mac_labelzone_free(label);
124137817Srwatson}
125137817Srwatson
126137817Srwatsonvoid
127172930Srwatsonmac_sysvmsq_destroy(struct msqid_kernel *msqkptr)
128137817Srwatson{
129137817Srwatson
130182063Srwatson	if (msqkptr->label != NULL) {
131182063Srwatson		mac_sysv_msgqueue_label_free(msqkptr->label);
132182063Srwatson		msqkptr->label = NULL;
133182063Srwatson	}
134137817Srwatson}
135137817Srwatson
136137817Srwatsonvoid
137172930Srwatsonmac_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
138137817Srwatson    struct msg *msgptr)
139137817Srwatson{
140165427Srwatson
141172930Srwatson	MAC_PERFORM(sysvmsg_create, cred, msqkptr, msqkptr->label,
142137817Srwatson		msgptr, msgptr->label);
143137817Srwatson}
144137817Srwatson
145137817Srwatsonvoid
146172930Srwatsonmac_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr)
147137817Srwatson{
148165427Srwatson
149172930Srwatson	MAC_PERFORM(sysvmsq_create, cred, msqkptr, msqkptr->label);
150137817Srwatson}
151137817Srwatson
152137817Srwatsonvoid
153172930Srwatsonmac_sysvmsg_cleanup(struct msg *msgptr)
154137817Srwatson{
155137817Srwatson
156172930Srwatson	MAC_PERFORM(sysvmsg_cleanup, msgptr->label);
157137817Srwatson}
158137817Srwatson
159137817Srwatsonvoid
160172930Srwatsonmac_sysvmsq_cleanup(struct msqid_kernel *msqkptr)
161137817Srwatson{
162165427Srwatson
163172930Srwatson	MAC_PERFORM(sysvmsq_cleanup, msqkptr->label);
164137817Srwatson}
165137817Srwatson
166137817Srwatsonint
167172930Srwatsonmac_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr,
168137817Srwatson	struct msqid_kernel *msqkptr)
169137817Srwatson{
170137817Srwatson	int error;
171137817Srwatson
172172930Srwatson	MAC_CHECK(sysvmsq_check_msgmsq, cred,  msgptr, msgptr->label,
173172930Srwatson	    msqkptr, msqkptr->label);
174137817Srwatson
175165434Srwatson	return (error);
176137817Srwatson}
177137817Srwatson
178137817Srwatsonint
179172930Srwatsonmac_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr)
180137817Srwatson{
181137817Srwatson	int error;
182137817Srwatson
183172930Srwatson	MAC_CHECK(sysvmsq_check_msgrcv, cred, msgptr, msgptr->label);
184137817Srwatson
185165434Srwatson	return (error);
186137817Srwatson}
187137817Srwatson
188137817Srwatsonint
189172930Srwatsonmac_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr)
190137817Srwatson{
191137817Srwatson	int error;
192137817Srwatson
193172930Srwatson	MAC_CHECK(sysvmsq_check_msgrmid, cred,  msgptr, msgptr->label);
194137817Srwatson
195165434Srwatson	return (error);
196137817Srwatson}
197137817Srwatson
198137817Srwatsonint
199172930Srwatsonmac_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr)
200137817Srwatson{
201137817Srwatson	int error;
202137817Srwatson
203172930Srwatson	MAC_CHECK(sysvmsq_check_msqget, cred, msqkptr, msqkptr->label);
204137817Srwatson
205165434Srwatson	return (error);
206137817Srwatson}
207137817Srwatson
208137817Srwatsonint
209172930Srwatsonmac_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr)
210137817Srwatson{
211137817Srwatson	int error;
212137817Srwatson
213172930Srwatson	MAC_CHECK(sysvmsq_check_msqsnd, cred, msqkptr, msqkptr->label);
214137817Srwatson
215165434Srwatson	return (error);
216137817Srwatson}
217137817Srwatson
218137817Srwatsonint
219172930Srwatsonmac_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr)
220137817Srwatson{
221137817Srwatson	int error;
222137817Srwatson
223172930Srwatson	MAC_CHECK(sysvmsq_check_msqrcv, cred, msqkptr, msqkptr->label);
224137817Srwatson
225165434Srwatson	return (error);
226137817Srwatson}
227137817Srwatson
228137817Srwatsonint
229172930Srwatsonmac_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
230137817Srwatson    int cmd)
231137817Srwatson{
232137817Srwatson	int error;
233137817Srwatson
234172930Srwatson	MAC_CHECK(sysvmsq_check_msqctl, cred, msqkptr, msqkptr->label, cmd);
235137817Srwatson
236165434Srwatson	return (error);
237137817Srwatson}
238