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