This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathvfs_syscalls.c
More file actions
8255 lines (7248 loc) · 203 KB
/
vfs_syscalls.c
File metadata and controls
8255 lines (7248 loc) · 203 KB
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) 1995-2008 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) 1989, 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.
*
* @(#)vfs_syscalls.c 8.41 (Berkeley) 6/15/95
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2005 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/namei.h>
#include <sys/filedesc.h>
#include <sys/kernel.h>
#include <sys/file_internal.h>
#include <sys/stat.h>
#include <sys/vnode_internal.h>
#include <sys/mount_internal.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/uio_internal.h>
#include <sys/malloc.h>
#include <sys/mman.h>
#include <sys/dirent.h>
#include <sys/attr.h>
#include <sys/sysctl.h>
#include <sys/ubc.h>
#include <sys/quota.h>
#include <sys/kdebug.h>
#include <sys/fsevents.h>
#include <sys/sysproto.h>
#include <sys/xattr.h>
#include <sys/fcntl.h>
#include <sys/fsctl.h>
#include <sys/ubc_internal.h>
#include <sys/disk.h>
#include <machine/cons.h>
#include <machine/limits.h>
#include <miscfs/specfs/specdev.h>
#include <miscfs/union/union.h>
#include <security/audit/audit.h>
#include <bsm/audit_kevents.h>
#include <mach/mach_types.h>
#include <kern/kern_types.h>
#include <kern/kalloc.h>
#include <vm/vm_pageout.h>
#include <libkern/OSAtomic.h>
#include <pexpert/pexpert.h>
#if CONFIG_MACF
#include <security/mac.h>
#include <security/mac_framework.h>
#endif
#if CONFIG_FSE
#define GET_PATH(x) \
(x) = get_pathbuff();
#define RELEASE_PATH(x) \
release_pathbuff(x);
#else
#define GET_PATH(x) \
MALLOC_ZONE((x), char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
#define RELEASE_PATH(x) \
FREE_ZONE((x), MAXPATHLEN, M_NAMEI);
#endif /* CONFIG_FSE */
/* struct for checkdirs iteration */
struct cdirargs {
vnode_t olddp;
vnode_t newdp;
};
/* callback for checkdirs iteration */
static int checkdirs_callback(proc_t p, void * arg);
static int change_dir(struct nameidata *ndp, vfs_context_t ctx);
static int checkdirs(vnode_t olddp, vfs_context_t ctx);
void enablequotas(struct mount *mp, vfs_context_t ctx);
static int getfsstat_callback(mount_t mp, void * arg);
static int getutimes(user_addr_t usrtvp, struct timespec *tsp);
static int setutimes(vfs_context_t ctx, vnode_t vp, const struct timespec *ts, int nullflag);
static int sync_callback(mount_t, void *);
static int munge_statfs(struct mount *mp, struct vfsstatfs *sfsp,
user_addr_t bufp, int *sizep, boolean_t is_64_bit,
boolean_t partial_copy);
static int statfs64_common(struct mount *mp, struct vfsstatfs *sfsp,
user_addr_t bufp);
static int fsync_common(proc_t p, struct fsync_args *uap, int flags);
#ifdef CONFIG_IMGSRC_ACCESS
static int prepare_coveredvp(vnode_t vp, vfs_context_t ctx, struct componentname *cnp, const char *fsname);
static int authorize_devpath_and_update_mntfromname(mount_t mp, user_addr_t devpath, vnode_t *devvpp, vfs_context_t ctx);
static int place_mount_and_checkdirs(mount_t mp, vnode_t vp, vfs_context_t ctx);
static void undo_place_on_covered_vp(mount_t mp, vnode_t vp);
static int mount_begin_update(mount_t mp, vfs_context_t ctx, int flags);
static void mount_end_update(mount_t mp);
static int relocate_imageboot_source(vnode_t vp, struct componentname *cnp, const char *fsname, vfs_context_t ctx, boolean_t is64bit, user_addr_t fsmountargs);
#endif /* CONFIG_IMGSRC_ACCESS */
int (*union_dircheckp)(struct vnode **, struct fileproc *, vfs_context_t);
__private_extern__
int sync_internal(void);
__private_extern__
int open1(vfs_context_t, struct nameidata *, int, struct vnode_attr *, int32_t *);
__private_extern__
int unlink1(vfs_context_t, struct nameidata *, int);
#ifdef __APPLE_API_OBSOLETE
struct fstatv_args {
int fd; /* file descriptor of the target file */
struct vstat *vsb; /* vstat structure for returned info */
};
struct lstatv_args {
const char *path; /* pathname of the target file */
struct vstat *vsb; /* vstat structure for returned info */
};
struct mkcomplex_args {
const char *path; /* pathname of the file to be created */
mode_t mode; /* access mode for the newly created file */
u_int32_t type; /* format of the complex file */
};
struct statv_args {
const char *path; /* pathname of the target file */
struct vstat *vsb; /* vstat structure for returned info */
};
int fstatv(proc_t p, struct fstatv_args *uap, int32_t *retval);
int lstatv(proc_t p, struct lstatv_args *uap, int32_t *retval);
int mkcomplex(proc_t p, struct mkcomplex_args *uap, int32_t *retval);
int statv(proc_t p, struct statv_args *uap, int32_t *retval);
#endif /* __APPLE_API_OBSOLETE */
/*
* incremented each time a mount or unmount operation occurs
* used to invalidate the cached value of the rootvp in the
* mount structure utilized by cache_lookup_path
*/
uint32_t mount_generation = 0;
/* counts number of mount and unmount operations */
unsigned int vfs_nummntops=0;
extern struct fileops vnops;
extern errno_t rmdir_remove_orphaned_appleDouble(vnode_t, vfs_context_t, int *);
/*
* Virtual File System System Calls
*/
/*
* Mount a file system.
*/
/* ARGSUSED */
int
mount(proc_t p, struct mount_args *uap, __unused int32_t *retval)
{
struct __mac_mount_args muap;
muap.type = uap->type;
muap.path = uap->path;
muap.flags = uap->flags;
muap.data = uap->data;
muap.mac_p = USER_ADDR_NULL;
return (__mac_mount(p, &muap, retval));
}
/*
* __mac_mount:
* Mount a file system taking into account MAC label behavior.
* See mount(2) man page for more information
*
* Parameters: p Process requesting the mount
* uap User argument descriptor (see below)
* retval (ignored)
*
* Indirect: uap->type Filesystem type
* uap->path Path to mount
* uap->data Mount arguments
* uap->mac_p MAC info
* uap->flags Mount flags
*
*
* Returns: 0 Success
* !0 Not success
*/
int
__mac_mount(struct proc *p, register struct __mac_mount_args *uap, __unused int32_t *retval)
{
struct vnode *vp, *pvp;
struct vnode *devvp = NULLVP;
struct vnode *device_vnode = NULLVP;
#if CONFIG_MACF
struct vnode *rvp;
#endif
struct mount *mp;
struct vfstable *vfsp = (struct vfstable *)0;
int error, flag = 0;
struct vnode_attr va;
vfs_context_t ctx = vfs_context_current();
struct nameidata nd;
struct nameidata nd1;
char fstypename[MFSNAMELEN];
size_t dummy=0;
user_addr_t devpath = USER_ADDR_NULL;
user_addr_t fsmountargs = uap->data;
int ronly = 0;
int mntalloc = 0;
boolean_t vfsp_ref = FALSE;
mode_t accessmode;
boolean_t is_64bit;
boolean_t is_rwlock_locked = FALSE;
boolean_t did_rele = FALSE;
boolean_t have_usecount = FALSE;
AUDIT_ARG(fflags, uap->flags);
is_64bit = proc_is64bit(p);
/*
* Get vnode to be covered
*/
NDINIT(&nd, LOOKUP, NOTRIGGER | FOLLOW | AUDITVNPATH1 | WANTPARENT,
UIO_USERSPACE, uap->path, ctx);
error = namei(&nd);
if (error)
return (error);
vp = nd.ni_vp;
pvp = nd.ni_dvp;
if ((vp->v_flag & VROOT) &&
(vp->v_mount->mnt_flag & MNT_ROOTFS))
uap->flags |= MNT_UPDATE;
error = copyinstr(uap->type, fstypename, MFSNAMELEN, &dummy);
if (error)
goto out1;
#ifdef CONFIG_IMGSRC_ACCESS
if (uap->flags == MNT_IMGSRC) {
error = relocate_imageboot_source(vp, &nd.ni_cnd, fstypename, ctx, is_64bit, fsmountargs);
vnode_put(pvp);
vnode_put(vp);
return error;
}
#endif /* CONFIG_IMGSRC_ACCESS */
if (uap->flags & MNT_UPDATE) {
if ((vp->v_flag & VROOT) == 0) {
error = EINVAL;
goto out1;
}
mp = vp->v_mount;
/* unmount in progress return error */
mount_lock_spin(mp);
if (mp->mnt_lflag & MNT_LUNMOUNT) {
mount_unlock(mp);
error = EBUSY;
goto out1;
}
mount_unlock(mp);
lck_rw_lock_exclusive(&mp->mnt_rwlock);
is_rwlock_locked = TRUE;
/*
* We only allow the filesystem to be reloaded if it
* is currently mounted read-only.
*/
if ((uap->flags & MNT_RELOAD) &&
((mp->mnt_flag & MNT_RDONLY) == 0)) {
error = ENOTSUP;
goto out1;
}
#ifdef CONFIG_IMGSRC_ACCESS
/* Can't downgrade the backer of the root FS */
if ((mp->mnt_kern_flag & MNTK_BACKS_ROOT) &&
(!vfs_isrdonly(mp)) && (uap->flags & MNT_RDONLY))
{
error = ENOTSUP;
goto out1;
}
#endif /* CONFIG_IMGSRC_ACCESS */
/*
* Only root, or the user that did the original mount is
* permitted to update it.
*/
if (mp->mnt_vfsstat.f_owner != kauth_cred_getuid(vfs_context_ucred(ctx)) &&
(error = suser(vfs_context_ucred(ctx), &p->p_acflag))) {
goto out1;
}
#if CONFIG_MACF
error = mac_mount_check_remount(ctx, mp);
if (error != 0) {
lck_rw_done(&mp->mnt_rwlock);
goto out1;
}
#endif
/*
* For non-root users, silently enforce MNT_NOSUID and MNT_NODEV,
* and MNT_NOEXEC if mount point is already MNT_NOEXEC.
*/
if (suser(vfs_context_ucred(ctx), NULL)) {
uap->flags |= MNT_NOSUID | MNT_NODEV;
if (mp->mnt_flag & MNT_NOEXEC)
uap->flags |= MNT_NOEXEC;
}
flag = mp->mnt_flag;
mp->mnt_flag |=
uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
vfsp = mp->mnt_vtable;
goto update;
}
/*
* If the user is not root, ensure that they own the directory
* onto which we are attempting to mount.
*/
VATTR_INIT(&va);
VATTR_WANTED(&va, va_uid);
if ((error = vnode_getattr(vp, &va, ctx)) ||
(va.va_uid != kauth_cred_getuid(vfs_context_ucred(ctx)) &&
(error = suser(vfs_context_ucred(ctx), &p->p_acflag)))) {
goto out1;
}
/*
* For non-root users, silently enforce MNT_NOSUID and MNT_NODEV, and
* MNT_NOEXEC if mount point is already MNT_NOEXEC.
*/
if (suser(vfs_context_ucred(ctx), NULL)) {
uap->flags |= MNT_NOSUID | MNT_NODEV;
if (vp->v_mount->mnt_flag & MNT_NOEXEC)
uap->flags |= MNT_NOEXEC;
}
if ( (error = VNOP_FSYNC(vp, MNT_WAIT, ctx)) )
goto out1;
if ( (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0)) )
goto out1;
if (vp->v_type != VDIR) {
error = ENOTDIR;
goto out1;
}
/* XXXAUDIT: Should we capture the type on the error path as well? */
AUDIT_ARG(text, fstypename);
mount_list_lock();
for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
if (!strncmp(vfsp->vfc_name, fstypename, MFSNAMELEN)) {
vfsp->vfc_refcount++;
vfsp_ref = TRUE;
break;
}
mount_list_unlock();
if (vfsp == NULL) {
error = ENODEV;
goto out1;
}
#if CONFIG_MACF
error = mac_mount_check_mount(ctx, vp,
&nd.ni_cnd, vfsp->vfc_name);
if (error != 0)
goto out1;
#endif
if (ISSET(vp->v_flag, VMOUNT) && (vp->v_mountedhere != NULL)) {
error = EBUSY;
goto out1;
}
vnode_lock_spin(vp);
SET(vp->v_flag, VMOUNT);
vnode_unlock(vp);
/*
* Allocate and initialize the filesystem.
*/
MALLOC_ZONE(mp, struct mount *, (u_int32_t)sizeof(struct mount),
M_MOUNT, M_WAITOK);
bzero((char *)mp, (u_int32_t)sizeof(struct mount));
mntalloc = 1;
/* Initialize the default IO constraints */
mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt;
mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt;
mp->mnt_devblocksize = DEV_BSIZE;
mp->mnt_alignmentmask = PAGE_MASK;
mp->mnt_ioqueue_depth = MNT_DEFAULT_IOQUEUE_DEPTH;
mp->mnt_ioscale = 1;
mp->mnt_ioflags = 0;
mp->mnt_realrootvp = NULLVP;
mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
TAILQ_INIT(&mp->mnt_vnodelist);
TAILQ_INIT(&mp->mnt_workerqueue);
TAILQ_INIT(&mp->mnt_newvnodes);
mount_lock_init(mp);
lck_rw_lock_exclusive(&mp->mnt_rwlock);
is_rwlock_locked = TRUE;
mp->mnt_op = vfsp->vfc_vfsops;
mp->mnt_vtable = vfsp;
//mp->mnt_stat.f_type = vfsp->vfc_typenum;
mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSTYPENAMELEN);
strncpy(mp->mnt_vfsstat.f_mntonname, nd.ni_cnd.cn_pnbuf, MAXPATHLEN);
mp->mnt_vnodecovered = vp;
mp->mnt_vfsstat.f_owner = kauth_cred_getuid(vfs_context_ucred(ctx));
mp->mnt_devbsdunit = LOWPRI_MAX_NUM_DEV - 1;
/* XXX 3762912 hack to support HFS filesystem 'owner' - filesystem may update later */
vfs_setowner(mp, KAUTH_UID_NONE, KAUTH_GID_NONE);
update:
/*
* Set the mount level flags.
*/
if (uap->flags & MNT_RDONLY)
mp->mnt_flag |= MNT_RDONLY;
else if (mp->mnt_flag & MNT_RDONLY)
mp->mnt_kern_flag |= MNTK_WANTRDWR;
mp->mnt_flag &= ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
MNT_UNKNOWNPERMISSIONS | MNT_DONTBROWSE | MNT_AUTOMOUNTED |
MNT_DEFWRITE | MNT_NOATIME | MNT_QUARANTINE | MNT_CPROTECT );
mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
MNT_UNKNOWNPERMISSIONS | MNT_DONTBROWSE | MNT_AUTOMOUNTED |
MNT_DEFWRITE | MNT_NOATIME | MNT_QUARANTINE | MNT_CPROTECT );
#if CONFIG_MACF
if (uap->flags & MNT_MULTILABEL) {
if (vfsp->vfc_vfsflags & VFC_VFSNOMACLABEL) {
error = EINVAL;
goto out1;
}
mp->mnt_flag |= MNT_MULTILABEL;
}
#endif
if (vfsp->vfc_vfsflags & VFC_VFSLOCALARGS) {
if (is_64bit) {
if ( (error = copyin(fsmountargs, (caddr_t)&devpath, sizeof(devpath))) )
goto out1;
fsmountargs += sizeof(devpath);
} else {
user32_addr_t tmp;
if ( (error = copyin(fsmountargs, (caddr_t)&tmp, sizeof(tmp))) )
goto out1;
/* munge into LP64 addr */
devpath = CAST_USER_ADDR_T(tmp);
fsmountargs += sizeof(tmp);
}
/* if it is not update and device name needs to be parsed */
if ((devpath)) {
NDINIT(&nd1, LOOKUP, FOLLOW, UIO_USERSPACE, devpath, ctx);
if ( (error = namei(&nd1)) )
goto out1;
strncpy(mp->mnt_vfsstat.f_mntfromname, nd1.ni_cnd.cn_pnbuf, MAXPATHLEN);
devvp = nd1.ni_vp;
nameidone(&nd1);
if (devvp->v_type != VBLK) {
error = ENOTBLK;
goto out2;
}
if (major(devvp->v_rdev) >= nblkdev) {
error = ENXIO;
goto out2;
}
/*
* If mount by non-root, then verify that user has necessary
* permissions on the device.
*/
if (suser(vfs_context_ucred(ctx), NULL) != 0) {
accessmode = KAUTH_VNODE_READ_DATA;
if ((mp->mnt_flag & MNT_RDONLY) == 0)
accessmode |= KAUTH_VNODE_WRITE_DATA;
if ((error = vnode_authorize(devvp, NULL, accessmode, ctx)) != 0)
goto out2;
}
}
if (devpath && ((uap->flags & MNT_UPDATE) == 0)) {
if ( (error = vnode_ref(devvp)) )
goto out2;
/*
* Disallow multiple mounts of the same device.
* Disallow mounting of a device that is currently in use
* (except for root, which might share swap device for miniroot).
* Flush out any old buffers remaining from a previous use.
*/
if ( (error = vfs_mountedon(devvp)) )
goto out3;
if (vcount(devvp) > 1 && !(vfs_flags(mp) & MNT_ROOTFS)) {
error = EBUSY;
goto out3;
}
if ( (error = VNOP_FSYNC(devvp, MNT_WAIT, ctx)) ) {
error = ENOTBLK;
goto out3;
}
if ( (error = buf_invalidateblks(devvp, BUF_WRITE_DATA, 0, 0)) )
goto out3;
ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
#if CONFIG_MACF
error = mac_vnode_check_open(ctx,
devvp,
ronly ? FREAD : FREAD|FWRITE);
if (error)
goto out3;
#endif /* MAC */
if ( (error = VNOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, ctx)) )
goto out3;
mp->mnt_devvp = devvp;
device_vnode = devvp;
} else {
if ((mp->mnt_flag & MNT_RDONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
dev_t dev;
int maj;
/*
* If upgrade to read-write by non-root, then verify
* that user has necessary permissions on the device.
*/
device_vnode = mp->mnt_devvp;
if (device_vnode) {
vnode_getalways(device_vnode);
if (suser(vfs_context_ucred(ctx), NULL)) {
if ((error = vnode_authorize(device_vnode, NULL,
KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, ctx)) != 0) {
vnode_put(device_vnode);
goto out2;
}
}
/* Tell the device that we're upgrading */
dev = (dev_t)device_vnode->v_rdev;
maj = major(dev);
if ((u_int)maj >= (u_int)nblkdev)
panic("Volume mounted on a device with invalid major number.\n");
error = bdevsw[maj].d_open(dev, FREAD | FWRITE, S_IFBLK, p);
vnode_put(device_vnode);
if (error != 0) {
goto out2;
}
}
}
device_vnode = NULLVP;
}
}
#if CONFIG_MACF
if ((uap->flags & MNT_UPDATE) == 0) {
mac_mount_label_init(mp);
mac_mount_label_associate(ctx, mp);
}
if (uap->mac_p != USER_ADDR_NULL) {
struct user_mac mac;
char *labelstr = NULL;
size_t ulen = 0;
if ((uap->flags & MNT_UPDATE) != 0) {
error = mac_mount_check_label_update(
ctx, mp);
if (error != 0)
goto out3;
}
if (is_64bit) {
error = copyin(uap->mac_p, &mac, sizeof(mac));
} else {
struct mac mac32;
error = copyin(uap->mac_p, &mac32, sizeof(mac32));
mac.m_buflen = mac32.m_buflen;
mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
}
if (error != 0)
goto out3;
if ((mac.m_buflen > MAC_MAX_LABEL_BUF_LEN) ||
(mac.m_buflen < 2)) {
error = EINVAL;
goto out3;
}
MALLOC(labelstr, char *, mac.m_buflen, M_MACTEMP, M_WAITOK);
error = copyinstr(mac.m_string, labelstr, mac.m_buflen, &ulen);
if (error != 0) {
FREE(labelstr, M_MACTEMP);
goto out3;
}
AUDIT_ARG(mac_string, labelstr);
error = mac_mount_label_internalize(mp->mnt_mntlabel, labelstr);
FREE(labelstr, M_MACTEMP);
if (error != 0)
goto out3;
}
#endif
if (device_vnode != NULL) {
VNOP_IOCTL(device_vnode, DKIOCGETBSDUNIT, (caddr_t)&mp->mnt_devbsdunit, 0, NULL);
mp->mnt_devbsdunit %= LOWPRI_MAX_NUM_DEV;
}
/*
* Mount the filesystem.
*/
error = VFS_MOUNT(mp, device_vnode, fsmountargs, ctx);
if (uap->flags & MNT_UPDATE) {
if (mp->mnt_kern_flag & MNTK_WANTRDWR)
mp->mnt_flag &= ~MNT_RDONLY;
mp->mnt_flag &=~
(MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
if (error)
mp->mnt_flag = flag;
vfs_event_signal(NULL, VQ_UPDATE, (intptr_t)NULL);
lck_rw_done(&mp->mnt_rwlock);
is_rwlock_locked = FALSE;
if (!error)
enablequotas(mp, ctx);
goto out2;
}
/*
* Put the new filesystem on the mount list after root.
*/
if (error == 0) {
struct vfs_attr vfsattr;
#if CONFIG_MACF
if (vfs_flags(mp) & MNT_MULTILABEL) {
error = VFS_ROOT(mp, &rvp, ctx);
if (error) {
printf("%s() VFS_ROOT returned %d\n", __func__, error);
goto out3;
}
error = vnode_label(mp, NULL, rvp, NULL, 0, ctx);
/*
* drop reference provided by VFS_ROOT
*/
vnode_put(rvp);
if (error)
goto out3;
}
#endif /* MAC */
vnode_lock_spin(vp);
CLR(vp->v_flag, VMOUNT);
vp->v_mountedhere = mp;
vnode_unlock(vp);
/*
* taking the name_cache_lock exclusively will
* insure that everyone is out of the fast path who
* might be trying to use a now stale copy of
* vp->v_mountedhere->mnt_realrootvp
* bumping mount_generation causes the cached values
* to be invalidated
*/
name_cache_lock();
mount_generation++;
name_cache_unlock();
error = vnode_ref(vp);
if (error != 0) {
goto out4;
}
have_usecount = TRUE;
error = checkdirs(vp, ctx);
if (error != 0) {
/* Unmount the filesystem as cdir/rdirs cannot be updated */
goto out4;
}
/*
* there is no cleanup code here so I have made it void
* we need to revisit this
*/
(void)VFS_START(mp, 0, ctx);
error = mount_list_add(mp);
if (error != 0) {
goto out4;
}
lck_rw_done(&mp->mnt_rwlock);
is_rwlock_locked = FALSE;
/* Check if this mounted file system supports EAs or named streams. */
/* Skip WebDAV file systems for now since they hang in VFS_GETATTR here. */
VFSATTR_INIT(&vfsattr);
VFSATTR_WANTED(&vfsattr, f_capabilities);
if (strncmp(mp->mnt_vfsstat.f_fstypename, "webdav", sizeof("webdav")) != 0 &&
vfs_getattr(mp, &vfsattr, ctx) == 0 &&
VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) {
if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR) &&
(vfsattr.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR)) {
mp->mnt_kern_flag |= MNTK_EXTENDED_ATTRS;
}
#if NAMEDSTREAMS
if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_NAMEDSTREAMS) &&
(vfsattr.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_NAMEDSTREAMS)) {
mp->mnt_kern_flag |= MNTK_NAMED_STREAMS;
}
#endif
/* Check if this file system supports path from id lookups. */
if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_PATH_FROM_ID) &&
(vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_PATH_FROM_ID)) {
mp->mnt_kern_flag |= MNTK_PATH_FROM_ID;
} else if (mp->mnt_flag & MNT_DOVOLFS) {
/* Legacy MNT_DOVOLFS flag also implies path from id lookups. */
mp->mnt_kern_flag |= MNTK_PATH_FROM_ID;
}
}
if (mp->mnt_vtable->vfc_vfsflags & VFC_VFSNATIVEXATTR) {
mp->mnt_kern_flag |= MNTK_EXTENDED_ATTRS;
}
if (mp->mnt_vtable->vfc_vfsflags & VFC_VFSPREFLIGHT) {
mp->mnt_kern_flag |= MNTK_UNMOUNT_PREFLIGHT;
}
/* increment the operations count */
OSAddAtomic(1, &vfs_nummntops);
enablequotas(mp, ctx);
if (device_vnode) {
device_vnode->v_specflags |= SI_MOUNTEDON;
/*
* cache the IO attributes for the underlying physical media...
* an error return indicates the underlying driver doesn't
* support all the queries necessary... however, reasonable
* defaults will have been set, so no reason to bail or care
*/
vfs_init_io_attributes(device_vnode, mp);
}
/* Now that mount is setup, notify the listeners */
vfs_event_signal(NULL, VQ_MOUNT, (intptr_t)NULL);
} else {
vnode_lock_spin(vp);
CLR(vp->v_flag, VMOUNT);
vnode_unlock(vp);
mount_list_lock();
mp->mnt_vtable->vfc_refcount--;
mount_list_unlock();
if (device_vnode ) {
vnode_rele(device_vnode);
VNOP_CLOSE(device_vnode, ronly ? FREAD : FREAD|FWRITE, ctx);
}
lck_rw_done(&mp->mnt_rwlock);
is_rwlock_locked = FALSE;
mount_lock_destroy(mp);
#if CONFIG_MACF
mac_mount_label_destroy(mp);
#endif
FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT);
}
nameidone(&nd);
/*
* drop I/O count on covered 'vp' and
* on the device vp if there was one
*/
if (devpath && devvp)
vnode_put(devvp);
vnode_put(vp);
/* Note that we've changed something in the parent directory */
post_event_if_success(pvp, error, NOTE_WRITE);
vnode_put(pvp);
return(error);
out4:
(void)VFS_UNMOUNT(mp, MNT_FORCE, ctx);
if (device_vnode != NULLVP) {
vnode_rele(device_vnode);
VNOP_CLOSE(device_vnode, mp->mnt_flag & MNT_RDONLY ? FREAD : FREAD|FWRITE,
ctx);
did_rele = TRUE;
}
vnode_lock_spin(vp);
vp->v_mountedhere = (mount_t) 0;
vnode_unlock(vp);
if (have_usecount) {
vnode_rele(vp);
}
out3:
if (devpath && ((uap->flags & MNT_UPDATE) == 0) && (!did_rele))
vnode_rele(devvp);
out2:
if (devpath && devvp)
vnode_put(devvp);
out1:
/* Release mnt_rwlock only when it was taken */
if (is_rwlock_locked == TRUE) {
lck_rw_done(&mp->mnt_rwlock);
}
if (mntalloc) {
#if CONFIG_MACF
mac_mount_label_destroy(mp);
#endif
FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT);
}
if (vfsp_ref) {
mount_list_lock();
vfsp->vfc_refcount--;
mount_list_unlock();
}
vnode_put(vp);
vnode_put(pvp);
nameidone(&nd);
return(error);
}
#ifdef CONFIG_IMGSRC_ACCESS
/*
* Flush in-core data, check for competing mount attempts,
* and set VMOUNT
*/
static int
prepare_coveredvp(vnode_t vp, vfs_context_t ctx, struct componentname *cnp, const char *fsname)
{
struct vnode_attr va;
int error;
/*
* If the user is not root, ensure that they own the directory
* onto which we are attempting to mount.
*/
VATTR_INIT(&va);
VATTR_WANTED(&va, va_uid);
if ((error = vnode_getattr(vp, &va, ctx)) ||
(va.va_uid != kauth_cred_getuid(vfs_context_ucred(ctx)) &&
(!vfs_context_issuser(ctx)))) {
error = EPERM;
goto out;
}
if ( (error = VNOP_FSYNC(vp, MNT_WAIT, ctx)) )
goto out;
if ( (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0)) )
goto out;
if (vp->v_type != VDIR) {
error = ENOTDIR;
goto out;
}
if (ISSET(vp->v_flag, VMOUNT) && (vp->v_mountedhere != NULL)) {
error = EBUSY;
goto out;
}
#if CONFIG_MACF
error = mac_mount_check_mount(ctx, vp,
cnp, fsname);
if (error != 0)
goto out;
#endif
vnode_lock_spin(vp);
SET(vp->v_flag, VMOUNT);
vnode_unlock(vp);
out:
return error;
}
static int
authorize_devpath_and_update_mntfromname(mount_t mp, user_addr_t devpath, vnode_t *devvpp, vfs_context_t ctx)
{
struct nameidata nd;
vnode_t vp;
mode_t accessmode;
int error;
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, devpath, ctx);
if ( (error = namei(&nd)) )
return error;
strncpy(mp->mnt_vfsstat.f_mntfromname, nd.ni_cnd.cn_pnbuf, MAXPATHLEN);
vp = nd.ni_vp;
nameidone(&nd);
if (vp->v_type != VBLK) {
error = ENOTBLK;
goto out;
}
if (major(vp->v_rdev) >= nblkdev) {
error = ENXIO;
goto out;
}
/*
* If mount by non-root, then verify that user has necessary
* permissions on the device.
*/
if (!vfs_context_issuser(ctx)) {
accessmode = KAUTH_VNODE_READ_DATA;
if ((mp->mnt_flag & MNT_RDONLY) == 0)
accessmode |= KAUTH_VNODE_WRITE_DATA;
if ((error = vnode_authorize(vp, NULL, accessmode, ctx)) != 0)
goto out;
}
*devvpp = vp;
out:
if (error) {
vnode_put(vp);
}
return error;