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