1 /*
2 * Copyright (c) 2000-2020 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 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
67 *
68 */
69 /*
70 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
71 * support for mandatory and extensible security protections. This notice
72 * is included in support of clause 2.2 (b) of the Apple Public License,
73 * Version 2.0.
74 */
75
76 #include <sys/param.h>
77 #include <sys/types.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/file_internal.h>
81 #include <sys/stat.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <sys/mount_internal.h>
85 #include <sys/namei.h>
86 #include <sys/vnode_internal.h>
87 #include <sys/ioctl.h>
88 #include <sys/tty.h>
89 /* Temporary workaround for ubc.h until <rdar://4714366 is resolved */
90 #define ubc_setcred ubc_setcred_deprecated
91 #include <sys/ubc.h>
92 #undef ubc_setcred
93 int ubc_setcred(struct vnode *, struct proc *);
94 #include <sys/conf.h>
95 #include <sys/disk.h>
96 #include <sys/fsevents.h>
97 #include <sys/kdebug.h>
98 #include <sys/xattr.h>
99 #include <sys/ubc_internal.h>
100 #include <sys/uio_internal.h>
101 #include <sys/resourcevar.h>
102 #include <sys/signalvar.h>
103
104 #include <vm/vm_kern.h>
105 #include <vm/vm_map.h>
106
107 #include <miscfs/specfs/specdev.h>
108 #include <miscfs/fifofs/fifo.h>
109
110 #if CONFIG_MACF
111 #include <security/mac_framework.h>
112 #endif
113
114 #include <IOKit/IOBSD.h>
115 #include <libkern/section_keywords.h>
116
117 static int vn_closefile(struct fileglob *fp, vfs_context_t ctx);
118 static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data,
119 vfs_context_t ctx);
120 static int vn_read(struct fileproc *fp, struct uio *uio, int flags,
121 vfs_context_t ctx);
122 static int vn_write(struct fileproc *fp, struct uio *uio, int flags,
123 vfs_context_t ctx);
124 static int vn_select( struct fileproc *fp, int which, void * wql,
125 vfs_context_t ctx);
126 static int vn_kqfilter(struct fileproc *fp, struct knote *kn,
127 struct kevent_qos_s *kev);
128 static void filt_vndetach(struct knote *kn);
129 static int filt_vnode(struct knote *kn, long hint);
130 static int filt_vnode_common(struct knote *kn, struct kevent_qos_s *kev,
131 vnode_t vp, long hint);
132 static int vn_open_auth_finish(vnode_t vp, int fmode, vfs_context_t ctx);
133
134 const struct fileops vnops = {
135 .fo_type = DTYPE_VNODE,
136 .fo_read = vn_read,
137 .fo_write = vn_write,
138 .fo_ioctl = vn_ioctl,
139 .fo_select = vn_select,
140 .fo_close = vn_closefile,
141 .fo_drain = fo_no_drain,
142 .fo_kqfilter = vn_kqfilter,
143 };
144
145 static int filt_vntouch(struct knote *kn, struct kevent_qos_s *kev);
146 static int filt_vnprocess(struct knote *kn, struct kevent_qos_s*kev);
147
148 SECURITY_READ_ONLY_EARLY(struct filterops) vnode_filtops = {
149 .f_isfd = 1,
150 .f_attach = NULL,
151 .f_detach = filt_vndetach,
152 .f_event = filt_vnode,
153 .f_touch = filt_vntouch,
154 .f_process = filt_vnprocess,
155 };
156
157 /*
158 * Common code for vnode open operations.
159 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
160 *
161 * XXX the profusion of interfaces here is probably a bad thing.
162 */
163 int
vn_open(struct nameidata * ndp,int fmode,int cmode)164 vn_open(struct nameidata *ndp, int fmode, int cmode)
165 {
166 return vn_open_modflags(ndp, &fmode, cmode);
167 }
168
169 int
vn_open_modflags(struct nameidata * ndp,int * fmodep,int cmode)170 vn_open_modflags(struct nameidata *ndp, int *fmodep, int cmode)
171 {
172 int error;
173 struct vnode_attr *vap;
174
175 vap = kalloc_type(struct vnode_attr, Z_WAITOK);
176
177 VATTR_INIT(vap);
178 VATTR_SET(vap, va_mode, (mode_t)cmode);
179
180 error = vn_open_auth(ndp, fmodep, vap);
181
182 kfree_type(struct vnode_attr, vap);
183
184 return error;
185 }
186
187 static int
vn_open_auth_finish(vnode_t vp,int fmode,vfs_context_t ctx)188 vn_open_auth_finish(vnode_t vp, int fmode, vfs_context_t ctx)
189 {
190 int error;
191
192 if ((error = vnode_ref_ext(vp, fmode, 0)) != 0) {
193 goto bad;
194 }
195
196 /* Call out to allow 3rd party notification of open.
197 * Ignore result of kauth_authorize_fileop call.
198 */
199 #if CONFIG_MACF
200 mac_vnode_notify_open(ctx, vp, fmode);
201 #endif
202 kauth_authorize_fileop(vfs_context_ucred(ctx), KAUTH_FILEOP_OPEN,
203 (uintptr_t)vp, 0);
204
205 return 0;
206
207 bad:
208 return error;
209 }
210
211 /*
212 * May do nameidone() to allow safely adding an FSEvent. Cue off of ni_dvp to
213 * determine whether that has happened.
214 */
215 static int
vn_open_auth_do_create(struct nameidata * ndp,struct vnode_attr * vap,int fmode,boolean_t * did_create,boolean_t * did_open,vfs_context_t ctx)216 vn_open_auth_do_create(struct nameidata *ndp, struct vnode_attr *vap, int fmode, boolean_t *did_create, boolean_t *did_open, vfs_context_t ctx)
217 {
218 uint32_t status = 0;
219 vnode_t dvp = ndp->ni_dvp;
220 int batched;
221 int error;
222 vnode_t vp;
223
224 batched = vnode_compound_open_available(ndp->ni_dvp);
225 *did_open = FALSE;
226
227 VATTR_SET(vap, va_type, VREG);
228 if (fmode & O_EXCL) {
229 vap->va_vaflags |= VA_EXCLUSIVE;
230 }
231
232 #if NAMEDRSRCFORK
233 if (ndp->ni_cnd.cn_flags & CN_WANTSRSRCFORK) {
234 if ((error = vn_authorize_create(dvp, &ndp->ni_cnd, vap, ctx, NULL)) != 0) {
235 goto out;
236 }
237 if ((error = vnode_makenamedstream(dvp, &ndp->ni_vp, XATTR_RESOURCEFORK_NAME, 0, ctx)) != 0) {
238 goto out;
239 }
240 *did_create = TRUE;
241 } else {
242 #endif
243 if (!batched) {
244 if ((error = vn_authorize_create(dvp, &ndp->ni_cnd, vap, ctx, NULL)) != 0) {
245 goto out;
246 }
247 }
248
249 error = vn_create(dvp, &ndp->ni_vp, ndp, vap, VN_CREATE_DOOPEN, fmode, &status, ctx);
250 if (error != 0) {
251 if (batched) {
252 *did_create = (status & COMPOUND_OPEN_STATUS_DID_CREATE) ? TRUE : FALSE;
253 } else {
254 *did_create = FALSE;
255 }
256
257 if (error == EKEEPLOOKING) {
258 if (*did_create) {
259 panic("EKEEPLOOKING, but we did a create?");
260 }
261 if (!batched) {
262 panic("EKEEPLOOKING from filesystem that doesn't support compound vnops?");
263 }
264 if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) {
265 panic("EKEEPLOOKING, but continue flag not set?");
266 }
267
268 /*
269 * Do NOT drop the dvp: we need everything to continue the lookup.
270 */
271 return error;
272 }
273 } else {
274 if (batched) {
275 *did_create = (status & COMPOUND_OPEN_STATUS_DID_CREATE) ? 1 : 0;
276 *did_open = TRUE;
277 } else {
278 *did_create = TRUE;
279 }
280 }
281 #if NAMEDRSRCFORK
282 }
283 #endif
284
285 vp = ndp->ni_vp;
286
287 if (*did_create) {
288 int update_flags = 0;
289
290 // Make sure the name & parent pointers are hooked up
291 if (vp->v_name == NULL) {
292 update_flags |= VNODE_UPDATE_NAME;
293 }
294 if (vp->v_parent == NULLVP) {
295 update_flags |= VNODE_UPDATE_PARENT;
296 }
297
298 if (update_flags) {
299 vnode_update_identity(vp, dvp, ndp->ni_cnd.cn_nameptr, ndp->ni_cnd.cn_namelen, ndp->ni_cnd.cn_hash, update_flags);
300 }
301
302 vnode_put(dvp);
303 ndp->ni_dvp = NULLVP;
304
305 #if CONFIG_FSE
306 if (need_fsevent(FSE_CREATE_FILE, vp)) {
307 add_fsevent(FSE_CREATE_FILE, ctx,
308 FSE_ARG_VNODE, vp,
309 FSE_ARG_DONE);
310 }
311 #endif
312 }
313 out:
314 if (ndp->ni_dvp != NULLVP) {
315 vnode_put(dvp);
316 ndp->ni_dvp = NULLVP;
317 }
318
319 return error;
320 }
321
322 /*
323 * This is the number of times we'll loop in vn_open_auth without explicitly
324 * yielding the CPU when we determine we have to retry.
325 */
326 #define RETRY_NO_YIELD_COUNT 5
327
328 /*
329 * Open a file with authorization, updating the contents of the structures
330 * pointed to by ndp, fmodep, and vap as necessary to perform the requested
331 * operation. This function is used for both opens of existing files, and
332 * creation of new files.
333 *
334 * Parameters: ndp The nami data pointer describing the
335 * file
336 * fmodep A pointer to an int containg the mode
337 * information to be used for the open
338 * vap A pointer to the vnode attribute
339 * descriptor to be used for the open
340 *
341 * Indirect: * Contents of the data structures pointed
342 * to by the parameters are modified as
343 * necessary to the requested operation.
344 *
345 * Returns: 0 Success
346 * !0 errno value
347 *
348 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
349 *
350 * The contents of '*ndp' will be modified, based on the other
351 * arguments to this function, and to return file and directory
352 * data necessary to satisfy the requested operation.
353 *
354 * If the file does not exist and we are creating it, then the
355 * O_TRUNC flag will be cleared in '*fmodep' to indicate to the
356 * caller that the file was not truncated.
357 *
358 * If the file exists and the O_EXCL flag was not specified, then
359 * the O_CREAT flag will be cleared in '*fmodep' to indicate to
360 * the caller that the existing file was merely opened rather
361 * than created.
362 *
363 * The contents of '*vap' will be modified as necessary to
364 * complete the operation, including setting of supported
365 * attribute, clearing of fields containing unsupported attributes
366 * in the request, if the request proceeds without them, etc..
367 *
368 * XXX: This function is too complicated in actings on its arguments
369 *
370 * XXX: We should enummerate the possible errno values here, and where
371 * in the code they originated.
372 */
373 int
vn_open_auth(struct nameidata * ndp,int * fmodep,struct vnode_attr * vap)374 vn_open_auth(struct nameidata *ndp, int *fmodep, struct vnode_attr *vap)
375 {
376 struct vnode *vp;
377 struct vnode *dvp;
378 vfs_context_t ctx = ndp->ni_cnd.cn_context;
379 int error;
380 int fmode;
381 uint32_t origcnflags;
382 boolean_t did_create;
383 boolean_t did_open;
384 boolean_t need_vnop_open;
385 boolean_t batched;
386 boolean_t ref_failed;
387 int nretries = 0;
388
389 again:
390 vp = NULL;
391 dvp = NULL;
392 batched = FALSE;
393 did_create = FALSE;
394 need_vnop_open = TRUE;
395 ref_failed = FALSE;
396 fmode = *fmodep;
397 origcnflags = ndp->ni_cnd.cn_flags;
398
399 // If raw encrypted mode is requested, handle that here
400 if (VATTR_IS_ACTIVE(vap, va_dataprotect_flags)
401 && ISSET(vap->va_dataprotect_flags, VA_DP_RAWENCRYPTED)) {
402 fmode |= FENCRYPTED;
403 }
404
405 if ((fmode & O_NOFOLLOW_ANY) && (fmode & (O_SYMLINK | O_NOFOLLOW))) {
406 error = EINVAL;
407 goto out;
408 }
409
410 /*
411 * O_CREAT
412 */
413 if (fmode & O_CREAT) {
414 if ((fmode & O_DIRECTORY)) {
415 error = EINVAL;
416 goto out;
417 }
418 ndp->ni_cnd.cn_nameiop = CREATE;
419 #if CONFIG_TRIGGERS
420 ndp->ni_op = OP_LINK;
421 #endif
422 /* Inherit USEDVP, vnode_open() supported flags only */
423 ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT);
424 ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF | AUDITVNPATH1;
425 ndp->ni_flag = NAMEI_COMPOUNDOPEN;
426 #if NAMEDRSRCFORK
427 /* open calls are allowed for resource forks. */
428 ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK;
429 #endif
430 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0 && (origcnflags & FOLLOW) != 0) {
431 ndp->ni_cnd.cn_flags |= FOLLOW;
432 }
433 if (fmode & O_NOFOLLOW_ANY) {
434 /* will return ELOOP on the first symlink to be hit */
435 ndp->ni_flag |= NAMEI_NOFOLLOW_ANY;
436 }
437
438 continue_create_lookup:
439 if ((error = namei(ndp))) {
440 goto out;
441 }
442
443 dvp = ndp->ni_dvp;
444 vp = ndp->ni_vp;
445
446 batched = vnode_compound_open_available(dvp);
447
448 /* not found, create */
449 if (vp == NULL) {
450 /* must have attributes for a new file */
451 if (vap == NULL) {
452 vnode_put(dvp);
453 error = EINVAL;
454 goto out;
455 }
456 /*
457 * Attempt a create. For a system supporting compound VNOPs, we may
458 * find an existing file or create one; in either case, we will already
459 * have the file open and no VNOP_OPEN() will be needed.
460 */
461 error = vn_open_auth_do_create(ndp, vap, fmode, &did_create, &did_open, ctx);
462
463 dvp = ndp->ni_dvp;
464 vp = ndp->ni_vp;
465
466 /*
467 * Detected a node that the filesystem couldn't handle. Don't call
468 * nameidone() yet, because we need that path buffer.
469 */
470 if (error == EKEEPLOOKING) {
471 if (!batched) {
472 panic("EKEEPLOOKING from a filesystem that doesn't support compound VNOPs?");
473 }
474 goto continue_create_lookup;
475 }
476
477 nameidone(ndp);
478 if (dvp) {
479 panic("Shouldn't have a dvp here.");
480 }
481
482 if (error) {
483 /*
484 * Check for a create race.
485 */
486 if ((error == EEXIST) && !(fmode & O_EXCL)) {
487 if (vp) {
488 vnode_put(vp);
489 }
490 goto again;
491 }
492 goto bad;
493 }
494
495 need_vnop_open = !did_open;
496 } else {
497 if (fmode & O_EXCL) {
498 error = EEXIST;
499 }
500
501 /*
502 * We have a vnode. Use compound open if available
503 * or else fall through to "traditional" path. Note: can't
504 * do a compound open for root, because the parent belongs
505 * to a different FS.
506 */
507 if (error == 0 && batched && (vnode_mount(dvp) == vnode_mount(vp))) {
508 error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode, NULL, NULL, ctx);
509
510 if (error == 0) {
511 vp = ndp->ni_vp;
512 need_vnop_open = FALSE;
513 } else if (error == EKEEPLOOKING) {
514 if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) {
515 panic("EKEEPLOOKING, but continue flag not set?");
516 }
517 goto continue_create_lookup;
518 }
519 }
520 nameidone(ndp);
521 vnode_put(dvp);
522 ndp->ni_dvp = NULLVP;
523
524 if (error) {
525 goto bad;
526 }
527
528 fmode &= ~O_CREAT;
529
530 /* Fall through */
531 }
532 } else {
533 /*
534 * Not O_CREAT
535 */
536 ndp->ni_cnd.cn_nameiop = LOOKUP;
537 /* Inherit USEDVP, vnode_open() supported flags only */
538 ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT);
539 ndp->ni_cnd.cn_flags |= FOLLOW | LOCKLEAF | AUDITVNPATH1 | WANTPARENT;
540 #if NAMEDRSRCFORK
541 /* open calls are allowed for resource forks. */
542 ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK;
543 #endif
544 if (fmode & FENCRYPTED) {
545 ndp->ni_cnd.cn_flags |= CN_RAW_ENCRYPTED | CN_SKIPNAMECACHE;
546 }
547 ndp->ni_flag = NAMEI_COMPOUNDOPEN;
548
549 /* preserve NOFOLLOW from vnode_open() */
550 if (fmode & O_NOFOLLOW || fmode & O_SYMLINK || (origcnflags & FOLLOW) == 0) {
551 ndp->ni_cnd.cn_flags &= ~FOLLOW;
552 }
553 if (fmode & O_NOFOLLOW_ANY) {
554 /* will return ELOOP on the first symlink to be hit */
555 ndp->ni_flag |= NAMEI_NOFOLLOW_ANY;
556 }
557
558 /* Do a lookup, possibly going directly to filesystem for compound operation */
559 do {
560 if ((error = namei(ndp))) {
561 goto out;
562 }
563 vp = ndp->ni_vp;
564 dvp = ndp->ni_dvp;
565
566 /* Check for batched lookup-open */
567 batched = vnode_compound_open_available(dvp);
568 if (batched && ((vp == NULLVP) || (vnode_mount(dvp) == vnode_mount(vp)))) {
569 error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode, NULL, NULL, ctx);
570 vp = ndp->ni_vp;
571 if (error == 0) {
572 need_vnop_open = FALSE;
573 } else if (error == EKEEPLOOKING) {
574 if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) {
575 panic("EKEEPLOOKING, but continue flag not set?");
576 }
577 }
578 }
579 } while (error == EKEEPLOOKING);
580
581 nameidone(ndp);
582 vnode_put(dvp);
583 ndp->ni_dvp = NULLVP;
584
585 if (error) {
586 goto bad;
587 }
588 }
589
590 /*
591 * By this point, nameidone() is called, dvp iocount is dropped,
592 * and dvp pointer is cleared.
593 */
594 if (ndp->ni_dvp != NULLVP) {
595 panic("Haven't cleaned up adequately in vn_open_auth()");
596 }
597
598 /*
599 * Expect to use this code for filesystems without compound VNOPs, for the root
600 * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(),
601 * and for shadow files, which do not live on the same filesystems as their "parents."
602 */
603 if (need_vnop_open) {
604 if (batched && !vnode_isvroot(vp) && !vnode_isnamedstream(vp)) {
605 panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?");
606 }
607
608 if (!did_create) {
609 error = vn_authorize_open_existing(vp, &ndp->ni_cnd, fmode, ctx, NULL);
610 if (error) {
611 goto bad;
612 }
613 }
614
615 if (VATTR_IS_ACTIVE(vap, va_dataprotect_flags)
616 && ISSET(vap->va_dataprotect_flags, VA_DP_RAWUNENCRYPTED)) {
617 /* Don't allow unencrypted io request from user space unless entitled */
618 boolean_t entitled = FALSE;
619 #if !SECURE_KERNEL
620 entitled = IOCurrentTaskHasEntitlement("com.apple.private.security.file-unencrypt-access");
621 #endif
622 if (!entitled) {
623 error = EPERM;
624 goto bad;
625 }
626 fmode |= FUNENCRYPTED;
627 }
628
629 error = VNOP_OPEN(vp, fmode, ctx);
630 if (error) {
631 goto bad;
632 }
633 need_vnop_open = FALSE;
634 }
635
636 // if the vnode is tagged VOPENEVT and the current process
637 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
638 // flag to the open mode so that this open won't count against
639 // the vnode when carbon delete() does a vnode_isinuse() to see
640 // if a file is currently in use. this allows spotlight
641 // importers to not interfere with carbon apps that depend on
642 // the no-delete-if-busy semantics of carbon delete().
643 //
644 if (!did_create && (vp->v_flag & VOPENEVT) && (current_proc()->p_flag & P_CHECKOPENEVT)) {
645 fmode |= O_EVTONLY;
646 }
647
648 /*
649 * Grab reference, etc.
650 */
651 error = vn_open_auth_finish(vp, fmode, ctx);
652 if (error) {
653 ref_failed = TRUE;
654 goto bad;
655 }
656
657 /* Compound VNOP open is responsible for doing the truncate */
658 if (batched || did_create) {
659 fmode &= ~O_TRUNC;
660 }
661
662 *fmodep = fmode;
663 return 0;
664
665 bad:
666 /* Opened either explicitly or by a batched create */
667 if (!need_vnop_open) {
668 VNOP_CLOSE(vp, fmode, ctx);
669 }
670
671 ndp->ni_vp = NULL;
672 if (vp) {
673 #if NAMEDRSRCFORK
674 /* Aggressively recycle shadow files if we error'd out during open() */
675 if ((vnode_isnamedstream(vp)) &&
676 (vp->v_parent != NULLVP) &&
677 (vnode_isshadow(vp))) {
678 vnode_recycle(vp);
679 }
680 #endif
681 vnode_put(vp);
682 /*
683 * Check for a race against unlink. We had a vnode
684 * but according to vnode_authorize or VNOP_OPEN it
685 * no longer exists.
686 *
687 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
688 */
689 if (((error == ENOENT) && (*fmodep & O_CREAT)) || (error == EREDRIVEOPEN) || ref_failed) {
690 /*
691 * We'll retry here but it may be possible that we get
692 * into a retry "spin" inside the kernel and not allow
693 * threads, which need to run in order for the retry
694 * loop to end, to run. An example is an open of a
695 * terminal which is getting revoked and we spin here
696 * without yielding becasue namei and VNOP_OPEN are
697 * successful but vnode_ref fails. The revoke needs
698 * threads with an iocount to run but if spin here we
699 * may possibly be blcoking other threads from running.
700 *
701 * We start yielding the CPU after some number of
702 * retries for increasing durations. Note that this is
703 * still a loop without an exit condition.
704 */
705 nretries += 1;
706 if (nretries > RETRY_NO_YIELD_COUNT) {
707 /* Every hz/100 secs is 10 msecs ... */
708 tsleep(&nretries, PVFS, "vn_open_auth_retry",
709 MIN((nretries * (hz / 100)), hz));
710 }
711 goto again;
712 }
713 }
714
715 out:
716 return error;
717 }
718
719 #if vn_access_DEPRECATED
720 /*
721 * Authorize an action against a vnode. This has been the canonical way to
722 * ensure that the credential/process/etc. referenced by a vfs_context
723 * is granted the rights called out in 'mode' against the vnode 'vp'.
724 *
725 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
726 * to add support for more rights. As such, this interface will be deprecated
727 * and callers will use vnode_authorize instead.
728 */
729 int
vn_access(vnode_t vp,int mode,vfs_context_t context)730 vn_access(vnode_t vp, int mode, vfs_context_t context)
731 {
732 kauth_action_t action;
733
734 action = 0;
735 if (mode & VREAD) {
736 action |= KAUTH_VNODE_READ_DATA;
737 }
738 if (mode & VWRITE) {
739 action |= KAUTH_VNODE_WRITE_DATA;
740 }
741 if (mode & VEXEC) {
742 action |= KAUTH_VNODE_EXECUTE;
743 }
744
745 return vnode_authorize(vp, NULL, action, context);
746 }
747 #endif /* vn_access_DEPRECATED */
748
749 /*
750 * Vnode close call
751 */
752 int
vn_close(struct vnode * vp,int flags,vfs_context_t ctx)753 vn_close(struct vnode *vp, int flags, vfs_context_t ctx)
754 {
755 int error;
756 int flusherror = 0;
757
758 #if NAMEDRSRCFORK
759 /* Sync data from resource fork shadow file if needed. */
760 if ((vp->v_flag & VISNAMEDSTREAM) &&
761 (vp->v_parent != NULLVP) &&
762 vnode_isshadow(vp)) {
763 if (flags & FWASWRITTEN) {
764 flusherror = vnode_flushnamedstream(vp->v_parent, vp, ctx);
765 }
766 }
767 #endif
768 /*
769 * If vnode @vp belongs to a chardev or a blkdev then it is handled
770 * specially. We first drop its user reference count @vp->v_usecount
771 * before calling VNOP_CLOSE(). This was done historically to ensure
772 * that the last close of a special device vnode performed some
773 * conditional cleanups. Now we still need to drop this reference here
774 * to ensure that devfsspec_close() can check if the vnode is still in
775 * use.
776 */
777 if (vnode_isspec(vp)) {
778 (void)vnode_rele_ext(vp, flags, 0);
779 }
780
781 /*
782 * On HFS, we flush when the last writer closes. We do this
783 * because resource fork vnodes hold a reference on data fork
784 * vnodes and that will prevent them from getting VNOP_INACTIVE
785 * which will delay when we flush cached data. In future, we
786 * might find it beneficial to do this for all file systems.
787 * Note that it's OK to access v_writecount without the lock
788 * in this context.
789 */
790 if (vp->v_tag == VT_HFS && (flags & FWRITE) && vp->v_writecount == 1) {
791 VNOP_FSYNC(vp, MNT_NOWAIT, ctx);
792 }
793
794 error = VNOP_CLOSE(vp, flags, ctx);
795
796 #if CONFIG_FSE
797 if (flags & FWASWRITTEN) {
798 if (need_fsevent(FSE_CONTENT_MODIFIED, vp)) {
799 add_fsevent(FSE_CONTENT_MODIFIED, ctx,
800 FSE_ARG_VNODE, vp,
801 FSE_ARG_DONE);
802 }
803 }
804 #endif
805
806 if (!vnode_isspec(vp)) {
807 (void)vnode_rele_ext(vp, flags, 0);
808 }
809
810 if (flusherror) {
811 error = flusherror;
812 }
813 return error;
814 }
815
816 static int
vn_read_swapfile(struct vnode * vp,uio_t uio)817 vn_read_swapfile(
818 struct vnode *vp,
819 uio_t uio)
820 {
821 int error;
822 off_t swap_count, this_count;
823 off_t file_end, read_end;
824 off_t prev_resid;
825 char *my_swap_page;
826
827 /*
828 * Reading from a swap file will get you zeroes.
829 */
830
831 my_swap_page = NULL;
832 error = 0;
833 swap_count = uio_resid(uio);
834
835 file_end = ubc_getsize(vp);
836 read_end = uio->uio_offset + uio_resid(uio);
837 if (uio->uio_offset >= file_end) {
838 /* uio starts after end of file: nothing to read */
839 swap_count = 0;
840 } else if (read_end > file_end) {
841 /* uio extends beyond end of file: stop before that */
842 swap_count -= (read_end - file_end);
843 }
844
845 while (swap_count > 0) {
846 if (my_swap_page == NULL) {
847 my_swap_page = kalloc_data(PAGE_SIZE, Z_WAITOK | Z_ZERO);
848 /* add an end-of-line to keep line counters happy */
849 my_swap_page[PAGE_SIZE - 1] = '\n';
850 }
851 this_count = swap_count;
852 if (this_count > PAGE_SIZE) {
853 this_count = PAGE_SIZE;
854 }
855
856 prev_resid = uio_resid(uio);
857 error = uiomove((caddr_t) my_swap_page,
858 (int)this_count,
859 uio);
860 if (error) {
861 break;
862 }
863 swap_count -= (prev_resid - uio_resid(uio));
864 }
865 kfree_data(my_swap_page, PAGE_SIZE);
866
867 return error;
868 }
869 /*
870 * Package up an I/O request on a vnode into a uio and do it.
871 */
872 int
vn_rdwr(enum uio_rw rw,struct vnode * vp,caddr_t base,int len,off_t offset,enum uio_seg segflg,int ioflg,kauth_cred_t cred,int * aresid,proc_t p)873 vn_rdwr(
874 enum uio_rw rw,
875 struct vnode *vp,
876 caddr_t base,
877 int len,
878 off_t offset,
879 enum uio_seg segflg,
880 int ioflg,
881 kauth_cred_t cred,
882 int *aresid,
883 proc_t p)
884 {
885 int64_t resid;
886 int result;
887
888 if (len < 0) {
889 return EINVAL;
890 }
891
892 result = vn_rdwr_64(rw,
893 vp,
894 (uint64_t)(uintptr_t)base,
895 (int64_t)len,
896 offset,
897 segflg,
898 ioflg,
899 cred,
900 &resid,
901 p);
902
903 /* "resid" should be bounded above by "len," which is an int */
904 if (aresid != NULL) {
905 *aresid = (int)resid;
906 }
907
908 return result;
909 }
910
911
912 int
vn_rdwr_64(enum uio_rw rw,struct vnode * vp,uint64_t base,int64_t len,off_t offset,enum uio_seg segflg,int ioflg,kauth_cred_t cred,int64_t * aresid,proc_t p)913 vn_rdwr_64(
914 enum uio_rw rw,
915 struct vnode *vp,
916 uint64_t base,
917 int64_t len,
918 off_t offset,
919 enum uio_seg segflg,
920 int ioflg,
921 kauth_cred_t cred,
922 int64_t *aresid,
923 proc_t p)
924 {
925 uio_t auio;
926 int spacetype;
927 struct vfs_context context;
928 int error = 0;
929 uio_stackbuf_t uio_buf[UIO_SIZEOF(1)];
930
931 context.vc_thread = current_thread();
932 context.vc_ucred = cred;
933
934 if (UIO_SEG_IS_USER_SPACE(segflg)) {
935 spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32;
936 } else {
937 spacetype = UIO_SYSSPACE;
938 }
939
940 if (len < 0) {
941 return EINVAL;
942 }
943
944 auio = uio_createwithbuffer(1, offset, spacetype, rw,
945 &uio_buf[0], sizeof(uio_buf));
946 uio_addiov(auio, CAST_USER_ADDR_T(base), (user_size_t)len);
947
948 #if CONFIG_MACF
949 /* XXXMAC
950 * IO_NOAUTH should be re-examined.
951 * Likely that mediation should be performed in caller.
952 */
953 if ((ioflg & IO_NOAUTH) == 0) {
954 /* passed cred is fp->f_cred */
955 if (rw == UIO_READ) {
956 error = mac_vnode_check_read(&context, cred, vp);
957 } else {
958 error = mac_vnode_check_write(&context, cred, vp);
959 }
960 }
961 #endif
962
963 if (error == 0) {
964 if (rw == UIO_READ) {
965 if (vnode_isswap(vp) && ((ioflg & IO_SWAP_DISPATCH) == 0)) {
966 error = vn_read_swapfile(vp, auio);
967 } else {
968 error = VNOP_READ(vp, auio, ioflg, &context);
969 }
970 } else {
971 error = VNOP_WRITE(vp, auio, ioflg, &context);
972 }
973 }
974
975 if (aresid) {
976 *aresid = uio_resid(auio);
977 assert(*aresid <= len);
978 } else if (uio_resid(auio) && error == 0) {
979 error = EIO;
980 }
981 return error;
982 }
983
984 void
vn_offset_lock(struct fileglob * fg)985 vn_offset_lock(struct fileglob *fg)
986 {
987 lck_mtx_lock_spin(&fg->fg_lock);
988 while (fg->fg_lflags & FG_OFF_LOCKED) {
989 fg->fg_lflags |= FG_OFF_LOCKWANT;
990 msleep(&fg->fg_lflags, &fg->fg_lock, PVFS | PSPIN,
991 "fg_offset_lock_wait", 0);
992 }
993 fg->fg_lflags |= FG_OFF_LOCKED;
994 lck_mtx_unlock(&fg->fg_lock);
995 }
996
997 void
vn_offset_unlock(struct fileglob * fg)998 vn_offset_unlock(struct fileglob *fg)
999 {
1000 int lock_wanted = 0;
1001
1002 lck_mtx_lock_spin(&fg->fg_lock);
1003 if (fg->fg_lflags & FG_OFF_LOCKWANT) {
1004 lock_wanted = 1;
1005 }
1006 fg->fg_lflags &= ~(FG_OFF_LOCKED | FG_OFF_LOCKWANT);
1007 lck_mtx_unlock(&fg->fg_lock);
1008 if (lock_wanted) {
1009 wakeup(&fg->fg_lflags);
1010 }
1011 }
1012
1013 /*
1014 * File table vnode read routine.
1015 */
1016 static int
vn_read(struct fileproc * fp,struct uio * uio,int flags,vfs_context_t ctx)1017 vn_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
1018 {
1019 struct vnode *vp;
1020 int error;
1021 int ioflag;
1022 off_t read_offset;
1023 user_ssize_t read_len;
1024 user_ssize_t adjusted_read_len;
1025 user_ssize_t clippedsize;
1026 bool offset_locked;
1027
1028 read_len = uio_resid(uio);
1029 if (read_len < 0 || read_len > INT_MAX) {
1030 return EINVAL;
1031 }
1032 adjusted_read_len = read_len;
1033 clippedsize = 0;
1034
1035 if ((flags & FOF_OFFSET) == 0) {
1036 vn_offset_lock(fp->fp_glob);
1037 offset_locked = true;
1038 } else {
1039 offset_locked = false;
1040 }
1041 vp = (struct vnode *)fp_get_data(fp);
1042 if ((error = vnode_getwithref(vp))) {
1043 if (offset_locked) {
1044 vn_offset_unlock(fp->fp_glob);
1045 }
1046 return error;
1047 }
1048
1049 if (offset_locked && (vnode_vtype(vp) != VREG || vnode_isswap(vp))) {
1050 vn_offset_unlock(fp->fp_glob);
1051 offset_locked = false;
1052 }
1053
1054 #if CONFIG_MACF
1055 error = mac_vnode_check_read(ctx, vfs_context_ucred(ctx), vp);
1056 if (error) {
1057 (void)vnode_put(vp);
1058 if (offset_locked) {
1059 vn_offset_unlock(fp->fp_glob);
1060 }
1061 return error;
1062 }
1063 #endif
1064
1065 /* This signals to VNOP handlers that this read came from a file table read */
1066 ioflag = IO_SYSCALL_DISPATCH;
1067
1068 if (fp->fp_glob->fg_flag & FNONBLOCK) {
1069 ioflag |= IO_NDELAY;
1070 }
1071 if ((fp->fp_glob->fg_flag & FNOCACHE) || vnode_isnocache(vp)) {
1072 ioflag |= IO_NOCACHE;
1073 }
1074 if (fp->fp_glob->fg_flag & FENCRYPTED) {
1075 ioflag |= IO_ENCRYPTED;
1076 }
1077 if (fp->fp_glob->fg_flag & FUNENCRYPTED) {
1078 ioflag |= IO_SKIP_ENCRYPTION;
1079 }
1080 if (fp->fp_glob->fg_flag & O_EVTONLY) {
1081 ioflag |= IO_EVTONLY;
1082 }
1083 if (fp->fp_glob->fg_flag & FNORDAHEAD) {
1084 ioflag |= IO_RAOFF;
1085 }
1086
1087 if ((flags & FOF_OFFSET) == 0) {
1088 read_offset = fp->fp_glob->fg_offset;
1089 uio_setoffset(uio, read_offset);
1090 } else {
1091 read_offset = uio_offset(uio);
1092 /* POSIX allows negative offsets for character devices. */
1093 if ((read_offset < 0) && (vnode_vtype(vp) != VCHR)) {
1094 error = EINVAL;
1095 goto error_out;
1096 }
1097 }
1098
1099 if (read_offset == INT64_MAX) {
1100 /* can't read any more */
1101 error = 0;
1102 goto error_out;
1103 }
1104
1105 /*
1106 * If offset + len will cause overflow, reduce the len to a value
1107 * (adjusted_read_len) where it won't
1108 */
1109 if ((read_offset >= 0) && (INT64_MAX - read_offset) < read_len) {
1110 /*
1111 * 0 read_offset INT64_MAX
1112 * |-----------------------------------------------|----------|~~~
1113 * <--read_len-->
1114 * <-adjusted->
1115 */
1116 adjusted_read_len = (user_ssize_t)(INT64_MAX - read_offset);
1117 }
1118
1119 if (adjusted_read_len < read_len) {
1120 uio_setresid(uio, adjusted_read_len);
1121 clippedsize = read_len - adjusted_read_len;
1122 }
1123
1124 if (vnode_isswap(vp) && !(IO_SKIP_ENCRYPTION & ioflag)) {
1125 /* special case for swap files */
1126 error = vn_read_swapfile(vp, uio);
1127 } else {
1128 error = VNOP_READ(vp, uio, ioflag, ctx);
1129 }
1130
1131 if (clippedsize) {
1132 uio_setresid(uio, (uio_resid(uio) + clippedsize));
1133 }
1134
1135 if ((flags & FOF_OFFSET) == 0) {
1136 fp->fp_glob->fg_offset += read_len - uio_resid(uio);
1137 }
1138
1139 error_out:
1140 if (offset_locked) {
1141 vn_offset_unlock(fp->fp_glob);
1142 offset_locked = false;
1143 }
1144
1145 (void)vnode_put(vp);
1146 return error;
1147 }
1148
1149
1150 /*
1151 * File table vnode write routine.
1152 */
1153 static int
vn_write(struct fileproc * fp,struct uio * uio,int flags,vfs_context_t ctx)1154 vn_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
1155 {
1156 struct vnode *vp;
1157 int error, ioflag;
1158 off_t write_offset;
1159 off_t write_end_offset;
1160 user_ssize_t write_len;
1161 user_ssize_t adjusted_write_len;
1162 user_ssize_t clippedsize;
1163 bool offset_locked;
1164 proc_t p = vfs_context_proc(ctx);
1165 rlim_t rlim_cur_fsize = p ? proc_limitgetcur(p, RLIMIT_FSIZE) : 0;
1166
1167 write_len = uio_resid(uio);
1168 if (write_len < 0 || write_len > INT_MAX) {
1169 return EINVAL;
1170 }
1171 adjusted_write_len = write_len;
1172 clippedsize = 0;
1173
1174 if ((flags & FOF_OFFSET) == 0) {
1175 vn_offset_lock(fp->fp_glob);
1176 offset_locked = true;
1177 } else {
1178 offset_locked = false;
1179 }
1180
1181 vp = (struct vnode *)fp_get_data(fp);
1182 if ((error = vnode_getwithref(vp))) {
1183 if (offset_locked) {
1184 vn_offset_unlock(fp->fp_glob);
1185 }
1186 return error;
1187 }
1188
1189 if (offset_locked && (vnode_vtype(vp) != VREG || vnode_isswap(vp))) {
1190 vn_offset_unlock(fp->fp_glob);
1191 offset_locked = false;
1192 }
1193
1194 #if CONFIG_MACF
1195 error = mac_vnode_check_write(ctx, vfs_context_ucred(ctx), vp);
1196 if (error) {
1197 (void)vnode_put(vp);
1198 if (offset_locked) {
1199 vn_offset_unlock(fp->fp_glob);
1200 }
1201 return error;
1202 }
1203 #endif
1204
1205 /*
1206 * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from
1207 * a file table write
1208 */
1209 ioflag = (IO_UNIT | IO_SYSCALL_DISPATCH);
1210
1211 if (vp->v_type == VREG && (fp->fp_glob->fg_flag & O_APPEND)) {
1212 ioflag |= IO_APPEND;
1213 }
1214 if (fp->fp_glob->fg_flag & FNONBLOCK) {
1215 ioflag |= IO_NDELAY;
1216 }
1217 if ((fp->fp_glob->fg_flag & FNOCACHE) || vnode_isnocache(vp)) {
1218 ioflag |= IO_NOCACHE;
1219 }
1220 if (fp->fp_glob->fg_flag & FNODIRECT) {
1221 ioflag |= IO_NODIRECT;
1222 }
1223 if (fp->fp_glob->fg_flag & FSINGLE_WRITER) {
1224 ioflag |= IO_SINGLE_WRITER;
1225 }
1226 if (fp->fp_glob->fg_flag & O_EVTONLY) {
1227 ioflag |= IO_EVTONLY;
1228 }
1229
1230 /*
1231 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
1232 *
1233 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
1234 * XXX the non-essential metadata without some additional VFS work;
1235 * XXX the intent at this point is to plumb the interface for it.
1236 */
1237 if ((fp->fp_glob->fg_flag & (O_FSYNC | O_DSYNC)) ||
1238 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) {
1239 ioflag |= IO_SYNC;
1240 }
1241
1242 if ((flags & FOF_OFFSET) == 0) {
1243 write_offset = fp->fp_glob->fg_offset;
1244 uio_setoffset(uio, write_offset);
1245 } else {
1246 /* for pwrite, append should be ignored */
1247 ioflag &= ~IO_APPEND;
1248 write_offset = uio_offset(uio);
1249 /* POSIX allows negative offsets for character devices. */
1250 if ((write_offset < 0) && (vnode_vtype(vp) != VCHR)) {
1251 error = EINVAL;
1252 goto error_out;
1253 }
1254 }
1255
1256 if (write_offset == INT64_MAX) {
1257 /* writes are not possible */
1258 error = EFBIG;
1259 goto error_out;
1260 }
1261
1262 /*
1263 * write_len is the original write length that was requested.
1264 * We may however need to reduce that becasue of two reasons
1265 *
1266 * 1) If write_offset + write_len will exceed OFF_T_MAX (i.e. INT64_MAX)
1267 * and/or
1268 * 2) If write_offset + write_len will exceed the administrative
1269 * limit for the maximum file size.
1270 *
1271 * In both cases the write will be denied if we can't write even a single
1272 * byte otherwise it will be "clipped" (i.e. a short write).
1273 */
1274
1275 /*
1276 * If offset + len will cause overflow, reduce the len
1277 * to a value (adjusted_write_len) where it won't
1278 */
1279 if ((write_offset >= 0) && (INT64_MAX - write_offset) < write_len) {
1280 /*
1281 * 0 write_offset INT64_MAX
1282 * |-----------------------------------------------|----------|~~~
1283 * <--write_len-->
1284 * <-adjusted->
1285 */
1286 adjusted_write_len = (user_ssize_t)(INT64_MAX - write_offset);
1287 }
1288
1289 /* write_end_offset will always be [0, INT64_MAX] */
1290 write_end_offset = write_offset + adjusted_write_len;
1291
1292 if (p && (vp->v_type == VREG) &&
1293 (rlim_cur_fsize != RLIM_INFINITY) &&
1294 (rlim_cur_fsize <= INT64_MAX) &&
1295 (write_end_offset > (off_t)rlim_cur_fsize)) {
1296 /*
1297 * If the requested residual would cause us to go past the
1298 * administrative limit, then we need to adjust the residual
1299 * down to cause fewer bytes than requested to be written. If
1300 * we can't do that (e.g. the residual is already 1 byte),
1301 * then we fail the write with EFBIG.
1302 */
1303 if (write_offset >= (off_t)rlim_cur_fsize) {
1304 /*
1305 * 0 rlim_fsize write_offset write_end INT64_MAX
1306 * |------------------------|----------|-------------|--------|
1307 * <--write_len-->
1308 *
1309 * write not permitted
1310 */
1311 psignal(p, SIGXFSZ);
1312 error = EFBIG;
1313 goto error_out;
1314 }
1315
1316 /*
1317 * 0 write_offset rlim_fsize write_end INT64_MAX
1318 * |------------------------|-----------|---------|------------|
1319 * <------write_len------>
1320 * <-adjusted-->
1321 */
1322 adjusted_write_len = (user_ssize_t)((off_t)rlim_cur_fsize - write_offset);
1323 assert((adjusted_write_len > 0) && (adjusted_write_len < write_len));
1324 }
1325
1326 if (adjusted_write_len < write_len) {
1327 uio_setresid(uio, adjusted_write_len);
1328 clippedsize = write_len - adjusted_write_len;
1329 }
1330
1331 error = VNOP_WRITE(vp, uio, ioflag, ctx);
1332
1333 /*
1334 * If we had to reduce the size of write requested either because
1335 * of rlimit or because it would have exceeded
1336 * maximum file size, we have to add that back to the residual so
1337 * it correctly reflects what we did in this function.
1338 */
1339 if (clippedsize) {
1340 uio_setresid(uio, (uio_resid(uio) + clippedsize));
1341 }
1342
1343 if ((flags & FOF_OFFSET) == 0) {
1344 if (ioflag & IO_APPEND) {
1345 fp->fp_glob->fg_offset = uio_offset(uio);
1346 } else {
1347 fp->fp_glob->fg_offset += (write_len - uio_resid(uio));
1348 }
1349 if (offset_locked) {
1350 vn_offset_unlock(fp->fp_glob);
1351 offset_locked = false;
1352 }
1353 }
1354
1355 /*
1356 * Set the credentials on successful writes
1357 */
1358 if ((error == 0) && (vp->v_tag == VT_NFS) && (UBCINFOEXISTS(vp))) {
1359 /*
1360 * When called from aio subsystem, we only have the proc from
1361 * which to get the credential, at this point, so use that
1362 * instead. This means aio functions are incompatible with
1363 * per-thread credentials (aio operations are proxied). We
1364 * can't easily correct the aio vs. settid race in this case
1365 * anyway, so we disallow it.
1366 */
1367 if ((flags & FOF_PCRED) == 0) {
1368 ubc_setthreadcred(vp, p, current_thread());
1369 } else {
1370 ubc_setcred(vp, p);
1371 }
1372 }
1373 (void)vnode_put(vp);
1374 return error;
1375
1376 error_out:
1377 if (offset_locked) {
1378 vn_offset_unlock(fp->fp_glob);
1379 }
1380 (void)vnode_put(vp);
1381 return error;
1382 }
1383
1384 /*
1385 * File table vnode stat routine.
1386 *
1387 * Returns: 0 Success
1388 * EBADF
1389 * ENOMEM
1390 * vnode_getattr:???
1391 */
1392 int
vn_stat_noauth(struct vnode * vp,void * sbptr,kauth_filesec_t * xsec,int isstat64,int needsrealdev,vfs_context_t ctx,struct ucred * file_cred)1393 vn_stat_noauth(struct vnode *vp, void *sbptr, kauth_filesec_t *xsec, int isstat64,
1394 int needsrealdev, vfs_context_t ctx, struct ucred *file_cred)
1395 {
1396 struct vnode_attr va;
1397 int error;
1398 u_short mode;
1399 kauth_filesec_t fsec;
1400 struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */
1401 struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */
1402
1403 if (isstat64 != 0) {
1404 sb64 = (struct stat64 *)sbptr;
1405 } else {
1406 sb = (struct stat *)sbptr;
1407 }
1408 memset(&va, 0, sizeof(va));
1409 VATTR_INIT(&va);
1410 VATTR_WANTED(&va, va_fsid);
1411 VATTR_WANTED(&va, va_fileid);
1412 VATTR_WANTED(&va, va_mode);
1413 VATTR_WANTED(&va, va_type);
1414 VATTR_WANTED(&va, va_nlink);
1415 VATTR_WANTED(&va, va_uid);
1416 VATTR_WANTED(&va, va_gid);
1417 VATTR_WANTED(&va, va_rdev);
1418 VATTR_WANTED(&va, va_data_size);
1419 VATTR_WANTED(&va, va_access_time);
1420 VATTR_WANTED(&va, va_modify_time);
1421 VATTR_WANTED(&va, va_change_time);
1422 VATTR_WANTED(&va, va_create_time);
1423 VATTR_WANTED(&va, va_flags);
1424 VATTR_WANTED(&va, va_gen);
1425 VATTR_WANTED(&va, va_iosize);
1426 /* lower layers will synthesise va_total_alloc from va_data_size if required */
1427 VATTR_WANTED(&va, va_total_alloc);
1428 if (xsec != NULL) {
1429 VATTR_WANTED(&va, va_uuuid);
1430 VATTR_WANTED(&va, va_guuid);
1431 VATTR_WANTED(&va, va_acl);
1432 }
1433 if (needsrealdev) {
1434 va.va_vaflags = VA_REALFSID;
1435 }
1436 error = vnode_getattr(vp, &va, ctx);
1437 if (error) {
1438 goto out;
1439 }
1440 #if CONFIG_MACF
1441 /*
1442 * Give MAC polices a chance to reject or filter the attributes
1443 * returned by the filesystem. Note that MAC policies are consulted
1444 * *after* calling the filesystem because filesystems can return more
1445 * attributes than were requested so policies wouldn't be authoritative
1446 * is consulted beforehand. This also gives policies an opportunity
1447 * to change the values of attributes retrieved.
1448 */
1449 error = mac_vnode_check_getattr(ctx, file_cred, vp, &va);
1450 if (error) {
1451 goto out;
1452 }
1453 #endif
1454 /*
1455 * Copy from vattr table
1456 */
1457 if (isstat64 != 0) {
1458 sb64->st_dev = va.va_fsid;
1459 sb64->st_ino = (ino64_t)va.va_fileid;
1460 } else {
1461 sb->st_dev = va.va_fsid;
1462 sb->st_ino = (ino_t)va.va_fileid;
1463 }
1464 mode = va.va_mode;
1465 switch (vp->v_type) {
1466 case VREG:
1467 mode |= S_IFREG;
1468 break;
1469 case VDIR:
1470 mode |= S_IFDIR;
1471 break;
1472 case VBLK:
1473 mode |= S_IFBLK;
1474 break;
1475 case VCHR:
1476 mode |= S_IFCHR;
1477 break;
1478 case VLNK:
1479 mode |= S_IFLNK;
1480 break;
1481 case VSOCK:
1482 mode |= S_IFSOCK;
1483 break;
1484 case VFIFO:
1485 mode |= S_IFIFO;
1486 break;
1487 default:
1488 error = EBADF;
1489 goto out;
1490 }
1491 ;
1492 if (isstat64 != 0) {
1493 sb64->st_mode = mode;
1494 sb64->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? va.va_nlink > UINT16_MAX ? UINT16_MAX : (u_int16_t)va.va_nlink : 1;
1495 sb64->st_uid = va.va_uid;
1496 sb64->st_gid = va.va_gid;
1497 sb64->st_rdev = va.va_rdev;
1498 sb64->st_size = va.va_data_size;
1499 sb64->st_atimespec = va.va_access_time;
1500 sb64->st_mtimespec = va.va_modify_time;
1501 sb64->st_ctimespec = va.va_change_time;
1502 if (VATTR_IS_SUPPORTED(&va, va_create_time)) {
1503 sb64->st_birthtimespec = va.va_create_time;
1504 } else {
1505 sb64->st_birthtimespec.tv_sec = sb64->st_birthtimespec.tv_nsec = 0;
1506 }
1507 sb64->st_blksize = va.va_iosize;
1508 sb64->st_flags = va.va_flags;
1509 sb64->st_blocks = roundup(va.va_total_alloc, 512) / 512;
1510 } else {
1511 sb->st_mode = mode;
1512 sb->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? va.va_nlink > UINT16_MAX ? UINT16_MAX : (u_int16_t)va.va_nlink : 1;
1513 sb->st_uid = va.va_uid;
1514 sb->st_gid = va.va_gid;
1515 sb->st_rdev = va.va_rdev;
1516 sb->st_size = va.va_data_size;
1517 sb->st_atimespec = va.va_access_time;
1518 sb->st_mtimespec = va.va_modify_time;
1519 sb->st_ctimespec = va.va_change_time;
1520 sb->st_blksize = va.va_iosize;
1521 sb->st_flags = va.va_flags;
1522 sb->st_blocks = roundup(va.va_total_alloc, 512) / 512;
1523 }
1524
1525 /* if we're interested in extended security data and we got an ACL */
1526 if (xsec != NULL) {
1527 if (!VATTR_IS_SUPPORTED(&va, va_acl) &&
1528 !VATTR_IS_SUPPORTED(&va, va_uuuid) &&
1529 !VATTR_IS_SUPPORTED(&va, va_guuid)) {
1530 *xsec = KAUTH_FILESEC_NONE;
1531 } else {
1532 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
1533 fsec = kauth_filesec_alloc(va.va_acl->acl_entrycount);
1534 } else {
1535 fsec = kauth_filesec_alloc(0);
1536 }
1537 if (fsec == NULL) {
1538 error = ENOMEM;
1539 goto out;
1540 }
1541 fsec->fsec_magic = KAUTH_FILESEC_MAGIC;
1542 if (VATTR_IS_SUPPORTED(&va, va_uuuid)) {
1543 fsec->fsec_owner = va.va_uuuid;
1544 } else {
1545 fsec->fsec_owner = kauth_null_guid;
1546 }
1547 if (VATTR_IS_SUPPORTED(&va, va_guuid)) {
1548 fsec->fsec_group = va.va_guuid;
1549 } else {
1550 fsec->fsec_group = kauth_null_guid;
1551 }
1552 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
1553 __nochk_bcopy(va.va_acl, &(fsec->fsec_acl), KAUTH_ACL_COPYSIZE(va.va_acl));
1554 } else {
1555 fsec->fsec_acl.acl_entrycount = KAUTH_FILESEC_NOACL;
1556 }
1557 *xsec = fsec;
1558 }
1559 }
1560
1561 /* Do not give the generation number out to unpriviledged users */
1562 if (va.va_gen && !vfs_context_issuser(ctx)) {
1563 if (isstat64 != 0) {
1564 sb64->st_gen = 0;
1565 } else {
1566 sb->st_gen = 0;
1567 }
1568 } else {
1569 if (isstat64 != 0) {
1570 sb64->st_gen = va.va_gen;
1571 } else {
1572 sb->st_gen = va.va_gen;
1573 }
1574 }
1575
1576 error = 0;
1577 out:
1578 if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL) {
1579 kauth_acl_free(va.va_acl);
1580 }
1581 return error;
1582 }
1583
1584 int
vn_stat(struct vnode * vp,void * sb,kauth_filesec_t * xsec,int isstat64,int needsrealdev,vfs_context_t ctx)1585 vn_stat(struct vnode *vp, void *sb, kauth_filesec_t *xsec, int isstat64, int needsrealdev, vfs_context_t ctx)
1586 {
1587 int error;
1588
1589 #if CONFIG_MACF
1590 error = mac_vnode_check_stat(ctx, NOCRED, vp);
1591 if (error) {
1592 return error;
1593 }
1594 #endif
1595
1596 /* authorize */
1597 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, ctx)) != 0) {
1598 return error;
1599 }
1600
1601 /* actual stat */
1602 return vn_stat_noauth(vp, sb, xsec, isstat64, needsrealdev, ctx, NOCRED);
1603 }
1604
1605
1606 /*
1607 * File table vnode ioctl routine.
1608 */
1609 static int
vn_ioctl(struct fileproc * fp,u_long com,caddr_t data,vfs_context_t ctx)1610 vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx)
1611 {
1612 struct vnode *vp = (struct vnode *)fp_get_data(fp);
1613 off_t file_size;
1614 int error;
1615 struct vnode *ttyvp;
1616
1617 if ((error = vnode_getwithref(vp))) {
1618 return error;
1619 }
1620
1621 #if CONFIG_MACF
1622 error = mac_vnode_check_ioctl(ctx, vp, com);
1623 if (error) {
1624 goto out;
1625 }
1626 #endif
1627
1628 switch (vp->v_type) {
1629 case VREG:
1630 case VDIR:
1631 if (com == FIONREAD) {
1632 off_t temp_nbytes;
1633 if ((error = vnode_size(vp, &file_size, ctx)) != 0) {
1634 goto out;
1635 }
1636 temp_nbytes = file_size - fp->fp_glob->fg_offset;
1637 if (temp_nbytes > INT_MAX) {
1638 *(int *)data = INT_MAX;
1639 } else if (temp_nbytes < 0) {
1640 *(int *)data = 0;
1641 } else {
1642 *(int *)data = (int)temp_nbytes;
1643 }
1644 goto out;
1645 }
1646 if (com == FIONBIO || com == FIOASYNC) { /* XXX */
1647 goto out;
1648 }
1649 OS_FALLTHROUGH;
1650
1651 default:
1652 error = ENOTTY;
1653 goto out;
1654
1655 case VFIFO:
1656 case VCHR:
1657 case VBLK:
1658
1659 if (com == TIOCREVOKE || com == TIOCREVOKECLEAR) {
1660 error = ENOTTY;
1661 goto out;
1662 }
1663
1664 /* Should not be able to set block size from user space */
1665 if (com == DKIOCSETBLOCKSIZE) {
1666 error = EPERM;
1667 goto out;
1668 }
1669
1670 if (com == FIODTYPE) {
1671 if (vp->v_type == VBLK) {
1672 if (major(vp->v_rdev) >= nblkdev) {
1673 error = ENXIO;
1674 goto out;
1675 }
1676 *(int *)data = bdevsw[major(vp->v_rdev)].d_type;
1677 } else if (vp->v_type == VCHR) {
1678 if (major(vp->v_rdev) >= nchrdev) {
1679 error = ENXIO;
1680 goto out;
1681 }
1682 *(int *)data = cdevsw[major(vp->v_rdev)].d_type;
1683 } else {
1684 error = ENOTTY;
1685 goto out;
1686 }
1687 goto out;
1688 }
1689 error = VNOP_IOCTL(vp, com, data, fp->fp_glob->fg_flag, ctx);
1690
1691 if (error == 0 && com == TIOCSCTTY) {
1692 struct session *sessp;
1693 struct pgrp *pg;
1694
1695 pg = proc_pgrp(vfs_context_proc(ctx), &sessp);
1696
1697 session_lock(sessp);
1698 ttyvp = sessp->s_ttyvp;
1699 sessp->s_ttyvp = vp;
1700 sessp->s_ttyvid = vnode_vid(vp);
1701 session_unlock(sessp);
1702
1703 pgrp_rele(pg);
1704 }
1705 }
1706 out:
1707 (void)vnode_put(vp);
1708 return error;
1709 }
1710
1711 /*
1712 * File table vnode select routine.
1713 */
1714 static int
vn_select(struct fileproc * fp,int which,void * wql,__unused vfs_context_t ctx)1715 vn_select(struct fileproc *fp, int which, void *wql, __unused vfs_context_t ctx)
1716 {
1717 int error;
1718 struct vnode * vp = (struct vnode *)fp_get_data(fp);
1719 struct vfs_context context;
1720
1721 if ((error = vnode_getwithref(vp)) == 0) {
1722 context.vc_thread = current_thread();
1723 context.vc_ucred = fp->fp_glob->fg_cred;
1724
1725 #if CONFIG_MACF
1726 /*
1727 * XXX We should use a per thread credential here; minimally,
1728 * XXX the process credential should have a persistent
1729 * XXX reference on it before being passed in here.
1730 */
1731 error = mac_vnode_check_select(ctx, vp, which);
1732 if (error == 0)
1733 #endif
1734 error = VNOP_SELECT(vp, which, fp->fp_glob->fg_flag, wql, ctx);
1735
1736 (void)vnode_put(vp);
1737 }
1738 return error;
1739 }
1740
1741 /*
1742 * File table vnode close routine.
1743 */
1744 static int
vn_closefile(struct fileglob * fg,vfs_context_t ctx)1745 vn_closefile(struct fileglob *fg, vfs_context_t ctx)
1746 {
1747 struct vnode *vp = fg_get_data(fg);
1748 int error;
1749
1750 if ((error = vnode_getwithref(vp)) == 0) {
1751 if (FILEGLOB_DTYPE(fg) == DTYPE_VNODE &&
1752 ((fg->fg_flag & FWASLOCKED) != 0 ||
1753 (fg->fg_lflags & FG_HAS_OFDLOCK) != 0)) {
1754 struct flock lf = {
1755 .l_whence = SEEK_SET,
1756 .l_start = 0,
1757 .l_len = 0,
1758 .l_type = F_UNLCK
1759 };
1760
1761 if ((fg->fg_flag & FWASLOCKED) != 0) {
1762 (void) VNOP_ADVLOCK(vp, (caddr_t)fg,
1763 F_UNLCK, &lf, F_FLOCK, ctx, NULL);
1764 }
1765
1766 if ((fg->fg_lflags & FG_HAS_OFDLOCK) != 0) {
1767 (void) VNOP_ADVLOCK(vp, (caddr_t)fg,
1768 F_UNLCK, &lf, F_OFD_LOCK, ctx, NULL);
1769 }
1770 }
1771 error = vn_close(vp, fg->fg_flag, ctx);
1772 (void) vnode_put(vp);
1773 }
1774 return error;
1775 }
1776
1777 /*
1778 * Returns: 0 Success
1779 * VNOP_PATHCONF:???
1780 */
1781 int
vn_pathconf(vnode_t vp,int name,int32_t * retval,vfs_context_t ctx)1782 vn_pathconf(vnode_t vp, int name, int32_t *retval, vfs_context_t ctx)
1783 {
1784 int error = 0;
1785 struct vfs_attr vfa;
1786
1787 switch (name) {
1788 case _PC_EXTENDED_SECURITY_NP:
1789 *retval = vfs_extendedsecurity(vnode_mount(vp)) ? 1 : 0;
1790 break;
1791 case _PC_AUTH_OPAQUE_NP:
1792 *retval = vfs_authopaque(vnode_mount(vp));
1793 break;
1794 case _PC_2_SYMLINKS:
1795 *retval = 1; /* XXX NOTSUP on MSDOS, etc. */
1796 break;
1797 case _PC_ALLOC_SIZE_MIN:
1798 *retval = 1; /* XXX lie: 1 byte */
1799 break;
1800 case _PC_ASYNC_IO: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1801 *retval = 1; /* [AIO] option is supported */
1802 break;
1803 case _PC_PRIO_IO: /* unistd.h: _POSIX_PRIORITIZED_IO */
1804 *retval = 0; /* [PIO] option is not supported */
1805 break;
1806 case _PC_REC_INCR_XFER_SIZE:
1807 *retval = 4096; /* XXX go from MIN to MAX 4K at a time */
1808 break;
1809 case _PC_REC_MIN_XFER_SIZE:
1810 *retval = 4096; /* XXX recommend 4K minimum reads/writes */
1811 break;
1812 case _PC_REC_MAX_XFER_SIZE:
1813 *retval = 65536; /* XXX recommend 64K maximum reads/writes */
1814 break;
1815 case _PC_REC_XFER_ALIGN:
1816 *retval = 4096; /* XXX recommend page aligned buffers */
1817 break;
1818 case _PC_SYMLINK_MAX:
1819 *retval = 255; /* Minimum acceptable POSIX value */
1820 break;
1821 case _PC_SYNC_IO: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1822 *retval = 0; /* [SIO] option is not supported */
1823 break;
1824 case _PC_XATTR_SIZE_BITS:
1825 /* The number of bits used to store maximum extended
1826 * attribute size in bytes. For example, if the maximum
1827 * attribute size supported by a file system is 128K, the
1828 * value returned will be 18. However a value 18 can mean
1829 * that the maximum attribute size can be anywhere from
1830 * (256KB - 1) to 128KB. As a special case, the resource
1831 * fork can have much larger size, and some file system
1832 * specific extended attributes can have smaller and preset
1833 * size; for example, Finder Info is always 32 bytes.
1834 */
1835 memset(&vfa, 0, sizeof(vfa));
1836 VFSATTR_INIT(&vfa);
1837 VFSATTR_WANTED(&vfa, f_capabilities);
1838 if (vfs_getattr(vnode_mount(vp), &vfa, ctx) == 0 &&
1839 (VFSATTR_IS_SUPPORTED(&vfa, f_capabilities)) &&
1840 (vfa.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR) &&
1841 (vfa.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR)) {
1842 /* Supports native extended attributes */
1843 error = VNOP_PATHCONF(vp, name, retval, ctx);
1844 } else {
1845 /* Number of bits used to represent the maximum size of
1846 * extended attribute stored in an Apple Double file.
1847 */
1848 *retval = AD_XATTR_SIZE_BITS;
1849 }
1850 break;
1851 default:
1852 error = VNOP_PATHCONF(vp, name, retval, ctx);
1853 break;
1854 }
1855
1856 return error;
1857 }
1858
1859 static int
vn_kqfilter(struct fileproc * fp,struct knote * kn,struct kevent_qos_s * kev)1860 vn_kqfilter(struct fileproc *fp, struct knote *kn, struct kevent_qos_s *kev)
1861 {
1862 vfs_context_t ctx = vfs_context_current();
1863 struct vnode *vp;
1864 int error = 0;
1865 int result = 0;
1866
1867 vp = (struct vnode *)fp_get_data(fp);
1868
1869 /*
1870 * Don't attach a knote to a dead vnode.
1871 */
1872 if ((error = vget_internal(vp, 0, VNODE_NODEAD)) == 0) {
1873 switch (kn->kn_filter) {
1874 case EVFILT_READ:
1875 case EVFILT_WRITE:
1876 if (vnode_isfifo(vp)) {
1877 /* We'll only watch FIFOs that use our fifofs */
1878 if (!(vp->v_fifoinfo && vp->v_fifoinfo->fi_readsock)) {
1879 error = ENOTSUP;
1880 }
1881 } else if (!vnode_isreg(vp)) {
1882 if (vnode_ischr(vp)) {
1883 result = spec_kqfilter(vp, kn, kev);
1884 if ((kn->kn_flags & EV_ERROR) == 0) {
1885 /* claimed by a special device */
1886 vnode_put(vp);
1887 return result;
1888 }
1889 }
1890 error = EINVAL;
1891 }
1892 break;
1893 case EVFILT_VNODE:
1894 break;
1895 default:
1896 error = EINVAL;
1897 }
1898
1899 if (error == 0) {
1900 #if CONFIG_MACF
1901 error = mac_vnode_check_kqfilter(ctx, fp->fp_glob->fg_cred, kn, vp);
1902 if (error) {
1903 vnode_put(vp);
1904 goto out;
1905 }
1906 #endif
1907
1908 kn->kn_hook = (void*)vp;
1909 kn->kn_filtid = EVFILTID_VN;
1910
1911 vnode_lock(vp);
1912 KNOTE_ATTACH(&vp->v_knotes, kn);
1913 result = filt_vnode_common(kn, NULL, vp, 0);
1914 vnode_unlock(vp);
1915
1916 /*
1917 * Ask the filesystem to provide remove notifications,
1918 * but ignore failure
1919 */
1920 VNOP_MONITOR(vp, 0, VNODE_MONITOR_BEGIN, (void*) kn, ctx);
1921 }
1922
1923 vnode_put(vp);
1924 }
1925
1926 out:
1927 if (error) {
1928 knote_set_error(kn, error);
1929 }
1930
1931 return result;
1932 }
1933
1934 static void
filt_vndetach(struct knote * kn)1935 filt_vndetach(struct knote *kn)
1936 {
1937 vfs_context_t ctx = vfs_context_current();
1938 struct vnode *vp = (struct vnode *)kn->kn_hook;
1939 uint32_t vid = vnode_vid(vp);
1940 if (vnode_getwithvid(vp, vid)) {
1941 return;
1942 }
1943
1944 vnode_lock(vp);
1945 KNOTE_DETACH(&vp->v_knotes, kn);
1946 vnode_unlock(vp);
1947
1948 /*
1949 * Tell a (generally networked) filesystem that we're no longer watching
1950 * If the FS wants to track contexts, it should still be using the one from
1951 * the VNODE_MONITOR_BEGIN.
1952 */
1953 VNOP_MONITOR(vp, 0, VNODE_MONITOR_END, (void*)kn, ctx);
1954 vnode_put(vp);
1955 }
1956
1957
1958 /*
1959 * Used for EVFILT_READ
1960 *
1961 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1962 * differently than the regular case for VREG files. If not in poll(),
1963 * then we need to know current fileproc offset for VREG.
1964 */
1965 static int64_t
vnode_readable_data_count(vnode_t vp,off_t current_offset,int ispoll)1966 vnode_readable_data_count(vnode_t vp, off_t current_offset, int ispoll)
1967 {
1968 if (vnode_isfifo(vp)) {
1969 #if FIFO
1970 int cnt;
1971 int err = fifo_charcount(vp, &cnt);
1972 if (err == 0) {
1973 return (int64_t)cnt;
1974 } else
1975 #endif
1976 {
1977 return 0;
1978 }
1979 } else if (vnode_isreg(vp)) {
1980 if (ispoll) {
1981 return 1;
1982 }
1983
1984 off_t amount;
1985 amount = vp->v_un.vu_ubcinfo->ui_size - current_offset;
1986 if (amount > INT64_MAX) {
1987 return INT64_MAX;
1988 } else if (amount < INT64_MIN) {
1989 return INT64_MIN;
1990 } else {
1991 return (int64_t)amount;
1992 }
1993 } else {
1994 panic("Should never have an EVFILT_READ except for reg or fifo.");
1995 return 0;
1996 }
1997 }
1998
1999 /*
2000 * Used for EVFILT_WRITE.
2001 *
2002 * For regular vnodes, we can always write (1). For named pipes,
2003 * see how much space there is in the buffer. Nothing else is covered.
2004 */
2005 static intptr_t
vnode_writable_space_count(vnode_t vp)2006 vnode_writable_space_count(vnode_t vp)
2007 {
2008 if (vnode_isfifo(vp)) {
2009 #if FIFO
2010 long spc;
2011 int err = fifo_freespace(vp, &spc);
2012 if (err == 0) {
2013 return (intptr_t)spc;
2014 } else
2015 #endif
2016 {
2017 return (intptr_t)0;
2018 }
2019 } else if (vnode_isreg(vp)) {
2020 return (intptr_t)1;
2021 } else {
2022 panic("Should never have an EVFILT_READ except for reg or fifo.");
2023 return 0;
2024 }
2025 }
2026
2027 /*
2028 * Determine whether this knote should be active
2029 *
2030 * This is kind of subtle.
2031 * --First, notice if the vnode has been revoked: in so, override hint
2032 * --EVFILT_READ knotes are checked no matter what the hint is
2033 * --Other knotes activate based on hint.
2034 * --If hint is revoke, set special flags and activate
2035 */
2036 static int
filt_vnode_common(struct knote * kn,struct kevent_qos_s * kev,vnode_t vp,long hint)2037 filt_vnode_common(struct knote *kn, struct kevent_qos_s *kev, vnode_t vp, long hint)
2038 {
2039 int activate = 0;
2040 int64_t data = 0;
2041
2042 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
2043
2044 /* Special handling for vnodes that are in recycle or already gone */
2045 if (NOTE_REVOKE == hint) {
2046 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2047 activate = 1;
2048
2049 if ((kn->kn_filter == EVFILT_VNODE) && (kn->kn_sfflags & NOTE_REVOKE)) {
2050 kn->kn_fflags |= NOTE_REVOKE;
2051 }
2052 } else {
2053 switch (kn->kn_filter) {
2054 case EVFILT_READ:
2055 data = vnode_readable_data_count(vp, kn->kn_fp->fp_glob->fg_offset, (kn->kn_flags & EV_POLL));
2056 activate = (data != 0);
2057 break;
2058 case EVFILT_WRITE:
2059 data = vnode_writable_space_count(vp);
2060 activate = (data != 0);
2061 break;
2062 case EVFILT_VNODE:
2063 /* Check events this note matches against the hint */
2064 if (kn->kn_sfflags & hint) {
2065 kn->kn_fflags |= hint; /* Set which event occurred */
2066 }
2067 activate = (kn->kn_fflags != 0);
2068 break;
2069 default:
2070 panic("Invalid knote filter on a vnode!");
2071 }
2072 }
2073
2074 if (kev && activate) {
2075 knote_fill_kevent(kn, kev, data);
2076 }
2077
2078 return activate;
2079 }
2080
2081 static int
filt_vnode(struct knote * kn,long hint)2082 filt_vnode(struct knote *kn, long hint)
2083 {
2084 vnode_t vp = (struct vnode *)kn->kn_hook;
2085
2086 return filt_vnode_common(kn, NULL, vp, hint);
2087 }
2088
2089 static int
filt_vntouch(struct knote * kn,struct kevent_qos_s * kev)2090 filt_vntouch(struct knote *kn, struct kevent_qos_s *kev)
2091 {
2092 vnode_t vp = (struct vnode *)kn->kn_hook;
2093 uint32_t vid = vnode_vid(vp);
2094 int activate;
2095 int hint = 0;
2096
2097 vnode_lock(vp);
2098 if (vnode_getiocount(vp, vid, VNODE_NODEAD | VNODE_WITHID) != 0) {
2099 /* is recycled */
2100 hint = NOTE_REVOKE;
2101 }
2102
2103 /* accept new input fflags mask */
2104 kn->kn_sfflags = kev->fflags;
2105
2106 activate = filt_vnode_common(kn, NULL, vp, hint);
2107
2108 if (hint == 0) {
2109 vnode_put_locked(vp);
2110 }
2111 vnode_unlock(vp);
2112
2113 return activate;
2114 }
2115
2116 static int
filt_vnprocess(struct knote * kn,struct kevent_qos_s * kev)2117 filt_vnprocess(struct knote *kn, struct kevent_qos_s *kev)
2118 {
2119 vnode_t vp = (struct vnode *)kn->kn_hook;
2120 uint32_t vid = vnode_vid(vp);
2121 int activate;
2122 int hint = 0;
2123
2124 vnode_lock(vp);
2125 if (vnode_getiocount(vp, vid, VNODE_NODEAD | VNODE_WITHID) != 0) {
2126 /* Is recycled */
2127 hint = NOTE_REVOKE;
2128 }
2129 activate = filt_vnode_common(kn, kev, vp, hint);
2130
2131 /* Definitely need to unlock, may need to put */
2132 if (hint == 0) {
2133 vnode_put_locked(vp);
2134 }
2135 vnode_unlock(vp);
2136
2137 return activate;
2138 }
2139