]> git.saurik.com Git - apple/xnu.git/blame - security/mac_vfs_subr.c
xnu-1456.1.26.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@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
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,
46 struct componentname *cnp, int flags, vfs_context_t ctx)
47{
b0d623f7 48 int error = 0;
2d21ac55 49
2d21ac55 50
b0d623f7 51 /* fast path checks... */
2d21ac55 52
b0d623f7
A
53 /* are we labeling vnodes? If not still notify of create */
54 if (mac_label_vnodes == 0) {
55 if (flags & VNODE_LABEL_CREATE)
56 error = mac_vnode_notify_create(ctx,
57 mp, dvp, vp, cnp);
58 return 0;
2d21ac55
A
59 }
60
b0d623f7
A
61 /* if already VL_LABELED */
62 if (vp->v_lflag & VL_LABELED)
63 return (0);
64
65 vnode_lock_spin(vp);
66
67 /*
68 * must revalidate state once we hold the lock
69 * since we could have blocked and someone else
70 * has since labeled this vnode
71 */
72 if (vp->v_lflag & VL_LABELED) {
2d21ac55 73 vnode_unlock(vp);
b0d623f7 74 return (0);
2d21ac55
A
75 }
76
77 if ((vp->v_lflag & VL_LABEL) == 0) {
78 vp->v_lflag |= VL_LABEL;
79
80 /* Could sleep on disk I/O, drop lock. */
81 vnode_unlock(vp);
b0d623f7
A
82
83 if (vp->v_label == NULL)
84 vp->v_label = mac_vnode_label_alloc();
85
2d21ac55
A
86 if (flags & VNODE_LABEL_CREATE)
87 error = mac_vnode_notify_create(ctx,
88 mp, dvp, vp, cnp);
89 else
90 error = mac_vnode_label_associate(mp, vp, ctx);
b0d623f7
A
91
92 vnode_lock_spin(vp);
2d21ac55
A
93
94 if ((error == 0) && (vp->v_flag & VNCACHEABLE))
95 vp->v_lflag |= VL_LABELED;
96 vp->v_lflag &= ~VL_LABEL;
97
98 if (vp->v_lflag & VL_LABELWAIT) {
99 vp->v_lflag &= ~VL_LABELWAIT;
b0d623f7 100 wakeup(&vp->v_label);
2d21ac55 101 }
2d21ac55
A
102 } else {
103 struct timespec ts;
104
105 ts.tv_sec = 10;
106 ts.tv_nsec = 0;
107
108 while (vp->v_lflag & VL_LABEL) {
109 vp->v_lflag |= VL_LABELWAIT;
b0d623f7
A
110
111 error = msleep(&vp->v_label, &vp->v_lock, PVFS|PDROP,
112 "vnode_label", &ts);
113 vnode_lock_spin(vp);
114
2d21ac55
A
115 if (error == EWOULDBLOCK) {
116 vprint("vnode label timeout", vp);
117 break;
118 }
119 }
120 /* XXX: what should be done if labeling failed (above)? */
2d21ac55 121 }
b0d623f7 122 vnode_unlock(vp);
2d21ac55
A
123
124 return (error);
125}
126
127
128/*
129 * Clear the "labeled" flag on a VNODE.
130 * VNODE will have label re-associated upon
131 * next call to lookup().
132 *
133 * Caller verifies vfs_flags(vnode_mount(vp)) & MNT_MULTILABEL
134 * Caller holds vnode lock.
135 */
136void
137vnode_relabel(struct vnode *vp)
138{
139
140 /* Wait for any other labeling to complete. */
141 while (vp->v_lflag & VL_LABEL) {
142 vp->v_lflag |= VL_LABELWAIT;
b0d623f7 143 (void)msleep(&vp->v_label, &vp->v_lock, PVFS, "vnode_relabel", 0);
2d21ac55
A
144 }
145
146 /* Clear labeled flag */
147 vp->v_lflag &= ~VL_LABELED;
148
149 return;
150}
151
152/*
153 * VFS XATTR helpers.
154 */
155
156int
157mac_vnop_setxattr (struct vnode *vp, const char *name, char *buf, size_t len)
158{
159 vfs_context_t ctx;
160 int options = XATTR_NOSECURITY;
161 char uio_buf[ UIO_SIZEOF(1) ];
162 uio_t auio;
163 int error;
164
165 if (vfs_isrdonly(vp->v_mount))
166 return (EROFS);
167
168 ctx = vfs_context_current();
169 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE,
170 &uio_buf[0], sizeof(uio_buf));
171 uio_addiov(auio, CAST_USER_ADDR_T(buf), len);
172
173 error = vn_setxattr(vp, name, auio, options, ctx);
b0d623f7
A
174#if CONFIG_FSE
175 if (error == 0) {
176 add_fsevent(FSE_XATTR_MODIFIED, ctx,
177 FSE_ARG_VNODE, vp,
178 FSE_ARG_DONE);
179 }
180#endif
2d21ac55
A
181
182 return (error);
183}
184
185int
186mac_vnop_getxattr (struct vnode *vp, const char *name, char *buf, size_t len,
187 size_t *attrlen)
188{
189 vfs_context_t ctx = vfs_context_current();
190 int options = XATTR_NOSECURITY;
191 char uio_buf[ UIO_SIZEOF(1) ];
192 uio_t auio;
193 int error;
194
195 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
196 &uio_buf[0], sizeof(uio_buf));
197 uio_addiov(auio, CAST_USER_ADDR_T(buf), len);
198
199 error = vn_getxattr(vp, name, auio, attrlen, options, ctx);
200 *attrlen = len - uio_resid(auio);
201
202 return (error);
203}
204
205int
206mac_vnop_removexattr (struct vnode *vp, const char *name)
207{
208 vfs_context_t ctx = vfs_context_current();
209 int options = XATTR_NOSECURITY;
210 int error;
211
212 if (vfs_isrdonly(vp->v_mount))
213 return (EROFS);
214
215 error = vn_removexattr(vp, name, options, ctx);
b0d623f7
A
216#if CONFIG_FSE
217 if (error == 0) {
218 add_fsevent(FSE_XATTR_REMOVED, ctx,
219 FSE_ARG_VNODE, vp,
220 FSE_ARG_DONE);
221 }
222#endif
2d21ac55
A
223
224 return (error);
225}