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