1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2007 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions /*-
29*c54f35caSApple OSS Distributions * Copyright (c) 2003-2005 Networks Associates Technology, Inc.
30*c54f35caSApple OSS Distributions * All rights reserved.
31*c54f35caSApple OSS Distributions *
32*c54f35caSApple OSS Distributions * This software was developed for the FreeBSD Project in part by Network
33*c54f35caSApple OSS Distributions * Associates Laboratories, the Security Research Division of Network
34*c54f35caSApple OSS Distributions * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
35*c54f35caSApple OSS Distributions * as part of the DARPA CHATS research program.
36*c54f35caSApple OSS Distributions *
37*c54f35caSApple OSS Distributions * Redistribution and use in source and binary forms, with or without
38*c54f35caSApple OSS Distributions * modification, are permitted provided that the following conditions
39*c54f35caSApple OSS Distributions * are met:
40*c54f35caSApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
41*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer.
42*c54f35caSApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
43*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
44*c54f35caSApple OSS Distributions * documentation and/or other materials provided with the distribution.
45*c54f35caSApple OSS Distributions *
46*c54f35caSApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47*c54f35caSApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48*c54f35caSApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49*c54f35caSApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50*c54f35caSApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51*c54f35caSApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52*c54f35caSApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53*c54f35caSApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54*c54f35caSApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55*c54f35caSApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56*c54f35caSApple OSS Distributions * SUCH DAMAGE.
57*c54f35caSApple OSS Distributions */
58*c54f35caSApple OSS Distributions
59*c54f35caSApple OSS Distributions #include <sys/cdefs.h>
60*c54f35caSApple OSS Distributions
61*c54f35caSApple OSS Distributions #include <sys/param.h>
62*c54f35caSApple OSS Distributions #include <sys/systm.h>
63*c54f35caSApple OSS Distributions #include <sys/kernel.h>
64*c54f35caSApple OSS Distributions #include <sys/lock.h>
65*c54f35caSApple OSS Distributions #include <sys/malloc.h>
66*c54f35caSApple OSS Distributions #include <sys/sbuf.h>
67*c54f35caSApple OSS Distributions #include <sys/systm.h>
68*c54f35caSApple OSS Distributions #include <sys/vnode.h>
69*c54f35caSApple OSS Distributions #include <sys/mount.h>
70*c54f35caSApple OSS Distributions #include <sys/file.h>
71*c54f35caSApple OSS Distributions #include <sys/namei.h>
72*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
73*c54f35caSApple OSS Distributions #include <sys/msg.h>
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions #include <security/mac_internal.h>
76*c54f35caSApple OSS Distributions
77*c54f35caSApple OSS Distributions void
mac_sysvmsg_label_init(struct msg * msgptr)78*c54f35caSApple OSS Distributions mac_sysvmsg_label_init(struct msg *msgptr)
79*c54f35caSApple OSS Distributions {
80*c54f35caSApple OSS Distributions mac_labelzone_alloc_owned(&msgptr->label, MAC_WAITOK, ^(struct label *label) {
81*c54f35caSApple OSS Distributions MAC_PERFORM(sysvmsg_label_init, label);
82*c54f35caSApple OSS Distributions });
83*c54f35caSApple OSS Distributions }
84*c54f35caSApple OSS Distributions
85*c54f35caSApple OSS Distributions struct label *
mac_sysvmsg_label(struct msg * msgptr)86*c54f35caSApple OSS Distributions mac_sysvmsg_label(struct msg *msgptr)
87*c54f35caSApple OSS Distributions {
88*c54f35caSApple OSS Distributions return mac_label_verify(&msgptr->label);
89*c54f35caSApple OSS Distributions }
90*c54f35caSApple OSS Distributions
91*c54f35caSApple OSS Distributions void
mac_sysvmsq_label_init(struct msqid_kernel * msqptr)92*c54f35caSApple OSS Distributions mac_sysvmsq_label_init(struct msqid_kernel *msqptr)
93*c54f35caSApple OSS Distributions {
94*c54f35caSApple OSS Distributions mac_labelzone_alloc_owned(&msqptr->label, MAC_WAITOK, ^(struct label *label) {
95*c54f35caSApple OSS Distributions MAC_PERFORM(sysvmsq_label_init, label);
96*c54f35caSApple OSS Distributions });
97*c54f35caSApple OSS Distributions }
98*c54f35caSApple OSS Distributions
99*c54f35caSApple OSS Distributions struct label *
mac_sysvmsq_label(struct msqid_kernel * msqptr)100*c54f35caSApple OSS Distributions mac_sysvmsq_label(struct msqid_kernel *msqptr)
101*c54f35caSApple OSS Distributions {
102*c54f35caSApple OSS Distributions return mac_label_verify(&msqptr->label);
103*c54f35caSApple OSS Distributions }
104*c54f35caSApple OSS Distributions
105*c54f35caSApple OSS Distributions void
mac_sysvmsg_label_associate(kauth_cred_t cred,struct msqid_kernel * msqptr,struct msg * msgptr)106*c54f35caSApple OSS Distributions mac_sysvmsg_label_associate(kauth_cred_t cred, struct msqid_kernel *msqptr,
107*c54f35caSApple OSS Distributions struct msg *msgptr)
108*c54f35caSApple OSS Distributions {
109*c54f35caSApple OSS Distributions MAC_PERFORM(sysvmsg_label_associate, cred, msqptr, mac_sysvmsq_label(msqptr),
110*c54f35caSApple OSS Distributions msgptr, mac_sysvmsg_label(msgptr));
111*c54f35caSApple OSS Distributions }
112*c54f35caSApple OSS Distributions
113*c54f35caSApple OSS Distributions void
mac_sysvmsq_label_associate(kauth_cred_t cred,struct msqid_kernel * msqptr)114*c54f35caSApple OSS Distributions mac_sysvmsq_label_associate(kauth_cred_t cred, struct msqid_kernel *msqptr)
115*c54f35caSApple OSS Distributions {
116*c54f35caSApple OSS Distributions MAC_PERFORM(sysvmsq_label_associate, cred, msqptr, mac_sysvmsq_label(msqptr));
117*c54f35caSApple OSS Distributions }
118*c54f35caSApple OSS Distributions
119*c54f35caSApple OSS Distributions void
mac_sysvmsg_label_recycle(struct msg * msgptr)120*c54f35caSApple OSS Distributions mac_sysvmsg_label_recycle(struct msg *msgptr)
121*c54f35caSApple OSS Distributions {
122*c54f35caSApple OSS Distributions MAC_PERFORM(sysvmsg_label_recycle, mac_sysvmsg_label(msgptr));
123*c54f35caSApple OSS Distributions }
124*c54f35caSApple OSS Distributions
125*c54f35caSApple OSS Distributions void
mac_sysvmsq_label_recycle(struct msqid_kernel * msqptr)126*c54f35caSApple OSS Distributions mac_sysvmsq_label_recycle(struct msqid_kernel *msqptr)
127*c54f35caSApple OSS Distributions {
128*c54f35caSApple OSS Distributions MAC_PERFORM(sysvmsq_label_recycle, mac_sysvmsq_label(msqptr));
129*c54f35caSApple OSS Distributions }
130*c54f35caSApple OSS Distributions
131*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_enqueue(kauth_cred_t cred,struct msg * msgptr,struct msqid_kernel * msqptr)132*c54f35caSApple OSS Distributions mac_sysvmsq_check_enqueue(kauth_cred_t cred, struct msg *msgptr,
133*c54f35caSApple OSS Distributions struct msqid_kernel *msqptr)
134*c54f35caSApple OSS Distributions {
135*c54f35caSApple OSS Distributions int error;
136*c54f35caSApple OSS Distributions
137*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
138*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
139*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
140*c54f35caSApple OSS Distributions return 0;
141*c54f35caSApple OSS Distributions }
142*c54f35caSApple OSS Distributions #endif
143*c54f35caSApple OSS Distributions
144*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_enqueue, cred, msgptr, mac_sysvmsg_label(msgptr), msqptr,
145*c54f35caSApple OSS Distributions mac_sysvmsq_label(msqptr));
146*c54f35caSApple OSS Distributions
147*c54f35caSApple OSS Distributions return error;
148*c54f35caSApple OSS Distributions }
149*c54f35caSApple OSS Distributions
150*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_msgrcv(kauth_cred_t cred,struct msg * msgptr)151*c54f35caSApple OSS Distributions mac_sysvmsq_check_msgrcv(kauth_cred_t cred, struct msg *msgptr)
152*c54f35caSApple OSS Distributions {
153*c54f35caSApple OSS Distributions int error;
154*c54f35caSApple OSS Distributions
155*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
156*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
157*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
158*c54f35caSApple OSS Distributions return 0;
159*c54f35caSApple OSS Distributions }
160*c54f35caSApple OSS Distributions #endif
161*c54f35caSApple OSS Distributions
162*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_msgrcv, cred, msgptr, mac_sysvmsg_label(msgptr));
163*c54f35caSApple OSS Distributions
164*c54f35caSApple OSS Distributions return error;
165*c54f35caSApple OSS Distributions }
166*c54f35caSApple OSS Distributions
167*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_msgrmid(kauth_cred_t cred,struct msg * msgptr)168*c54f35caSApple OSS Distributions mac_sysvmsq_check_msgrmid(kauth_cred_t cred, struct msg *msgptr)
169*c54f35caSApple OSS Distributions {
170*c54f35caSApple OSS Distributions int error;
171*c54f35caSApple OSS Distributions
172*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
173*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
174*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
175*c54f35caSApple OSS Distributions return 0;
176*c54f35caSApple OSS Distributions }
177*c54f35caSApple OSS Distributions #endif
178*c54f35caSApple OSS Distributions
179*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_msgrmid, cred, msgptr, mac_sysvmsg_label(msgptr));
180*c54f35caSApple OSS Distributions
181*c54f35caSApple OSS Distributions return error;
182*c54f35caSApple OSS Distributions }
183*c54f35caSApple OSS Distributions
184*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_msqget(kauth_cred_t cred,struct msqid_kernel * msqptr)185*c54f35caSApple OSS Distributions mac_sysvmsq_check_msqget(kauth_cred_t cred, struct msqid_kernel *msqptr)
186*c54f35caSApple OSS Distributions {
187*c54f35caSApple OSS Distributions int error;
188*c54f35caSApple OSS Distributions
189*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
190*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
191*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
192*c54f35caSApple OSS Distributions return 0;
193*c54f35caSApple OSS Distributions }
194*c54f35caSApple OSS Distributions #endif
195*c54f35caSApple OSS Distributions
196*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_msqget, cred, msqptr, mac_sysvmsq_label(msqptr));
197*c54f35caSApple OSS Distributions
198*c54f35caSApple OSS Distributions return error;
199*c54f35caSApple OSS Distributions }
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_msqsnd(kauth_cred_t cred,struct msqid_kernel * msqptr)202*c54f35caSApple OSS Distributions mac_sysvmsq_check_msqsnd(kauth_cred_t cred, struct msqid_kernel *msqptr)
203*c54f35caSApple OSS Distributions {
204*c54f35caSApple OSS Distributions int error;
205*c54f35caSApple OSS Distributions
206*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
207*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
208*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
209*c54f35caSApple OSS Distributions return 0;
210*c54f35caSApple OSS Distributions }
211*c54f35caSApple OSS Distributions #endif
212*c54f35caSApple OSS Distributions
213*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_msqsnd, cred, msqptr, mac_sysvmsq_label(msqptr));
214*c54f35caSApple OSS Distributions
215*c54f35caSApple OSS Distributions return error;
216*c54f35caSApple OSS Distributions }
217*c54f35caSApple OSS Distributions
218*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_msqrcv(kauth_cred_t cred,struct msqid_kernel * msqptr)219*c54f35caSApple OSS Distributions mac_sysvmsq_check_msqrcv(kauth_cred_t cred, struct msqid_kernel *msqptr)
220*c54f35caSApple OSS Distributions {
221*c54f35caSApple OSS Distributions int error;
222*c54f35caSApple OSS Distributions
223*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
224*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
225*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
226*c54f35caSApple OSS Distributions return 0;
227*c54f35caSApple OSS Distributions }
228*c54f35caSApple OSS Distributions #endif
229*c54f35caSApple OSS Distributions
230*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_msqrcv, cred, msqptr, mac_sysvmsq_label(msqptr));
231*c54f35caSApple OSS Distributions
232*c54f35caSApple OSS Distributions return error;
233*c54f35caSApple OSS Distributions }
234*c54f35caSApple OSS Distributions
235*c54f35caSApple OSS Distributions int
mac_sysvmsq_check_msqctl(kauth_cred_t cred,struct msqid_kernel * msqptr,int cmd)236*c54f35caSApple OSS Distributions mac_sysvmsq_check_msqctl(kauth_cred_t cred, struct msqid_kernel *msqptr,
237*c54f35caSApple OSS Distributions int cmd)
238*c54f35caSApple OSS Distributions {
239*c54f35caSApple OSS Distributions int error;
240*c54f35caSApple OSS Distributions
241*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
242*c54f35caSApple OSS Distributions /* 21167099 - only check if we allow write */
243*c54f35caSApple OSS Distributions if (!mac_sysvmsg_enforce) {
244*c54f35caSApple OSS Distributions return 0;
245*c54f35caSApple OSS Distributions }
246*c54f35caSApple OSS Distributions #endif
247*c54f35caSApple OSS Distributions
248*c54f35caSApple OSS Distributions MAC_CHECK(sysvmsq_check_msqctl, cred, msqptr, mac_sysvmsq_label(msqptr), cmd);
249*c54f35caSApple OSS Distributions
250*c54f35caSApple OSS Distributions return error;
251*c54f35caSApple OSS Distributions }
252