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