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