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