]> git.saurik.com Git - apple/xnu.git/blame - security/mac_vfs_subr.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / security / mac_vfs_subr.c
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28#include <sys/param.h>
29#include <sys/vnode.h>
30#include <sys/vnode_internal.h>
31#include <sys/kauth.h>
32#include <sys/namei.h>
33#include <sys/mount.h>
34#include <sys/mount_internal.h>
35#include <sys/uio_internal.h>
36#include <sys/xattr.h>
b0d623f7 37#include "../bsd/sys/fsevents.h"
2d21ac55
A
38
39#include <security/mac_internal.h>
40
41/*
b0d623f7 42 * Caller holds I/O reference on vnode
2d21ac55
A
43 */
44int
45vnode_label(struct mount *mp, struct vnode *dvp, struct vnode *vp,
0a7de745 46 struct componentname *cnp, int flags, vfs_context_t ctx)
2d21ac55 47{
b0d623f7 48 int error = 0;
cb323159 49 bool exit_fast;
2d21ac55 50
b0d623f7 51 /* fast path checks... */
2d21ac55 52
b0d623f7 53 /* are we labeling vnodes? If not still notify of create */
cb323159
A
54#if CONFIG_MACF_LAZY_VNODE_LABELS
55 exit_fast = true;
56#else
57 exit_fast = (mac_label_vnodes == 0);
58#endif
59 if (exit_fast) {
0a7de745 60 if (flags & VNODE_LABEL_CREATE) {
b0d623f7
A
61 error = mac_vnode_notify_create(ctx,
62 mp, dvp, vp, cnp);
0a7de745 63 }
b0d623f7 64 return 0;
2d21ac55
A
65 }
66
b0d623f7 67 /* if already VL_LABELED */
0a7de745
A
68 if (vp->v_lflag & VL_LABELED) {
69 return 0;
70 }
b0d623f7
A
71
72 vnode_lock_spin(vp);
73
74 /*
75 * must revalidate state once we hold the lock
76 * since we could have blocked and someone else
77 * has since labeled this vnode
78 */
79 if (vp->v_lflag & VL_LABELED) {
2d21ac55 80 vnode_unlock(vp);
0a7de745 81 return 0;
2d21ac55
A
82 }
83
84 if ((vp->v_lflag & VL_LABEL) == 0) {
85 vp->v_lflag |= VL_LABEL;
86
87 /* Could sleep on disk I/O, drop lock. */
88 vnode_unlock(vp);
b0d623f7 89
0a7de745 90 if (vp->v_label == NULL) {
b0d623f7 91 vp->v_label = mac_vnode_label_alloc();
0a7de745 92 }
b0d623f7 93
0a7de745 94 if (flags & VNODE_LABEL_CREATE) {
2d21ac55
A
95 error = mac_vnode_notify_create(ctx,
96 mp, dvp, vp, cnp);
0a7de745 97 } else {
2d21ac55 98 error = mac_vnode_label_associate(mp, vp, ctx);
0a7de745 99 }
b0d623f7
A
100
101 vnode_lock_spin(vp);
2d21ac55 102
0a7de745 103 if ((error == 0) && (vp->v_flag & VNCACHEABLE)) {
2d21ac55 104 vp->v_lflag |= VL_LABELED;
0a7de745 105 }
2d21ac55
A
106 vp->v_lflag &= ~VL_LABEL;
107
108 if (vp->v_lflag & VL_LABELWAIT) {
109 vp->v_lflag &= ~VL_LABELWAIT;
b0d623f7 110 wakeup(&vp->v_label);
2d21ac55 111 }
2d21ac55
A
112 } else {
113 struct timespec ts;
114
115 ts.tv_sec = 10;
116 ts.tv_nsec = 0;
117
118 while (vp->v_lflag & VL_LABEL) {
119 vp->v_lflag |= VL_LABELWAIT;
b0d623f7 120
0a7de745
A
121 error = msleep(&vp->v_label, &vp->v_lock, PVFS | PDROP,
122 "vnode_label", &ts);
b0d623f7
A
123 vnode_lock_spin(vp);
124
2d21ac55
A
125 if (error == EWOULDBLOCK) {
126 vprint("vnode label timeout", vp);
127 break;
128 }
129 }
130 /* XXX: what should be done if labeling failed (above)? */
2d21ac55 131 }
b0d623f7 132 vnode_unlock(vp);
2d21ac55 133
0a7de745 134 return error;
2d21ac55
A
135}
136
137
138/*
139 * Clear the "labeled" flag on a VNODE.
140 * VNODE will have label re-associated upon
141 * next call to lookup().
142 *
143 * Caller verifies vfs_flags(vnode_mount(vp)) & MNT_MULTILABEL
144 * Caller holds vnode lock.
145 */
146void
147vnode_relabel(struct vnode *vp)
148{
2d21ac55
A
149 /* Wait for any other labeling to complete. */
150 while (vp->v_lflag & VL_LABEL) {
151 vp->v_lflag |= VL_LABELWAIT;
b0d623f7 152 (void)msleep(&vp->v_label, &vp->v_lock, PVFS, "vnode_relabel", 0);
2d21ac55
A
153 }
154
155 /* Clear labeled flag */
156 vp->v_lflag &= ~VL_LABELED;
157
158 return;
159}
160
161/*
162 * VFS XATTR helpers.
163 */
164
165int
0a7de745 166mac_vnop_setxattr(struct vnode *vp, const char *name, char *buf, size_t len)
2d21ac55
A
167{
168 vfs_context_t ctx;
169 int options = XATTR_NOSECURITY;
0a7de745
A
170 char uio_buf[UIO_SIZEOF(1)];
171 uio_t auio;
2d21ac55
A
172 int error;
173
0a7de745
A
174 if (vfs_isrdonly(vp->v_mount)) {
175 return EROFS;
176 }
2d21ac55
A
177
178 ctx = vfs_context_current();
179 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE,
0a7de745 180 &uio_buf[0], sizeof(uio_buf));
2d21ac55
A
181 uio_addiov(auio, CAST_USER_ADDR_T(buf), len);
182
183 error = vn_setxattr(vp, name, auio, options, ctx);
b0d623f7
A
184#if CONFIG_FSE
185 if (error == 0) {
186 add_fsevent(FSE_XATTR_MODIFIED, ctx,
187 FSE_ARG_VNODE, vp,
188 FSE_ARG_DONE);
189 }
190#endif
2d21ac55 191
0a7de745 192 return error;
2d21ac55
A
193}
194
195int
0a7de745
A
196mac_vnop_getxattr(struct vnode *vp, const char *name, char *buf, size_t len,
197 size_t *attrlen)
2d21ac55
A
198{
199 vfs_context_t ctx = vfs_context_current();
200 int options = XATTR_NOSECURITY;
0a7de745
A
201 char uio_buf[UIO_SIZEOF(1)];
202 uio_t auio;
2d21ac55
A
203 int error;
204
205 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
0a7de745 206 &uio_buf[0], sizeof(uio_buf));
2d21ac55
A
207 uio_addiov(auio, CAST_USER_ADDR_T(buf), len);
208
209 error = vn_getxattr(vp, name, auio, attrlen, options, ctx);
210 *attrlen = len - uio_resid(auio);
211
0a7de745 212 return error;
2d21ac55
A
213}
214
215int
0a7de745 216mac_vnop_removexattr(struct vnode *vp, const char *name)
2d21ac55
A
217{
218 vfs_context_t ctx = vfs_context_current();
219 int options = XATTR_NOSECURITY;
220 int error;
221
0a7de745
A
222 if (vfs_isrdonly(vp->v_mount)) {
223 return EROFS;
224 }
2d21ac55
A
225
226 error = vn_removexattr(vp, name, options, ctx);
b0d623f7
A
227#if CONFIG_FSE
228 if (error == 0) {
229 add_fsevent(FSE_XATTR_REMOVED, ctx,
230 FSE_ARG_VNODE, vp,
231 FSE_ARG_DONE);
232 }
233#endif
2d21ac55 234
0a7de745 235 return error;
2d21ac55 236}