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