1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 */
38
39 #include <sys/cdefs.h>
40 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #ifdef COMPAT_FREEBSD11
46 #include <sys/abi_compat.h>
47 #endif
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/capsicum.h>
51 #include <sys/disk.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
60 #include <sys/file.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
65 #include <sys/sdt.h>
66 #include <sys/stat.h>
67 #include <sys/sx.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
70 #include <sys/priv.h>
71 #include <sys/proc.h>
72 #include <sys/dirent.h>
73 #include <sys/jail.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
76 #ifdef KTRACE
77 #include <sys/ktrace.h>
78 #endif
79
80 #include <machine/stdarg.h>
81
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
84
85 #include <vm/vm.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vnode_pager.h>
89 #include <vm/uma.h>
90
91 #include <fs/devfs/devfs.h>
92
93 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94
95 static int kern_chflagsat(struct thread *td, int fd, const char *path,
96 enum uio_seg pathseg, u_long flags, int atflag);
97 static int setfflags(struct thread *td, struct vnode *, u_long);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
99 static int getutimens(const struct timespec *, enum uio_seg,
100 struct timespec *, int *);
101 static int setutimes(struct thread *td, struct vnode *,
102 const struct timespec *, int, int);
103 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
104 struct thread *td);
105 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
106 enum uio_seg pathseg, fhandle_t *fhp);
107 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
108 size_t count, struct thread *td);
109 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
110 const char *path, enum uio_seg segflag);
111
112 uint64_t
at2cnpflags(u_int at_flags,u_int mask)113 at2cnpflags(u_int at_flags, u_int mask)
114 {
115 uint64_t res;
116
117 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
118 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
119
120 res = 0;
121 at_flags &= mask;
122 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
123 res |= RBENEATH;
124 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
125 res |= FOLLOW;
126 /* NOFOLLOW is pseudo flag */
127 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
128 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
129 FOLLOW;
130 }
131 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
132 res |= EMPTYPATH;
133 return (res);
134 }
135
136 int
kern_sync(struct thread * td)137 kern_sync(struct thread *td)
138 {
139 struct mount *mp, *nmp;
140 int save;
141
142 mtx_lock(&mountlist_mtx);
143 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
144 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
145 nmp = TAILQ_NEXT(mp, mnt_list);
146 continue;
147 }
148 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
149 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
150 save = curthread_pflags_set(TDP_SYNCIO);
151 vfs_periodic(mp, MNT_NOWAIT);
152 VFS_SYNC(mp, MNT_NOWAIT);
153 curthread_pflags_restore(save);
154 vn_finished_write(mp);
155 }
156 mtx_lock(&mountlist_mtx);
157 nmp = TAILQ_NEXT(mp, mnt_list);
158 vfs_unbusy(mp);
159 }
160 mtx_unlock(&mountlist_mtx);
161 return (0);
162 }
163
164 /*
165 * Sync each mounted filesystem.
166 */
167 #ifndef _SYS_SYSPROTO_H_
168 struct sync_args {
169 int dummy;
170 };
171 #endif
172 /* ARGSUSED */
173 int
sys_sync(struct thread * td,struct sync_args * uap)174 sys_sync(struct thread *td, struct sync_args *uap)
175 {
176
177 return (kern_sync(td));
178 }
179
180 /*
181 * Change filesystem quotas.
182 */
183 #ifndef _SYS_SYSPROTO_H_
184 struct quotactl_args {
185 char *path;
186 int cmd;
187 int uid;
188 caddr_t arg;
189 };
190 #endif
191 int
sys_quotactl(struct thread * td,struct quotactl_args * uap)192 sys_quotactl(struct thread *td, struct quotactl_args *uap)
193 {
194 struct mount *mp;
195 struct nameidata nd;
196 int error;
197 bool mp_busy;
198
199 AUDIT_ARG_CMD(uap->cmd);
200 AUDIT_ARG_UID(uap->uid);
201 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
202 return (EPERM);
203 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
204 uap->path);
205 if ((error = namei(&nd)) != 0)
206 return (error);
207 NDFREE_PNBUF(&nd);
208 mp = nd.ni_vp->v_mount;
209 vfs_ref(mp);
210 vput(nd.ni_vp);
211 error = vfs_busy(mp, 0);
212 if (error != 0) {
213 vfs_rel(mp);
214 return (error);
215 }
216 mp_busy = true;
217 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
218
219 /*
220 * Since quota on/off operations typically need to open quota
221 * files, the implementation may need to unbusy the mount point
222 * before calling into namei. Otherwise, unmount might be
223 * started between two vfs_busy() invocations (first is ours,
224 * second is from mount point cross-walk code in lookup()),
225 * causing deadlock.
226 *
227 * Avoid unbusying mp if the implementation indicates it has
228 * already done so.
229 */
230 if (mp_busy)
231 vfs_unbusy(mp);
232 vfs_rel(mp);
233 return (error);
234 }
235
236 /*
237 * Used by statfs conversion routines to scale the block size up if
238 * necessary so that all of the block counts are <= 'max_size'. Note
239 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
240 * value of 'n'.
241 */
242 void
statfs_scale_blocks(struct statfs * sf,long max_size)243 statfs_scale_blocks(struct statfs *sf, long max_size)
244 {
245 uint64_t count;
246 int shift;
247
248 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
249
250 /*
251 * Attempt to scale the block counts to give a more accurate
252 * overview to userland of the ratio of free space to used
253 * space. To do this, find the largest block count and compute
254 * a divisor that lets it fit into a signed integer <= max_size.
255 */
256 if (sf->f_bavail < 0)
257 count = -sf->f_bavail;
258 else
259 count = sf->f_bavail;
260 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
261 if (count <= max_size)
262 return;
263
264 count >>= flsl(max_size);
265 shift = 0;
266 while (count > 0) {
267 shift++;
268 count >>=1;
269 }
270
271 sf->f_bsize <<= shift;
272 sf->f_blocks >>= shift;
273 sf->f_bfree >>= shift;
274 sf->f_bavail >>= shift;
275 }
276
277 static int
kern_do_statfs(struct thread * td,struct mount * mp,struct statfs * buf)278 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
279 {
280 int error;
281
282 if (mp == NULL)
283 return (EBADF);
284 error = vfs_busy(mp, 0);
285 vfs_rel(mp);
286 if (error != 0)
287 return (error);
288 #ifdef MAC
289 error = mac_mount_check_stat(td->td_ucred, mp);
290 if (error != 0)
291 goto out;
292 #endif
293 error = VFS_STATFS(mp, buf);
294 if (error != 0)
295 goto out;
296 if (priv_check_cred_vfs_generation(td->td_ucred)) {
297 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
298 prison_enforce_statfs(td->td_ucred, mp, buf);
299 }
300 out:
301 vfs_unbusy(mp);
302 return (error);
303 }
304
305 /*
306 * Get filesystem statistics.
307 */
308 #ifndef _SYS_SYSPROTO_H_
309 struct statfs_args {
310 char *path;
311 struct statfs *buf;
312 };
313 #endif
314 int
sys_statfs(struct thread * td,struct statfs_args * uap)315 sys_statfs(struct thread *td, struct statfs_args *uap)
316 {
317 struct statfs *sfp;
318 int error;
319
320 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
321 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
322 if (error == 0)
323 error = copyout(sfp, uap->buf, sizeof(struct statfs));
324 free(sfp, M_STATFS);
325 return (error);
326 }
327
328 int
kern_statfs(struct thread * td,const char * path,enum uio_seg pathseg,struct statfs * buf)329 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
330 struct statfs *buf)
331 {
332 struct mount *mp;
333 struct nameidata nd;
334 int error;
335
336 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
337 error = namei(&nd);
338 if (error != 0)
339 return (error);
340 NDFREE_PNBUF(&nd);
341 mp = vfs_ref_from_vp(nd.ni_vp);
342 vrele(nd.ni_vp);
343 return (kern_do_statfs(td, mp, buf));
344 }
345
346 /*
347 * Get filesystem statistics.
348 */
349 #ifndef _SYS_SYSPROTO_H_
350 struct fstatfs_args {
351 int fd;
352 struct statfs *buf;
353 };
354 #endif
355 int
sys_fstatfs(struct thread * td,struct fstatfs_args * uap)356 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
357 {
358 struct statfs *sfp;
359 int error;
360
361 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
362 error = kern_fstatfs(td, uap->fd, sfp);
363 if (error == 0)
364 error = copyout(sfp, uap->buf, sizeof(struct statfs));
365 free(sfp, M_STATFS);
366 return (error);
367 }
368
369 int
kern_fstatfs(struct thread * td,int fd,struct statfs * buf)370 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
371 {
372 struct file *fp;
373 struct mount *mp;
374 struct vnode *vp;
375 int error;
376
377 AUDIT_ARG_FD(fd);
378 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
379 if (error != 0)
380 return (error);
381 vp = fp->f_vnode;
382 #ifdef AUDIT
383 if (AUDITING_TD(td)) {
384 vn_lock(vp, LK_SHARED | LK_RETRY);
385 AUDIT_ARG_VNODE1(vp);
386 VOP_UNLOCK(vp);
387 }
388 #endif
389 mp = vfs_ref_from_vp(vp);
390 fdrop(fp, td);
391 return (kern_do_statfs(td, mp, buf));
392 }
393
394 /*
395 * Get statistics on all filesystems.
396 */
397 #ifndef _SYS_SYSPROTO_H_
398 struct getfsstat_args {
399 struct statfs *buf;
400 long bufsize;
401 int mode;
402 };
403 #endif
404 int
sys_getfsstat(struct thread * td,struct getfsstat_args * uap)405 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
406 {
407 size_t count;
408 int error;
409
410 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
411 return (EINVAL);
412 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
413 UIO_USERSPACE, uap->mode);
414 if (error == 0)
415 td->td_retval[0] = count;
416 return (error);
417 }
418
419 /*
420 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
421 * The caller is responsible for freeing memory which will be allocated
422 * in '*buf'.
423 */
424 int
kern_getfsstat(struct thread * td,struct statfs ** buf,size_t bufsize,size_t * countp,enum uio_seg bufseg,int mode)425 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
426 size_t *countp, enum uio_seg bufseg, int mode)
427 {
428 struct mount *mp, *nmp;
429 struct statfs *sfsp, *sp, *sptmp, *tofree;
430 size_t count, maxcount;
431 int error;
432
433 switch (mode) {
434 case MNT_WAIT:
435 case MNT_NOWAIT:
436 break;
437 default:
438 if (bufseg == UIO_SYSSPACE)
439 *buf = NULL;
440 return (EINVAL);
441 }
442 restart:
443 maxcount = bufsize / sizeof(struct statfs);
444 if (bufsize == 0) {
445 sfsp = NULL;
446 tofree = NULL;
447 } else if (bufseg == UIO_USERSPACE) {
448 sfsp = *buf;
449 tofree = NULL;
450 } else /* if (bufseg == UIO_SYSSPACE) */ {
451 count = 0;
452 mtx_lock(&mountlist_mtx);
453 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
454 count++;
455 }
456 mtx_unlock(&mountlist_mtx);
457 if (maxcount > count)
458 maxcount = count;
459 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
460 M_STATFS, M_WAITOK);
461 }
462
463 count = 0;
464
465 /*
466 * If there is no target buffer they only want the count.
467 *
468 * This could be TAILQ_FOREACH but it is open-coded to match the original
469 * code below.
470 */
471 if (sfsp == NULL) {
472 mtx_lock(&mountlist_mtx);
473 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
474 if (prison_canseemount(td->td_ucred, mp) != 0) {
475 nmp = TAILQ_NEXT(mp, mnt_list);
476 continue;
477 }
478 #ifdef MAC
479 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
480 nmp = TAILQ_NEXT(mp, mnt_list);
481 continue;
482 }
483 #endif
484 count++;
485 nmp = TAILQ_NEXT(mp, mnt_list);
486 }
487 mtx_unlock(&mountlist_mtx);
488 *countp = count;
489 return (0);
490 }
491
492 /*
493 * They want the entire thing.
494 *
495 * Short-circuit the corner case of no room for anything, avoids
496 * relocking below.
497 */
498 if (maxcount < 1) {
499 goto out;
500 }
501
502 mtx_lock(&mountlist_mtx);
503 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
504 if (prison_canseemount(td->td_ucred, mp) != 0) {
505 nmp = TAILQ_NEXT(mp, mnt_list);
506 continue;
507 }
508 #ifdef MAC
509 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
510 nmp = TAILQ_NEXT(mp, mnt_list);
511 continue;
512 }
513 #endif
514 if (mode == MNT_WAIT) {
515 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
516 /*
517 * If vfs_busy() failed, and MBF_NOWAIT
518 * wasn't passed, then the mp is gone.
519 * Furthermore, because of MBF_MNTLSTLOCK,
520 * the mountlist_mtx was dropped. We have
521 * no other choice than to start over.
522 */
523 mtx_unlock(&mountlist_mtx);
524 free(tofree, M_STATFS);
525 goto restart;
526 }
527 } else {
528 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
529 nmp = TAILQ_NEXT(mp, mnt_list);
530 continue;
531 }
532 }
533 sp = &mp->mnt_stat;
534 /*
535 * If MNT_NOWAIT is specified, do not refresh
536 * the fsstat cache.
537 */
538 if (mode != MNT_NOWAIT) {
539 error = VFS_STATFS(mp, sp);
540 if (error != 0) {
541 mtx_lock(&mountlist_mtx);
542 nmp = TAILQ_NEXT(mp, mnt_list);
543 vfs_unbusy(mp);
544 continue;
545 }
546 }
547 if (priv_check_cred_vfs_generation(td->td_ucred)) {
548 sptmp = malloc(sizeof(struct statfs), M_STATFS,
549 M_WAITOK);
550 *sptmp = *sp;
551 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
552 prison_enforce_statfs(td->td_ucred, mp, sptmp);
553 sp = sptmp;
554 } else
555 sptmp = NULL;
556 if (bufseg == UIO_SYSSPACE) {
557 bcopy(sp, sfsp, sizeof(*sp));
558 free(sptmp, M_STATFS);
559 } else /* if (bufseg == UIO_USERSPACE) */ {
560 error = copyout(sp, sfsp, sizeof(*sp));
561 free(sptmp, M_STATFS);
562 if (error != 0) {
563 vfs_unbusy(mp);
564 return (error);
565 }
566 }
567 sfsp++;
568 count++;
569
570 if (count == maxcount) {
571 vfs_unbusy(mp);
572 goto out;
573 }
574
575 mtx_lock(&mountlist_mtx);
576 nmp = TAILQ_NEXT(mp, mnt_list);
577 vfs_unbusy(mp);
578 }
579 mtx_unlock(&mountlist_mtx);
580 out:
581 *countp = count;
582 return (0);
583 }
584
585 #ifdef COMPAT_FREEBSD4
586 /*
587 * Get old format filesystem statistics.
588 */
589 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
590
591 #ifndef _SYS_SYSPROTO_H_
592 struct freebsd4_statfs_args {
593 char *path;
594 struct ostatfs *buf;
595 };
596 #endif
597 int
freebsd4_statfs(struct thread * td,struct freebsd4_statfs_args * uap)598 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
599 {
600 struct ostatfs osb;
601 struct statfs *sfp;
602 int error;
603
604 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
605 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
606 if (error == 0) {
607 freebsd4_cvtstatfs(sfp, &osb);
608 error = copyout(&osb, uap->buf, sizeof(osb));
609 }
610 free(sfp, M_STATFS);
611 return (error);
612 }
613
614 /*
615 * Get filesystem statistics.
616 */
617 #ifndef _SYS_SYSPROTO_H_
618 struct freebsd4_fstatfs_args {
619 int fd;
620 struct ostatfs *buf;
621 };
622 #endif
623 int
freebsd4_fstatfs(struct thread * td,struct freebsd4_fstatfs_args * uap)624 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
625 {
626 struct ostatfs osb;
627 struct statfs *sfp;
628 int error;
629
630 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
631 error = kern_fstatfs(td, uap->fd, sfp);
632 if (error == 0) {
633 freebsd4_cvtstatfs(sfp, &osb);
634 error = copyout(&osb, uap->buf, sizeof(osb));
635 }
636 free(sfp, M_STATFS);
637 return (error);
638 }
639
640 /*
641 * Get statistics on all filesystems.
642 */
643 #ifndef _SYS_SYSPROTO_H_
644 struct freebsd4_getfsstat_args {
645 struct ostatfs *buf;
646 long bufsize;
647 int mode;
648 };
649 #endif
650 int
freebsd4_getfsstat(struct thread * td,struct freebsd4_getfsstat_args * uap)651 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
652 {
653 struct statfs *buf, *sp;
654 struct ostatfs osb;
655 size_t count, size;
656 int error;
657
658 if (uap->bufsize < 0)
659 return (EINVAL);
660 count = uap->bufsize / sizeof(struct ostatfs);
661 if (count > SIZE_MAX / sizeof(struct statfs))
662 return (EINVAL);
663 size = count * sizeof(struct statfs);
664 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
665 uap->mode);
666 if (error == 0)
667 td->td_retval[0] = count;
668 if (size != 0) {
669 sp = buf;
670 while (count != 0 && error == 0) {
671 freebsd4_cvtstatfs(sp, &osb);
672 error = copyout(&osb, uap->buf, sizeof(osb));
673 sp++;
674 uap->buf++;
675 count--;
676 }
677 free(buf, M_STATFS);
678 }
679 return (error);
680 }
681
682 /*
683 * Implement fstatfs() for (NFS) file handles.
684 */
685 #ifndef _SYS_SYSPROTO_H_
686 struct freebsd4_fhstatfs_args {
687 struct fhandle *u_fhp;
688 struct ostatfs *buf;
689 };
690 #endif
691 int
freebsd4_fhstatfs(struct thread * td,struct freebsd4_fhstatfs_args * uap)692 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
693 {
694 struct ostatfs osb;
695 struct statfs *sfp;
696 fhandle_t fh;
697 int error;
698
699 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
700 if (error != 0)
701 return (error);
702 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
703 error = kern_fhstatfs(td, fh, sfp);
704 if (error == 0) {
705 freebsd4_cvtstatfs(sfp, &osb);
706 error = copyout(&osb, uap->buf, sizeof(osb));
707 }
708 free(sfp, M_STATFS);
709 return (error);
710 }
711
712 /*
713 * Convert a new format statfs structure to an old format statfs structure.
714 */
715 static void
freebsd4_cvtstatfs(struct statfs * nsp,struct ostatfs * osp)716 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
717 {
718
719 statfs_scale_blocks(nsp, LONG_MAX);
720 bzero(osp, sizeof(*osp));
721 osp->f_bsize = nsp->f_bsize;
722 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
723 osp->f_blocks = nsp->f_blocks;
724 osp->f_bfree = nsp->f_bfree;
725 osp->f_bavail = nsp->f_bavail;
726 osp->f_files = MIN(nsp->f_files, LONG_MAX);
727 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
728 osp->f_owner = nsp->f_owner;
729 osp->f_type = nsp->f_type;
730 osp->f_flags = nsp->f_flags;
731 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
732 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
733 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
734 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
735 strlcpy(osp->f_fstypename, nsp->f_fstypename,
736 MIN(MFSNAMELEN, OMFSNAMELEN));
737 strlcpy(osp->f_mntonname, nsp->f_mntonname,
738 MIN(MNAMELEN, OMNAMELEN));
739 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
740 MIN(MNAMELEN, OMNAMELEN));
741 osp->f_fsid = nsp->f_fsid;
742 }
743 #endif /* COMPAT_FREEBSD4 */
744
745 #if defined(COMPAT_FREEBSD11)
746 /*
747 * Get old format filesystem statistics.
748 */
749 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
750
751 int
freebsd11_statfs(struct thread * td,struct freebsd11_statfs_args * uap)752 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
753 {
754 struct freebsd11_statfs osb;
755 struct statfs *sfp;
756 int error;
757
758 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
759 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
760 if (error == 0) {
761 freebsd11_cvtstatfs(sfp, &osb);
762 error = copyout(&osb, uap->buf, sizeof(osb));
763 }
764 free(sfp, M_STATFS);
765 return (error);
766 }
767
768 /*
769 * Get filesystem statistics.
770 */
771 int
freebsd11_fstatfs(struct thread * td,struct freebsd11_fstatfs_args * uap)772 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
773 {
774 struct freebsd11_statfs osb;
775 struct statfs *sfp;
776 int error;
777
778 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
779 error = kern_fstatfs(td, uap->fd, sfp);
780 if (error == 0) {
781 freebsd11_cvtstatfs(sfp, &osb);
782 error = copyout(&osb, uap->buf, sizeof(osb));
783 }
784 free(sfp, M_STATFS);
785 return (error);
786 }
787
788 /*
789 * Get statistics on all filesystems.
790 */
791 int
freebsd11_getfsstat(struct thread * td,struct freebsd11_getfsstat_args * uap)792 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
793 {
794 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
795 }
796
797 int
kern_freebsd11_getfsstat(struct thread * td,struct freebsd11_statfs * ubuf,long bufsize,int mode)798 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
799 long bufsize, int mode)
800 {
801 struct freebsd11_statfs osb;
802 struct statfs *buf, *sp;
803 size_t count, size;
804 int error;
805
806 if (bufsize < 0)
807 return (EINVAL);
808
809 count = bufsize / sizeof(struct ostatfs);
810 size = count * sizeof(struct statfs);
811 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
812 if (error == 0)
813 td->td_retval[0] = count;
814 if (size > 0) {
815 sp = buf;
816 while (count > 0 && error == 0) {
817 freebsd11_cvtstatfs(sp, &osb);
818 error = copyout(&osb, ubuf, sizeof(osb));
819 sp++;
820 ubuf++;
821 count--;
822 }
823 free(buf, M_STATFS);
824 }
825 return (error);
826 }
827
828 /*
829 * Implement fstatfs() for (NFS) file handles.
830 */
831 int
freebsd11_fhstatfs(struct thread * td,struct freebsd11_fhstatfs_args * uap)832 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
833 {
834 struct freebsd11_statfs osb;
835 struct statfs *sfp;
836 fhandle_t fh;
837 int error;
838
839 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
840 if (error)
841 return (error);
842 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
843 error = kern_fhstatfs(td, fh, sfp);
844 if (error == 0) {
845 freebsd11_cvtstatfs(sfp, &osb);
846 error = copyout(&osb, uap->buf, sizeof(osb));
847 }
848 free(sfp, M_STATFS);
849 return (error);
850 }
851
852 /*
853 * Convert a new format statfs structure to an old format statfs structure.
854 */
855 static void
freebsd11_cvtstatfs(struct statfs * nsp,struct freebsd11_statfs * osp)856 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
857 {
858
859 bzero(osp, sizeof(*osp));
860 osp->f_version = FREEBSD11_STATFS_VERSION;
861 osp->f_type = nsp->f_type;
862 osp->f_flags = nsp->f_flags;
863 osp->f_bsize = nsp->f_bsize;
864 osp->f_iosize = nsp->f_iosize;
865 osp->f_blocks = nsp->f_blocks;
866 osp->f_bfree = nsp->f_bfree;
867 osp->f_bavail = nsp->f_bavail;
868 osp->f_files = nsp->f_files;
869 osp->f_ffree = nsp->f_ffree;
870 osp->f_syncwrites = nsp->f_syncwrites;
871 osp->f_asyncwrites = nsp->f_asyncwrites;
872 osp->f_syncreads = nsp->f_syncreads;
873 osp->f_asyncreads = nsp->f_asyncreads;
874 osp->f_namemax = nsp->f_namemax;
875 osp->f_owner = nsp->f_owner;
876 osp->f_fsid = nsp->f_fsid;
877 strlcpy(osp->f_fstypename, nsp->f_fstypename,
878 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
879 strlcpy(osp->f_mntonname, nsp->f_mntonname,
880 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
881 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
882 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
883 }
884 #endif /* COMPAT_FREEBSD11 */
885
886 /*
887 * Change current working directory to a given file descriptor.
888 */
889 #ifndef _SYS_SYSPROTO_H_
890 struct fchdir_args {
891 int fd;
892 };
893 #endif
894 int
sys_fchdir(struct thread * td,struct fchdir_args * uap)895 sys_fchdir(struct thread *td, struct fchdir_args *uap)
896 {
897 struct vnode *vp, *tdp;
898 struct mount *mp;
899 struct file *fp;
900 int error;
901
902 AUDIT_ARG_FD(uap->fd);
903 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
904 &fp);
905 if (error != 0)
906 return (error);
907 vp = fp->f_vnode;
908 vrefact(vp);
909 fdrop(fp, td);
910 vn_lock(vp, LK_SHARED | LK_RETRY);
911 AUDIT_ARG_VNODE1(vp);
912 error = change_dir(vp, td);
913 while (!error && (mp = vp->v_mountedhere) != NULL) {
914 if (vfs_busy(mp, 0))
915 continue;
916 error = VFS_ROOT(mp, LK_SHARED, &tdp);
917 vfs_unbusy(mp);
918 if (error != 0)
919 break;
920 vput(vp);
921 vp = tdp;
922 }
923 if (error != 0) {
924 vput(vp);
925 return (error);
926 }
927 VOP_UNLOCK(vp);
928 pwd_chdir(td, vp);
929 return (0);
930 }
931
932 /*
933 * Change current working directory (``.'').
934 */
935 #ifndef _SYS_SYSPROTO_H_
936 struct chdir_args {
937 char *path;
938 };
939 #endif
940 int
sys_chdir(struct thread * td,struct chdir_args * uap)941 sys_chdir(struct thread *td, struct chdir_args *uap)
942 {
943
944 return (kern_chdir(td, uap->path, UIO_USERSPACE));
945 }
946
947 int
kern_chdir(struct thread * td,const char * path,enum uio_seg pathseg)948 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
949 {
950 struct nameidata nd;
951 int error;
952
953 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
954 pathseg, path);
955 if ((error = namei(&nd)) != 0)
956 return (error);
957 if ((error = change_dir(nd.ni_vp, td)) != 0) {
958 vput(nd.ni_vp);
959 NDFREE_PNBUF(&nd);
960 return (error);
961 }
962 VOP_UNLOCK(nd.ni_vp);
963 NDFREE_PNBUF(&nd);
964 pwd_chdir(td, nd.ni_vp);
965 return (0);
966 }
967
968 static int unprivileged_chroot = 0;
969 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
970 &unprivileged_chroot, 0,
971 "Unprivileged processes can use chroot(2)");
972 /*
973 * Change notion of root (``/'') directory.
974 */
975 #ifndef _SYS_SYSPROTO_H_
976 struct chroot_args {
977 char *path;
978 };
979 #endif
980 int
sys_chroot(struct thread * td,struct chroot_args * uap)981 sys_chroot(struct thread *td, struct chroot_args *uap)
982 {
983 struct nameidata nd;
984 struct proc *p;
985 int error;
986
987 error = priv_check(td, PRIV_VFS_CHROOT);
988 if (error != 0) {
989 p = td->td_proc;
990 PROC_LOCK(p);
991 if (unprivileged_chroot == 0 ||
992 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
993 PROC_UNLOCK(p);
994 return (error);
995 }
996 PROC_UNLOCK(p);
997 }
998 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
999 UIO_USERSPACE, uap->path);
1000 error = namei(&nd);
1001 if (error != 0)
1002 return (error);
1003 NDFREE_PNBUF(&nd);
1004 error = change_dir(nd.ni_vp, td);
1005 if (error != 0)
1006 goto e_vunlock;
1007 #ifdef MAC
1008 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1009 if (error != 0)
1010 goto e_vunlock;
1011 #endif
1012 VOP_UNLOCK(nd.ni_vp);
1013 error = pwd_chroot(td, nd.ni_vp);
1014 vrele(nd.ni_vp);
1015 return (error);
1016 e_vunlock:
1017 vput(nd.ni_vp);
1018 return (error);
1019 }
1020
1021 /*
1022 * Common routine for chroot and chdir. Callers must provide a locked vnode
1023 * instance.
1024 */
1025 int
change_dir(struct vnode * vp,struct thread * td)1026 change_dir(struct vnode *vp, struct thread *td)
1027 {
1028 #ifdef MAC
1029 int error;
1030 #endif
1031
1032 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1033 if (vp->v_type != VDIR)
1034 return (ENOTDIR);
1035 #ifdef MAC
1036 error = mac_vnode_check_chdir(td->td_ucred, vp);
1037 if (error != 0)
1038 return (error);
1039 #endif
1040 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1041 }
1042
1043 static __inline void
flags_to_rights(int flags,cap_rights_t * rightsp)1044 flags_to_rights(int flags, cap_rights_t *rightsp)
1045 {
1046 if (flags & O_EXEC) {
1047 cap_rights_set_one(rightsp, CAP_FEXECVE);
1048 if (flags & O_PATH)
1049 return;
1050 } else {
1051 switch ((flags & O_ACCMODE)) {
1052 case O_RDONLY:
1053 cap_rights_set_one(rightsp, CAP_READ);
1054 break;
1055 case O_RDWR:
1056 cap_rights_set_one(rightsp, CAP_READ);
1057 /* FALLTHROUGH */
1058 case O_WRONLY:
1059 cap_rights_set_one(rightsp, CAP_WRITE);
1060 if (!(flags & (O_APPEND | O_TRUNC)))
1061 cap_rights_set_one(rightsp, CAP_SEEK);
1062 break;
1063 }
1064 }
1065
1066 if (flags & O_CREAT)
1067 cap_rights_set_one(rightsp, CAP_CREATE);
1068
1069 if (flags & O_TRUNC)
1070 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1071
1072 if (flags & (O_SYNC | O_FSYNC))
1073 cap_rights_set_one(rightsp, CAP_FSYNC);
1074
1075 if (flags & (O_EXLOCK | O_SHLOCK))
1076 cap_rights_set_one(rightsp, CAP_FLOCK);
1077 }
1078
1079 /*
1080 * Check permissions, allocate an open file structure, and call the device
1081 * open routine if any.
1082 */
1083 #ifndef _SYS_SYSPROTO_H_
1084 struct open_args {
1085 char *path;
1086 int flags;
1087 int mode;
1088 };
1089 #endif
1090 int
sys_open(struct thread * td,struct open_args * uap)1091 sys_open(struct thread *td, struct open_args *uap)
1092 {
1093
1094 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1095 uap->flags, uap->mode));
1096 }
1097
1098 #ifndef _SYS_SYSPROTO_H_
1099 struct openat_args {
1100 int fd;
1101 char *path;
1102 int flag;
1103 int mode;
1104 };
1105 #endif
1106 int
sys_openat(struct thread * td,struct openat_args * uap)1107 sys_openat(struct thread *td, struct openat_args *uap)
1108 {
1109
1110 AUDIT_ARG_FD(uap->fd);
1111 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1112 uap->mode));
1113 }
1114
1115 /*
1116 * If fpp != NULL, opened file is not installed into the file
1117 * descriptor table, instead it is returned in *fpp. This is
1118 * incompatible with fdopen(), in which case we return EINVAL.
1119 */
1120 static int
openatfp(struct thread * td,int dirfd,const char * path,enum uio_seg pathseg,int flags,int mode,struct file ** fpp)1121 openatfp(struct thread *td, int dirfd, const char *path,
1122 enum uio_seg pathseg, int flags, int mode, struct file **fpp)
1123 {
1124 struct proc *p;
1125 struct filedesc *fdp;
1126 struct pwddesc *pdp;
1127 struct file *fp;
1128 struct vnode *vp;
1129 struct filecaps *fcaps;
1130 struct nameidata nd;
1131 cap_rights_t rights;
1132 int cmode, error, indx;
1133
1134 indx = -1;
1135 p = td->td_proc;
1136 fdp = p->p_fd;
1137 pdp = p->p_pd;
1138
1139 AUDIT_ARG_FFLAGS(flags);
1140 AUDIT_ARG_MODE(mode);
1141 cap_rights_init_one(&rights, CAP_LOOKUP);
1142 flags_to_rights(flags, &rights);
1143
1144 /*
1145 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1146 * may be specified. On the other hand, for O_PATH any mode
1147 * except O_EXEC is ignored.
1148 */
1149 if ((flags & O_PATH) != 0) {
1150 flags &= ~(O_CREAT | O_ACCMODE);
1151 } else if ((flags & O_EXEC) != 0) {
1152 if (flags & O_ACCMODE)
1153 return (EINVAL);
1154 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1155 return (EINVAL);
1156 } else {
1157 flags = FFLAGS(flags);
1158 }
1159
1160 /*
1161 * Allocate a file structure. The descriptor to reference it
1162 * is allocated and used by finstall_refed() below.
1163 */
1164 error = falloc_noinstall(td, &fp);
1165 if (error != 0)
1166 return (error);
1167 /* Set the flags early so the finit in devfs can pick them up. */
1168 fp->f_flag = flags & FMASK;
1169 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1170 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1171 pathseg, path, dirfd, &rights);
1172 td->td_dupfd = -1; /* XXX check for fdopen */
1173 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1174 td->td_ucred, fp);
1175 if (error != 0) {
1176 /*
1177 * If the vn_open replaced the method vector, something
1178 * wonderous happened deep below and we just pass it up
1179 * pretending we know what we do.
1180 */
1181 if (error == ENXIO && fp->f_ops != &badfileops) {
1182 MPASS((flags & O_PATH) == 0);
1183 goto success;
1184 }
1185
1186 /*
1187 * Handle special fdopen() case. bleh.
1188 *
1189 * Don't do this for relative (capability) lookups; we don't
1190 * understand exactly what would happen, and we don't think
1191 * that it ever should.
1192 */
1193 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1194 (error == ENODEV || error == ENXIO) &&
1195 td->td_dupfd >= 0) {
1196 MPASS(fpp == NULL);
1197 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1198 &indx);
1199 if (error == 0)
1200 goto success;
1201 }
1202
1203 goto bad;
1204 }
1205 td->td_dupfd = 0;
1206 NDFREE_PNBUF(&nd);
1207 vp = nd.ni_vp;
1208
1209 /*
1210 * Store the vnode, for any f_type. Typically, the vnode use
1211 * count is decremented by direct call to vn_closefile() for
1212 * files that switched type in the cdevsw fdopen() method.
1213 */
1214 fp->f_vnode = vp;
1215
1216 /*
1217 * If the file wasn't claimed by devfs bind it to the normal
1218 * vnode operations here.
1219 */
1220 if (fp->f_ops == &badfileops) {
1221 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1222 ("Unexpected fifo fp %p vp %p", fp, vp));
1223 if ((flags & O_PATH) != 0) {
1224 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1225 DTYPE_VNODE, NULL, &path_fileops);
1226 } else {
1227 finit_vnode(fp, flags, NULL, &vnops);
1228 }
1229 }
1230
1231 VOP_UNLOCK(vp);
1232 if (flags & O_TRUNC) {
1233 error = fo_truncate(fp, 0, td->td_ucred, td);
1234 if (error != 0)
1235 goto bad;
1236 }
1237 success:
1238 if (fpp != NULL) {
1239 MPASS(error == 0);
1240 NDFREE_IOCTLCAPS(&nd);
1241 *fpp = fp;
1242 return (0);
1243 }
1244
1245 /*
1246 * If we haven't already installed the FD (for dupfdopen), do so now.
1247 */
1248 if (indx == -1) {
1249 #ifdef CAPABILITIES
1250 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1251 fcaps = &nd.ni_filecaps;
1252 else
1253 #endif
1254 fcaps = NULL;
1255 error = finstall_refed(td, fp, &indx, flags, fcaps);
1256 /* On success finstall_refed() consumes fcaps. */
1257 if (error != 0) {
1258 goto bad;
1259 }
1260 } else {
1261 NDFREE_IOCTLCAPS(&nd);
1262 falloc_abort(td, fp);
1263 }
1264
1265 td->td_retval[0] = indx;
1266 return (0);
1267 bad:
1268 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1269 NDFREE_IOCTLCAPS(&nd);
1270 falloc_abort(td, fp);
1271 return (error);
1272 }
1273
1274 int
kern_openat(struct thread * td,int dirfd,const char * path,enum uio_seg pathseg,int flags,int mode)1275 kern_openat(struct thread *td, int dirfd, const char *path,
1276 enum uio_seg pathseg, int flags, int mode)
1277 {
1278 return (openatfp(td, dirfd, path, pathseg, flags, mode, NULL));
1279 }
1280
1281 int
kern_openatfp(struct thread * td,int dirfd,const char * path,enum uio_seg pathseg,int flags,int mode,struct file ** fpp)1282 kern_openatfp(struct thread *td, int dirfd, const char *path,
1283 enum uio_seg pathseg, int flags, int mode, struct file **fpp)
1284 {
1285 int error, old_dupfd;
1286
1287 old_dupfd = td->td_dupfd;
1288 td->td_dupfd = -1;
1289 error = openatfp(td, dirfd, path, pathseg, flags, mode, fpp);
1290 td->td_dupfd = old_dupfd;
1291 return (error);
1292 }
1293
1294 #ifdef COMPAT_43
1295 /*
1296 * Create a file.
1297 */
1298 #ifndef _SYS_SYSPROTO_H_
1299 struct ocreat_args {
1300 char *path;
1301 int mode;
1302 };
1303 #endif
1304 int
ocreat(struct thread * td,struct ocreat_args * uap)1305 ocreat(struct thread *td, struct ocreat_args *uap)
1306 {
1307
1308 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1309 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1310 }
1311 #endif /* COMPAT_43 */
1312
1313 /*
1314 * Create a special file.
1315 */
1316 #ifndef _SYS_SYSPROTO_H_
1317 struct mknodat_args {
1318 int fd;
1319 char *path;
1320 mode_t mode;
1321 dev_t dev;
1322 };
1323 #endif
1324 int
sys_mknodat(struct thread * td,struct mknodat_args * uap)1325 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1326 {
1327
1328 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1329 uap->dev));
1330 }
1331
1332 #if defined(COMPAT_FREEBSD11)
1333 int
freebsd11_mknod(struct thread * td,struct freebsd11_mknod_args * uap)1334 freebsd11_mknod(struct thread *td,
1335 struct freebsd11_mknod_args *uap)
1336 {
1337
1338 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1339 uap->mode, uap->dev));
1340 }
1341
1342 int
freebsd11_mknodat(struct thread * td,struct freebsd11_mknodat_args * uap)1343 freebsd11_mknodat(struct thread *td,
1344 struct freebsd11_mknodat_args *uap)
1345 {
1346
1347 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1348 uap->dev));
1349 }
1350 #endif /* COMPAT_FREEBSD11 */
1351
1352 int
kern_mknodat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,int mode,dev_t dev)1353 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1354 int mode, dev_t dev)
1355 {
1356 struct vnode *vp;
1357 struct mount *mp;
1358 struct vattr vattr;
1359 struct nameidata nd;
1360 int error, whiteout = 0;
1361
1362 AUDIT_ARG_MODE(mode);
1363 AUDIT_ARG_DEV(dev);
1364 switch (mode & S_IFMT) {
1365 case S_IFCHR:
1366 case S_IFBLK:
1367 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1368 if (error == 0 && dev == VNOVAL)
1369 error = EINVAL;
1370 break;
1371 case S_IFWHT:
1372 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1373 break;
1374 case S_IFIFO:
1375 if (dev == 0)
1376 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1377 /* FALLTHROUGH */
1378 default:
1379 error = EINVAL;
1380 break;
1381 }
1382 if (error != 0)
1383 return (error);
1384 NDPREINIT(&nd);
1385 restart:
1386 bwillwrite();
1387 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1388 pathseg, path, fd, &cap_mknodat_rights);
1389 if ((error = namei(&nd)) != 0)
1390 return (error);
1391 vp = nd.ni_vp;
1392 if (vp != NULL) {
1393 NDFREE_PNBUF(&nd);
1394 if (vp == nd.ni_dvp)
1395 vrele(nd.ni_dvp);
1396 else
1397 vput(nd.ni_dvp);
1398 vrele(vp);
1399 return (EEXIST);
1400 } else {
1401 VATTR_NULL(&vattr);
1402 vattr.va_mode = (mode & ALLPERMS) &
1403 ~td->td_proc->p_pd->pd_cmask;
1404 vattr.va_rdev = dev;
1405 whiteout = 0;
1406
1407 switch (mode & S_IFMT) {
1408 case S_IFCHR:
1409 vattr.va_type = VCHR;
1410 break;
1411 case S_IFBLK:
1412 vattr.va_type = VBLK;
1413 break;
1414 case S_IFWHT:
1415 whiteout = 1;
1416 break;
1417 default:
1418 panic("kern_mknod: invalid mode");
1419 }
1420 }
1421 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1422 NDFREE_PNBUF(&nd);
1423 vput(nd.ni_dvp);
1424 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1425 return (error);
1426 goto restart;
1427 }
1428 #ifdef MAC
1429 if (error == 0 && !whiteout)
1430 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1431 &nd.ni_cnd, &vattr);
1432 #endif
1433 if (error == 0) {
1434 if (whiteout)
1435 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1436 else {
1437 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1438 &nd.ni_cnd, &vattr);
1439 }
1440 }
1441 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1442 true);
1443 vn_finished_write(mp);
1444 NDFREE_PNBUF(&nd);
1445 if (error == ERELOOKUP)
1446 goto restart;
1447 return (error);
1448 }
1449
1450 /*
1451 * Create a named pipe.
1452 */
1453 #ifndef _SYS_SYSPROTO_H_
1454 struct mkfifo_args {
1455 char *path;
1456 int mode;
1457 };
1458 #endif
1459 int
sys_mkfifo(struct thread * td,struct mkfifo_args * uap)1460 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1461 {
1462
1463 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1464 uap->mode));
1465 }
1466
1467 #ifndef _SYS_SYSPROTO_H_
1468 struct mkfifoat_args {
1469 int fd;
1470 char *path;
1471 mode_t mode;
1472 };
1473 #endif
1474 int
sys_mkfifoat(struct thread * td,struct mkfifoat_args * uap)1475 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1476 {
1477
1478 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1479 uap->mode));
1480 }
1481
1482 int
kern_mkfifoat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,int mode)1483 kern_mkfifoat(struct thread *td, int fd, const char *path,
1484 enum uio_seg pathseg, int mode)
1485 {
1486 struct mount *mp;
1487 struct vattr vattr;
1488 struct nameidata nd;
1489 int error;
1490
1491 AUDIT_ARG_MODE(mode);
1492 NDPREINIT(&nd);
1493 restart:
1494 bwillwrite();
1495 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1496 pathseg, path, fd, &cap_mkfifoat_rights);
1497 if ((error = namei(&nd)) != 0)
1498 return (error);
1499 if (nd.ni_vp != NULL) {
1500 NDFREE_PNBUF(&nd);
1501 if (nd.ni_vp == nd.ni_dvp)
1502 vrele(nd.ni_dvp);
1503 else
1504 vput(nd.ni_dvp);
1505 vrele(nd.ni_vp);
1506 return (EEXIST);
1507 }
1508 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1509 NDFREE_PNBUF(&nd);
1510 vput(nd.ni_dvp);
1511 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1512 return (error);
1513 goto restart;
1514 }
1515 VATTR_NULL(&vattr);
1516 vattr.va_type = VFIFO;
1517 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1518 #ifdef MAC
1519 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1520 &vattr);
1521 if (error != 0)
1522 goto out;
1523 #endif
1524 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1525 #ifdef MAC
1526 out:
1527 #endif
1528 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1529 vn_finished_write(mp);
1530 NDFREE_PNBUF(&nd);
1531 if (error == ERELOOKUP)
1532 goto restart;
1533 return (error);
1534 }
1535
1536 /*
1537 * Make a hard file link.
1538 */
1539 #ifndef _SYS_SYSPROTO_H_
1540 struct link_args {
1541 char *path;
1542 char *link;
1543 };
1544 #endif
1545 int
sys_link(struct thread * td,struct link_args * uap)1546 sys_link(struct thread *td, struct link_args *uap)
1547 {
1548
1549 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1550 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1551 }
1552
1553 #ifndef _SYS_SYSPROTO_H_
1554 struct linkat_args {
1555 int fd1;
1556 char *path1;
1557 int fd2;
1558 char *path2;
1559 int flag;
1560 };
1561 #endif
1562 int
sys_linkat(struct thread * td,struct linkat_args * uap)1563 sys_linkat(struct thread *td, struct linkat_args *uap)
1564 {
1565
1566 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1567 UIO_USERSPACE, uap->flag));
1568 }
1569
1570 int hardlink_check_uid = 0;
1571 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1572 &hardlink_check_uid, 0,
1573 "Unprivileged processes cannot create hard links to files owned by other "
1574 "users");
1575 static int hardlink_check_gid = 0;
1576 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1577 &hardlink_check_gid, 0,
1578 "Unprivileged processes cannot create hard links to files owned by other "
1579 "groups");
1580
1581 static int
can_hardlink(struct vnode * vp,struct ucred * cred)1582 can_hardlink(struct vnode *vp, struct ucred *cred)
1583 {
1584 struct vattr va;
1585 int error;
1586
1587 if (!hardlink_check_uid && !hardlink_check_gid)
1588 return (0);
1589
1590 error = VOP_GETATTR(vp, &va, cred);
1591 if (error != 0)
1592 return (error);
1593
1594 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1595 error = priv_check_cred(cred, PRIV_VFS_LINK);
1596 if (error != 0)
1597 return (error);
1598 }
1599
1600 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1601 error = priv_check_cred(cred, PRIV_VFS_LINK);
1602 if (error != 0)
1603 return (error);
1604 }
1605
1606 return (0);
1607 }
1608
1609 int
kern_linkat(struct thread * td,int fd1,int fd2,const char * path1,const char * path2,enum uio_seg segflag,int flag)1610 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1611 const char *path2, enum uio_seg segflag, int flag)
1612 {
1613 struct nameidata nd;
1614 int error;
1615
1616 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1617 AT_EMPTY_PATH)) != 0)
1618 return (EINVAL);
1619
1620 NDPREINIT(&nd);
1621 do {
1622 bwillwrite();
1623 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1624 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1625 segflag, path1, fd1, &cap_linkat_source_rights);
1626 if ((error = namei(&nd)) != 0)
1627 return (error);
1628 NDFREE_PNBUF(&nd);
1629 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1630 error = priv_check(td, PRIV_VFS_FHOPEN);
1631 if (error != 0) {
1632 vrele(nd.ni_vp);
1633 return (error);
1634 }
1635 }
1636 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1637 } while (error == EAGAIN || error == ERELOOKUP);
1638 return (error);
1639 }
1640
1641 static int
kern_linkat_vp(struct thread * td,struct vnode * vp,int fd,const char * path,enum uio_seg segflag)1642 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1643 enum uio_seg segflag)
1644 {
1645 struct nameidata nd;
1646 struct mount *mp;
1647 int error;
1648
1649 if (vp->v_type == VDIR) {
1650 vrele(vp);
1651 return (EPERM); /* POSIX */
1652 }
1653 NDINIT_ATRIGHTS(&nd, CREATE,
1654 LOCKPARENT | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1655 &cap_linkat_target_rights);
1656 if ((error = namei(&nd)) == 0) {
1657 if (nd.ni_vp != NULL) {
1658 NDFREE_PNBUF(&nd);
1659 if (nd.ni_dvp == nd.ni_vp)
1660 vrele(nd.ni_dvp);
1661 else
1662 vput(nd.ni_dvp);
1663 vrele(nd.ni_vp);
1664 vrele(vp);
1665 return (EEXIST);
1666 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1667 /*
1668 * Cross-device link. No need to recheck
1669 * vp->v_type, since it cannot change, except
1670 * to VBAD.
1671 */
1672 NDFREE_PNBUF(&nd);
1673 vput(nd.ni_dvp);
1674 vrele(vp);
1675 return (EXDEV);
1676 } else if (vn_lock(vp, LK_EXCLUSIVE) == 0) {
1677 error = can_hardlink(vp, td->td_ucred);
1678 #ifdef MAC
1679 if (error == 0)
1680 error = mac_vnode_check_link(td->td_ucred,
1681 nd.ni_dvp, vp, &nd.ni_cnd);
1682 #endif
1683 if (error != 0) {
1684 vput(vp);
1685 vput(nd.ni_dvp);
1686 NDFREE_PNBUF(&nd);
1687 return (error);
1688 }
1689 error = vn_start_write(vp, &mp, V_NOWAIT);
1690 if (error != 0) {
1691 vput(vp);
1692 vput(nd.ni_dvp);
1693 NDFREE_PNBUF(&nd);
1694 error = vn_start_write(NULL, &mp,
1695 V_XSLEEP | V_PCATCH);
1696 if (error != 0)
1697 return (error);
1698 return (EAGAIN);
1699 }
1700 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1701 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1702 vn_finished_write(mp);
1703 NDFREE_PNBUF(&nd);
1704 vp = NULL;
1705 } else {
1706 vput(nd.ni_dvp);
1707 NDFREE_PNBUF(&nd);
1708 vrele(vp);
1709 return (EAGAIN);
1710 }
1711 }
1712 if (vp != NULL)
1713 vrele(vp);
1714 return (error);
1715 }
1716
1717 /*
1718 * Make a symbolic link.
1719 */
1720 #ifndef _SYS_SYSPROTO_H_
1721 struct symlink_args {
1722 char *path;
1723 char *link;
1724 };
1725 #endif
1726 int
sys_symlink(struct thread * td,struct symlink_args * uap)1727 sys_symlink(struct thread *td, struct symlink_args *uap)
1728 {
1729
1730 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1731 UIO_USERSPACE));
1732 }
1733
1734 #ifndef _SYS_SYSPROTO_H_
1735 struct symlinkat_args {
1736 char *path;
1737 int fd;
1738 char *path2;
1739 };
1740 #endif
1741 int
sys_symlinkat(struct thread * td,struct symlinkat_args * uap)1742 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1743 {
1744
1745 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1746 UIO_USERSPACE));
1747 }
1748
1749 int
kern_symlinkat(struct thread * td,const char * path1,int fd,const char * path2,enum uio_seg segflg)1750 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1751 enum uio_seg segflg)
1752 {
1753 struct mount *mp;
1754 struct vattr vattr;
1755 const char *syspath;
1756 char *tmppath;
1757 struct nameidata nd;
1758 int error;
1759
1760 if (segflg == UIO_SYSSPACE) {
1761 syspath = path1;
1762 } else {
1763 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1764 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1765 goto out;
1766 syspath = tmppath;
1767 }
1768 AUDIT_ARG_TEXT(syspath);
1769 NDPREINIT(&nd);
1770 restart:
1771 bwillwrite();
1772 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, segflg,
1773 path2, fd, &cap_symlinkat_rights);
1774 if ((error = namei(&nd)) != 0)
1775 goto out;
1776 if (nd.ni_vp) {
1777 NDFREE_PNBUF(&nd);
1778 if (nd.ni_vp == nd.ni_dvp)
1779 vrele(nd.ni_dvp);
1780 else
1781 vput(nd.ni_dvp);
1782 vrele(nd.ni_vp);
1783 nd.ni_vp = NULL;
1784 error = EEXIST;
1785 goto out;
1786 }
1787 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1788 NDFREE_PNBUF(&nd);
1789 vput(nd.ni_dvp);
1790 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1791 goto out;
1792 goto restart;
1793 }
1794 VATTR_NULL(&vattr);
1795 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1796 #ifdef MAC
1797 vattr.va_type = VLNK;
1798 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1799 &vattr);
1800 if (error != 0)
1801 goto out2;
1802 #endif
1803 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1804 #ifdef MAC
1805 out2:
1806 #endif
1807 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1808 vn_finished_write(mp);
1809 NDFREE_PNBUF(&nd);
1810 if (error == ERELOOKUP)
1811 goto restart;
1812 out:
1813 if (segflg != UIO_SYSSPACE)
1814 uma_zfree(namei_zone, tmppath);
1815 return (error);
1816 }
1817
1818 /*
1819 * Delete a whiteout from the filesystem.
1820 */
1821 #ifndef _SYS_SYSPROTO_H_
1822 struct undelete_args {
1823 char *path;
1824 };
1825 #endif
1826 int
sys_undelete(struct thread * td,struct undelete_args * uap)1827 sys_undelete(struct thread *td, struct undelete_args *uap)
1828 {
1829 struct mount *mp;
1830 struct nameidata nd;
1831 int error;
1832
1833 NDPREINIT(&nd);
1834 restart:
1835 bwillwrite();
1836 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1837 UIO_USERSPACE, uap->path);
1838 error = namei(&nd);
1839 if (error != 0)
1840 return (error);
1841
1842 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1843 NDFREE_PNBUF(&nd);
1844 if (nd.ni_vp == nd.ni_dvp)
1845 vrele(nd.ni_dvp);
1846 else
1847 vput(nd.ni_dvp);
1848 if (nd.ni_vp)
1849 vrele(nd.ni_vp);
1850 return (EEXIST);
1851 }
1852 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1853 NDFREE_PNBUF(&nd);
1854 vput(nd.ni_dvp);
1855 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1856 return (error);
1857 goto restart;
1858 }
1859 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1860 NDFREE_PNBUF(&nd);
1861 vput(nd.ni_dvp);
1862 vn_finished_write(mp);
1863 if (error == ERELOOKUP)
1864 goto restart;
1865 return (error);
1866 }
1867
1868 /*
1869 * Delete a name from the filesystem.
1870 */
1871 #ifndef _SYS_SYSPROTO_H_
1872 struct unlink_args {
1873 char *path;
1874 };
1875 #endif
1876 int
sys_unlink(struct thread * td,struct unlink_args * uap)1877 sys_unlink(struct thread *td, struct unlink_args *uap)
1878 {
1879
1880 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1881 0, 0));
1882 }
1883
1884 static int
kern_funlinkat_ex(struct thread * td,int dfd,const char * path,int fd,int flag,enum uio_seg pathseg,ino_t oldinum)1885 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1886 int flag, enum uio_seg pathseg, ino_t oldinum)
1887 {
1888
1889 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1890 return (EINVAL);
1891
1892 if ((flag & AT_REMOVEDIR) != 0)
1893 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1894
1895 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1896 }
1897
1898 #ifndef _SYS_SYSPROTO_H_
1899 struct unlinkat_args {
1900 int fd;
1901 char *path;
1902 int flag;
1903 };
1904 #endif
1905 int
sys_unlinkat(struct thread * td,struct unlinkat_args * uap)1906 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1907 {
1908
1909 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1910 UIO_USERSPACE, 0));
1911 }
1912
1913 #ifndef _SYS_SYSPROTO_H_
1914 struct funlinkat_args {
1915 int dfd;
1916 const char *path;
1917 int fd;
1918 int flag;
1919 };
1920 #endif
1921 int
sys_funlinkat(struct thread * td,struct funlinkat_args * uap)1922 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1923 {
1924
1925 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1926 UIO_USERSPACE, 0));
1927 }
1928
1929 int
kern_funlinkat(struct thread * td,int dfd,const char * path,int fd,enum uio_seg pathseg,int flag,ino_t oldinum)1930 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1931 enum uio_seg pathseg, int flag, ino_t oldinum)
1932 {
1933 struct mount *mp;
1934 struct file *fp;
1935 struct vnode *vp;
1936 struct nameidata nd;
1937 struct stat sb;
1938 int error;
1939
1940 fp = NULL;
1941 if (fd != FD_NONE) {
1942 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1943 if (error != 0)
1944 return (error);
1945 }
1946
1947 NDPREINIT(&nd);
1948 restart:
1949 bwillwrite();
1950 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1951 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1952 pathseg, path, dfd, &cap_unlinkat_rights);
1953 if ((error = namei(&nd)) != 0) {
1954 if (error == EINVAL)
1955 error = EPERM;
1956 goto fdout;
1957 }
1958 vp = nd.ni_vp;
1959 if (vp->v_type == VDIR && oldinum == 0) {
1960 error = EPERM; /* POSIX */
1961 } else if (oldinum != 0 &&
1962 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
1963 sb.st_ino != oldinum) {
1964 error = EIDRM; /* Identifier removed */
1965 } else if (fp != NULL && fp->f_vnode != vp) {
1966 if (VN_IS_DOOMED(fp->f_vnode))
1967 error = EBADF;
1968 else
1969 error = EDEADLK;
1970 } else {
1971 /*
1972 * The root of a mounted filesystem cannot be deleted.
1973 *
1974 * XXX: can this only be a VDIR case?
1975 */
1976 if (vp->v_vflag & VV_ROOT)
1977 error = EBUSY;
1978 }
1979 if (error == 0) {
1980 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1981 NDFREE_PNBUF(&nd);
1982 vput(nd.ni_dvp);
1983 if (vp == nd.ni_dvp)
1984 vrele(vp);
1985 else
1986 vput(vp);
1987 if ((error = vn_start_write(NULL, &mp,
1988 V_XSLEEP | V_PCATCH)) != 0) {
1989 goto fdout;
1990 }
1991 goto restart;
1992 }
1993 #ifdef MAC
1994 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1995 &nd.ni_cnd);
1996 if (error != 0)
1997 goto out;
1998 #endif
1999 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
2000 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
2001 #ifdef MAC
2002 out:
2003 #endif
2004 vn_finished_write(mp);
2005 }
2006 NDFREE_PNBUF(&nd);
2007 vput(nd.ni_dvp);
2008 if (vp == nd.ni_dvp)
2009 vrele(vp);
2010 else
2011 vput(vp);
2012 if (error == ERELOOKUP)
2013 goto restart;
2014 fdout:
2015 if (fp != NULL)
2016 fdrop(fp, td);
2017 return (error);
2018 }
2019
2020 /*
2021 * Reposition read/write file offset.
2022 */
2023 #ifndef _SYS_SYSPROTO_H_
2024 struct lseek_args {
2025 int fd;
2026 int pad;
2027 off_t offset;
2028 int whence;
2029 };
2030 #endif
2031 int
sys_lseek(struct thread * td,struct lseek_args * uap)2032 sys_lseek(struct thread *td, struct lseek_args *uap)
2033 {
2034
2035 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2036 }
2037
2038 int
kern_lseek(struct thread * td,int fd,off_t offset,int whence)2039 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2040 {
2041 struct file *fp;
2042 int error;
2043
2044 AUDIT_ARG_FD(fd);
2045 error = fget(td, fd, &cap_seek_rights, &fp);
2046 if (error != 0)
2047 return (error);
2048 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2049 fo_seek(fp, offset, whence, td) : ESPIPE;
2050 fdrop(fp, td);
2051 return (error);
2052 }
2053
2054 #if defined(COMPAT_43)
2055 /*
2056 * Reposition read/write file offset.
2057 */
2058 #ifndef _SYS_SYSPROTO_H_
2059 struct olseek_args {
2060 int fd;
2061 long offset;
2062 int whence;
2063 };
2064 #endif
2065 int
olseek(struct thread * td,struct olseek_args * uap)2066 olseek(struct thread *td, struct olseek_args *uap)
2067 {
2068
2069 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2070 }
2071 #endif /* COMPAT_43 */
2072
2073 #if defined(COMPAT_FREEBSD6)
2074 /* Version with the 'pad' argument */
2075 int
freebsd6_lseek(struct thread * td,struct freebsd6_lseek_args * uap)2076 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2077 {
2078
2079 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2080 }
2081 #endif
2082
2083 /*
2084 * Check access permissions using passed credentials.
2085 */
2086 static int
vn_access(struct vnode * vp,int user_flags,struct ucred * cred,struct thread * td)2087 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2088 struct thread *td)
2089 {
2090 accmode_t accmode;
2091 int error;
2092
2093 /* Flags == 0 means only check for existence. */
2094 if (user_flags == 0)
2095 return (0);
2096
2097 accmode = 0;
2098 if (user_flags & R_OK)
2099 accmode |= VREAD;
2100 if (user_flags & W_OK)
2101 accmode |= VWRITE;
2102 if (user_flags & X_OK)
2103 accmode |= VEXEC;
2104 #ifdef MAC
2105 error = mac_vnode_check_access(cred, vp, accmode);
2106 if (error != 0)
2107 return (error);
2108 #endif
2109 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2110 error = VOP_ACCESS(vp, accmode, cred, td);
2111 return (error);
2112 }
2113
2114 /*
2115 * Check access permissions using "real" credentials.
2116 */
2117 #ifndef _SYS_SYSPROTO_H_
2118 struct access_args {
2119 char *path;
2120 int amode;
2121 };
2122 #endif
2123 int
sys_access(struct thread * td,struct access_args * uap)2124 sys_access(struct thread *td, struct access_args *uap)
2125 {
2126
2127 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2128 0, uap->amode));
2129 }
2130
2131 #ifndef _SYS_SYSPROTO_H_
2132 struct faccessat_args {
2133 int dirfd;
2134 char *path;
2135 int amode;
2136 int flag;
2137 }
2138 #endif
2139 int
sys_faccessat(struct thread * td,struct faccessat_args * uap)2140 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2141 {
2142
2143 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2144 uap->amode));
2145 }
2146
2147 int
kern_accessat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,int flag,int amode)2148 kern_accessat(struct thread *td, int fd, const char *path,
2149 enum uio_seg pathseg, int flag, int amode)
2150 {
2151 struct ucred *cred, *usecred;
2152 struct vnode *vp;
2153 struct nameidata nd;
2154 int error;
2155
2156 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH |
2157 AT_SYMLINK_NOFOLLOW)) != 0)
2158 return (EINVAL);
2159 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2160 return (EINVAL);
2161
2162 /*
2163 * Create and modify a temporary credential instead of one that
2164 * is potentially shared (if we need one).
2165 */
2166 cred = td->td_ucred;
2167 if ((flag & AT_EACCESS) == 0 &&
2168 ((cred->cr_uid != cred->cr_ruid ||
2169 cred->cr_rgid != cred->cr_groups[0]))) {
2170 usecred = crdup(cred);
2171 usecred->cr_uid = cred->cr_ruid;
2172 usecred->cr_groups[0] = cred->cr_rgid;
2173 td->td_ucred = usecred;
2174 } else
2175 usecred = cred;
2176 AUDIT_ARG_VALUE(amode);
2177 NDINIT_ATRIGHTS(&nd, LOOKUP, LOCKSHARED | LOCKLEAF |
2178 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH | AT_SYMLINK_NOFOLLOW |
2179 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2180 if ((error = namei(&nd)) != 0)
2181 goto out;
2182 vp = nd.ni_vp;
2183
2184 error = vn_access(vp, amode, usecred, td);
2185 NDFREE_PNBUF(&nd);
2186 vput(vp);
2187 out:
2188 if (usecred != cred) {
2189 td->td_ucred = cred;
2190 crfree(usecred);
2191 }
2192 return (error);
2193 }
2194
2195 /*
2196 * Check access permissions using "effective" credentials.
2197 */
2198 #ifndef _SYS_SYSPROTO_H_
2199 struct eaccess_args {
2200 char *path;
2201 int amode;
2202 };
2203 #endif
2204 int
sys_eaccess(struct thread * td,struct eaccess_args * uap)2205 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2206 {
2207
2208 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2209 AT_EACCESS, uap->amode));
2210 }
2211
2212 #if defined(COMPAT_43)
2213 /*
2214 * Get file status; this version follows links.
2215 */
2216 #ifndef _SYS_SYSPROTO_H_
2217 struct ostat_args {
2218 char *path;
2219 struct ostat *ub;
2220 };
2221 #endif
2222 int
ostat(struct thread * td,struct ostat_args * uap)2223 ostat(struct thread *td, struct ostat_args *uap)
2224 {
2225 struct stat sb;
2226 struct ostat osb;
2227 int error;
2228
2229 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2230 if (error != 0)
2231 return (error);
2232 cvtstat(&sb, &osb);
2233 return (copyout(&osb, uap->ub, sizeof (osb)));
2234 }
2235
2236 /*
2237 * Get file status; this version does not follow links.
2238 */
2239 #ifndef _SYS_SYSPROTO_H_
2240 struct olstat_args {
2241 char *path;
2242 struct ostat *ub;
2243 };
2244 #endif
2245 int
olstat(struct thread * td,struct olstat_args * uap)2246 olstat(struct thread *td, struct olstat_args *uap)
2247 {
2248 struct stat sb;
2249 struct ostat osb;
2250 int error;
2251
2252 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2253 UIO_USERSPACE, &sb);
2254 if (error != 0)
2255 return (error);
2256 cvtstat(&sb, &osb);
2257 return (copyout(&osb, uap->ub, sizeof (osb)));
2258 }
2259
2260 /*
2261 * Convert from an old to a new stat structure.
2262 * XXX: many values are blindly truncated.
2263 */
2264 void
cvtstat(struct stat * st,struct ostat * ost)2265 cvtstat(struct stat *st, struct ostat *ost)
2266 {
2267
2268 bzero(ost, sizeof(*ost));
2269 ost->st_dev = st->st_dev;
2270 ost->st_ino = st->st_ino;
2271 ost->st_mode = st->st_mode;
2272 ost->st_nlink = st->st_nlink;
2273 ost->st_uid = st->st_uid;
2274 ost->st_gid = st->st_gid;
2275 ost->st_rdev = st->st_rdev;
2276 ost->st_size = MIN(st->st_size, INT32_MAX);
2277 ost->st_atim = st->st_atim;
2278 ost->st_mtim = st->st_mtim;
2279 ost->st_ctim = st->st_ctim;
2280 ost->st_blksize = st->st_blksize;
2281 ost->st_blocks = st->st_blocks;
2282 ost->st_flags = st->st_flags;
2283 ost->st_gen = st->st_gen;
2284 }
2285 #endif /* COMPAT_43 */
2286
2287 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2288 int ino64_trunc_error;
2289 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2290 &ino64_trunc_error, 0,
2291 "Error on truncation of device, file or inode number, or link count");
2292
2293 int
freebsd11_cvtstat(struct stat * st,struct freebsd11_stat * ost)2294 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2295 {
2296
2297 ost->st_dev = st->st_dev;
2298 if (ost->st_dev != st->st_dev) {
2299 switch (ino64_trunc_error) {
2300 default:
2301 /*
2302 * Since dev_t is almost raw, don't clamp to the
2303 * maximum for case 2, but ignore the error.
2304 */
2305 break;
2306 case 1:
2307 return (EOVERFLOW);
2308 }
2309 }
2310 ost->st_ino = st->st_ino;
2311 if (ost->st_ino != st->st_ino) {
2312 switch (ino64_trunc_error) {
2313 default:
2314 case 0:
2315 break;
2316 case 1:
2317 return (EOVERFLOW);
2318 case 2:
2319 ost->st_ino = UINT32_MAX;
2320 break;
2321 }
2322 }
2323 ost->st_mode = st->st_mode;
2324 ost->st_nlink = st->st_nlink;
2325 if (ost->st_nlink != st->st_nlink) {
2326 switch (ino64_trunc_error) {
2327 default:
2328 case 0:
2329 break;
2330 case 1:
2331 return (EOVERFLOW);
2332 case 2:
2333 ost->st_nlink = UINT16_MAX;
2334 break;
2335 }
2336 }
2337 ost->st_uid = st->st_uid;
2338 ost->st_gid = st->st_gid;
2339 ost->st_rdev = st->st_rdev;
2340 if (ost->st_rdev != st->st_rdev) {
2341 switch (ino64_trunc_error) {
2342 default:
2343 break;
2344 case 1:
2345 return (EOVERFLOW);
2346 }
2347 }
2348 ost->st_atim = st->st_atim;
2349 ost->st_mtim = st->st_mtim;
2350 ost->st_ctim = st->st_ctim;
2351 ost->st_size = st->st_size;
2352 ost->st_blocks = st->st_blocks;
2353 ost->st_blksize = st->st_blksize;
2354 ost->st_flags = st->st_flags;
2355 ost->st_gen = st->st_gen;
2356 ost->st_lspare = 0;
2357 ost->st_birthtim = st->st_birthtim;
2358 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2359 sizeof(*ost) - offsetof(struct freebsd11_stat,
2360 st_birthtim) - sizeof(ost->st_birthtim));
2361 return (0);
2362 }
2363
2364 int
freebsd11_stat(struct thread * td,struct freebsd11_stat_args * uap)2365 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2366 {
2367 struct stat sb;
2368 struct freebsd11_stat osb;
2369 int error;
2370
2371 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2372 if (error != 0)
2373 return (error);
2374 error = freebsd11_cvtstat(&sb, &osb);
2375 if (error == 0)
2376 error = copyout(&osb, uap->ub, sizeof(osb));
2377 return (error);
2378 }
2379
2380 int
freebsd11_lstat(struct thread * td,struct freebsd11_lstat_args * uap)2381 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2382 {
2383 struct stat sb;
2384 struct freebsd11_stat osb;
2385 int error;
2386
2387 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2388 UIO_USERSPACE, &sb);
2389 if (error != 0)
2390 return (error);
2391 error = freebsd11_cvtstat(&sb, &osb);
2392 if (error == 0)
2393 error = copyout(&osb, uap->ub, sizeof(osb));
2394 return (error);
2395 }
2396
2397 int
freebsd11_fhstat(struct thread * td,struct freebsd11_fhstat_args * uap)2398 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2399 {
2400 struct fhandle fh;
2401 struct stat sb;
2402 struct freebsd11_stat osb;
2403 int error;
2404
2405 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2406 if (error != 0)
2407 return (error);
2408 error = kern_fhstat(td, fh, &sb);
2409 if (error != 0)
2410 return (error);
2411 error = freebsd11_cvtstat(&sb, &osb);
2412 if (error == 0)
2413 error = copyout(&osb, uap->sb, sizeof(osb));
2414 return (error);
2415 }
2416
2417 int
freebsd11_fstatat(struct thread * td,struct freebsd11_fstatat_args * uap)2418 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2419 {
2420 struct stat sb;
2421 struct freebsd11_stat osb;
2422 int error;
2423
2424 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2425 UIO_USERSPACE, &sb);
2426 if (error != 0)
2427 return (error);
2428 error = freebsd11_cvtstat(&sb, &osb);
2429 if (error == 0)
2430 error = copyout(&osb, uap->buf, sizeof(osb));
2431 return (error);
2432 }
2433 #endif /* COMPAT_FREEBSD11 */
2434
2435 /*
2436 * Get file status
2437 */
2438 #ifndef _SYS_SYSPROTO_H_
2439 struct fstatat_args {
2440 int fd;
2441 char *path;
2442 struct stat *buf;
2443 int flag;
2444 }
2445 #endif
2446 int
sys_fstatat(struct thread * td,struct fstatat_args * uap)2447 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2448 {
2449 struct stat sb;
2450 int error;
2451
2452 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2453 UIO_USERSPACE, &sb);
2454 if (error == 0)
2455 error = copyout(&sb, uap->buf, sizeof (sb));
2456 return (error);
2457 }
2458
2459 int
kern_statat(struct thread * td,int flag,int fd,const char * path,enum uio_seg pathseg,struct stat * sbp)2460 kern_statat(struct thread *td, int flag, int fd, const char *path,
2461 enum uio_seg pathseg, struct stat *sbp)
2462 {
2463 struct nameidata nd;
2464 int error;
2465
2466 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2467 AT_EMPTY_PATH)) != 0)
2468 return (EINVAL);
2469
2470 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2471 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2472 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2473
2474 if ((error = namei(&nd)) != 0) {
2475 if (error == ENOTDIR &&
2476 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2477 error = kern_fstat(td, fd, sbp);
2478 return (error);
2479 }
2480 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2481 NDFREE_PNBUF(&nd);
2482 vput(nd.ni_vp);
2483 #ifdef __STAT_TIME_T_EXT
2484 sbp->st_atim_ext = 0;
2485 sbp->st_mtim_ext = 0;
2486 sbp->st_ctim_ext = 0;
2487 sbp->st_btim_ext = 0;
2488 #endif
2489 #ifdef KTRACE
2490 if (KTRPOINT(td, KTR_STRUCT))
2491 ktrstat_error(sbp, error);
2492 #endif
2493 return (error);
2494 }
2495
2496 #if defined(COMPAT_FREEBSD11)
2497 /*
2498 * Implementation of the NetBSD [l]stat() functions.
2499 */
2500 int
freebsd11_cvtnstat(struct stat * sb,struct nstat * nsb)2501 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2502 {
2503 struct freebsd11_stat sb11;
2504 int error;
2505
2506 error = freebsd11_cvtstat(sb, &sb11);
2507 if (error != 0)
2508 return (error);
2509
2510 bzero(nsb, sizeof(*nsb));
2511 CP(sb11, *nsb, st_dev);
2512 CP(sb11, *nsb, st_ino);
2513 CP(sb11, *nsb, st_mode);
2514 CP(sb11, *nsb, st_nlink);
2515 CP(sb11, *nsb, st_uid);
2516 CP(sb11, *nsb, st_gid);
2517 CP(sb11, *nsb, st_rdev);
2518 CP(sb11, *nsb, st_atim);
2519 CP(sb11, *nsb, st_mtim);
2520 CP(sb11, *nsb, st_ctim);
2521 CP(sb11, *nsb, st_size);
2522 CP(sb11, *nsb, st_blocks);
2523 CP(sb11, *nsb, st_blksize);
2524 CP(sb11, *nsb, st_flags);
2525 CP(sb11, *nsb, st_gen);
2526 CP(sb11, *nsb, st_birthtim);
2527 return (0);
2528 }
2529
2530 #ifndef _SYS_SYSPROTO_H_
2531 struct freebsd11_nstat_args {
2532 char *path;
2533 struct nstat *ub;
2534 };
2535 #endif
2536 int
freebsd11_nstat(struct thread * td,struct freebsd11_nstat_args * uap)2537 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2538 {
2539 struct stat sb;
2540 struct nstat nsb;
2541 int error;
2542
2543 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2544 if (error != 0)
2545 return (error);
2546 error = freebsd11_cvtnstat(&sb, &nsb);
2547 if (error == 0)
2548 error = copyout(&nsb, uap->ub, sizeof (nsb));
2549 return (error);
2550 }
2551
2552 /*
2553 * NetBSD lstat. Get file status; this version does not follow links.
2554 */
2555 #ifndef _SYS_SYSPROTO_H_
2556 struct freebsd11_nlstat_args {
2557 char *path;
2558 struct nstat *ub;
2559 };
2560 #endif
2561 int
freebsd11_nlstat(struct thread * td,struct freebsd11_nlstat_args * uap)2562 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2563 {
2564 struct stat sb;
2565 struct nstat nsb;
2566 int error;
2567
2568 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2569 UIO_USERSPACE, &sb);
2570 if (error != 0)
2571 return (error);
2572 error = freebsd11_cvtnstat(&sb, &nsb);
2573 if (error == 0)
2574 error = copyout(&nsb, uap->ub, sizeof (nsb));
2575 return (error);
2576 }
2577 #endif /* COMPAT_FREEBSD11 */
2578
2579 /*
2580 * Get configurable pathname variables.
2581 */
2582 #ifndef _SYS_SYSPROTO_H_
2583 struct pathconf_args {
2584 char *path;
2585 int name;
2586 };
2587 #endif
2588 int
sys_pathconf(struct thread * td,struct pathconf_args * uap)2589 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2590 {
2591 long value;
2592 int error;
2593
2594 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2595 &value);
2596 if (error == 0)
2597 td->td_retval[0] = value;
2598 return (error);
2599 }
2600
2601 #ifndef _SYS_SYSPROTO_H_
2602 struct lpathconf_args {
2603 char *path;
2604 int name;
2605 };
2606 #endif
2607 int
sys_lpathconf(struct thread * td,struct lpathconf_args * uap)2608 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2609 {
2610 long value;
2611 int error;
2612
2613 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2614 NOFOLLOW, &value);
2615 if (error == 0)
2616 td->td_retval[0] = value;
2617 return (error);
2618 }
2619
2620 int
kern_pathconf(struct thread * td,const char * path,enum uio_seg pathseg,int name,u_long flags,long * valuep)2621 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2622 int name, u_long flags, long *valuep)
2623 {
2624 struct nameidata nd;
2625 int error;
2626
2627 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2628 pathseg, path);
2629 if ((error = namei(&nd)) != 0)
2630 return (error);
2631 NDFREE_PNBUF(&nd);
2632
2633 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2634 vput(nd.ni_vp);
2635 return (error);
2636 }
2637
2638 /*
2639 * Return target name of a symbolic link.
2640 */
2641 #ifndef _SYS_SYSPROTO_H_
2642 struct readlink_args {
2643 char *path;
2644 char *buf;
2645 size_t count;
2646 };
2647 #endif
2648 int
sys_readlink(struct thread * td,struct readlink_args * uap)2649 sys_readlink(struct thread *td, struct readlink_args *uap)
2650 {
2651
2652 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2653 uap->buf, UIO_USERSPACE, uap->count));
2654 }
2655 #ifndef _SYS_SYSPROTO_H_
2656 struct readlinkat_args {
2657 int fd;
2658 char *path;
2659 char *buf;
2660 size_t bufsize;
2661 };
2662 #endif
2663 int
sys_readlinkat(struct thread * td,struct readlinkat_args * uap)2664 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2665 {
2666
2667 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2668 uap->buf, UIO_USERSPACE, uap->bufsize));
2669 }
2670
2671 int
kern_readlinkat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,char * buf,enum uio_seg bufseg,size_t count)2672 kern_readlinkat(struct thread *td, int fd, const char *path,
2673 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2674 {
2675 struct vnode *vp;
2676 struct nameidata nd;
2677 int error;
2678
2679 if (count > IOSIZE_MAX)
2680 return (EINVAL);
2681
2682 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2683 EMPTYPATH, pathseg, path, fd);
2684
2685 if ((error = namei(&nd)) != 0)
2686 return (error);
2687 NDFREE_PNBUF(&nd);
2688 vp = nd.ni_vp;
2689
2690 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2691 vput(vp);
2692
2693 return (error);
2694 }
2695
2696 /*
2697 * Helper function to readlink from a vnode
2698 */
2699 static int
kern_readlink_vp(struct vnode * vp,char * buf,enum uio_seg bufseg,size_t count,struct thread * td)2700 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2701 struct thread *td)
2702 {
2703 struct iovec aiov;
2704 struct uio auio;
2705 int error;
2706
2707 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2708 #ifdef MAC
2709 error = mac_vnode_check_readlink(td->td_ucred, vp);
2710 if (error != 0)
2711 return (error);
2712 #endif
2713 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2714 return (EINVAL);
2715
2716 aiov.iov_base = buf;
2717 aiov.iov_len = count;
2718 auio.uio_iov = &aiov;
2719 auio.uio_iovcnt = 1;
2720 auio.uio_offset = 0;
2721 auio.uio_rw = UIO_READ;
2722 auio.uio_segflg = bufseg;
2723 auio.uio_td = td;
2724 auio.uio_resid = count;
2725 error = VOP_READLINK(vp, &auio, td->td_ucred);
2726 td->td_retval[0] = count - auio.uio_resid;
2727 return (error);
2728 }
2729
2730 /*
2731 * Common implementation code for chflags() and fchflags().
2732 */
2733 static int
setfflags(struct thread * td,struct vnode * vp,u_long flags)2734 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2735 {
2736 struct mount *mp;
2737 struct vattr vattr;
2738 int error;
2739
2740 /* We can't support the value matching VNOVAL. */
2741 if (flags == VNOVAL)
2742 return (EOPNOTSUPP);
2743
2744 /*
2745 * Prevent non-root users from setting flags on devices. When
2746 * a device is reused, users can retain ownership of the device
2747 * if they are allowed to set flags and programs assume that
2748 * chown can't fail when done as root.
2749 */
2750 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2751 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2752 if (error != 0)
2753 return (error);
2754 }
2755
2756 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2757 return (error);
2758 VATTR_NULL(&vattr);
2759 vattr.va_flags = flags;
2760 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2761 #ifdef MAC
2762 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2763 if (error == 0)
2764 #endif
2765 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2766 VOP_UNLOCK(vp);
2767 vn_finished_write(mp);
2768 return (error);
2769 }
2770
2771 /*
2772 * Change flags of a file given a path name.
2773 */
2774 #ifndef _SYS_SYSPROTO_H_
2775 struct chflags_args {
2776 const char *path;
2777 u_long flags;
2778 };
2779 #endif
2780 int
sys_chflags(struct thread * td,struct chflags_args * uap)2781 sys_chflags(struct thread *td, struct chflags_args *uap)
2782 {
2783
2784 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2785 uap->flags, 0));
2786 }
2787
2788 #ifndef _SYS_SYSPROTO_H_
2789 struct chflagsat_args {
2790 int fd;
2791 const char *path;
2792 u_long flags;
2793 int atflag;
2794 }
2795 #endif
2796 int
sys_chflagsat(struct thread * td,struct chflagsat_args * uap)2797 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2798 {
2799
2800 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2801 uap->flags, uap->atflag));
2802 }
2803
2804 /*
2805 * Same as chflags() but doesn't follow symlinks.
2806 */
2807 #ifndef _SYS_SYSPROTO_H_
2808 struct lchflags_args {
2809 const char *path;
2810 u_long flags;
2811 };
2812 #endif
2813 int
sys_lchflags(struct thread * td,struct lchflags_args * uap)2814 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2815 {
2816
2817 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2818 uap->flags, AT_SYMLINK_NOFOLLOW));
2819 }
2820
2821 static int
kern_chflagsat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,u_long flags,int atflag)2822 kern_chflagsat(struct thread *td, int fd, const char *path,
2823 enum uio_seg pathseg, u_long flags, int atflag)
2824 {
2825 struct nameidata nd;
2826 int error;
2827
2828 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2829 AT_EMPTY_PATH)) != 0)
2830 return (EINVAL);
2831
2832 AUDIT_ARG_FFLAGS(flags);
2833 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2834 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2835 fd, &cap_fchflags_rights);
2836 if ((error = namei(&nd)) != 0)
2837 return (error);
2838 NDFREE_PNBUF(&nd);
2839 error = setfflags(td, nd.ni_vp, flags);
2840 vrele(nd.ni_vp);
2841 return (error);
2842 }
2843
2844 /*
2845 * Change flags of a file given a file descriptor.
2846 */
2847 #ifndef _SYS_SYSPROTO_H_
2848 struct fchflags_args {
2849 int fd;
2850 u_long flags;
2851 };
2852 #endif
2853 int
sys_fchflags(struct thread * td,struct fchflags_args * uap)2854 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2855 {
2856 struct file *fp;
2857 int error;
2858
2859 AUDIT_ARG_FD(uap->fd);
2860 AUDIT_ARG_FFLAGS(uap->flags);
2861 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2862 &fp);
2863 if (error != 0)
2864 return (error);
2865 #ifdef AUDIT
2866 if (AUDITING_TD(td)) {
2867 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2868 AUDIT_ARG_VNODE1(fp->f_vnode);
2869 VOP_UNLOCK(fp->f_vnode);
2870 }
2871 #endif
2872 error = setfflags(td, fp->f_vnode, uap->flags);
2873 fdrop(fp, td);
2874 return (error);
2875 }
2876
2877 /*
2878 * Common implementation code for chmod(), lchmod() and fchmod().
2879 */
2880 int
setfmode(struct thread * td,struct ucred * cred,struct vnode * vp,int mode)2881 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2882 {
2883 struct mount *mp;
2884 struct vattr vattr;
2885 int error;
2886
2887 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2888 return (error);
2889 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2890 VATTR_NULL(&vattr);
2891 vattr.va_mode = mode & ALLPERMS;
2892 #ifdef MAC
2893 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2894 if (error == 0)
2895 #endif
2896 error = VOP_SETATTR(vp, &vattr, cred);
2897 VOP_UNLOCK(vp);
2898 vn_finished_write(mp);
2899 return (error);
2900 }
2901
2902 /*
2903 * Change mode of a file given path name.
2904 */
2905 #ifndef _SYS_SYSPROTO_H_
2906 struct chmod_args {
2907 char *path;
2908 int mode;
2909 };
2910 #endif
2911 int
sys_chmod(struct thread * td,struct chmod_args * uap)2912 sys_chmod(struct thread *td, struct chmod_args *uap)
2913 {
2914
2915 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2916 uap->mode, 0));
2917 }
2918
2919 #ifndef _SYS_SYSPROTO_H_
2920 struct fchmodat_args {
2921 int dirfd;
2922 char *path;
2923 mode_t mode;
2924 int flag;
2925 }
2926 #endif
2927 int
sys_fchmodat(struct thread * td,struct fchmodat_args * uap)2928 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2929 {
2930
2931 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2932 uap->mode, uap->flag));
2933 }
2934
2935 /*
2936 * Change mode of a file given path name (don't follow links.)
2937 */
2938 #ifndef _SYS_SYSPROTO_H_
2939 struct lchmod_args {
2940 char *path;
2941 int mode;
2942 };
2943 #endif
2944 int
sys_lchmod(struct thread * td,struct lchmod_args * uap)2945 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2946 {
2947
2948 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2949 uap->mode, AT_SYMLINK_NOFOLLOW));
2950 }
2951
2952 int
kern_fchmodat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,mode_t mode,int flag)2953 kern_fchmodat(struct thread *td, int fd, const char *path,
2954 enum uio_seg pathseg, mode_t mode, int flag)
2955 {
2956 struct nameidata nd;
2957 int error;
2958
2959 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2960 AT_EMPTY_PATH)) != 0)
2961 return (EINVAL);
2962
2963 AUDIT_ARG_MODE(mode);
2964 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2965 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2966 fd, &cap_fchmod_rights);
2967 if ((error = namei(&nd)) != 0)
2968 return (error);
2969 NDFREE_PNBUF(&nd);
2970 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2971 vrele(nd.ni_vp);
2972 return (error);
2973 }
2974
2975 /*
2976 * Change mode of a file given a file descriptor.
2977 */
2978 #ifndef _SYS_SYSPROTO_H_
2979 struct fchmod_args {
2980 int fd;
2981 int mode;
2982 };
2983 #endif
2984 int
sys_fchmod(struct thread * td,struct fchmod_args * uap)2985 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2986 {
2987 struct file *fp;
2988 int error;
2989
2990 AUDIT_ARG_FD(uap->fd);
2991 AUDIT_ARG_MODE(uap->mode);
2992
2993 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2994 if (error != 0)
2995 return (error);
2996 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2997 fdrop(fp, td);
2998 return (error);
2999 }
3000
3001 /*
3002 * Common implementation for chown(), lchown(), and fchown()
3003 */
3004 int
setfown(struct thread * td,struct ucred * cred,struct vnode * vp,uid_t uid,gid_t gid)3005 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
3006 gid_t gid)
3007 {
3008 struct mount *mp;
3009 struct vattr vattr;
3010 int error;
3011
3012 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3013 return (error);
3014 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3015 VATTR_NULL(&vattr);
3016 vattr.va_uid = uid;
3017 vattr.va_gid = gid;
3018 #ifdef MAC
3019 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3020 vattr.va_gid);
3021 if (error == 0)
3022 #endif
3023 error = VOP_SETATTR(vp, &vattr, cred);
3024 VOP_UNLOCK(vp);
3025 vn_finished_write(mp);
3026 return (error);
3027 }
3028
3029 /*
3030 * Set ownership given a path name.
3031 */
3032 #ifndef _SYS_SYSPROTO_H_
3033 struct chown_args {
3034 char *path;
3035 int uid;
3036 int gid;
3037 };
3038 #endif
3039 int
sys_chown(struct thread * td,struct chown_args * uap)3040 sys_chown(struct thread *td, struct chown_args *uap)
3041 {
3042
3043 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3044 uap->gid, 0));
3045 }
3046
3047 #ifndef _SYS_SYSPROTO_H_
3048 struct fchownat_args {
3049 int fd;
3050 const char * path;
3051 uid_t uid;
3052 gid_t gid;
3053 int flag;
3054 };
3055 #endif
3056 int
sys_fchownat(struct thread * td,struct fchownat_args * uap)3057 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3058 {
3059
3060 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3061 uap->gid, uap->flag));
3062 }
3063
3064 int
kern_fchownat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,int uid,int gid,int flag)3065 kern_fchownat(struct thread *td, int fd, const char *path,
3066 enum uio_seg pathseg, int uid, int gid, int flag)
3067 {
3068 struct nameidata nd;
3069 int error;
3070
3071 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3072 AT_EMPTY_PATH)) != 0)
3073 return (EINVAL);
3074
3075 AUDIT_ARG_OWNER(uid, gid);
3076 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3077 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3078 fd, &cap_fchown_rights);
3079
3080 if ((error = namei(&nd)) != 0)
3081 return (error);
3082 NDFREE_PNBUF(&nd);
3083 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3084 vrele(nd.ni_vp);
3085 return (error);
3086 }
3087
3088 /*
3089 * Set ownership given a path name, do not cross symlinks.
3090 */
3091 #ifndef _SYS_SYSPROTO_H_
3092 struct lchown_args {
3093 char *path;
3094 int uid;
3095 int gid;
3096 };
3097 #endif
3098 int
sys_lchown(struct thread * td,struct lchown_args * uap)3099 sys_lchown(struct thread *td, struct lchown_args *uap)
3100 {
3101
3102 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3103 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3104 }
3105
3106 /*
3107 * Set ownership given a file descriptor.
3108 */
3109 #ifndef _SYS_SYSPROTO_H_
3110 struct fchown_args {
3111 int fd;
3112 int uid;
3113 int gid;
3114 };
3115 #endif
3116 int
sys_fchown(struct thread * td,struct fchown_args * uap)3117 sys_fchown(struct thread *td, struct fchown_args *uap)
3118 {
3119 struct file *fp;
3120 int error;
3121
3122 AUDIT_ARG_FD(uap->fd);
3123 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3124 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3125 if (error != 0)
3126 return (error);
3127 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3128 fdrop(fp, td);
3129 return (error);
3130 }
3131
3132 /*
3133 * Common implementation code for utimes(), lutimes(), and futimes().
3134 */
3135 static int
getutimes(const struct timeval * usrtvp,enum uio_seg tvpseg,struct timespec * tsp)3136 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3137 struct timespec *tsp)
3138 {
3139 struct timeval tv[2];
3140 const struct timeval *tvp;
3141 int error;
3142
3143 if (usrtvp == NULL) {
3144 vfs_timestamp(&tsp[0]);
3145 tsp[1] = tsp[0];
3146 } else {
3147 if (tvpseg == UIO_SYSSPACE) {
3148 tvp = usrtvp;
3149 } else {
3150 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3151 return (error);
3152 tvp = tv;
3153 }
3154
3155 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3156 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3157 return (EINVAL);
3158 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3159 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3160 }
3161 return (0);
3162 }
3163
3164 /*
3165 * Common implementation code for futimens(), utimensat().
3166 */
3167 #define UTIMENS_NULL 0x1
3168 #define UTIMENS_EXIT 0x2
3169 static int
getutimens(const struct timespec * usrtsp,enum uio_seg tspseg,struct timespec * tsp,int * retflags)3170 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3171 struct timespec *tsp, int *retflags)
3172 {
3173 struct timespec tsnow;
3174 int error;
3175
3176 vfs_timestamp(&tsnow);
3177 *retflags = 0;
3178 if (usrtsp == NULL) {
3179 tsp[0] = tsnow;
3180 tsp[1] = tsnow;
3181 *retflags |= UTIMENS_NULL;
3182 return (0);
3183 }
3184 if (tspseg == UIO_SYSSPACE) {
3185 tsp[0] = usrtsp[0];
3186 tsp[1] = usrtsp[1];
3187 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3188 return (error);
3189 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3190 *retflags |= UTIMENS_EXIT;
3191 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3192 *retflags |= UTIMENS_NULL;
3193 if (tsp[0].tv_nsec == UTIME_OMIT)
3194 tsp[0].tv_sec = VNOVAL;
3195 else if (tsp[0].tv_nsec == UTIME_NOW)
3196 tsp[0] = tsnow;
3197 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3198 return (EINVAL);
3199 if (tsp[1].tv_nsec == UTIME_OMIT)
3200 tsp[1].tv_sec = VNOVAL;
3201 else if (tsp[1].tv_nsec == UTIME_NOW)
3202 tsp[1] = tsnow;
3203 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3204 return (EINVAL);
3205
3206 return (0);
3207 }
3208
3209 /*
3210 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3211 * and utimensat().
3212 */
3213 static int
setutimes(struct thread * td,struct vnode * vp,const struct timespec * ts,int numtimes,int nullflag)3214 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3215 int numtimes, int nullflag)
3216 {
3217 struct mount *mp;
3218 struct vattr vattr;
3219 int error;
3220 bool setbirthtime;
3221
3222 setbirthtime = false;
3223 vattr.va_birthtime.tv_sec = VNOVAL;
3224 vattr.va_birthtime.tv_nsec = 0;
3225
3226 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3227 return (error);
3228 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3229 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3230 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3231 setbirthtime = true;
3232 VATTR_NULL(&vattr);
3233 vattr.va_atime = ts[0];
3234 vattr.va_mtime = ts[1];
3235 if (setbirthtime)
3236 vattr.va_birthtime = ts[1];
3237 if (numtimes > 2)
3238 vattr.va_birthtime = ts[2];
3239 if (nullflag)
3240 vattr.va_vaflags |= VA_UTIMES_NULL;
3241 #ifdef MAC
3242 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3243 vattr.va_mtime);
3244 #endif
3245 if (error == 0)
3246 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3247 VOP_UNLOCK(vp);
3248 vn_finished_write(mp);
3249 return (error);
3250 }
3251
3252 /*
3253 * Set the access and modification times of a file.
3254 */
3255 #ifndef _SYS_SYSPROTO_H_
3256 struct utimes_args {
3257 char *path;
3258 struct timeval *tptr;
3259 };
3260 #endif
3261 int
sys_utimes(struct thread * td,struct utimes_args * uap)3262 sys_utimes(struct thread *td, struct utimes_args *uap)
3263 {
3264
3265 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3266 uap->tptr, UIO_USERSPACE));
3267 }
3268
3269 #ifndef _SYS_SYSPROTO_H_
3270 struct futimesat_args {
3271 int fd;
3272 const char * path;
3273 const struct timeval * times;
3274 };
3275 #endif
3276 int
sys_futimesat(struct thread * td,struct futimesat_args * uap)3277 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3278 {
3279
3280 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3281 uap->times, UIO_USERSPACE));
3282 }
3283
3284 int
kern_utimesat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,const struct timeval * tptr,enum uio_seg tptrseg)3285 kern_utimesat(struct thread *td, int fd, const char *path,
3286 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3287 {
3288 struct nameidata nd;
3289 struct timespec ts[2];
3290 int error;
3291
3292 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3293 return (error);
3294 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3295 &cap_futimes_rights);
3296
3297 if ((error = namei(&nd)) != 0)
3298 return (error);
3299 NDFREE_PNBUF(&nd);
3300 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3301 vrele(nd.ni_vp);
3302 return (error);
3303 }
3304
3305 /*
3306 * Set the access and modification times of a file.
3307 */
3308 #ifndef _SYS_SYSPROTO_H_
3309 struct lutimes_args {
3310 char *path;
3311 struct timeval *tptr;
3312 };
3313 #endif
3314 int
sys_lutimes(struct thread * td,struct lutimes_args * uap)3315 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3316 {
3317
3318 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3319 UIO_USERSPACE));
3320 }
3321
3322 int
kern_lutimes(struct thread * td,const char * path,enum uio_seg pathseg,const struct timeval * tptr,enum uio_seg tptrseg)3323 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3324 const struct timeval *tptr, enum uio_seg tptrseg)
3325 {
3326 struct timespec ts[2];
3327 struct nameidata nd;
3328 int error;
3329
3330 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3331 return (error);
3332 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3333 if ((error = namei(&nd)) != 0)
3334 return (error);
3335 NDFREE_PNBUF(&nd);
3336 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3337 vrele(nd.ni_vp);
3338 return (error);
3339 }
3340
3341 /*
3342 * Set the access and modification times of a file.
3343 */
3344 #ifndef _SYS_SYSPROTO_H_
3345 struct futimes_args {
3346 int fd;
3347 struct timeval *tptr;
3348 };
3349 #endif
3350 int
sys_futimes(struct thread * td,struct futimes_args * uap)3351 sys_futimes(struct thread *td, struct futimes_args *uap)
3352 {
3353
3354 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3355 }
3356
3357 int
kern_futimes(struct thread * td,int fd,const struct timeval * tptr,enum uio_seg tptrseg)3358 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3359 enum uio_seg tptrseg)
3360 {
3361 struct timespec ts[2];
3362 struct file *fp;
3363 int error;
3364
3365 AUDIT_ARG_FD(fd);
3366 error = getutimes(tptr, tptrseg, ts);
3367 if (error != 0)
3368 return (error);
3369 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3370 if (error != 0)
3371 return (error);
3372 #ifdef AUDIT
3373 if (AUDITING_TD(td)) {
3374 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3375 AUDIT_ARG_VNODE1(fp->f_vnode);
3376 VOP_UNLOCK(fp->f_vnode);
3377 }
3378 #endif
3379 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3380 fdrop(fp, td);
3381 return (error);
3382 }
3383
3384 int
sys_futimens(struct thread * td,struct futimens_args * uap)3385 sys_futimens(struct thread *td, struct futimens_args *uap)
3386 {
3387
3388 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3389 }
3390
3391 int
kern_futimens(struct thread * td,int fd,const struct timespec * tptr,enum uio_seg tptrseg)3392 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3393 enum uio_seg tptrseg)
3394 {
3395 struct timespec ts[2];
3396 struct file *fp;
3397 int error, flags;
3398
3399 AUDIT_ARG_FD(fd);
3400 error = getutimens(tptr, tptrseg, ts, &flags);
3401 if (error != 0)
3402 return (error);
3403 if (flags & UTIMENS_EXIT)
3404 return (0);
3405 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3406 if (error != 0)
3407 return (error);
3408 #ifdef AUDIT
3409 if (AUDITING_TD(td)) {
3410 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3411 AUDIT_ARG_VNODE1(fp->f_vnode);
3412 VOP_UNLOCK(fp->f_vnode);
3413 }
3414 #endif
3415 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3416 fdrop(fp, td);
3417 return (error);
3418 }
3419
3420 int
sys_utimensat(struct thread * td,struct utimensat_args * uap)3421 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3422 {
3423
3424 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3425 uap->times, UIO_USERSPACE, uap->flag));
3426 }
3427
3428 int
kern_utimensat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,const struct timespec * tptr,enum uio_seg tptrseg,int flag)3429 kern_utimensat(struct thread *td, int fd, const char *path,
3430 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3431 int flag)
3432 {
3433 struct nameidata nd;
3434 struct timespec ts[2];
3435 int error, flags;
3436
3437 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3438 AT_EMPTY_PATH)) != 0)
3439 return (EINVAL);
3440
3441 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3442 return (error);
3443 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3444 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3445 pathseg, path, fd, &cap_futimes_rights);
3446 if ((error = namei(&nd)) != 0)
3447 return (error);
3448 /*
3449 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3450 * POSIX states:
3451 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3452 * "Search permission is denied by a component of the path prefix."
3453 */
3454 NDFREE_PNBUF(&nd);
3455 if ((flags & UTIMENS_EXIT) == 0)
3456 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3457 vrele(nd.ni_vp);
3458 return (error);
3459 }
3460
3461 /*
3462 * Truncate a file given its path name.
3463 */
3464 #ifndef _SYS_SYSPROTO_H_
3465 struct truncate_args {
3466 char *path;
3467 int pad;
3468 off_t length;
3469 };
3470 #endif
3471 int
sys_truncate(struct thread * td,struct truncate_args * uap)3472 sys_truncate(struct thread *td, struct truncate_args *uap)
3473 {
3474
3475 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3476 }
3477
3478 int
kern_truncate(struct thread * td,const char * path,enum uio_seg pathseg,off_t length)3479 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3480 off_t length)
3481 {
3482 struct mount *mp;
3483 struct vnode *vp;
3484 void *rl_cookie;
3485 struct nameidata nd;
3486 int error;
3487
3488 if (length < 0)
3489 return (EINVAL);
3490 NDPREINIT(&nd);
3491 retry:
3492 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3493 if ((error = namei(&nd)) != 0)
3494 return (error);
3495 vp = nd.ni_vp;
3496 NDFREE_PNBUF(&nd);
3497 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3498 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3499 vn_rangelock_unlock(vp, rl_cookie);
3500 vrele(vp);
3501 return (error);
3502 }
3503 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3504 if (vp->v_type == VDIR) {
3505 error = EISDIR;
3506 goto out;
3507 }
3508 #ifdef MAC
3509 error = mac_vnode_check_write(td->td_ucred, NOCRED, vp);
3510 if (error != 0)
3511 goto out;
3512 #endif
3513 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
3514 if (error != 0)
3515 goto out;
3516
3517 error = vn_truncate_locked(vp, length, false, td->td_ucred);
3518 out:
3519 VOP_UNLOCK(vp);
3520 vn_finished_write(mp);
3521 vn_rangelock_unlock(vp, rl_cookie);
3522 vrele(vp);
3523 if (error == ERELOOKUP)
3524 goto retry;
3525 return (error);
3526 }
3527
3528 #if defined(COMPAT_43)
3529 /*
3530 * Truncate a file given its path name.
3531 */
3532 #ifndef _SYS_SYSPROTO_H_
3533 struct otruncate_args {
3534 char *path;
3535 long length;
3536 };
3537 #endif
3538 int
otruncate(struct thread * td,struct otruncate_args * uap)3539 otruncate(struct thread *td, struct otruncate_args *uap)
3540 {
3541
3542 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3543 }
3544 #endif /* COMPAT_43 */
3545
3546 #if defined(COMPAT_FREEBSD6)
3547 /* Versions with the pad argument */
3548 int
freebsd6_truncate(struct thread * td,struct freebsd6_truncate_args * uap)3549 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3550 {
3551
3552 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3553 }
3554
3555 int
freebsd6_ftruncate(struct thread * td,struct freebsd6_ftruncate_args * uap)3556 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3557 {
3558
3559 return (kern_ftruncate(td, uap->fd, uap->length));
3560 }
3561 #endif
3562
3563 int
kern_fsync(struct thread * td,int fd,bool fullsync)3564 kern_fsync(struct thread *td, int fd, bool fullsync)
3565 {
3566 struct vnode *vp;
3567 struct mount *mp;
3568 struct file *fp;
3569 int error;
3570
3571 AUDIT_ARG_FD(fd);
3572 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3573 if (error != 0)
3574 return (error);
3575 vp = fp->f_vnode;
3576 #if 0
3577 if (!fullsync)
3578 /* XXXKIB: compete outstanding aio writes */;
3579 #endif
3580 retry:
3581 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3582 if (error != 0)
3583 goto drop;
3584 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3585 AUDIT_ARG_VNODE1(vp);
3586 vnode_pager_clean_async(vp);
3587 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3588 VOP_UNLOCK(vp);
3589 vn_finished_write(mp);
3590 if (error == ERELOOKUP)
3591 goto retry;
3592 drop:
3593 fdrop(fp, td);
3594 return (error);
3595 }
3596
3597 /*
3598 * Sync an open file.
3599 */
3600 #ifndef _SYS_SYSPROTO_H_
3601 struct fsync_args {
3602 int fd;
3603 };
3604 #endif
3605 int
sys_fsync(struct thread * td,struct fsync_args * uap)3606 sys_fsync(struct thread *td, struct fsync_args *uap)
3607 {
3608
3609 return (kern_fsync(td, uap->fd, true));
3610 }
3611
3612 int
sys_fdatasync(struct thread * td,struct fdatasync_args * uap)3613 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3614 {
3615
3616 return (kern_fsync(td, uap->fd, false));
3617 }
3618
3619 /*
3620 * Rename files. Source and destination must either both be directories, or
3621 * both not be directories. If target is a directory, it must be empty.
3622 */
3623 #ifndef _SYS_SYSPROTO_H_
3624 struct rename_args {
3625 char *from;
3626 char *to;
3627 };
3628 #endif
3629 int
sys_rename(struct thread * td,struct rename_args * uap)3630 sys_rename(struct thread *td, struct rename_args *uap)
3631 {
3632
3633 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3634 uap->to, UIO_USERSPACE));
3635 }
3636
3637 #ifndef _SYS_SYSPROTO_H_
3638 struct renameat_args {
3639 int oldfd;
3640 char *old;
3641 int newfd;
3642 char *new;
3643 };
3644 #endif
3645 int
sys_renameat(struct thread * td,struct renameat_args * uap)3646 sys_renameat(struct thread *td, struct renameat_args *uap)
3647 {
3648
3649 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3650 UIO_USERSPACE));
3651 }
3652
3653 #ifdef MAC
3654 static int
kern_renameat_mac(struct thread * td,int oldfd,const char * old,int newfd,const char * new,enum uio_seg pathseg,struct nameidata * fromnd)3655 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3656 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3657 {
3658 int error;
3659
3660 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3661 pathseg, old, oldfd, &cap_renameat_source_rights);
3662 if ((error = namei(fromnd)) != 0)
3663 return (error);
3664 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3665 fromnd->ni_vp, &fromnd->ni_cnd);
3666 VOP_UNLOCK(fromnd->ni_dvp);
3667 if (fromnd->ni_dvp != fromnd->ni_vp)
3668 VOP_UNLOCK(fromnd->ni_vp);
3669 if (error != 0) {
3670 NDFREE_PNBUF(fromnd);
3671 vrele(fromnd->ni_dvp);
3672 vrele(fromnd->ni_vp);
3673 }
3674 return (error);
3675 }
3676 #endif
3677
3678 int
kern_renameat(struct thread * td,int oldfd,const char * old,int newfd,const char * new,enum uio_seg pathseg)3679 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3680 const char *new, enum uio_seg pathseg)
3681 {
3682 struct mount *mp = NULL;
3683 struct vnode *tvp, *fvp, *tdvp;
3684 struct nameidata fromnd, tond;
3685 uint64_t tondflags;
3686 int error;
3687
3688 again:
3689 bwillwrite();
3690 #ifdef MAC
3691 if (mac_vnode_check_rename_from_enabled()) {
3692 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3693 &fromnd);
3694 if (error != 0)
3695 return (error);
3696 } else {
3697 #endif
3698 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | AUDITVNODE1,
3699 pathseg, old, oldfd, &cap_renameat_source_rights);
3700 if ((error = namei(&fromnd)) != 0)
3701 return (error);
3702 #ifdef MAC
3703 }
3704 #endif
3705 fvp = fromnd.ni_vp;
3706 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2;
3707 if (fromnd.ni_vp->v_type == VDIR)
3708 tondflags |= WILLBEDIR;
3709 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3710 &cap_renameat_target_rights);
3711 if ((error = namei(&tond)) != 0) {
3712 /* Translate error code for rename("dir1", "dir2/."). */
3713 if (error == EISDIR && fvp->v_type == VDIR)
3714 error = EINVAL;
3715 NDFREE_PNBUF(&fromnd);
3716 vrele(fromnd.ni_dvp);
3717 vrele(fvp);
3718 goto out1;
3719 }
3720 tdvp = tond.ni_dvp;
3721 tvp = tond.ni_vp;
3722 error = vn_start_write(fvp, &mp, V_NOWAIT);
3723 if (error != 0) {
3724 NDFREE_PNBUF(&fromnd);
3725 NDFREE_PNBUF(&tond);
3726 if (tvp != NULL)
3727 vput(tvp);
3728 if (tdvp == tvp)
3729 vrele(tdvp);
3730 else
3731 vput(tdvp);
3732 vrele(fromnd.ni_dvp);
3733 vrele(fvp);
3734 error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH);
3735 if (error != 0)
3736 return (error);
3737 goto again;
3738 }
3739 if (tvp != NULL) {
3740 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3741 error = ENOTDIR;
3742 goto out;
3743 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3744 error = EISDIR;
3745 goto out;
3746 }
3747 #ifdef CAPABILITIES
3748 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3749 /*
3750 * If the target already exists we require CAP_UNLINKAT
3751 * from 'newfd', when newfd was used for the lookup.
3752 */
3753 error = cap_check(&tond.ni_filecaps.fc_rights,
3754 &cap_unlinkat_rights);
3755 if (error != 0)
3756 goto out;
3757 }
3758 #endif
3759 }
3760 if (fvp == tdvp) {
3761 error = EINVAL;
3762 goto out;
3763 }
3764 /*
3765 * If the source is the same as the destination (that is, if they
3766 * are links to the same vnode), then there is nothing to do.
3767 */
3768 if (fvp == tvp)
3769 error = ERESTART;
3770 #ifdef MAC
3771 else
3772 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3773 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3774 #endif
3775 out:
3776 if (error == 0) {
3777 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3778 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3779 NDFREE_PNBUF(&fromnd);
3780 NDFREE_PNBUF(&tond);
3781 } else {
3782 NDFREE_PNBUF(&fromnd);
3783 NDFREE_PNBUF(&tond);
3784 if (tvp != NULL)
3785 vput(tvp);
3786 if (tdvp == tvp)
3787 vrele(tdvp);
3788 else
3789 vput(tdvp);
3790 vrele(fromnd.ni_dvp);
3791 vrele(fvp);
3792 }
3793 vn_finished_write(mp);
3794 out1:
3795 if (error == ERESTART)
3796 return (0);
3797 if (error == ERELOOKUP)
3798 goto again;
3799 return (error);
3800 }
3801
3802 /*
3803 * Make a directory file.
3804 */
3805 #ifndef _SYS_SYSPROTO_H_
3806 struct mkdir_args {
3807 char *path;
3808 int mode;
3809 };
3810 #endif
3811 int
sys_mkdir(struct thread * td,struct mkdir_args * uap)3812 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3813 {
3814
3815 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3816 uap->mode));
3817 }
3818
3819 #ifndef _SYS_SYSPROTO_H_
3820 struct mkdirat_args {
3821 int fd;
3822 char *path;
3823 mode_t mode;
3824 };
3825 #endif
3826 int
sys_mkdirat(struct thread * td,struct mkdirat_args * uap)3827 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3828 {
3829
3830 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3831 }
3832
3833 int
kern_mkdirat(struct thread * td,int fd,const char * path,enum uio_seg segflg,int mode)3834 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3835 int mode)
3836 {
3837 struct mount *mp;
3838 struct vattr vattr;
3839 struct nameidata nd;
3840 int error;
3841
3842 AUDIT_ARG_MODE(mode);
3843 NDPREINIT(&nd);
3844 restart:
3845 bwillwrite();
3846 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 |
3847 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3848 segflg, path, fd, &cap_mkdirat_rights);
3849 if ((error = namei(&nd)) != 0)
3850 return (error);
3851 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3852 NDFREE_PNBUF(&nd);
3853 vput(nd.ni_dvp);
3854 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3855 return (error);
3856 goto restart;
3857 }
3858 VATTR_NULL(&vattr);
3859 vattr.va_type = VDIR;
3860 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3861 #ifdef MAC
3862 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3863 &vattr);
3864 if (error != 0)
3865 goto out;
3866 #endif
3867 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3868 #ifdef MAC
3869 out:
3870 #endif
3871 NDFREE_PNBUF(&nd);
3872 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3873 vn_finished_write(mp);
3874 if (error == ERELOOKUP)
3875 goto restart;
3876 return (error);
3877 }
3878
3879 /*
3880 * Remove a directory file.
3881 */
3882 #ifndef _SYS_SYSPROTO_H_
3883 struct rmdir_args {
3884 char *path;
3885 };
3886 #endif
3887 int
sys_rmdir(struct thread * td,struct rmdir_args * uap)3888 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3889 {
3890
3891 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3892 0));
3893 }
3894
3895 int
kern_frmdirat(struct thread * td,int dfd,const char * path,int fd,enum uio_seg pathseg,int flag)3896 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3897 enum uio_seg pathseg, int flag)
3898 {
3899 struct mount *mp;
3900 struct vnode *vp;
3901 struct file *fp;
3902 struct nameidata nd;
3903 cap_rights_t rights;
3904 int error;
3905
3906 fp = NULL;
3907 if (fd != FD_NONE) {
3908 error = getvnode(td, fd, cap_rights_init_one(&rights,
3909 CAP_LOOKUP), &fp);
3910 if (error != 0)
3911 return (error);
3912 }
3913
3914 NDPREINIT(&nd);
3915 restart:
3916 bwillwrite();
3917 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3918 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3919 pathseg, path, dfd, &cap_unlinkat_rights);
3920 if ((error = namei(&nd)) != 0)
3921 goto fdout;
3922 vp = nd.ni_vp;
3923 if (vp->v_type != VDIR) {
3924 error = ENOTDIR;
3925 goto out;
3926 }
3927 /*
3928 * No rmdir "." please.
3929 */
3930 if (nd.ni_dvp == vp) {
3931 error = EINVAL;
3932 goto out;
3933 }
3934 /*
3935 * The root of a mounted filesystem cannot be deleted.
3936 */
3937 if (vp->v_vflag & VV_ROOT) {
3938 error = EBUSY;
3939 goto out;
3940 }
3941
3942 if (fp != NULL && fp->f_vnode != vp) {
3943 if (VN_IS_DOOMED(fp->f_vnode))
3944 error = EBADF;
3945 else
3946 error = EDEADLK;
3947 goto out;
3948 }
3949
3950 #ifdef MAC
3951 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3952 &nd.ni_cnd);
3953 if (error != 0)
3954 goto out;
3955 #endif
3956 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3957 NDFREE_PNBUF(&nd);
3958 vput(vp);
3959 if (nd.ni_dvp == vp)
3960 vrele(nd.ni_dvp);
3961 else
3962 vput(nd.ni_dvp);
3963 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3964 goto fdout;
3965 goto restart;
3966 }
3967 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3968 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3969 vn_finished_write(mp);
3970 out:
3971 NDFREE_PNBUF(&nd);
3972 vput(vp);
3973 if (nd.ni_dvp == vp)
3974 vrele(nd.ni_dvp);
3975 else
3976 vput(nd.ni_dvp);
3977 if (error == ERELOOKUP)
3978 goto restart;
3979 fdout:
3980 if (fp != NULL)
3981 fdrop(fp, td);
3982 return (error);
3983 }
3984
3985 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3986 int
freebsd11_kern_getdirentries(struct thread * td,int fd,char * ubuf,u_int count,long * basep,void (* func)(struct freebsd11_dirent *))3987 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3988 long *basep, void (*func)(struct freebsd11_dirent *))
3989 {
3990 struct freebsd11_dirent dstdp;
3991 struct dirent *dp, *edp;
3992 char *dirbuf;
3993 off_t base;
3994 ssize_t resid, ucount;
3995 int error;
3996
3997 /* XXX arbitrary sanity limit on `count'. */
3998 count = min(count, 64 * 1024);
3999
4000 dirbuf = malloc(count, M_TEMP, M_WAITOK);
4001
4002 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
4003 UIO_SYSSPACE);
4004 if (error != 0)
4005 goto done;
4006 if (basep != NULL)
4007 *basep = base;
4008
4009 ucount = 0;
4010 for (dp = (struct dirent *)dirbuf,
4011 edp = (struct dirent *)&dirbuf[count - resid];
4012 ucount < count && dp < edp; ) {
4013 if (dp->d_reclen == 0)
4014 break;
4015 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
4016 if (dp->d_namlen >= sizeof(dstdp.d_name))
4017 continue;
4018 dstdp.d_type = dp->d_type;
4019 dstdp.d_namlen = dp->d_namlen;
4020 dstdp.d_fileno = dp->d_fileno; /* truncate */
4021 if (dstdp.d_fileno != dp->d_fileno) {
4022 switch (ino64_trunc_error) {
4023 default:
4024 case 0:
4025 break;
4026 case 1:
4027 error = EOVERFLOW;
4028 goto done;
4029 case 2:
4030 dstdp.d_fileno = UINT32_MAX;
4031 break;
4032 }
4033 }
4034 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4035 ((dp->d_namlen + 1 + 3) &~ 3);
4036 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4037 bzero(dstdp.d_name + dstdp.d_namlen,
4038 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4039 dstdp.d_namlen);
4040 MPASS(dstdp.d_reclen <= dp->d_reclen);
4041 MPASS(ucount + dstdp.d_reclen <= count);
4042 if (func != NULL)
4043 func(&dstdp);
4044 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4045 if (error != 0)
4046 break;
4047 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4048 ucount += dstdp.d_reclen;
4049 }
4050
4051 done:
4052 free(dirbuf, M_TEMP);
4053 if (error == 0)
4054 td->td_retval[0] = ucount;
4055 return (error);
4056 }
4057 #endif /* COMPAT */
4058
4059 #ifdef COMPAT_43
4060 static void
ogetdirentries_cvt(struct freebsd11_dirent * dp)4061 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4062 {
4063 #if (BYTE_ORDER == LITTLE_ENDIAN)
4064 /*
4065 * The expected low byte of dp->d_namlen is our dp->d_type.
4066 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4067 */
4068 dp->d_type = dp->d_namlen;
4069 dp->d_namlen = 0;
4070 #else
4071 /*
4072 * The dp->d_type is the high byte of the expected dp->d_namlen,
4073 * so must be zero'ed.
4074 */
4075 dp->d_type = 0;
4076 #endif
4077 }
4078
4079 /*
4080 * Read a block of directory entries in a filesystem independent format.
4081 */
4082 #ifndef _SYS_SYSPROTO_H_
4083 struct ogetdirentries_args {
4084 int fd;
4085 char *buf;
4086 u_int count;
4087 long *basep;
4088 };
4089 #endif
4090 int
ogetdirentries(struct thread * td,struct ogetdirentries_args * uap)4091 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4092 {
4093 long loff;
4094 int error;
4095
4096 error = kern_ogetdirentries(td, uap, &loff);
4097 if (error == 0)
4098 error = copyout(&loff, uap->basep, sizeof(long));
4099 return (error);
4100 }
4101
4102 int
kern_ogetdirentries(struct thread * td,struct ogetdirentries_args * uap,long * ploff)4103 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4104 long *ploff)
4105 {
4106 long base;
4107 int error;
4108
4109 /* XXX arbitrary sanity limit on `count'. */
4110 if (uap->count > 64 * 1024)
4111 return (EINVAL);
4112
4113 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4114 &base, ogetdirentries_cvt);
4115
4116 if (error == 0 && uap->basep != NULL)
4117 error = copyout(&base, uap->basep, sizeof(long));
4118
4119 return (error);
4120 }
4121 #endif /* COMPAT_43 */
4122
4123 #if defined(COMPAT_FREEBSD11)
4124 #ifndef _SYS_SYSPROTO_H_
4125 struct freebsd11_getdirentries_args {
4126 int fd;
4127 char *buf;
4128 u_int count;
4129 long *basep;
4130 };
4131 #endif
4132 int
freebsd11_getdirentries(struct thread * td,struct freebsd11_getdirentries_args * uap)4133 freebsd11_getdirentries(struct thread *td,
4134 struct freebsd11_getdirentries_args *uap)
4135 {
4136 long base;
4137 int error;
4138
4139 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4140 &base, NULL);
4141
4142 if (error == 0 && uap->basep != NULL)
4143 error = copyout(&base, uap->basep, sizeof(long));
4144 return (error);
4145 }
4146
4147 int
freebsd11_getdents(struct thread * td,struct freebsd11_getdents_args * uap)4148 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4149 {
4150 struct freebsd11_getdirentries_args ap;
4151
4152 ap.fd = uap->fd;
4153 ap.buf = uap->buf;
4154 ap.count = uap->count;
4155 ap.basep = NULL;
4156 return (freebsd11_getdirentries(td, &ap));
4157 }
4158 #endif /* COMPAT_FREEBSD11 */
4159
4160 /*
4161 * Read a block of directory entries in a filesystem independent format.
4162 */
4163 int
sys_getdirentries(struct thread * td,struct getdirentries_args * uap)4164 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4165 {
4166 off_t base;
4167 int error;
4168
4169 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4170 NULL, UIO_USERSPACE);
4171 if (error != 0)
4172 return (error);
4173 if (uap->basep != NULL)
4174 error = copyout(&base, uap->basep, sizeof(off_t));
4175 return (error);
4176 }
4177
4178 int
kern_getdirentries(struct thread * td,int fd,char * buf,size_t count,off_t * basep,ssize_t * residp,enum uio_seg bufseg)4179 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4180 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4181 {
4182 struct vnode *vp;
4183 struct file *fp;
4184 struct uio auio;
4185 struct iovec aiov;
4186 off_t loff;
4187 int error, eofflag;
4188 off_t foffset;
4189
4190 AUDIT_ARG_FD(fd);
4191 if (count > IOSIZE_MAX)
4192 return (EINVAL);
4193 auio.uio_resid = count;
4194 error = getvnode(td, fd, &cap_read_rights, &fp);
4195 if (error != 0)
4196 return (error);
4197 if ((fp->f_flag & FREAD) == 0) {
4198 fdrop(fp, td);
4199 return (EBADF);
4200 }
4201 vp = fp->f_vnode;
4202 foffset = foffset_lock(fp, 0);
4203 unionread:
4204 if (vp->v_type != VDIR) {
4205 error = EINVAL;
4206 goto fail;
4207 }
4208 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4209 error = ENOENT;
4210 goto fail;
4211 }
4212 aiov.iov_base = buf;
4213 aiov.iov_len = count;
4214 auio.uio_iov = &aiov;
4215 auio.uio_iovcnt = 1;
4216 auio.uio_rw = UIO_READ;
4217 auio.uio_segflg = bufseg;
4218 auio.uio_td = td;
4219 vn_lock(vp, LK_SHARED | LK_RETRY);
4220 AUDIT_ARG_VNODE1(vp);
4221 loff = auio.uio_offset = foffset;
4222 #ifdef MAC
4223 error = mac_vnode_check_readdir(td->td_ucred, vp);
4224 if (error == 0)
4225 #endif
4226 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4227 NULL);
4228 foffset = auio.uio_offset;
4229 if (error != 0) {
4230 VOP_UNLOCK(vp);
4231 goto fail;
4232 }
4233 if (count == auio.uio_resid &&
4234 (vp->v_vflag & VV_ROOT) &&
4235 (vp->v_mount->mnt_flag & MNT_UNION)) {
4236 struct vnode *tvp = vp;
4237
4238 vp = vp->v_mount->mnt_vnodecovered;
4239 VREF(vp);
4240 fp->f_vnode = vp;
4241 foffset = 0;
4242 vput(tvp);
4243 goto unionread;
4244 }
4245 VOP_UNLOCK(vp);
4246 *basep = loff;
4247 if (residp != NULL)
4248 *residp = auio.uio_resid;
4249 td->td_retval[0] = count - auio.uio_resid;
4250 fail:
4251 foffset_unlock(fp, foffset, 0);
4252 fdrop(fp, td);
4253 return (error);
4254 }
4255
4256 /*
4257 * Set the mode mask for creation of filesystem nodes.
4258 */
4259 #ifndef _SYS_SYSPROTO_H_
4260 struct umask_args {
4261 int newmask;
4262 };
4263 #endif
4264 int
sys_umask(struct thread * td,struct umask_args * uap)4265 sys_umask(struct thread *td, struct umask_args *uap)
4266 {
4267 struct pwddesc *pdp;
4268
4269 pdp = td->td_proc->p_pd;
4270 PWDDESC_XLOCK(pdp);
4271 td->td_retval[0] = pdp->pd_cmask;
4272 pdp->pd_cmask = uap->newmask & ALLPERMS;
4273 PWDDESC_XUNLOCK(pdp);
4274 return (0);
4275 }
4276
4277 /*
4278 * Void all references to file by ripping underlying filesystem away from
4279 * vnode.
4280 */
4281 #ifndef _SYS_SYSPROTO_H_
4282 struct revoke_args {
4283 char *path;
4284 };
4285 #endif
4286 int
sys_revoke(struct thread * td,struct revoke_args * uap)4287 sys_revoke(struct thread *td, struct revoke_args *uap)
4288 {
4289 struct vnode *vp;
4290 struct vattr vattr;
4291 struct nameidata nd;
4292 int error;
4293
4294 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4295 uap->path);
4296 if ((error = namei(&nd)) != 0)
4297 return (error);
4298 vp = nd.ni_vp;
4299 NDFREE_PNBUF(&nd);
4300 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4301 error = EINVAL;
4302 goto out;
4303 }
4304 #ifdef MAC
4305 error = mac_vnode_check_revoke(td->td_ucred, vp);
4306 if (error != 0)
4307 goto out;
4308 #endif
4309 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4310 if (error != 0)
4311 goto out;
4312 if (td->td_ucred->cr_uid != vattr.va_uid) {
4313 error = priv_check(td, PRIV_VFS_ADMIN);
4314 if (error != 0)
4315 goto out;
4316 }
4317 if (devfs_usecount(vp) > 0)
4318 VOP_REVOKE(vp, REVOKEALL);
4319 out:
4320 vput(vp);
4321 return (error);
4322 }
4323
4324 /*
4325 * This variant of getvnode() allows O_PATH files. Caller should
4326 * ensure that returned file and vnode are only used for compatible
4327 * semantics.
4328 */
4329 int
getvnode_path(struct thread * td,int fd,cap_rights_t * rightsp,struct file ** fpp)4330 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4331 struct file **fpp)
4332 {
4333 struct file *fp;
4334 int error;
4335
4336 error = fget_unlocked(td, fd, rightsp, &fp);
4337 if (error != 0)
4338 return (error);
4339
4340 /*
4341 * The file could be not of the vnode type, or it may be not
4342 * yet fully initialized, in which case the f_vnode pointer
4343 * may be set, but f_ops is still badfileops. E.g.,
4344 * devfs_open() transiently create such situation to
4345 * facilitate csw d_fdopen().
4346 *
4347 * Dupfdopen() handling in kern_openat() installs the
4348 * half-baked file into the process descriptor table, allowing
4349 * other thread to dereference it. Guard against the race by
4350 * checking f_ops.
4351 */
4352 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4353 fdrop(fp, td);
4354 *fpp = NULL;
4355 return (EINVAL);
4356 }
4357
4358 *fpp = fp;
4359 return (0);
4360 }
4361
4362 /*
4363 * Convert a user file descriptor to a kernel file entry and check
4364 * that, if it is a capability, the correct rights are present.
4365 * A reference on the file entry is held upon returning.
4366 */
4367 int
getvnode(struct thread * td,int fd,cap_rights_t * rightsp,struct file ** fpp)4368 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4369 {
4370 int error;
4371
4372 error = getvnode_path(td, fd, rightsp, fpp);
4373 if (__predict_false(error != 0))
4374 return (error);
4375
4376 /*
4377 * Filter out O_PATH file descriptors, most getvnode() callers
4378 * do not call fo_ methods.
4379 */
4380 if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4381 fdrop(*fpp, td);
4382 *fpp = NULL;
4383 error = EBADF;
4384 }
4385
4386 return (error);
4387 }
4388
4389 /*
4390 * Get an (NFS) file handle.
4391 */
4392 #ifndef _SYS_SYSPROTO_H_
4393 struct lgetfh_args {
4394 char *fname;
4395 fhandle_t *fhp;
4396 };
4397 #endif
4398 int
sys_lgetfh(struct thread * td,struct lgetfh_args * uap)4399 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4400 {
4401
4402 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4403 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4404 }
4405
4406 #ifndef _SYS_SYSPROTO_H_
4407 struct getfh_args {
4408 char *fname;
4409 fhandle_t *fhp;
4410 };
4411 #endif
4412 int
sys_getfh(struct thread * td,struct getfh_args * uap)4413 sys_getfh(struct thread *td, struct getfh_args *uap)
4414 {
4415
4416 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4417 uap->fhp, UIO_USERSPACE));
4418 }
4419
4420 /*
4421 * syscall for the rpc.lockd to use to translate an open descriptor into
4422 * a NFS file handle.
4423 *
4424 * warning: do not remove the priv_check() call or this becomes one giant
4425 * security hole.
4426 */
4427 #ifndef _SYS_SYSPROTO_H_
4428 struct getfhat_args {
4429 int fd;
4430 char *path;
4431 fhandle_t *fhp;
4432 int flags;
4433 };
4434 #endif
4435 int
sys_getfhat(struct thread * td,struct getfhat_args * uap)4436 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4437 {
4438
4439 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4440 uap->fhp, UIO_USERSPACE));
4441 }
4442
4443 int
kern_getfhat(struct thread * td,int flags,int fd,const char * path,enum uio_seg pathseg,fhandle_t * fhp,enum uio_seg fhseg)4444 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4445 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4446 {
4447 struct nameidata nd;
4448 fhandle_t fh;
4449 struct vnode *vp;
4450 int error;
4451
4452 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4453 return (EINVAL);
4454 error = priv_check(td, PRIV_VFS_GETFH);
4455 if (error != 0)
4456 return (error);
4457 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4458 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4459 fd);
4460 error = namei(&nd);
4461 if (error != 0)
4462 return (error);
4463 NDFREE_PNBUF(&nd);
4464 vp = nd.ni_vp;
4465 bzero(&fh, sizeof(fh));
4466 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4467 error = VOP_VPTOFH(vp, &fh.fh_fid);
4468 vput(vp);
4469 if (error == 0) {
4470 if (fhseg == UIO_USERSPACE)
4471 error = copyout(&fh, fhp, sizeof (fh));
4472 else
4473 memcpy(fhp, &fh, sizeof(fh));
4474 }
4475 return (error);
4476 }
4477
4478 #ifndef _SYS_SYSPROTO_H_
4479 struct fhlink_args {
4480 fhandle_t *fhp;
4481 const char *to;
4482 };
4483 #endif
4484 int
sys_fhlink(struct thread * td,struct fhlink_args * uap)4485 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4486 {
4487
4488 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4489 }
4490
4491 #ifndef _SYS_SYSPROTO_H_
4492 struct fhlinkat_args {
4493 fhandle_t *fhp;
4494 int tofd;
4495 const char *to;
4496 };
4497 #endif
4498 int
sys_fhlinkat(struct thread * td,struct fhlinkat_args * uap)4499 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4500 {
4501
4502 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4503 }
4504
4505 static int
kern_fhlinkat(struct thread * td,int fd,const char * path,enum uio_seg pathseg,fhandle_t * fhp)4506 kern_fhlinkat(struct thread *td, int fd, const char *path,
4507 enum uio_seg pathseg, fhandle_t *fhp)
4508 {
4509 fhandle_t fh;
4510 struct mount *mp;
4511 struct vnode *vp;
4512 int error;
4513
4514 error = priv_check(td, PRIV_VFS_GETFH);
4515 if (error != 0)
4516 return (error);
4517 error = copyin(fhp, &fh, sizeof(fh));
4518 if (error != 0)
4519 return (error);
4520 do {
4521 bwillwrite();
4522 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4523 return (ESTALE);
4524 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4525 vfs_unbusy(mp);
4526 if (error != 0)
4527 return (error);
4528 VOP_UNLOCK(vp);
4529 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4530 } while (error == EAGAIN || error == ERELOOKUP);
4531 return (error);
4532 }
4533
4534 #ifndef _SYS_SYSPROTO_H_
4535 struct fhreadlink_args {
4536 fhandle_t *fhp;
4537 char *buf;
4538 size_t bufsize;
4539 };
4540 #endif
4541 int
sys_fhreadlink(struct thread * td,struct fhreadlink_args * uap)4542 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4543 {
4544 fhandle_t fh;
4545 struct mount *mp;
4546 struct vnode *vp;
4547 int error;
4548
4549 error = priv_check(td, PRIV_VFS_GETFH);
4550 if (error != 0)
4551 return (error);
4552 if (uap->bufsize > IOSIZE_MAX)
4553 return (EINVAL);
4554 error = copyin(uap->fhp, &fh, sizeof(fh));
4555 if (error != 0)
4556 return (error);
4557 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4558 return (ESTALE);
4559 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4560 vfs_unbusy(mp);
4561 if (error != 0)
4562 return (error);
4563 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4564 vput(vp);
4565 return (error);
4566 }
4567
4568 /*
4569 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4570 * open descriptor.
4571 *
4572 * warning: do not remove the priv_check() call or this becomes one giant
4573 * security hole.
4574 */
4575 #ifndef _SYS_SYSPROTO_H_
4576 struct fhopen_args {
4577 const struct fhandle *u_fhp;
4578 int flags;
4579 };
4580 #endif
4581 int
sys_fhopen(struct thread * td,struct fhopen_args * uap)4582 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4583 {
4584 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4585 }
4586
4587 int
kern_fhopen(struct thread * td,const struct fhandle * u_fhp,int flags)4588 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4589 {
4590 struct mount *mp;
4591 struct vnode *vp;
4592 struct fhandle fhp;
4593 struct file *fp;
4594 int fmode, error;
4595 int indx;
4596
4597 error = priv_check(td, PRIV_VFS_FHOPEN);
4598 if (error != 0)
4599 return (error);
4600 indx = -1;
4601 fmode = FFLAGS(flags);
4602 /* why not allow a non-read/write open for our lockd? */
4603 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4604 return (EINVAL);
4605 error = copyin(u_fhp, &fhp, sizeof(fhp));
4606 if (error != 0)
4607 return(error);
4608 /* find the mount point */
4609 mp = vfs_busyfs(&fhp.fh_fsid);
4610 if (mp == NULL)
4611 return (ESTALE);
4612 /* now give me my vnode, it gets returned to me locked */
4613 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4614 vfs_unbusy(mp);
4615 if (error != 0)
4616 return (error);
4617
4618 error = falloc_noinstall(td, &fp);
4619 if (error != 0) {
4620 vput(vp);
4621 return (error);
4622 }
4623 /*
4624 * An extra reference on `fp' has been held for us by
4625 * falloc_noinstall().
4626 */
4627
4628 #ifdef INVARIANTS
4629 td->td_dupfd = -1;
4630 #endif
4631 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4632 if (error != 0) {
4633 KASSERT(fp->f_ops == &badfileops,
4634 ("VOP_OPEN in fhopen() set f_ops"));
4635 KASSERT(td->td_dupfd < 0,
4636 ("fhopen() encountered fdopen()"));
4637
4638 vput(vp);
4639 goto bad;
4640 }
4641 #ifdef INVARIANTS
4642 td->td_dupfd = 0;
4643 #endif
4644 fp->f_vnode = vp;
4645 finit_vnode(fp, fmode, NULL, &vnops);
4646 VOP_UNLOCK(vp);
4647 if ((fmode & O_TRUNC) != 0) {
4648 error = fo_truncate(fp, 0, td->td_ucred, td);
4649 if (error != 0)
4650 goto bad;
4651 }
4652
4653 error = finstall(td, fp, &indx, fmode, NULL);
4654 bad:
4655 fdrop(fp, td);
4656 td->td_retval[0] = indx;
4657 return (error);
4658 }
4659
4660 /*
4661 * Stat an (NFS) file handle.
4662 */
4663 #ifndef _SYS_SYSPROTO_H_
4664 struct fhstat_args {
4665 struct fhandle *u_fhp;
4666 struct stat *sb;
4667 };
4668 #endif
4669 int
sys_fhstat(struct thread * td,struct fhstat_args * uap)4670 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4671 {
4672 struct stat sb;
4673 struct fhandle fh;
4674 int error;
4675
4676 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4677 if (error != 0)
4678 return (error);
4679 error = kern_fhstat(td, fh, &sb);
4680 if (error == 0)
4681 error = copyout(&sb, uap->sb, sizeof(sb));
4682 return (error);
4683 }
4684
4685 int
kern_fhstat(struct thread * td,struct fhandle fh,struct stat * sb)4686 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4687 {
4688 struct mount *mp;
4689 struct vnode *vp;
4690 int error;
4691
4692 error = priv_check(td, PRIV_VFS_FHSTAT);
4693 if (error != 0)
4694 return (error);
4695 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4696 return (ESTALE);
4697 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4698 vfs_unbusy(mp);
4699 if (error != 0)
4700 return (error);
4701 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4702 vput(vp);
4703 return (error);
4704 }
4705
4706 /*
4707 * Implement fstatfs() for (NFS) file handles.
4708 */
4709 #ifndef _SYS_SYSPROTO_H_
4710 struct fhstatfs_args {
4711 struct fhandle *u_fhp;
4712 struct statfs *buf;
4713 };
4714 #endif
4715 int
sys_fhstatfs(struct thread * td,struct fhstatfs_args * uap)4716 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4717 {
4718 struct statfs *sfp;
4719 fhandle_t fh;
4720 int error;
4721
4722 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4723 if (error != 0)
4724 return (error);
4725 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4726 error = kern_fhstatfs(td, fh, sfp);
4727 if (error == 0)
4728 error = copyout(sfp, uap->buf, sizeof(*sfp));
4729 free(sfp, M_STATFS);
4730 return (error);
4731 }
4732
4733 int
kern_fhstatfs(struct thread * td,fhandle_t fh,struct statfs * buf)4734 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4735 {
4736 struct mount *mp;
4737 struct vnode *vp;
4738 int error;
4739
4740 error = priv_check(td, PRIV_VFS_FHSTATFS);
4741 if (error != 0)
4742 return (error);
4743 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4744 return (ESTALE);
4745 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4746 if (error != 0) {
4747 vfs_unbusy(mp);
4748 return (error);
4749 }
4750 vput(vp);
4751 error = prison_canseemount(td->td_ucred, mp);
4752 if (error != 0)
4753 goto out;
4754 #ifdef MAC
4755 error = mac_mount_check_stat(td->td_ucred, mp);
4756 if (error != 0)
4757 goto out;
4758 #endif
4759 error = VFS_STATFS(mp, buf);
4760 out:
4761 vfs_unbusy(mp);
4762 return (error);
4763 }
4764
4765 /*
4766 * Unlike madvise(2), we do not make a best effort to remember every
4767 * possible caching hint. Instead, we remember the last setting with
4768 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4769 * region of any current setting.
4770 */
4771 int
kern_posix_fadvise(struct thread * td,int fd,off_t offset,off_t len,int advice)4772 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4773 int advice)
4774 {
4775 struct fadvise_info *fa, *new;
4776 struct file *fp;
4777 struct vnode *vp;
4778 off_t end;
4779 int error;
4780
4781 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4782 return (EINVAL);
4783 AUDIT_ARG_VALUE(advice);
4784 switch (advice) {
4785 case POSIX_FADV_SEQUENTIAL:
4786 case POSIX_FADV_RANDOM:
4787 case POSIX_FADV_NOREUSE:
4788 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4789 break;
4790 case POSIX_FADV_NORMAL:
4791 case POSIX_FADV_WILLNEED:
4792 case POSIX_FADV_DONTNEED:
4793 new = NULL;
4794 break;
4795 default:
4796 return (EINVAL);
4797 }
4798 /* XXX: CAP_POSIX_FADVISE? */
4799 AUDIT_ARG_FD(fd);
4800 error = fget(td, fd, &cap_no_rights, &fp);
4801 if (error != 0)
4802 goto out;
4803 AUDIT_ARG_FILE(td->td_proc, fp);
4804 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4805 error = ESPIPE;
4806 goto out;
4807 }
4808 if (fp->f_type != DTYPE_VNODE) {
4809 error = ENODEV;
4810 goto out;
4811 }
4812 vp = fp->f_vnode;
4813 if (vp->v_type != VREG) {
4814 error = ENODEV;
4815 goto out;
4816 }
4817 if (len == 0)
4818 end = OFF_MAX;
4819 else
4820 end = offset + len - 1;
4821 switch (advice) {
4822 case POSIX_FADV_SEQUENTIAL:
4823 case POSIX_FADV_RANDOM:
4824 case POSIX_FADV_NOREUSE:
4825 /*
4826 * Try to merge any existing non-standard region with
4827 * this new region if possible, otherwise create a new
4828 * non-standard region for this request.
4829 */
4830 mtx_pool_lock(mtxpool_sleep, fp);
4831 fa = fp->f_advice;
4832 if (fa != NULL && fa->fa_advice == advice &&
4833 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4834 (end != OFF_MAX && fa->fa_start == end + 1) ||
4835 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4836 if (offset < fa->fa_start)
4837 fa->fa_start = offset;
4838 if (end > fa->fa_end)
4839 fa->fa_end = end;
4840 } else {
4841 new->fa_advice = advice;
4842 new->fa_start = offset;
4843 new->fa_end = end;
4844 fp->f_advice = new;
4845 new = fa;
4846 }
4847 mtx_pool_unlock(mtxpool_sleep, fp);
4848 break;
4849 case POSIX_FADV_NORMAL:
4850 /*
4851 * If a the "normal" region overlaps with an existing
4852 * non-standard region, trim or remove the
4853 * non-standard region.
4854 */
4855 mtx_pool_lock(mtxpool_sleep, fp);
4856 fa = fp->f_advice;
4857 if (fa != NULL) {
4858 if (offset <= fa->fa_start && end >= fa->fa_end) {
4859 new = fa;
4860 fp->f_advice = NULL;
4861 } else if (offset <= fa->fa_start &&
4862 end >= fa->fa_start)
4863 fa->fa_start = end + 1;
4864 else if (offset <= fa->fa_end && end >= fa->fa_end)
4865 fa->fa_end = offset - 1;
4866 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4867 /*
4868 * If the "normal" region is a middle
4869 * portion of the existing
4870 * non-standard region, just remove
4871 * the whole thing rather than picking
4872 * one side or the other to
4873 * preserve.
4874 */
4875 new = fa;
4876 fp->f_advice = NULL;
4877 }
4878 }
4879 mtx_pool_unlock(mtxpool_sleep, fp);
4880 break;
4881 case POSIX_FADV_WILLNEED:
4882 case POSIX_FADV_DONTNEED:
4883 error = VOP_ADVISE(vp, offset, end, advice);
4884 break;
4885 }
4886 out:
4887 if (fp != NULL)
4888 fdrop(fp, td);
4889 free(new, M_FADVISE);
4890 return (error);
4891 }
4892
4893 int
sys_posix_fadvise(struct thread * td,struct posix_fadvise_args * uap)4894 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4895 {
4896 int error;
4897
4898 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4899 uap->advice);
4900 return (kern_posix_error(td, error));
4901 }
4902
4903 int
kern_copy_file_range(struct thread * td,int infd,off_t * inoffp,int outfd,off_t * outoffp,size_t len,unsigned int flags)4904 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4905 off_t *outoffp, size_t len, unsigned int flags)
4906 {
4907 struct file *infp, *outfp;
4908 struct vnode *invp, *outvp;
4909 int error;
4910 size_t retlen;
4911 void *rl_rcookie, *rl_wcookie;
4912 off_t savinoff, savoutoff;
4913
4914 infp = outfp = NULL;
4915 rl_rcookie = rl_wcookie = NULL;
4916 savinoff = -1;
4917 error = 0;
4918 retlen = 0;
4919
4920 if (flags != 0) {
4921 error = EINVAL;
4922 goto out;
4923 }
4924 if (len > SSIZE_MAX)
4925 /*
4926 * Although the len argument is size_t, the return argument
4927 * is ssize_t (which is signed). Therefore a size that won't
4928 * fit in ssize_t can't be returned.
4929 */
4930 len = SSIZE_MAX;
4931
4932 /* Get the file structures for the file descriptors. */
4933 error = fget_read(td, infd,
4934 inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp);
4935 if (error != 0)
4936 goto out;
4937 if (infp->f_ops == &badfileops) {
4938 error = EBADF;
4939 goto out;
4940 }
4941 if (infp->f_vnode == NULL) {
4942 error = EINVAL;
4943 goto out;
4944 }
4945 error = fget_write(td, outfd,
4946 outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp);
4947 if (error != 0)
4948 goto out;
4949 if (outfp->f_ops == &badfileops) {
4950 error = EBADF;
4951 goto out;
4952 }
4953 if (outfp->f_vnode == NULL) {
4954 error = EINVAL;
4955 goto out;
4956 }
4957
4958 /* Set the offset pointers to the correct place. */
4959 if (inoffp == NULL)
4960 inoffp = &infp->f_offset;
4961 if (outoffp == NULL)
4962 outoffp = &outfp->f_offset;
4963 savinoff = *inoffp;
4964 savoutoff = *outoffp;
4965
4966 invp = infp->f_vnode;
4967 outvp = outfp->f_vnode;
4968 /* Sanity check the f_flag bits. */
4969 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4970 (infp->f_flag & FREAD) == 0) {
4971 error = EBADF;
4972 goto out;
4973 }
4974
4975 /* If len == 0, just return 0. */
4976 if (len == 0)
4977 goto out;
4978
4979 /*
4980 * If infp and outfp refer to the same file, the byte ranges cannot
4981 * overlap.
4982 */
4983 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4984 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4985 savinoff))) {
4986 error = EINVAL;
4987 goto out;
4988 }
4989
4990 /* Range lock the byte ranges for both invp and outvp. */
4991 for (;;) {
4992 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4993 len);
4994 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4995 len);
4996 if (rl_rcookie != NULL)
4997 break;
4998 vn_rangelock_unlock(outvp, rl_wcookie);
4999 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
5000 vn_rangelock_unlock(invp, rl_rcookie);
5001 }
5002
5003 retlen = len;
5004 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
5005 flags, infp->f_cred, outfp->f_cred, td);
5006 out:
5007 if (rl_rcookie != NULL)
5008 vn_rangelock_unlock(invp, rl_rcookie);
5009 if (rl_wcookie != NULL)
5010 vn_rangelock_unlock(outvp, rl_wcookie);
5011 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
5012 *inoffp = savinoff;
5013 *outoffp = savoutoff;
5014 }
5015 if (outfp != NULL)
5016 fdrop(outfp, td);
5017 if (infp != NULL)
5018 fdrop(infp, td);
5019 td->td_retval[0] = retlen;
5020 return (error);
5021 }
5022
5023 int
sys_copy_file_range(struct thread * td,struct copy_file_range_args * uap)5024 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
5025 {
5026 off_t inoff, outoff, *inoffp, *outoffp;
5027 int error;
5028
5029 inoffp = outoffp = NULL;
5030 if (uap->inoffp != NULL) {
5031 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5032 if (error != 0)
5033 return (error);
5034 inoffp = &inoff;
5035 }
5036 if (uap->outoffp != NULL) {
5037 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5038 if (error != 0)
5039 return (error);
5040 outoffp = &outoff;
5041 }
5042 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5043 outoffp, uap->len, uap->flags);
5044 if (error == 0 && uap->inoffp != NULL)
5045 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5046 if (error == 0 && uap->outoffp != NULL)
5047 error = copyout(outoffp, uap->outoffp, sizeof(off_t));
5048 return (error);
5049 }
5050