forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkern_descrip.c
5789 lines (5105 loc) · 133 KB
/
kern_descrip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2016 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
/*
* Copyright (c) 1982, 1986, 1989, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
* support for mandatory and extensible security protections. This notice
* is included in support of clause 2.2 (b) of the Apple Public License,
* Version 2.0.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filedesc.h>
#include <sys/kernel.h>
#include <sys/vnode_internal.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/file_internal.h>
#include <sys/guarded.h>
#include <sys/priv.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/fsctl.h>
#include <sys/malloc.h>
#include <sys/mman.h>
#include <sys/syslog.h>
#include <sys/unistd.h>
#include <sys/resourcevar.h>
#include <sys/aio_kern.h>
#include <sys/ev.h>
#include <kern/locks.h>
#include <sys/uio_internal.h>
#include <sys/codesign.h>
#include <sys/codedir_internal.h>
#include <sys/mount_internal.h>
#include <sys/kdebug.h>
#include <sys/sysproto.h>
#include <sys/pipe.h>
#include <sys/spawn.h>
#include <sys/cprotect.h>
#include <sys/ubc_internal.h>
#include <kern/kern_types.h>
#include <kern/kalloc.h>
#include <kern/waitq.h>
#include <kern/ipc_misc.h>
#include <vm/vm_protos.h>
#include <mach/mach_port.h>
#include <security/audit/audit.h>
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
#include <stdbool.h>
#include <os/atomic_private.h>
#include <IOKit/IOBSD.h>
#define IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND 0x1
kern_return_t ipc_object_copyin(ipc_space_t, mach_port_name_t,
mach_msg_type_name_t, ipc_port_t *, mach_port_context_t, mach_msg_guard_flags_t *, uint32_t);
void ipc_port_release_send(ipc_port_t);
static void fileproc_drain(proc_t, struct fileproc *);
static int finishdup(proc_t p,
struct filedesc *fdp, int old, int new, int flags, int32_t *retval);
void fileport_releasefg(struct fileglob *fg);
/* flags for fp_close_and_unlock */
#define FD_DUP2RESV 1
/* We don't want these exported */
__private_extern__
int unlink1(vfs_context_t, vnode_t, user_addr_t, enum uio_seg, int);
static void fdrelse(struct proc * p, int fd);
extern kauth_scope_t kauth_scope_fileop;
/* Conflict wait queue for when selects collide (opaque type) */
extern struct waitq select_conflict_queue;
#ifndef HFS_GET_BOOT_INFO
#define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
#endif
#ifndef HFS_SET_BOOT_INFO
#define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
#endif
#ifndef APFSIOC_REVERT_TO_SNAPSHOT
#define APFSIOC_REVERT_TO_SNAPSHOT _IOW('J', 1, u_int64_t)
#endif
#define f_flag fp_glob->fg_flag
#define f_type fp_glob->fg_ops->fo_type
#define f_cred fp_glob->fg_cred
#define f_ops fp_glob->fg_ops
#define f_offset fp_glob->fg_offset
#define f_data fp_glob->fg_data
#define CHECK_ADD_OVERFLOW_INT64L(x, y) \
(((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
(((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
? 1 : 0)
ZONE_DECLARE(fg_zone, "fileglob",
sizeof(struct fileglob), ZC_NOENCRYPT | ZC_ZFREE_CLEARMEM);
ZONE_DECLARE(fp_zone, "fileproc",
sizeof(struct fileproc), ZC_NOENCRYPT | ZC_ZFREE_CLEARMEM);
ZONE_DECLARE(fdp_zone, "filedesc",
sizeof(struct filedesc), ZC_NOENCRYPT | ZC_ZFREE_CLEARMEM);
/*
* If you need accounting for KM_OFILETABL consider using
* KALLOC_HEAP_DEFINE to define a view.
*/
#define KM_OFILETABL KHEAP_DEFAULT
/*
* Descriptor management.
*/
int nfiles; /* actual number of open files */
/*
* "uninitialized" ops -- ensure FILEGLOB_DTYPE(fg) always exists
*/
static const struct fileops uninitops;
os_refgrp_decl(, f_refgrp, "files refcounts", NULL);
static LCK_GRP_DECLARE(file_lck_grp, "file");
#pragma mark fileglobs
/*!
* @function fg_free
*
* @brief
* Free a file structure.
*/
static void
fg_free(struct fileglob *fg)
{
os_atomic_dec(&nfiles, relaxed);
if (fg->fg_vn_data) {
fg_vn_data_free(fg->fg_vn_data);
fg->fg_vn_data = NULL;
}
if (IS_VALID_CRED(fg->fg_cred)) {
kauth_cred_unref(&fg->fg_cred);
}
lck_mtx_destroy(&fg->fg_lock, &file_lck_grp);
#if CONFIG_MACF
mac_file_label_destroy(fg);
#endif
zfree(fg_zone, fg);
}
OS_ALWAYS_INLINE
void
fg_ref(proc_t p, struct fileglob *fg)
{
#if DEBUG || DEVELOPMENT
proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED);
#else
(void)p;
#endif
os_ref_retain_raw(&fg->fg_count, &f_refgrp);
}
void
fg_drop_live(struct fileglob *fg)
{
os_ref_release_live_raw(&fg->fg_count, &f_refgrp);
}
int
fg_drop(proc_t p, struct fileglob *fg)
{
struct vnode *vp;
struct vfs_context context;
int error = 0;
if (fg == NULL) {
return 0;
}
/* Set up context with cred stashed in fg */
if (p == current_proc()) {
context.vc_thread = current_thread();
} else {
context.vc_thread = NULL;
}
context.vc_ucred = fg->fg_cred;
/*
* POSIX record locking dictates that any close releases ALL
* locks owned by this process. This is handled by setting
* a flag in the unlock to free ONLY locks obeying POSIX
* semantics, and not to free BSD-style file locks.
* If the descriptor was in a message, POSIX-style locks
* aren't passed with the descriptor.
*/
if (p && DTYPE_VNODE == FILEGLOB_DTYPE(fg) &&
(p->p_ladvflag & P_LADVLOCK)) {
struct flock lf = {
.l_whence = SEEK_SET,
.l_type = F_UNLCK,
};
vp = (struct vnode *)fg->fg_data;
if ((error = vnode_getwithref(vp)) == 0) {
(void)VNOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX, &context, NULL);
(void)vnode_put(vp);
}
}
if (os_ref_release_raw(&fg->fg_count, &f_refgrp) == 0) {
/*
* Since we ensure that fg->fg_ops is always initialized,
* it is safe to invoke fo_close on the fg
*/
error = fo_close(fg, &context);
fg_free(fg);
}
return error;
}
/*
* fg_get_vnode
*
* Description: Return vnode associated with the file structure, if
* any. The lifetime of the returned vnode is bound to
* the lifetime of the file structure.
*
* Parameters: fg Pointer to fileglob to
* inspect
*
* Returns: vnode_t
*/
vnode_t
fg_get_vnode(struct fileglob *fg)
{
if (FILEGLOB_DTYPE(fg) == DTYPE_VNODE) {
return (vnode_t)fg->fg_data;
} else {
return NULL;
}
}
bool
fg_sendable(struct fileglob *fg)
{
switch (FILEGLOB_DTYPE(fg)) {
case DTYPE_VNODE:
case DTYPE_SOCKET:
case DTYPE_PIPE:
case DTYPE_PSXSHM:
case DTYPE_NETPOLICY:
return (fg->fg_lflags & FG_CONFINED) == 0;
default:
return false;
}
}
#pragma mark fileprocs
/*
* check_file_seek_range
*
* Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
*
* Parameters: fl Flock structure.
* cur_file_offset Current offset in the file.
*
* Returns: 0 on Success.
* EOVERFLOW on overflow.
* EINVAL on offset less than zero.
*/
static int
check_file_seek_range(struct flock *fl, off_t cur_file_offset)
{
if (fl->l_whence == SEEK_CUR) {
/* Check if the start marker is beyond LLONG_MAX. */
if (CHECK_ADD_OVERFLOW_INT64L(fl->l_start, cur_file_offset)) {
/* Check if start marker is negative */
if (fl->l_start < 0) {
return EINVAL;
}
return EOVERFLOW;
}
/* Check if the start marker is negative. */
if (fl->l_start + cur_file_offset < 0) {
return EINVAL;
}
/* Check if end marker is beyond LLONG_MAX. */
if ((fl->l_len > 0) && (CHECK_ADD_OVERFLOW_INT64L(fl->l_start +
cur_file_offset, fl->l_len - 1))) {
return EOVERFLOW;
}
/* Check if the end marker is negative. */
if ((fl->l_len <= 0) && (fl->l_start + cur_file_offset +
fl->l_len < 0)) {
return EINVAL;
}
} else if (fl->l_whence == SEEK_SET) {
/* Check if the start marker is negative. */
if (fl->l_start < 0) {
return EINVAL;
}
/* Check if the end marker is beyond LLONG_MAX. */
if ((fl->l_len > 0) &&
CHECK_ADD_OVERFLOW_INT64L(fl->l_start, fl->l_len - 1)) {
return EOVERFLOW;
}
/* Check if the end marker is negative. */
if ((fl->l_len < 0) && fl->l_start + fl->l_len < 0) {
return EINVAL;
}
}
return 0;
}
void
proc_dirs_lock_shared(proc_t p)
{
lck_rw_lock_shared(&p->p_dirs_lock);
}
void
proc_dirs_unlock_shared(proc_t p)
{
lck_rw_unlock_shared(&p->p_dirs_lock);
}
void
proc_dirs_lock_exclusive(proc_t p)
{
lck_rw_lock_exclusive(&p->p_dirs_lock);
}
void
proc_dirs_unlock_exclusive(proc_t p)
{
lck_rw_unlock_exclusive(&p->p_dirs_lock);
}
/*
* proc_fdlock, proc_fdlock_spin
*
* Description: Lock to control access to the per process struct fileproc
* and struct filedesc
*
* Parameters: p Process to take the lock on
*
* Returns: void
*
* Notes: The lock is initialized in forkproc() and destroyed in
* reap_child_process().
*/
void
proc_fdlock(proc_t p)
{
lck_mtx_lock(&p->p_fdmlock);
}
void
proc_fdlock_spin(proc_t p)
{
lck_mtx_lock_spin(&p->p_fdmlock);
}
void
proc_fdlock_assert(proc_t p, int assertflags)
{
lck_mtx_assert(&p->p_fdmlock, assertflags);
}
/*
* proc_fdunlock
*
* Description: Unlock the lock previously locked by a call to proc_fdlock()
*
* Parameters: p Process to drop the lock on
*
* Returns: void
*/
void
proc_fdunlock(proc_t p)
{
lck_mtx_unlock(&p->p_fdmlock);
}
struct fdt_iterator
fdt_next(proc_t p, int fd, bool only_settled)
{
struct fdt_iterator it;
struct filedesc *fdp = p->p_fd;
struct fileproc *fp;
int nfds = min(fdp->fd_lastfile + 1, fdp->fd_nfiles);
while (++fd < nfds) {
fp = fdp->fd_ofiles[fd];
if (fp == NULL || fp->fp_glob == NULL) {
continue;
}
if (only_settled && (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
continue;
}
it.fdti_fd = fd;
it.fdti_fp = fp;
return it;
}
it.fdti_fd = nfds;
it.fdti_fp = NULL;
return it;
}
struct fdt_iterator
fdt_prev(proc_t p, int fd, bool only_settled)
{
struct fdt_iterator it;
struct filedesc *fdp = p->p_fd;
struct fileproc *fp;
while (--fd >= 0) {
fp = fdp->fd_ofiles[fd];
if (fp == NULL || fp->fp_glob == NULL) {
continue;
}
if (only_settled && (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
continue;
}
it.fdti_fd = fd;
it.fdti_fp = fp;
return it;
}
it.fdti_fd = -1;
it.fdti_fp = NULL;
return it;
}
/*
* System calls on descriptors.
*/
/*
* sys_getdtablesize
*
* Description: Returns the per process maximum size of the descriptor table
*
* Parameters: p Process being queried
* retval Pointer to the call return area
*
* Returns: 0 Success
*
* Implicit returns:
* *retval (modified) Size of dtable
*/
int
sys_getdtablesize(proc_t p, __unused struct getdtablesize_args *uap, int32_t *retval)
{
*retval = (int32_t)MIN(proc_limitgetcur(p, RLIMIT_NOFILE, TRUE), maxfilesperproc);
return 0;
}
static void
procfdtbl_reservefd(struct proc * p, int fd)
{
p->p_fd->fd_ofiles[fd] = NULL;
p->p_fd->fd_ofileflags[fd] |= UF_RESERVED;
}
void
procfdtbl_releasefd(struct proc * p, int fd, struct fileproc * fp)
{
if (fp != NULL) {
p->p_fd->fd_ofiles[fd] = fp;
}
p->p_fd->fd_ofileflags[fd] &= ~UF_RESERVED;
if ((p->p_fd->fd_ofileflags[fd] & UF_RESVWAIT) == UF_RESVWAIT) {
p->p_fd->fd_ofileflags[fd] &= ~UF_RESVWAIT;
wakeup(&p->p_fd);
}
}
static void
procfdtbl_waitfd(struct proc * p, int fd)
{
p->p_fd->fd_ofileflags[fd] |= UF_RESVWAIT;
msleep(&p->p_fd, &p->p_fdmlock, PRIBIO, "ftbl_waitfd", NULL);
}
static void
procfdtbl_clearfd(struct proc * p, int fd)
{
int waiting;
waiting = (p->p_fd->fd_ofileflags[fd] & UF_RESVWAIT);
p->p_fd->fd_ofiles[fd] = NULL;
p->p_fd->fd_ofileflags[fd] = 0;
if (waiting == UF_RESVWAIT) {
wakeup(&p->p_fd);
}
}
/*
* fdrelse
*
* Description: Inline utility function to free an fd in a filedesc
*
* Parameters: fdp Pointer to filedesc fd lies in
* fd fd to free
* reserv fd should be reserved
*
* Returns: void
*
* Locks: Assumes proc_fdlock for process pointing to fdp is held by
* the caller
*/
static void
fdrelse(struct proc * p, int fd)
{
struct filedesc *fdp = p->p_fd;
int nfd = 0;
if (fd < fdp->fd_freefile) {
fdp->fd_freefile = fd;
}
#if DIAGNOSTIC
if (fd > fdp->fd_lastfile) {
panic("fdrelse: fd_lastfile inconsistent");
}
#endif
procfdtbl_clearfd(p, fd);
while ((nfd = fdp->fd_lastfile) > 0 &&
fdp->fd_ofiles[nfd] == NULL &&
!(fdp->fd_ofileflags[nfd] & UF_RESERVED)) {
/* JMM - What about files with lingering EV_VANISHED knotes? */
fdp->fd_lastfile--;
}
}
int
fd_rdwr(
int fd,
enum uio_rw rw,
uint64_t base,
int64_t len,
enum uio_seg segflg,
off_t offset,
int io_flg,
int64_t *aresid)
{
struct fileproc *fp;
proc_t p;
int error = 0;
int flags = 0;
int spacetype;
uio_t auio = NULL;
char uio_buf[UIO_SIZEOF(1)];
struct vfs_context context = *(vfs_context_current());
p = current_proc();
error = fp_lookup(p, fd, &fp, 0);
if (error) {
return error;
}
switch (FILEGLOB_DTYPE(fp->fp_glob)) {
case DTYPE_VNODE:
case DTYPE_PIPE:
case DTYPE_SOCKET:
break;
default:
error = EINVAL;
goto out;
}
if (rw == UIO_WRITE && !(fp->f_flag & FWRITE)) {
error = EBADF;
goto out;
}
if (rw == UIO_READ && !(fp->f_flag & FREAD)) {
error = EBADF;
goto out;
}
context.vc_ucred = fp->fp_glob->fg_cred;
if (UIO_SEG_IS_USER_SPACE(segflg)) {
spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32;
} else {
spacetype = UIO_SYSSPACE;
}
auio = uio_createwithbuffer(1, offset, spacetype, rw, &uio_buf[0], sizeof(uio_buf));
uio_addiov(auio, (user_addr_t)base, (user_size_t)len);
if (!(io_flg & IO_APPEND)) {
flags = FOF_OFFSET;
}
if (rw == UIO_WRITE) {
user_ssize_t orig_resid = uio_resid(auio);
error = fo_write(fp, auio, flags, &context);
if (uio_resid(auio) < orig_resid) {
os_atomic_or(&fp->fp_glob->fg_flag, FWASWRITTEN, relaxed);
}
} else {
error = fo_read(fp, auio, flags, &context);
}
if (aresid) {
*aresid = uio_resid(auio);
} else if (uio_resid(auio) && error == 0) {
error = EIO;
}
out:
fp_drop(p, fd, fp, 0);
return error;
}
/*
* sys_dup
*
* Description: Duplicate a file descriptor.
*
* Parameters: p Process performing the dup
* uap->fd The fd to dup
* retval Pointer to the call return area
*
* Returns: 0 Success
* !0 Errno
*
* Implicit returns:
* *retval (modified) The new descriptor
*/
int
sys_dup(proc_t p, struct dup_args *uap, int32_t *retval)
{
struct filedesc *fdp = p->p_fd;
int old = uap->fd;
int new, error;
struct fileproc *fp;
proc_fdlock(p);
if ((error = fp_lookup(p, old, &fp, 1))) {
proc_fdunlock(p);
return error;
}
if (fp_isguarded(fp, GUARD_DUP)) {
error = fp_guard_exception(p, old, fp, kGUARD_EXC_DUP);
(void) fp_drop(p, old, fp, 1);
proc_fdunlock(p);
return error;
}
if ((error = fdalloc(p, 0, &new))) {
fp_drop(p, old, fp, 1);
proc_fdunlock(p);
return error;
}
error = finishdup(p, fdp, old, new, 0, retval);
fp_drop(p, old, fp, 1);
proc_fdunlock(p);
if (ENTR_SHOULDTRACE && FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) {
KERNEL_ENERGYTRACE(kEnTrActKernSocket, DBG_FUNC_START,
new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp->f_data));
}
return error;
}
/*
* sys_dup2
*
* Description: Duplicate a file descriptor to a particular value.
*
* Parameters: p Process performing the dup
* uap->from The fd to dup
* uap->to The fd to dup it to
* retval Pointer to the call return area
*
* Returns: 0 Success
* !0 Errno
*
* Implicit returns:
* *retval (modified) The new descriptor
*/
int
sys_dup2(proc_t p, struct dup2_args *uap, int32_t *retval)
{
return dup2(p, uap->from, uap->to, retval);
}
int
dup2(proc_t p, int old, int new, int *retval)
{
struct filedesc *fdp = p->p_fd;
struct fileproc *fp, *nfp;
int i, error;
rlim_t nofile = proc_limitgetcur(p, RLIMIT_NOFILE, TRUE);
proc_fdlock(p);
startover:
if ((error = fp_lookup(p, old, &fp, 1))) {
proc_fdunlock(p);
return error;
}
if (fp_isguarded(fp, GUARD_DUP)) {
error = fp_guard_exception(p, old, fp, kGUARD_EXC_DUP);
(void) fp_drop(p, old, fp, 1);
proc_fdunlock(p);
return error;
}
if (new < 0 ||
(rlim_t)new >= nofile ||
new >= maxfilesperproc) {
fp_drop(p, old, fp, 1);
proc_fdunlock(p);
return EBADF;
}
if (old == new) {
fp_drop(p, old, fp, 1);
*retval = new;
proc_fdunlock(p);
return 0;
}
if (new < 0 || new >= fdp->fd_nfiles) {
if ((error = fdalloc(p, new, &i))) {
fp_drop(p, old, fp, 1);
proc_fdunlock(p);
return error;
}
if (new != i) {
fdrelse(p, i);
goto closeit;
}
} else {
closeit:
if ((fdp->fd_ofileflags[new] & UF_RESERVED) == UF_RESERVED) {
fp_drop(p, old, fp, 1);
procfdtbl_waitfd(p, new);
#if DIAGNOSTIC
proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED);
#endif
goto startover;
}
if ((nfp = fdp->fd_ofiles[new]) != NULL) {
if (fp_isguarded(nfp, GUARD_CLOSE)) {
fp_drop(p, old, fp, 1);
error = fp_guard_exception(p,
new, nfp, kGUARD_EXC_CLOSE);
proc_fdunlock(p);
return error;
}
(void)fp_close_and_unlock(p, new, nfp, FD_DUP2RESV);
proc_fdlock(p);
assert(fdp->fd_ofileflags[new] & UF_RESERVED);
} else {
#if DIAGNOSTIC
if (fdp->fd_ofiles[new] != NULL) {
panic("dup2: no ref on fileproc %d", new);
}
#endif
procfdtbl_reservefd(p, new);
}
}
#if DIAGNOSTIC
if (fdp->fd_ofiles[new] != 0) {
panic("dup2: overwriting fd_ofiles with new %d", new);
}
if ((fdp->fd_ofileflags[new] & UF_RESERVED) == 0) {
panic("dup2: unreserved fileflags with new %d", new);
}
#endif
error = finishdup(p, fdp, old, new, 0, retval);
fp_drop(p, old, fp, 1);
proc_fdunlock(p);
return error;
}
/*
* fcntl
*
* Description: The file control system call.
*
* Parameters: p Process performing the fcntl
* uap->fd The fd to operate against
* uap->cmd The command to perform
* uap->arg Pointer to the command argument
* retval Pointer to the call return area
*
* Returns: 0 Success
* !0 Errno (see fcntl_nocancel)
*
* Implicit returns:
* *retval (modified) fcntl return value (if any)
*
* Notes: This system call differs from fcntl_nocancel() in that it
* tests for cancellation prior to performing a potentially
* blocking operation.
*/
int
sys_fcntl(proc_t p, struct fcntl_args *uap, int32_t *retval)
{
__pthread_testcancel(1);
return sys_fcntl_nocancel(p, (struct fcntl_nocancel_args *)uap, retval);
}
#define ACCOUNT_OPENFROM_ENTITLEMENT \
"com.apple.private.vfs.role-account-openfrom"
/*
* sys_fcntl_nocancel
*
* Description: A non-cancel-testing file control system call.
*
* Parameters: p Process performing the fcntl
* uap->fd The fd to operate against
* uap->cmd The command to perform
* uap->arg Pointer to the command argument
* retval Pointer to the call return area
*
* Returns: 0 Success
* EINVAL
* fp_lookup:EBADF Bad file descriptor
* [F_DUPFD]
* fdalloc:EMFILE
* fdalloc:ENOMEM
* finishdup:EBADF
* finishdup:ENOMEM
* [F_SETOWN]
* ESRCH
* [F_SETLK]
* EBADF
* EOVERFLOW
* copyin:EFAULT
* vnode_getwithref:???
* VNOP_ADVLOCK:???
* msleep:ETIMEDOUT
* [F_GETLK]
* EBADF
* EOVERFLOW
* copyin:EFAULT
* copyout:EFAULT
* vnode_getwithref:???
* VNOP_ADVLOCK:???
* [F_PREALLOCATE]
* EBADF
* EINVAL
* copyin:EFAULT
* copyout:EFAULT
* vnode_getwithref:???
* VNOP_ALLOCATE:???
* [F_SETSIZE,F_RDADVISE]
* EBADF
* EINVAL
* copyin:EFAULT
* vnode_getwithref:???
* [F_RDAHEAD,F_NOCACHE]
* EBADF
* vnode_getwithref:???
* [???]
*
* Implicit returns:
* *retval (modified) fcntl return value (if any)
*/
int
sys_fcntl_nocancel(proc_t p, struct fcntl_nocancel_args *uap, int32_t *retval)
{
int fd = uap->fd;
struct filedesc *fdp = p->p_fd;
struct fileproc *fp;
char *pop;
struct vnode *vp = NULLVP; /* for AUDIT_ARG() at end */
unsigned int oflags, nflags;
int i, tmp, error, error2, flg = 0;
struct flock fl = {};
struct flocktimeout fltimeout;
struct timespec *timeout = NULL;
struct vfs_context context;
off_t offset;
int newmin;
daddr64_t lbn, bn;
unsigned int fflag;
user_addr_t argp;
boolean_t is64bit;
rlim_t nofile;
int has_entitlement = 0;
AUDIT_ARG(fd, uap->fd);
AUDIT_ARG(cmd, uap->cmd);
nofile = proc_limitgetcur(p, RLIMIT_NOFILE, TRUE);
proc_fdlock(p);
if ((error = fp_lookup(p, fd, &fp, 1))) {
proc_fdunlock(p);
return error;
}
context.vc_thread = current_thread();
context.vc_ucred = fp->f_cred;
is64bit = proc_is64bit(p);
if (is64bit) {
argp = uap->arg;