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