xref: /xnu-10002.61.3/security/mac_file.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions /*-
2*0f4c859eSApple OSS Distributions  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
3*0f4c859eSApple OSS Distributions  * Copyright (c) 2006 SPARTA, Inc.
4*0f4c859eSApple OSS Distributions  * All rights reserved.
5*0f4c859eSApple OSS Distributions  *
6*0f4c859eSApple OSS Distributions  * This software was developed for the FreeBSD Project in part by Network
7*0f4c859eSApple OSS Distributions  * Associates Laboratories, the Security Research Division of Network
8*0f4c859eSApple OSS Distributions  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9*0f4c859eSApple OSS Distributions  * as part of the DARPA CHATS research program.
10*0f4c859eSApple OSS Distributions  *
11*0f4c859eSApple OSS Distributions  * This software was enhanced by SPARTA ISSO under SPAWAR contract
12*0f4c859eSApple OSS Distributions  * N66001-04-C-6019 ("SEFOS").
13*0f4c859eSApple OSS Distributions  *
14*0f4c859eSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
15*0f4c859eSApple OSS Distributions  * modification, are permitted provided that the following conditions
16*0f4c859eSApple OSS Distributions  * are met:
17*0f4c859eSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
18*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
19*0f4c859eSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
20*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
21*0f4c859eSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
22*0f4c859eSApple OSS Distributions  *
23*0f4c859eSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24*0f4c859eSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*0f4c859eSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*0f4c859eSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27*0f4c859eSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*0f4c859eSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*0f4c859eSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*0f4c859eSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*0f4c859eSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*0f4c859eSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*0f4c859eSApple OSS Distributions  * SUCH DAMAGE.
34*0f4c859eSApple OSS Distributions  */
35*0f4c859eSApple OSS Distributions 
36*0f4c859eSApple OSS Distributions #include <sys/param.h>
37*0f4c859eSApple OSS Distributions #include <sys/kernel.h>
38*0f4c859eSApple OSS Distributions #include <sys/lock.h>
39*0f4c859eSApple OSS Distributions #include <sys/malloc.h>
40*0f4c859eSApple OSS Distributions #include <sys/proc.h>
41*0f4c859eSApple OSS Distributions #include <sys/sbuf.h>
42*0f4c859eSApple OSS Distributions #include <sys/systm.h>
43*0f4c859eSApple OSS Distributions #include <sys/vnode.h>
44*0f4c859eSApple OSS Distributions #include <sys/vnode_internal.h>
45*0f4c859eSApple OSS Distributions #include <sys/file.h>
46*0f4c859eSApple OSS Distributions #include <sys/file_internal.h>
47*0f4c859eSApple OSS Distributions 
48*0f4c859eSApple OSS Distributions #include <security/mac_internal.h>
49*0f4c859eSApple OSS Distributions 
50*0f4c859eSApple OSS Distributions int
mac_file_check_create(struct ucred * cred)51*0f4c859eSApple OSS Distributions mac_file_check_create(struct ucred *cred)
52*0f4c859eSApple OSS Distributions {
53*0f4c859eSApple OSS Distributions 	int error;
54*0f4c859eSApple OSS Distributions 
55*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_create, cred);
56*0f4c859eSApple OSS Distributions 	return error;
57*0f4c859eSApple OSS Distributions }
58*0f4c859eSApple OSS Distributions 
59*0f4c859eSApple OSS Distributions int
mac_file_check_dup(struct ucred * cred,struct fileglob * fg,int newfd)60*0f4c859eSApple OSS Distributions mac_file_check_dup(struct ucred *cred, struct fileglob *fg, int newfd)
61*0f4c859eSApple OSS Distributions {
62*0f4c859eSApple OSS Distributions 	int error;
63*0f4c859eSApple OSS Distributions 
64*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_dup, cred, fg, NULL, newfd);
65*0f4c859eSApple OSS Distributions 	return error;
66*0f4c859eSApple OSS Distributions }
67*0f4c859eSApple OSS Distributions 
68*0f4c859eSApple OSS Distributions int
mac_file_check_fcntl(struct ucred * cred,struct fileglob * fg,int cmd,user_long_t arg)69*0f4c859eSApple OSS Distributions mac_file_check_fcntl(struct ucred *cred, struct fileglob *fg, int cmd,
70*0f4c859eSApple OSS Distributions     user_long_t arg)
71*0f4c859eSApple OSS Distributions {
72*0f4c859eSApple OSS Distributions 	int error;
73*0f4c859eSApple OSS Distributions 
74*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_fcntl, cred, fg, NULL, cmd, arg);
75*0f4c859eSApple OSS Distributions 	return error;
76*0f4c859eSApple OSS Distributions }
77*0f4c859eSApple OSS Distributions 
78*0f4c859eSApple OSS Distributions int
mac_file_check_ioctl(struct ucred * cred,struct fileglob * fg,u_long cmd)79*0f4c859eSApple OSS Distributions mac_file_check_ioctl(struct ucred *cred, struct fileglob *fg, u_long cmd)
80*0f4c859eSApple OSS Distributions {
81*0f4c859eSApple OSS Distributions 	int error;
82*0f4c859eSApple OSS Distributions 
83*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_ioctl, cred, fg, NULL, cmd);
84*0f4c859eSApple OSS Distributions 	return error;
85*0f4c859eSApple OSS Distributions }
86*0f4c859eSApple OSS Distributions 
87*0f4c859eSApple OSS Distributions int
mac_file_check_inherit(struct ucred * cred,struct fileglob * fg)88*0f4c859eSApple OSS Distributions mac_file_check_inherit(struct ucred *cred, struct fileglob *fg)
89*0f4c859eSApple OSS Distributions {
90*0f4c859eSApple OSS Distributions 	int error;
91*0f4c859eSApple OSS Distributions 
92*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_inherit, cred, fg, NULL);
93*0f4c859eSApple OSS Distributions 	return error;
94*0f4c859eSApple OSS Distributions }
95*0f4c859eSApple OSS Distributions 
96*0f4c859eSApple OSS Distributions int
mac_file_check_receive(struct ucred * cred,struct fileglob * fg)97*0f4c859eSApple OSS Distributions mac_file_check_receive(struct ucred *cred, struct fileglob *fg)
98*0f4c859eSApple OSS Distributions {
99*0f4c859eSApple OSS Distributions 	int error;
100*0f4c859eSApple OSS Distributions 
101*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_receive, cred, fg, NULL);
102*0f4c859eSApple OSS Distributions 	return error;
103*0f4c859eSApple OSS Distributions }
104*0f4c859eSApple OSS Distributions 
105*0f4c859eSApple OSS Distributions int
mac_file_check_get_offset(struct ucred * cred,struct fileglob * fg)106*0f4c859eSApple OSS Distributions mac_file_check_get_offset(struct ucred *cred, struct fileglob *fg)
107*0f4c859eSApple OSS Distributions {
108*0f4c859eSApple OSS Distributions 	int error;
109*0f4c859eSApple OSS Distributions 
110*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_get_offset, cred, fg, NULL);
111*0f4c859eSApple OSS Distributions 	return error;
112*0f4c859eSApple OSS Distributions }
113*0f4c859eSApple OSS Distributions 
114*0f4c859eSApple OSS Distributions int
mac_file_check_change_offset(struct ucred * cred,struct fileglob * fg)115*0f4c859eSApple OSS Distributions mac_file_check_change_offset(struct ucred *cred, struct fileglob *fg)
116*0f4c859eSApple OSS Distributions {
117*0f4c859eSApple OSS Distributions 	int error;
118*0f4c859eSApple OSS Distributions 
119*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_change_offset, cred, fg, NULL);
120*0f4c859eSApple OSS Distributions 	return error;
121*0f4c859eSApple OSS Distributions }
122*0f4c859eSApple OSS Distributions 
123*0f4c859eSApple OSS Distributions int
mac_file_check_get(struct ucred * cred,struct fileglob * fg,char * elements,size_t len)124*0f4c859eSApple OSS Distributions mac_file_check_get(struct ucred *cred, struct fileglob *fg, char *elements,
125*0f4c859eSApple OSS Distributions     size_t len)
126*0f4c859eSApple OSS Distributions {
127*0f4c859eSApple OSS Distributions 	int error;
128*0f4c859eSApple OSS Distributions 
129*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_get, cred, fg, elements, len);
130*0f4c859eSApple OSS Distributions 	return error;
131*0f4c859eSApple OSS Distributions }
132*0f4c859eSApple OSS Distributions 
133*0f4c859eSApple OSS Distributions int
mac_file_check_set(struct ucred * cred,struct fileglob * fg,char * buf,size_t buflen)134*0f4c859eSApple OSS Distributions mac_file_check_set(struct ucred *cred, struct fileglob *fg, char *buf,
135*0f4c859eSApple OSS Distributions     size_t buflen)
136*0f4c859eSApple OSS Distributions {
137*0f4c859eSApple OSS Distributions 	int error;
138*0f4c859eSApple OSS Distributions 
139*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_set, cred, fg, buf, buflen);
140*0f4c859eSApple OSS Distributions 	return error;
141*0f4c859eSApple OSS Distributions }
142*0f4c859eSApple OSS Distributions 
143*0f4c859eSApple OSS Distributions int
mac_file_check_lock(struct ucred * cred,struct fileglob * fg,int op,struct flock * fl)144*0f4c859eSApple OSS Distributions mac_file_check_lock(struct ucred *cred, struct fileglob *fg, int op,
145*0f4c859eSApple OSS Distributions     struct flock *fl)
146*0f4c859eSApple OSS Distributions {
147*0f4c859eSApple OSS Distributions 	int error;
148*0f4c859eSApple OSS Distributions 
149*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_lock, cred, fg, NULL, op, fl);
150*0f4c859eSApple OSS Distributions 	return error;
151*0f4c859eSApple OSS Distributions }
152*0f4c859eSApple OSS Distributions 
153*0f4c859eSApple OSS Distributions int
mac_file_check_library_validation(struct proc * proc,struct fileglob * fg,off_t slice_offset,user_long_t error_message,size_t error_message_size)154*0f4c859eSApple OSS Distributions mac_file_check_library_validation(struct proc *proc,
155*0f4c859eSApple OSS Distributions     struct fileglob *fg, off_t slice_offset,
156*0f4c859eSApple OSS Distributions     user_long_t error_message, size_t error_message_size)
157*0f4c859eSApple OSS Distributions {
158*0f4c859eSApple OSS Distributions 	int error;
159*0f4c859eSApple OSS Distributions 
160*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_library_validation, proc, fg, slice_offset, error_message, error_message_size);
161*0f4c859eSApple OSS Distributions 	return error;
162*0f4c859eSApple OSS Distributions }
163*0f4c859eSApple OSS Distributions 
164*0f4c859eSApple OSS Distributions /*
165*0f4c859eSApple OSS Distributions  * On some platforms, VM_PROT_READ implies VM_PROT_EXECUTE. If that is true,
166*0f4c859eSApple OSS Distributions  * both prot and maxprot will have VM_PROT_EXECUTE set after file_check_mmap
167*0f4c859eSApple OSS Distributions  * if VM_PROT_READ is set.
168*0f4c859eSApple OSS Distributions  *
169*0f4c859eSApple OSS Distributions  * The type of maxprot in file_check_mmap must be equivalent to vm_prot_t *
170*0f4c859eSApple OSS Distributions  * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
171*0f4c859eSApple OSS Distributions  * files, so cannot use the typedef itself.
172*0f4c859eSApple OSS Distributions  */
173*0f4c859eSApple OSS Distributions int
mac_file_check_mmap(struct ucred * cred,struct fileglob * fg,int prot,int flags,uint64_t offset,int * maxprot)174*0f4c859eSApple OSS Distributions mac_file_check_mmap(struct ucred *cred, struct fileglob *fg, int prot,
175*0f4c859eSApple OSS Distributions     int flags, uint64_t offset, int *maxprot)
176*0f4c859eSApple OSS Distributions {
177*0f4c859eSApple OSS Distributions 	int error;
178*0f4c859eSApple OSS Distributions 	int maxp;
179*0f4c859eSApple OSS Distributions 
180*0f4c859eSApple OSS Distributions 	maxp = *maxprot;
181*0f4c859eSApple OSS Distributions 	MAC_CHECK(file_check_mmap, cred, fg, NULL, prot, flags, offset, &maxp);
182*0f4c859eSApple OSS Distributions 	if ((maxp | *maxprot) != *maxprot) {
183*0f4c859eSApple OSS Distributions 		panic("file_check_mmap increased max protections");
184*0f4c859eSApple OSS Distributions 	}
185*0f4c859eSApple OSS Distributions 	*maxprot = maxp;
186*0f4c859eSApple OSS Distributions 	return error;
187*0f4c859eSApple OSS Distributions }
188*0f4c859eSApple OSS Distributions 
189*0f4c859eSApple OSS Distributions void
mac_file_check_mmap_downgrade(struct ucred * cred,struct fileglob * fg,int * prot)190*0f4c859eSApple OSS Distributions mac_file_check_mmap_downgrade(struct ucred *cred, struct fileglob *fg,
191*0f4c859eSApple OSS Distributions     int *prot)
192*0f4c859eSApple OSS Distributions {
193*0f4c859eSApple OSS Distributions 	int result = *prot;
194*0f4c859eSApple OSS Distributions 
195*0f4c859eSApple OSS Distributions 	MAC_PERFORM(file_check_mmap_downgrade, cred, fg, NULL, &result);
196*0f4c859eSApple OSS Distributions 
197*0f4c859eSApple OSS Distributions 	*prot = result;
198*0f4c859eSApple OSS Distributions }
199*0f4c859eSApple OSS Distributions 
200*0f4c859eSApple OSS Distributions void
mac_file_notify_close(struct ucred * cred,struct fileglob * fg)201*0f4c859eSApple OSS Distributions mac_file_notify_close(struct ucred *cred, struct fileglob *fg)
202*0f4c859eSApple OSS Distributions {
203*0f4c859eSApple OSS Distributions 	MAC_PERFORM(file_notify_close, cred, fg, NULL, ((fg->fg_flag & FWASWRITTEN) ? 1 : 0));
204*0f4c859eSApple OSS Distributions }
205*0f4c859eSApple OSS Distributions 
206*0f4c859eSApple OSS Distributions 
207*0f4c859eSApple OSS Distributions /*
208*0f4c859eSApple OSS Distributions  * fileglob XATTR helpers.
209*0f4c859eSApple OSS Distributions  */
210*0f4c859eSApple OSS Distributions 
211*0f4c859eSApple OSS Distributions int
mac_file_setxattr(struct fileglob * fg,const char * name,char * buf,size_t len)212*0f4c859eSApple OSS Distributions mac_file_setxattr(struct fileglob *fg, const char *name, char *buf, size_t len)
213*0f4c859eSApple OSS Distributions {
214*0f4c859eSApple OSS Distributions 	struct vnode *vp = NULL;
215*0f4c859eSApple OSS Distributions 
216*0f4c859eSApple OSS Distributions 	if (!fg || FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
217*0f4c859eSApple OSS Distributions 		return EFTYPE;
218*0f4c859eSApple OSS Distributions 	}
219*0f4c859eSApple OSS Distributions 
220*0f4c859eSApple OSS Distributions 	vp = (struct vnode *)fg_get_data(fg);
221*0f4c859eSApple OSS Distributions 	int error = vnode_getwithref(vp);
222*0f4c859eSApple OSS Distributions 	if (error) {
223*0f4c859eSApple OSS Distributions 		return error;
224*0f4c859eSApple OSS Distributions 	}
225*0f4c859eSApple OSS Distributions 	error = mac_vnop_setxattr(vp, name, buf, len);
226*0f4c859eSApple OSS Distributions 	vnode_put(vp);
227*0f4c859eSApple OSS Distributions 	return error;
228*0f4c859eSApple OSS Distributions }
229*0f4c859eSApple OSS Distributions 
230*0f4c859eSApple OSS Distributions int
mac_file_getxattr(struct fileglob * fg,const char * name,char * buf,size_t len,size_t * attrlen)231*0f4c859eSApple OSS Distributions mac_file_getxattr(struct fileglob *fg, const char *name, char *buf, size_t len,
232*0f4c859eSApple OSS Distributions     size_t *attrlen)
233*0f4c859eSApple OSS Distributions {
234*0f4c859eSApple OSS Distributions 	struct vnode *vp = NULL;
235*0f4c859eSApple OSS Distributions 
236*0f4c859eSApple OSS Distributions 	if (!fg || FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
237*0f4c859eSApple OSS Distributions 		return EFTYPE;
238*0f4c859eSApple OSS Distributions 	}
239*0f4c859eSApple OSS Distributions 
240*0f4c859eSApple OSS Distributions 	vp = (struct vnode *)fg_get_data(fg);
241*0f4c859eSApple OSS Distributions 	int error = vnode_getwithref(vp);
242*0f4c859eSApple OSS Distributions 	if (error) {
243*0f4c859eSApple OSS Distributions 		return error;
244*0f4c859eSApple OSS Distributions 	}
245*0f4c859eSApple OSS Distributions 	error = mac_vnop_getxattr(vp, name, buf, len, attrlen);
246*0f4c859eSApple OSS Distributions 	vnode_put(vp);
247*0f4c859eSApple OSS Distributions 	return error;
248*0f4c859eSApple OSS Distributions }
249*0f4c859eSApple OSS Distributions 
250*0f4c859eSApple OSS Distributions int
mac_file_removexattr(struct fileglob * fg,const char * name)251*0f4c859eSApple OSS Distributions mac_file_removexattr(struct fileglob *fg, const char *name)
252*0f4c859eSApple OSS Distributions {
253*0f4c859eSApple OSS Distributions 	struct vnode *vp = NULL;
254*0f4c859eSApple OSS Distributions 
255*0f4c859eSApple OSS Distributions 	if (!fg || FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
256*0f4c859eSApple OSS Distributions 		return EFTYPE;
257*0f4c859eSApple OSS Distributions 	}
258*0f4c859eSApple OSS Distributions 
259*0f4c859eSApple OSS Distributions 	vp = (struct vnode *)fg_get_data(fg);
260*0f4c859eSApple OSS Distributions 	int error = vnode_getwithref(vp);
261*0f4c859eSApple OSS Distributions 	if (error) {
262*0f4c859eSApple OSS Distributions 		return error;
263*0f4c859eSApple OSS Distributions 	}
264*0f4c859eSApple OSS Distributions 	error = mac_vnop_removexattr(vp, name);
265*0f4c859eSApple OSS Distributions 	vnode_put(vp);
266*0f4c859eSApple OSS Distributions 	return error;
267*0f4c859eSApple OSS Distributions }
268