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