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