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
50int
51mac_file_check_create(struct ucred *cred)
52{
53 int error;
54
55 MAC_CHECK(file_check_create, cred);
56 return error;
57}
58
59int
60mac_file_check_dup(struct ucred *cred, struct fileglob *fg, int newfd)
61{
62 int error;
63
64 MAC_CHECK(file_check_dup, cred, fg, NULL, newfd);
65 return error;
66}
67
68int
69mac_file_check_fcntl(struct ucred *cred, struct fileglob *fg, int cmd,
70 user_long_t arg)
71{
72 int error;
73
74 MAC_CHECK(file_check_fcntl, cred, fg, NULL, cmd, arg);
75 return error;
76}
77
78int
79mac_file_check_ioctl(struct ucred *cred, struct fileglob *fg, u_long cmd)
80{
81 int error;
82
83 MAC_CHECK(file_check_ioctl, cred, fg, NULL, cmd);
84 return error;
85}
86
87int
88mac_file_check_inherit(struct ucred *cred, struct fileglob *fg)
89{
90 int error;
91
92 MAC_CHECK(file_check_inherit, cred, fg, NULL);
93 return error;
94}
95
96int
97mac_file_check_receive(struct ucred *cred, struct fileglob *fg)
98{
99 int error;
100
101 MAC_CHECK(file_check_receive, cred, fg, NULL);
102 return error;
103}
104
105int
106mac_file_check_get_offset(struct ucred *cred, struct fileglob *fg)
107{
108 int error;
109
110 MAC_CHECK(file_check_get_offset, cred, fg, NULL);
111 return error;
112}
113
114int
115mac_file_check_change_offset(struct ucred *cred, struct fileglob *fg)
116{
117 int error;
118
119 MAC_CHECK(file_check_change_offset, cred, fg, NULL);
120 return error;
121}
122
123int
124mac_file_check_get(struct ucred *cred, struct fileglob *fg, char *elements,
125 size_t len)
126{
127 int error;
128
129 MAC_CHECK(file_check_get, cred, fg, elements, len);
130 return error;
131}
132
133int
134mac_file_check_set(struct ucred *cred, struct fileglob *fg, char *buf,
135 size_t buflen)
136{
137 int error;
138
139 MAC_CHECK(file_check_set, cred, fg, buf, buflen);
140 return error;
141}
142
143int
144mac_file_check_lock(struct ucred *cred, struct fileglob *fg, int op,
145 struct flock *fl)
146{
147 int error;
148
149 MAC_CHECK(file_check_lock, cred, fg, NULL, op, fl);
150 return error;
151}
152
153int
154mac_file_check_library_validation(struct proc *proc,
155 struct fileglob *fg, off_t slice_offset,
156 user_long_t error_message, size_t error_message_size)
157{
158 int error;
159
160 MAC_CHECK(file_check_library_validation, proc, fg, slice_offset, error_message, error_message_size);
161 return error;
162}
163
164/*
165 * On some platforms, VM_PROT_READ implies VM_PROT_EXECUTE. If that is true,
166 * both prot and maxprot will have VM_PROT_EXECUTE set after file_check_mmap
167 * if VM_PROT_READ is set.
168 *
169 * The type of maxprot in file_check_mmap must be equivalent to vm_prot_t *
170 * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
171 * files, so cannot use the typedef itself.
172 */
173int
174mac_file_check_mmap(struct ucred *cred, struct fileglob *fg, int prot,
175 int flags, uint64_t offset, int *maxprot)
176{
177 int error;
178 int maxp;
179
180 maxp = *maxprot;
181 MAC_CHECK(file_check_mmap, cred, fg, NULL, prot, flags, offset, &maxp);
182 if ((maxp | *maxprot) != *maxprot) {
183 panic("file_check_mmap increased max protections");
184 }
185 *maxprot = maxp;
186 return error;
187}
188
189void
190mac_file_check_mmap_downgrade(struct ucred *cred, struct fileglob *fg,
191 int *prot)
192{
193 int result = *prot;
194
195 MAC_PERFORM(file_check_mmap_downgrade, cred, fg, NULL, &result);
196
197 *prot = result;
198}
199
200void
201mac_file_notify_close(struct ucred *cred, struct fileglob *fg)
202{
203 MAC_PERFORM(file_notify_close, cred, fg, NULL, ((fg->fg_flag & FWASWRITTEN) ? 1 : 0));
204}
205
206
207/*
208 * fileglob XATTR helpers.
209 */
210
211int
212mac_file_setxattr(struct fileglob *fg, const char *name, char *buf, size_t len)
213{
214 struct vnode *vp = NULL;
215
216 if (!fg || FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
217 return EFTYPE;
218 }
219
220 vp = (struct vnode *)fg_get_data(fg);
221 int error = vnode_getwithref(vp);
222 if (error) {
223 return error;
224 }
225 error = mac_vnop_setxattr(vp, name, buf, len);
226 vnode_put(vp);
227 return error;
228}
229
230int
231mac_file_getxattr(struct fileglob *fg, const char *name, char *buf, size_t len,
232 size_t *attrlen)
233{
234 struct vnode *vp = NULL;
235
236 if (!fg || FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
237 return EFTYPE;
238 }
239
240 vp = (struct vnode *)fg_get_data(fg);
241 int error = vnode_getwithref(vp);
242 if (error) {
243 return error;
244 }
245 error = mac_vnop_getxattr(vp, name, buf, len, attrlen);
246 vnode_put(vp);
247 return error;
248}
249
250int
251mac_file_removexattr(struct fileglob *fg, const char *name)
252{
253 struct vnode *vp = NULL;
254
255 if (!fg || FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
256 return EFTYPE;
257 }
258
259 vp = (struct vnode *)fg_get_data(fg);
260 int error = vnode_getwithref(vp);
261 if (error) {
262 return error;
263 }
264 error = mac_vnop_removexattr(vp, name);
265 vnode_put(vp);
266 return error;
267}
268