1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 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
37 #include <sys/cdefs.h>
38 #include "opt_capsicum.h"
39 #include "opt_ktrace.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/kernel.h>
45 #include <sys/capsicum.h>
46 #include <sys/fcntl.h>
47 #include <sys/jail.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/filedesc.h>
54 #include <sys/proc.h>
55 #include <sys/sdt.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
58 #ifdef KTRACE
59 #include <sys/ktrace.h>
60 #endif
61 #ifdef INVARIANTS
62 #include <machine/_inttypes.h>
63 #endif
64
65 #include <security/audit/audit.h>
66 #include <security/mac/mac_framework.h>
67
68 #include <vm/uma.h>
69
70 #ifdef INVARIANTS
71 static void NDVALIDATE_impl(struct nameidata *, int);
72 #define NDVALIDATE(ndp) NDVALIDATE_impl(ndp, __LINE__)
73 #else
74 #define NDVALIDATE(ndp)
75 #endif
76
77 /*
78 * Prepare namei() to restart. Reset components to its original state and set
79 * ISRESTARTED flag which signals the underlying lookup code to change the root
80 * from ABI root to actual root and prevents a further restarts.
81 */
82 #define NDRESTART(ndp) do { \
83 NDREINIT_DBG(ndp); \
84 ndp->ni_resflags = 0; \
85 ndp->ni_cnd.cn_flags &= ~NAMEI_INTERNAL_FLAGS; \
86 ndp->ni_cnd.cn_flags |= ISRESTARTED; \
87 } while (0)
88
89 SDT_PROVIDER_DEFINE(vfs);
90 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *",
91 "unsigned long", "bool");
92 SDT_PROBE_DEFINE4(vfs, namei, lookup, return, "int", "struct vnode *", "bool",
93 "struct nameidata");
94
95 /* Allocation zone for namei. */
96 uma_zone_t namei_zone;
97
98 /* Placeholder vnode for mp traversal. */
99 static struct vnode *vp_crossmp;
100
101 static int
crossmp_vop_islocked(struct vop_islocked_args * ap)102 crossmp_vop_islocked(struct vop_islocked_args *ap)
103 {
104
105 return (LK_SHARED);
106 }
107
108 static int
crossmp_vop_lock1(struct vop_lock1_args * ap)109 crossmp_vop_lock1(struct vop_lock1_args *ap)
110 {
111 struct vnode *vp;
112 struct lock *lk __diagused;
113 int flags;
114
115 vp = ap->a_vp;
116 lk = vp->v_vnlock;
117 flags = ap->a_flags;
118
119 KASSERT((flags & (LK_SHARED | LK_NOWAIT)) == (LK_SHARED | LK_NOWAIT),
120 ("%s: invalid lock request 0x%x for crossmp", __func__, flags));
121
122 if ((flags & LK_INTERLOCK) != 0)
123 VI_UNLOCK(vp);
124 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, ap->a_line);
125 return (0);
126 }
127
128 static int
crossmp_vop_unlock(struct vop_unlock_args * ap)129 crossmp_vop_unlock(struct vop_unlock_args *ap)
130 {
131 struct vnode *vp;
132 struct lock *lk __diagused;
133
134 vp = ap->a_vp;
135 lk = vp->v_vnlock;
136
137 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
138 LOCK_LINE);
139 return (0);
140 }
141
142 static struct vop_vector crossmp_vnodeops = {
143 .vop_default = &default_vnodeops,
144 .vop_islocked = crossmp_vop_islocked,
145 .vop_lock1 = crossmp_vop_lock1,
146 .vop_unlock = crossmp_vop_unlock,
147 };
148 /*
149 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
150 * gets allocated early. See nameiinit for the direct call below.
151 */
152
153 struct nameicap_tracker {
154 struct vnode *dp;
155 TAILQ_ENTRY(nameicap_tracker) nm_link;
156 };
157
158 /* Zone for cap mode tracker elements used for dotdot capability checks. */
159 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot");
160
161 static void
nameiinit(void * dummy __unused)162 nameiinit(void *dummy __unused)
163 {
164
165 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
166 UMA_ALIGN_PTR, 0);
167 vfs_vector_op_register(&crossmp_vnodeops);
168 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
169 vp_crossmp->v_state = VSTATE_CONSTRUCTED;
170 vp_crossmp->v_irflag |= VIRF_CROSSMP;
171 }
172 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
173
174 static int lookup_cap_dotdot = 1;
175 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
176 &lookup_cap_dotdot, 0,
177 "enables \"..\" components in path lookup in capability mode");
178 static int lookup_cap_dotdot_nonlocal = 1;
179 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
180 &lookup_cap_dotdot_nonlocal, 0,
181 "enables \"..\" components in path lookup in capability mode "
182 "on non-local mount");
183
184 static void
nameicap_tracker_add(struct nameidata * ndp,struct vnode * dp)185 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
186 {
187 struct nameicap_tracker *nt;
188
189 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
190 return;
191 nt = TAILQ_LAST(&ndp->ni_cap_tracker, nameicap_tracker_head);
192 if (nt != NULL && nt->dp == dp)
193 return;
194 nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK);
195 vhold(dp);
196 nt->dp = dp;
197 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
198 }
199
200 static void
nameicap_cleanup_from(struct nameidata * ndp,struct nameicap_tracker * first)201 nameicap_cleanup_from(struct nameidata *ndp, struct nameicap_tracker *first)
202 {
203 struct nameicap_tracker *nt, *nt1;
204
205 nt = first;
206 TAILQ_FOREACH_FROM_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
207 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
208 vdrop(nt->dp);
209 free(nt, M_NAMEITRACKER);
210 }
211 }
212
213 static void
nameicap_cleanup(struct nameidata * ndp)214 nameicap_cleanup(struct nameidata *ndp)
215 {
216 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
217 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
218 nameicap_cleanup_from(ndp, NULL);
219 }
220
221 /*
222 * For dotdot lookups in capability mode, only allow the component
223 * lookup to succeed if the resulting directory was already traversed
224 * during the operation. This catches situations where already
225 * traversed directory is moved to different parent, and then we walk
226 * over it with dotdots.
227 *
228 * Also allow to force failure of dotdot lookups for non-local
229 * filesystems, where external agents might assist local lookups to
230 * escape the compartment.
231 */
232 static int
nameicap_check_dotdot(struct nameidata * ndp,struct vnode * dp)233 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
234 {
235 struct nameicap_tracker *nt;
236 struct mount *mp;
237
238 if (dp == NULL || dp->v_type != VDIR || (ndp->ni_lcf &
239 NI_LCF_STRICTREL) == 0)
240 return (0);
241 if (__predict_false((ndp->ni_lcf & (NI_LCF_STRICTREL_KTR |
242 NI_LCF_CAP_DOTDOT_KTR)) == NI_LCF_STRICTREL_KTR))
243 NI_CAP_VIOLATION(ndp, ndp->ni_cnd.cn_pnbuf);
244 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0)
245 return (ENOTCAPABLE);
246 mp = dp->v_mount;
247 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
248 (mp->mnt_flag & MNT_LOCAL) == 0)
249 goto capfail;
250 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
251 nm_link) {
252 if (dp == nt->dp) {
253 nt = TAILQ_NEXT(nt, nm_link);
254 if (nt != NULL)
255 nameicap_cleanup_from(ndp, nt);
256 return (0);
257 }
258 }
259
260 capfail:
261 if (__predict_false((ndp->ni_lcf & NI_LCF_STRICTREL_KTR) != 0))
262 NI_CAP_VIOLATION(ndp, ndp->ni_cnd.cn_pnbuf);
263 return (ENOTCAPABLE);
264 }
265
266 static void
namei_cleanup_cnp(struct componentname * cnp)267 namei_cleanup_cnp(struct componentname *cnp)
268 {
269
270 uma_zfree(namei_zone, cnp->cn_pnbuf);
271 cnp->cn_pnbuf = NULL;
272 cnp->cn_nameptr = NULL;
273 }
274
275 static int
namei_handle_root(struct nameidata * ndp,struct vnode ** dpp)276 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
277 {
278 struct componentname *cnp;
279
280 cnp = &ndp->ni_cnd;
281 if (__predict_false((ndp->ni_lcf & (NI_LCF_STRICTREL |
282 NI_LCF_STRICTREL_KTR)) != 0)) {
283 if ((ndp->ni_lcf & NI_LCF_STRICTREL_KTR) != 0)
284 NI_CAP_VIOLATION(ndp, cnp->cn_pnbuf);
285 if ((ndp->ni_lcf & NI_LCF_STRICTREL) != 0)
286 return (ENOTCAPABLE);
287 }
288 while (*(cnp->cn_nameptr) == '/') {
289 cnp->cn_nameptr++;
290 ndp->ni_pathlen--;
291 }
292 *dpp = ndp->ni_rootdir;
293 vrefact(*dpp);
294 return (0);
295 }
296
297 static int
namei_setup(struct nameidata * ndp,struct vnode ** dpp,struct pwd ** pwdp)298 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
299 {
300 struct componentname *cnp;
301 struct thread *td;
302 struct pwd *pwd;
303 int error;
304 bool startdir_used;
305
306 cnp = &ndp->ni_cnd;
307 td = curthread;
308
309 startdir_used = false;
310 *pwdp = NULL;
311 *dpp = NULL;
312
313 #ifdef CAPABILITY_MODE
314 /*
315 * In capability mode, lookups must be restricted to happen in
316 * the subtree with the root specified by the file descriptor:
317 * - The root must be real file descriptor, not the pseudo-descriptor
318 * AT_FDCWD.
319 * - The passed path must be relative and not absolute.
320 * - If lookup_cap_dotdot is disabled, path must not contain the
321 * '..' components.
322 * - If lookup_cap_dotdot is enabled, we verify that all '..'
323 * components lookups result in the directories which were
324 * previously walked by us, which prevents an escape from
325 * the relative root.
326 */
327 if ((cnp->cn_flags & NOCAPCHECK) == 0) {
328 if (CAP_TRACING(td)) {
329 ndp->ni_lcf |= NI_LCF_STRICTREL_KTR;
330 if (ndp->ni_dirfd == AT_FDCWD)
331 NI_CAP_VIOLATION(ndp, "AT_FDCWD");
332 }
333 if (IN_CAPABILITY_MODE(td)) {
334 ndp->ni_lcf |= NI_LCF_STRICTREL;
335 ndp->ni_resflags |= NIRES_STRICTREL;
336 if (ndp->ni_dirfd == AT_FDCWD)
337 return (ECAPMODE);
338 }
339 }
340 #endif
341 error = 0;
342
343 /*
344 * Get starting point for the translation.
345 */
346 pwd = pwd_hold(td);
347 /*
348 * The reference on ni_rootdir is acquired in the block below to avoid
349 * back-to-back atomics for absolute lookups.
350 */
351 namei_setup_rootdir(ndp, cnp, pwd);
352 ndp->ni_topdir = pwd->pwd_jdir;
353
354 if (cnp->cn_pnbuf[0] == '/') {
355 ndp->ni_resflags |= NIRES_ABS;
356 error = namei_handle_root(ndp, dpp);
357 } else {
358 if (ndp->ni_startdir != NULL) {
359 *dpp = ndp->ni_startdir;
360 startdir_used = true;
361 } else if (ndp->ni_dirfd == AT_FDCWD) {
362 *dpp = pwd->pwd_cdir;
363 vrefact(*dpp);
364 } else {
365 if (cnp->cn_flags & AUDITVNODE1)
366 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
367 if (cnp->cn_flags & AUDITVNODE2)
368 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
369
370 error = fgetvp_lookup(ndp, dpp);
371 }
372 if (error == 0 && (*dpp)->v_type != VDIR &&
373 (cnp->cn_pnbuf[0] != '\0' ||
374 (cnp->cn_flags & EMPTYPATH) == 0))
375 error = ENOTDIR;
376 }
377 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
378 if (cnp->cn_pnbuf[0] == '/') {
379 error = ENOTCAPABLE;
380 } else if ((ndp->ni_lcf & NI_LCF_STRICTREL) == 0) {
381 ndp->ni_lcf |= NI_LCF_STRICTREL |
382 NI_LCF_CAP_DOTDOT;
383 }
384 }
385
386 /*
387 * If we are auditing the kernel pathname, save the user pathname.
388 */
389 if (AUDITING_TD(td)) {
390 if (cnp->cn_flags & AUDITVNODE1)
391 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
392 if (cnp->cn_flags & AUDITVNODE2)
393 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
394 }
395 if (ndp->ni_startdir != NULL && !startdir_used)
396 vrele(ndp->ni_startdir);
397 if (error != 0) {
398 if (*dpp != NULL)
399 vrele(*dpp);
400 pwd_drop(pwd);
401 return (error);
402 }
403 if (lookup_cap_dotdot != 0) {
404 if ((ndp->ni_lcf & NI_LCF_STRICTREL_KTR) != 0)
405 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT_KTR;
406 if ((ndp->ni_lcf & NI_LCF_STRICTREL) != 0)
407 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
408 }
409 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
410 cnp->cn_flags, false);
411 *pwdp = pwd;
412 return (0);
413 }
414
415 static int
namei_getpath(struct nameidata * ndp)416 namei_getpath(struct nameidata *ndp)
417 {
418 struct componentname *cnp;
419 int error;
420
421 cnp = &ndp->ni_cnd;
422
423 /*
424 * Get a buffer for the name to be translated, and copy the
425 * name into the buffer.
426 */
427 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
428 if (ndp->ni_segflg == UIO_SYSSPACE) {
429 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
430 &ndp->ni_pathlen);
431 } else {
432 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
433 &ndp->ni_pathlen);
434 }
435
436 return (error);
437 }
438
439 static int
namei_emptypath(struct nameidata * ndp)440 namei_emptypath(struct nameidata *ndp)
441 {
442 struct componentname *cnp;
443 struct pwd *pwd;
444 struct vnode *dp;
445 int error;
446
447 cnp = &ndp->ni_cnd;
448 MPASS(*cnp->cn_pnbuf == '\0');
449 MPASS((cnp->cn_flags & EMPTYPATH) != 0);
450 MPASS((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) == 0);
451
452 ndp->ni_resflags |= NIRES_EMPTYPATH;
453 error = namei_setup(ndp, &dp, &pwd);
454 if (error != 0) {
455 goto errout;
456 }
457
458 /*
459 * Usecount on dp already provided by namei_setup.
460 */
461 ndp->ni_vp = dp;
462 pwd_drop(pwd);
463 NDVALIDATE(ndp);
464 if ((cnp->cn_flags & LOCKLEAF) != 0) {
465 VOP_LOCK(dp, (cnp->cn_flags & LOCKSHARED) != 0 ?
466 LK_SHARED : LK_EXCLUSIVE);
467 if (VN_IS_DOOMED(dp)) {
468 vput(dp);
469 error = ENOENT;
470 goto errout;
471 }
472 }
473 SDT_PROBE4(vfs, namei, lookup, return, 0, ndp->ni_vp, false, ndp);
474 return (0);
475
476 errout:
477 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
478 namei_cleanup_cnp(cnp);
479 return (error);
480 }
481
482 static int __noinline
namei_follow_link(struct nameidata * ndp)483 namei_follow_link(struct nameidata *ndp)
484 {
485 char *cp;
486 struct iovec aiov;
487 struct uio auio;
488 struct componentname *cnp;
489 struct thread *td;
490 int error, linklen;
491
492 error = 0;
493 cnp = &ndp->ni_cnd;
494 td = curthread;
495
496 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
497 error = ELOOP;
498 goto out;
499 }
500 #ifdef MAC
501 if ((cnp->cn_flags & NOMACCHECK) == 0) {
502 error = mac_vnode_check_readlink(td->td_ucred, ndp->ni_vp);
503 if (error != 0)
504 goto out;
505 }
506 #endif
507 if (ndp->ni_pathlen > 1)
508 cp = uma_zalloc(namei_zone, M_WAITOK);
509 else
510 cp = cnp->cn_pnbuf;
511 aiov.iov_base = cp;
512 aiov.iov_len = MAXPATHLEN;
513 auio.uio_iov = &aiov;
514 auio.uio_iovcnt = 1;
515 auio.uio_offset = 0;
516 auio.uio_rw = UIO_READ;
517 auio.uio_segflg = UIO_SYSSPACE;
518 auio.uio_td = td;
519 auio.uio_resid = MAXPATHLEN;
520 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
521 if (error != 0) {
522 if (ndp->ni_pathlen > 1)
523 uma_zfree(namei_zone, cp);
524 goto out;
525 }
526 linklen = MAXPATHLEN - auio.uio_resid;
527 if (linklen == 0) {
528 if (ndp->ni_pathlen > 1)
529 uma_zfree(namei_zone, cp);
530 error = ENOENT;
531 goto out;
532 }
533 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
534 if (ndp->ni_pathlen > 1)
535 uma_zfree(namei_zone, cp);
536 error = ENAMETOOLONG;
537 goto out;
538 }
539 if (ndp->ni_pathlen > 1) {
540 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
541 uma_zfree(namei_zone, cnp->cn_pnbuf);
542 cnp->cn_pnbuf = cp;
543 } else
544 cnp->cn_pnbuf[linklen] = '\0';
545 ndp->ni_pathlen += linklen;
546 out:
547 return (error);
548 }
549
550 /*
551 * Convert a pathname into a pointer to a locked vnode.
552 *
553 * The FOLLOW flag is set when symbolic links are to be followed
554 * when they occur at the end of the name translation process.
555 * Symbolic links are always followed for all other pathname
556 * components other than the last.
557 *
558 * The segflg defines whether the name is to be copied from user
559 * space or kernel space.
560 *
561 * Overall outline of namei:
562 *
563 * copy in name
564 * get starting directory
565 * while (!done && !error) {
566 * call lookup to search path.
567 * if symbolic link, massage name in buffer and continue
568 * }
569 */
570 int
namei(struct nameidata * ndp)571 namei(struct nameidata *ndp)
572 {
573 struct vnode *dp; /* the directory we are searching */
574 struct componentname *cnp;
575 struct thread *td;
576 struct pwd *pwd;
577 int error;
578 enum cache_fpl_status status;
579
580 cnp = &ndp->ni_cnd;
581 td = curthread;
582 #ifdef INVARIANTS
583 KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0,
584 ("%s: repeated call to namei without NDREINIT", __func__));
585 KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED,
586 ("%s: bad debugflags %d", __func__, ndp->ni_debugflags));
587 ndp->ni_debugflags |= NAMEI_DBG_CALLED;
588 if (ndp->ni_startdir != NULL)
589 ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR;
590 if (cnp->cn_flags & FAILIFEXISTS) {
591 KASSERT(cnp->cn_nameiop == CREATE,
592 ("%s: FAILIFEXISTS passed for op %d", __func__, cnp->cn_nameiop));
593 /*
594 * The limitation below is to restrict hairy corner cases.
595 */
596 KASSERT((cnp->cn_flags & (LOCKPARENT | LOCKLEAF)) == LOCKPARENT,
597 ("%s: FAILIFEXISTS must be passed with LOCKPARENT and without LOCKLEAF",
598 __func__));
599 }
600 #endif
601 ndp->ni_cnd.cn_cred = td->td_ucred;
602 KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n",
603 __func__, ndp->ni_resflags));
604 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
605 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
606 ("namei: unexpected flags: %" PRIx64 "\n",
607 cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
608 if (cnp->cn_flags & NOCACHE)
609 KASSERT(cnp->cn_nameiop != LOOKUP,
610 ("%s: NOCACHE passed with LOOKUP", __func__));
611 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
612 ndp->ni_startdir->v_type == VBAD);
613
614 restart:
615 ndp->ni_lcf = 0;
616 ndp->ni_loopcnt = 0;
617 ndp->ni_vp = NULL;
618
619 error = namei_getpath(ndp);
620 if (__predict_false(error != 0)) {
621 namei_cleanup_cnp(cnp);
622 SDT_PROBE4(vfs, namei, lookup, return, error, NULL,
623 false, ndp);
624 return (error);
625 }
626
627 cnp->cn_nameptr = cnp->cn_pnbuf;
628
629 #ifdef KTRACE
630 if (KTRPOINT(td, KTR_NAMEI)) {
631 ktrnamei(cnp->cn_pnbuf);
632 }
633 #endif
634 TSNAMEI(curthread->td_proc->p_pid, cnp->cn_pnbuf);
635
636 /*
637 * First try looking up the target without locking any vnodes.
638 *
639 * We may need to start from scratch or pick up where it left off.
640 */
641 error = cache_fplookup(ndp, &status, &pwd);
642 switch (status) {
643 case CACHE_FPL_STATUS_UNSET:
644 __assert_unreachable();
645 break;
646 case CACHE_FPL_STATUS_HANDLED:
647 if (error == 0)
648 NDVALIDATE(ndp);
649 else if (__predict_false(pwd->pwd_adir != pwd->pwd_rdir &&
650 (cnp->cn_flags & ISRESTARTED) == 0)) {
651 namei_cleanup_cnp(cnp);
652 NDRESTART(ndp);
653 goto restart;
654 }
655 return (error);
656 case CACHE_FPL_STATUS_PARTIAL:
657 TAILQ_INIT(&ndp->ni_cap_tracker);
658 dp = ndp->ni_startdir;
659 break;
660 case CACHE_FPL_STATUS_DESTROYED:
661 ndp->ni_loopcnt = 0;
662 error = namei_getpath(ndp);
663 if (__predict_false(error != 0)) {
664 namei_cleanup_cnp(cnp);
665 return (error);
666 }
667 cnp->cn_nameptr = cnp->cn_pnbuf;
668 /* FALLTHROUGH */
669 case CACHE_FPL_STATUS_ABORTED:
670 TAILQ_INIT(&ndp->ni_cap_tracker);
671 MPASS(ndp->ni_lcf == 0);
672 if (*cnp->cn_pnbuf == '\0') {
673 if ((cnp->cn_flags & EMPTYPATH) != 0) {
674 return (namei_emptypath(ndp));
675 }
676 namei_cleanup_cnp(cnp);
677 SDT_PROBE4(vfs, namei, lookup, return, ENOENT, NULL,
678 false, ndp);
679 return (ENOENT);
680 }
681 error = namei_setup(ndp, &dp, &pwd);
682 if (error != 0) {
683 namei_cleanup_cnp(cnp);
684 return (error);
685 }
686 break;
687 }
688
689 /*
690 * Locked lookup.
691 */
692 for (;;) {
693 ndp->ni_startdir = dp;
694 error = vfs_lookup(ndp);
695 if (error != 0) {
696 if (__predict_false(pwd->pwd_adir != pwd->pwd_rdir &&
697 error == ENOENT &&
698 (cnp->cn_flags & ISRESTARTED) == 0)) {
699 nameicap_cleanup(ndp);
700 pwd_drop(pwd);
701 namei_cleanup_cnp(cnp);
702 NDRESTART(ndp);
703 goto restart;
704 } else
705 goto out;
706 }
707
708 /*
709 * If not a symbolic link, we're done.
710 */
711 if ((cnp->cn_flags & ISSYMLINK) == 0) {
712 SDT_PROBE4(vfs, namei, lookup, return, error,
713 ndp->ni_vp, false, ndp);
714 nameicap_cleanup(ndp);
715 pwd_drop(pwd);
716 NDVALIDATE(ndp);
717 return (0);
718 }
719 error = namei_follow_link(ndp);
720 if (error != 0)
721 break;
722 vput(ndp->ni_vp);
723 dp = ndp->ni_dvp;
724 /*
725 * Check if root directory should replace current directory.
726 */
727 cnp->cn_nameptr = cnp->cn_pnbuf;
728 if (*(cnp->cn_nameptr) == '/') {
729 /*
730 * Reset the lookup to start from the real root without
731 * origin path name reloading.
732 */
733 if (__predict_false(ndp->ni_rootdir != pwd->pwd_rdir)) {
734 cnp->cn_flags |= ISRESTARTED;
735 ndp->ni_rootdir = pwd->pwd_rdir;
736 }
737 vrele(dp);
738 error = namei_handle_root(ndp, &dp);
739 if (error != 0)
740 goto out;
741 }
742 }
743 vput(ndp->ni_vp);
744 ndp->ni_vp = NULL;
745 vrele(ndp->ni_dvp);
746 out:
747 MPASS(error != 0);
748 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
749 namei_cleanup_cnp(cnp);
750 nameicap_cleanup(ndp);
751 pwd_drop(pwd);
752 return (error);
753 }
754
755 static int
enforce_lkflags(struct mount * mp,int lkflags)756 enforce_lkflags(struct mount *mp, int lkflags)
757 {
758
759 if (mp == NULL || ((lkflags & LK_SHARED) &&
760 !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
761 lkflags &= ~LK_SHARED;
762 lkflags |= LK_EXCLUSIVE;
763 }
764 lkflags |= LK_NODDLKTREAT;
765 return (lkflags);
766 }
767
768 static __inline int
needs_exclusive_leaf(struct mount * mp,int flags)769 needs_exclusive_leaf(struct mount *mp, int flags)
770 {
771
772 /*
773 * Intermediate nodes can use shared locks, we only need to
774 * force an exclusive lock for leaf nodes.
775 */
776 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
777 return (0);
778
779 /* Always use exclusive locks if LOCKSHARED isn't set. */
780 if (!(flags & LOCKSHARED))
781 return (1);
782
783 /*
784 * For lookups during open(), if the mount point supports
785 * extended shared operations, then use a shared lock for the
786 * leaf node, otherwise use an exclusive lock.
787 */
788 if ((flags & ISOPEN) != 0)
789 return (!MNT_EXTENDED_SHARED(mp));
790
791 /*
792 * Lookup requests outside of open() that specify LOCKSHARED
793 * only need a shared lock on the leaf vnode.
794 */
795 return (0);
796 }
797
798 /*
799 * Various filesystems expect to be able to copy a name component with length
800 * bounded by NAME_MAX into a directory entry buffer of size MAXNAMLEN. Make
801 * sure that these are the same size.
802 */
803 _Static_assert(MAXNAMLEN == NAME_MAX,
804 "MAXNAMLEN and NAME_MAX have different values");
805
806 static int __noinline
vfs_lookup_degenerate(struct nameidata * ndp,struct vnode * dp,int wantparent)807 vfs_lookup_degenerate(struct nameidata *ndp, struct vnode *dp, int wantparent)
808 {
809 struct componentname *cnp;
810 struct mount *mp;
811 int error;
812
813 cnp = &ndp->ni_cnd;
814
815 cnp->cn_flags |= ISLASTCN;
816
817 mp = atomic_load_ptr(&dp->v_mount);
818 if (needs_exclusive_leaf(mp, cnp->cn_flags)) {
819 cnp->cn_lkflags &= ~LK_SHARED;
820 cnp->cn_lkflags |= LK_EXCLUSIVE;
821 }
822
823 vn_lock(dp, enforce_lkflags(mp, cnp->cn_lkflags | LK_RETRY));
824
825 if (dp->v_type != VDIR) {
826 error = ENOTDIR;
827 goto bad;
828 }
829 if (cnp->cn_nameiop != LOOKUP) {
830 error = EISDIR;
831 goto bad;
832 }
833 if (wantparent) {
834 ndp->ni_dvp = dp;
835 VREF(dp);
836 }
837 ndp->ni_vp = dp;
838 cnp->cn_namelen = 0;
839
840 if (cnp->cn_flags & AUDITVNODE1)
841 AUDIT_ARG_VNODE1(dp);
842 else if (cnp->cn_flags & AUDITVNODE2)
843 AUDIT_ARG_VNODE2(dp);
844
845 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
846 VOP_UNLOCK(dp);
847 return (0);
848 bad:
849 VOP_UNLOCK(dp);
850 return (error);
851 }
852
853 /*
854 * FAILIFEXISTS handling.
855 *
856 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange
857 * behaviour of leaving the vnode unlocked if the target is the same
858 * vnode as the parent.
859 */
860 static int __noinline
vfs_lookup_failifexists(struct nameidata * ndp)861 vfs_lookup_failifexists(struct nameidata *ndp)
862 {
863 struct componentname *cnp __diagused;
864
865 cnp = &ndp->ni_cnd;
866
867 MPASS((cnp->cn_flags & ISSYMLINK) == 0);
868 if (ndp->ni_vp == ndp->ni_dvp)
869 vrele(ndp->ni_dvp);
870 else
871 vput(ndp->ni_dvp);
872 vrele(ndp->ni_vp);
873 ndp->ni_dvp = NULL;
874 ndp->ni_vp = NULL;
875 NDFREE_PNBUF(ndp);
876 return (EEXIST);
877 }
878
879 static int __noinline
vfs_lookup_cross_mount(struct nameidata * ndp)880 vfs_lookup_cross_mount(struct nameidata *ndp)
881 {
882 struct componentname *cnp;
883 struct mount *mp;
884 struct vnode *dp, *tdp;
885 int error, crosslkflags;
886 bool crosslock;
887
888 cnp = &ndp->ni_cnd;
889 dp = ndp->ni_vp;
890
891 /*
892 * The vnode has been mounted on, find the root of the mounted
893 * filesystem.
894 */
895 do {
896 mp = dp->v_mountedhere;
897 ASSERT_VOP_LOCKED(dp, __func__);
898 VNPASS((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0 && mp != NULL, dp);
899
900 crosslock = (dp->v_vflag & VV_CROSSLOCK) != 0;
901 crosslkflags = enforce_lkflags(mp, cnp->cn_lkflags);
902 if (__predict_false(crosslock)) {
903 /*
904 * We are going to be holding the vnode lock, which
905 * in this case is shared by the root vnode of the
906 * filesystem mounted at mp, across the call to
907 * VFS_ROOT(). Make the situation clear to the
908 * filesystem by passing LK_CANRECURSE if the
909 * lock is held exclusive, or by clearinng
910 * LK_NODDLKTREAT to allow recursion on the shared
911 * lock in the presence of an exclusive waiter.
912 */
913 if (VOP_ISLOCKED(dp) == LK_EXCLUSIVE) {
914 crosslkflags &= ~LK_SHARED;
915 crosslkflags |= LK_EXCLUSIVE | LK_CANRECURSE;
916 } else if ((crosslkflags & LK_EXCLUSIVE) != 0) {
917 error = vn_lock(dp, LK_UPGRADE);
918 if (error != 0) {
919 MPASS(error == ENOENT);
920 vrele(dp);
921 if (dp != ndp->ni_dvp)
922 vput(ndp->ni_dvp);
923 else
924 vrele(ndp->ni_dvp);
925 break;
926 }
927 if (dp->v_mountedhere != mp) {
928 /*
929 * Note that we rely on the
930 * VIRF_MOUNTPOINT loop condition to
931 * ensure we stop iterating if dp is
932 * no longer a mountpoint at all.
933 */
934 continue;
935 }
936 } else
937 crosslkflags &= ~LK_NODDLKTREAT;
938 }
939 if (vfs_busy(mp, 0) != 0)
940 continue;
941 if (__predict_true(!crosslock))
942 vput(dp);
943 if (dp != ndp->ni_dvp)
944 vput(ndp->ni_dvp);
945 else
946 vrele(ndp->ni_dvp);
947 vrefact(vp_crossmp);
948 ndp->ni_dvp = vp_crossmp;
949 error = VFS_ROOT(mp, crosslkflags, &tdp);
950 vfs_unbusy(mp);
951 if (__predict_false(crosslock))
952 vput(dp);
953 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
954 panic("vp_crossmp exclusively locked or reclaimed");
955 if (error != 0)
956 break;
957 ndp->ni_vp = dp = tdp;
958 } while ((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0);
959
960 return (error);
961 }
962
963 /*
964 * Search a pathname.
965 * This is a very central and rather complicated routine.
966 *
967 * The pathname is pointed to by cn_nameptr and is of length ni_pathlen.
968 * The starting directory is taken from ni_startdir. The pathname is
969 * descended until done, or a symbolic link is encountered. The cn_flags
970 * has ISLASTCN or'ed if the path is completed or ISSYMLINK or'ed if a
971 * symbolic link needing interpretation is encountered.
972 *
973 * The cn_nameiop is LOOKUP, CREATE, RENAME, or DELETE depending on
974 * whether the name is to be looked up, created, renamed, or deleted.
975 * When CREATE, RENAME, or DELETE is specified, information usable in
976 * creating, renaming, or deleting a directory entry may be calculated.
977 * If cn_flags has LOCKPARENT or'ed into it, the parent directory is returned
978 * locked. If it has WANTPARENT or'ed into it, the parent directory is
979 * returned unlocked. Otherwise the parent directory is not returned. If
980 * the target of the pathname exists and LOCKLEAF is or'ed into the cn_flags
981 * the target is returned locked, otherwise it is returned unlocked.
982 *
983 * Overall outline of lookup:
984 *
985 * handle degenerate case where name is null string
986 *
987 * dirloop:
988 * identify next component of name at ndp->ni_cnd.cn_nameptr
989 * handle .. special cases related to capabilities, chroot, jail
990 * if .. and crossing mount points and on mounted filesys, find parent
991 * call VOP_LOOKUP routine for next component name
992 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
993 * component vnode returned in ni_vp (if it exists), locked.
994 * if result vnode is mounted on and crossing mount points,
995 * find mounted on vnode
996 * if more components of name, do next level at dirloop
997 * if VOP_LOOKUP returns ERELOOKUP, repeat the same level at dirloop
998 * return the answer in ni_vp, locked if LOCKLEAF set
999 * if LOCKPARENT set, return locked parent in ni_dvp
1000 * if WANTPARENT set, return unlocked parent in ni_dvp
1001 */
1002 int
vfs_lookup(struct nameidata * ndp)1003 vfs_lookup(struct nameidata *ndp)
1004 {
1005 char *cp; /* pointer into pathname argument */
1006 char *prev_ni_next; /* saved ndp->ni_next */
1007 char *nulchar; /* location of '\0' in cn_pnbuf */
1008 char *lastchar; /* location of the last character */
1009 struct vnode *dp = NULL; /* the directory we are searching */
1010 struct vnode *tdp; /* saved dp */
1011 struct prison *pr;
1012 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
1013 int docache; /* == 0 do not cache last component */
1014 int wantparent; /* 1 => wantparent or lockparent flag */
1015 int rdonly; /* lookup read-only flag bit */
1016 int error = 0;
1017 int relookup = 0; /* do not consume the path component */
1018 struct componentname *cnp = &ndp->ni_cnd;
1019 int lkflags_save;
1020 int ni_dvp_unlocked;
1021
1022 /*
1023 * Setup: break out flag bits into variables.
1024 */
1025 ni_dvp_unlocked = 0;
1026 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1027 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
1028 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
1029 /*
1030 * When set to zero, docache causes the last component of the
1031 * pathname to be deleted from the cache and the full lookup
1032 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
1033 * filesystems need some pre-computed values that are made
1034 * during the full lookup, for instance UFS sets dp->i_offset.
1035 *
1036 * The docache variable is set to zero when requested by the
1037 * NOCACHE flag and for all modifying operations except CREATE.
1038 */
1039 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
1040 if (cnp->cn_nameiop == DELETE ||
1041 (wantparent && cnp->cn_nameiop != CREATE &&
1042 cnp->cn_nameiop != LOOKUP))
1043 docache = 0;
1044 rdonly = cnp->cn_flags & RDONLY;
1045 cnp->cn_flags &= ~ISSYMLINK;
1046 ndp->ni_dvp = NULL;
1047
1048 cnp->cn_lkflags = LK_SHARED;
1049 dp = ndp->ni_startdir;
1050 ndp->ni_startdir = NULLVP;
1051
1052 /*
1053 * Leading slashes, if any, are supposed to be skipped by the caller.
1054 */
1055 MPASS(cnp->cn_nameptr[0] != '/');
1056
1057 /*
1058 * Check for degenerate name (e.g. / or "") which is a way of talking
1059 * about a directory, e.g. like "/." or ".".
1060 */
1061 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
1062 error = vfs_lookup_degenerate(ndp, dp, wantparent);
1063 if (error == 0)
1064 goto success_right_lock;
1065 goto bad_unlocked;
1066 }
1067
1068 /*
1069 * Nul-out trailing slashes (e.g., "foo///" -> "foo").
1070 *
1071 * This must be done before VOP_LOOKUP() because some fs's don't know
1072 * about trailing slashes. Remember if there were trailing slashes to
1073 * handle symlinks, existing non-directories and non-existing files
1074 * that won't be directories specially later.
1075 */
1076 MPASS(ndp->ni_pathlen >= 2);
1077 lastchar = &cnp->cn_nameptr[ndp->ni_pathlen - 2];
1078 if (*lastchar == '/') {
1079 while (lastchar >= cnp->cn_pnbuf) {
1080 *lastchar = '\0';
1081 lastchar--;
1082 ndp->ni_pathlen--;
1083 if (*lastchar != '/') {
1084 break;
1085 }
1086 }
1087 cnp->cn_flags |= TRAILINGSLASH;
1088 }
1089
1090 /*
1091 * We use shared locks until we hit the parent of the last cn then
1092 * we adjust based on the requesting flags.
1093 */
1094 vn_lock(dp,
1095 enforce_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
1096
1097 dirloop:
1098 /*
1099 * Search a new directory.
1100 *
1101 * The last component of the filename is left accessible via
1102 * cnp->cn_nameptr. It has to be freed with a call to NDFREE*.
1103 *
1104 * Store / as a temporary sentinel so that we only have one character
1105 * to test for. Pathnames tend to be short so this should not be
1106 * resulting in cache misses.
1107 */
1108 nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1];
1109 KASSERT(*nulchar == '\0',
1110 ("%s: expected nul at %p; string [%s]\n", __func__, nulchar,
1111 cnp->cn_pnbuf));
1112 *nulchar = '/';
1113 for (cp = cnp->cn_nameptr; *cp != '/'; cp++) {
1114 KASSERT(*cp != '\0',
1115 ("%s: encountered unexpected nul; string [%s]\n", __func__,
1116 cnp->cn_nameptr));
1117 continue;
1118 }
1119 *nulchar = '\0';
1120 cnp->cn_namelen = cp - cnp->cn_nameptr;
1121 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
1122 error = ENAMETOOLONG;
1123 goto bad;
1124 }
1125 prev_ni_pathlen = ndp->ni_pathlen;
1126 ndp->ni_pathlen -= cnp->cn_namelen;
1127 KASSERT(ndp->ni_pathlen <= PATH_MAX,
1128 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
1129 prev_ni_next = ndp->ni_next;
1130 ndp->ni_next = cp;
1131
1132 /*
1133 * Something else should be clearing this.
1134 */
1135 cnp->cn_flags &= ~(ISDOTDOT|ISLASTCN);
1136
1137 cnp->cn_flags |= MAKEENTRY;
1138 if (*cp == '\0' && docache == 0)
1139 cnp->cn_flags &= ~MAKEENTRY;
1140 if (cnp->cn_namelen == 2 &&
1141 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1142 cnp->cn_flags |= ISDOTDOT;
1143 if (*ndp->ni_next == 0) {
1144 cnp->cn_flags |= ISLASTCN;
1145
1146 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
1147 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))) {
1148 error = EINVAL;
1149 goto bad;
1150 }
1151 }
1152
1153 nameicap_tracker_add(ndp, dp);
1154
1155 /*
1156 * Make sure degenerate names don't get here, their handling was
1157 * previously found in this spot.
1158 */
1159 MPASS(cnp->cn_nameptr[0] != '\0');
1160
1161 /*
1162 * Handle "..": five special cases.
1163 * 0. If doing a capability lookup and lookup_cap_dotdot is
1164 * disabled, return ENOTCAPABLE.
1165 * 1. Return an error if this is the last component of
1166 * the name and the operation is DELETE or RENAME.
1167 * 2. If at root directory (e.g. after chroot)
1168 * or at absolute root directory
1169 * then ignore it so can't get out.
1170 * 3. If this vnode is the root of a mounted
1171 * filesystem, then replace it with the
1172 * vnode which was mounted on so we take the
1173 * .. in the other filesystem.
1174 * 4. If the vnode is the top directory of
1175 * the jail or chroot, don't let them out.
1176 * 5. If doing a capability lookup and lookup_cap_dotdot is
1177 * enabled, return ENOTCAPABLE if the lookup would escape
1178 * from the initial file descriptor directory. Checks are
1179 * done by ensuring that namei() already traversed the
1180 * result of dotdot lookup.
1181 */
1182 if (cnp->cn_flags & ISDOTDOT) {
1183 if (__predict_false((ndp->ni_lcf & (NI_LCF_STRICTREL_KTR |
1184 NI_LCF_CAP_DOTDOT_KTR)) == NI_LCF_STRICTREL_KTR))
1185 NI_CAP_VIOLATION(ndp, cnp->cn_pnbuf);
1186 if (__predict_false((ndp->ni_lcf & (NI_LCF_STRICTREL |
1187 NI_LCF_CAP_DOTDOT)) == NI_LCF_STRICTREL)) {
1188 error = ENOTCAPABLE;
1189 goto bad;
1190 }
1191 if ((cnp->cn_flags & ISLASTCN) != 0 &&
1192 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1193 error = EINVAL;
1194 goto bad;
1195 }
1196 for (;;) {
1197 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
1198 pr = pr->pr_parent)
1199 if (dp == pr->pr_root)
1200 break;
1201 bool isroot = dp == ndp->ni_rootdir ||
1202 dp == ndp->ni_topdir || dp == rootvnode ||
1203 pr != NULL;
1204 if (__predict_false(isroot && (ndp->ni_lcf &
1205 (NI_LCF_STRICTREL | NI_LCF_STRICTREL_KTR)) != 0)) {
1206 if ((ndp->ni_lcf & NI_LCF_STRICTREL_KTR) != 0)
1207 NI_CAP_VIOLATION(ndp, cnp->cn_pnbuf);
1208 if ((ndp->ni_lcf & NI_LCF_STRICTREL) != 0) {
1209 error = ENOTCAPABLE;
1210 goto capdotdot;
1211 }
1212 }
1213 if (isroot || ((dp->v_vflag & VV_ROOT) != 0 &&
1214 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
1215 ndp->ni_dvp = dp;
1216 ndp->ni_vp = dp;
1217 VREF(dp);
1218 goto nextname;
1219 }
1220 if ((dp->v_vflag & VV_ROOT) == 0)
1221 break;
1222 if (VN_IS_DOOMED(dp)) { /* forced unmount */
1223 error = ENOENT;
1224 goto bad;
1225 }
1226 tdp = dp;
1227 dp = dp->v_mount->mnt_vnodecovered;
1228 VREF(dp);
1229 vput(tdp);
1230 vn_lock(dp,
1231 enforce_lkflags(dp->v_mount, cnp->cn_lkflags |
1232 LK_RETRY));
1233 error = nameicap_check_dotdot(ndp, dp);
1234 if (error != 0) {
1235 capdotdot:
1236 goto bad;
1237 }
1238 }
1239 }
1240
1241 /*
1242 * We now have a segment name to search for, and a directory to search.
1243 */
1244 unionlookup:
1245 #ifdef MAC
1246 error = mac_vnode_check_lookup(cnp->cn_cred, dp, cnp);
1247 if (__predict_false(error))
1248 goto bad;
1249 #endif
1250 ndp->ni_dvp = dp;
1251 ndp->ni_vp = NULL;
1252 ASSERT_VOP_LOCKED(dp, "lookup");
1253 /*
1254 * If we have a shared lock we may need to upgrade the lock for the
1255 * last operation.
1256 */
1257 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1258 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1259 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1260 if (VN_IS_DOOMED(dp)) {
1261 error = ENOENT;
1262 goto bad;
1263 }
1264 /*
1265 * If we're looking up the last component and we need an exclusive
1266 * lock, adjust our lkflags.
1267 */
1268 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1269 cnp->cn_lkflags = LK_EXCLUSIVE;
1270 lkflags_save = cnp->cn_lkflags;
1271 cnp->cn_lkflags = enforce_lkflags(dp->v_mount, cnp->cn_lkflags);
1272 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1273 cnp->cn_lkflags = lkflags_save;
1274 if (error != 0) {
1275 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1276 if ((error == ENOENT) &&
1277 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1278 (dp->v_mount->mnt_flag & MNT_UNION)) {
1279 tdp = dp;
1280 dp = dp->v_mount->mnt_vnodecovered;
1281 VREF(dp);
1282 vput(tdp);
1283 vn_lock(dp,
1284 enforce_lkflags(dp->v_mount, cnp->cn_lkflags |
1285 LK_RETRY));
1286 nameicap_tracker_add(ndp, dp);
1287 goto unionlookup;
1288 }
1289
1290 if (error == ERELOOKUP) {
1291 vref(dp);
1292 ndp->ni_vp = dp;
1293 error = 0;
1294 relookup = 1;
1295 goto good;
1296 }
1297
1298 if (error != EJUSTRETURN)
1299 goto bad;
1300 /*
1301 * At this point, we know we're at the end of the
1302 * pathname. If creating / renaming, we can consider
1303 * allowing the file or directory to be created / renamed,
1304 * provided we're not on a read-only filesystem.
1305 */
1306 if (rdonly) {
1307 error = EROFS;
1308 goto bad;
1309 }
1310 /* trailing slash only allowed for directories */
1311 if ((cnp->cn_flags & TRAILINGSLASH) &&
1312 !(cnp->cn_flags & WILLBEDIR)) {
1313 error = ENOENT;
1314 goto bad;
1315 }
1316 if ((cnp->cn_flags & LOCKPARENT) == 0)
1317 VOP_UNLOCK(dp);
1318 /*
1319 * We return with ni_vp NULL to indicate that the entry
1320 * doesn't currently exist, leaving a pointer to the
1321 * (possibly locked) directory vnode in ndp->ni_dvp.
1322 */
1323 goto success;
1324 }
1325
1326 good:
1327 dp = ndp->ni_vp;
1328
1329 /*
1330 * Check for symbolic link
1331 */
1332 if ((dp->v_type == VLNK) &&
1333 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1334 *ndp->ni_next == '/')) {
1335 cnp->cn_flags |= ISSYMLINK;
1336 if (VN_IS_DOOMED(dp)) {
1337 /*
1338 * We can't know whether the directory was mounted with
1339 * NOSYMFOLLOW, so we can't follow safely.
1340 */
1341 error = ENOENT;
1342 goto bad2;
1343 }
1344 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1345 error = EACCES;
1346 goto bad2;
1347 }
1348 /*
1349 * Symlink code always expects an unlocked dvp.
1350 */
1351 if (ndp->ni_dvp != ndp->ni_vp) {
1352 VOP_UNLOCK(ndp->ni_dvp);
1353 ni_dvp_unlocked = 1;
1354 }
1355 goto success;
1356 }
1357
1358 if ((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0 &&
1359 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1360 error = vfs_lookup_cross_mount(ndp);
1361 if (error != 0)
1362 goto bad_unlocked;
1363 /*
1364 * FALLTHROUGH to nextname
1365 */
1366 dp = ndp->ni_vp;
1367 }
1368
1369 nextname:
1370 /*
1371 * Not a symbolic link that we will follow. Continue with the
1372 * next component if there is any; otherwise, we're done.
1373 */
1374 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1375 ("lookup: invalid path state."));
1376 if (relookup) {
1377 relookup = 0;
1378 ndp->ni_pathlen = prev_ni_pathlen;
1379 ndp->ni_next = prev_ni_next;
1380 if (ndp->ni_dvp != dp)
1381 vput(ndp->ni_dvp);
1382 else
1383 vrele(ndp->ni_dvp);
1384 goto dirloop;
1385 }
1386 if (cnp->cn_flags & ISDOTDOT) {
1387 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1388 if (error != 0)
1389 goto bad2;
1390 }
1391 if (*ndp->ni_next == '/') {
1392 cnp->cn_nameptr = ndp->ni_next;
1393 while (*cnp->cn_nameptr == '/') {
1394 cnp->cn_nameptr++;
1395 ndp->ni_pathlen--;
1396 }
1397 if (ndp->ni_dvp != dp)
1398 vput(ndp->ni_dvp);
1399 else
1400 vrele(ndp->ni_dvp);
1401 goto dirloop;
1402 }
1403 /*
1404 * If we're processing a path with a trailing slash,
1405 * check that the end result is a directory.
1406 */
1407 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1408 error = ENOTDIR;
1409 goto bad2;
1410 }
1411 /*
1412 * Disallow directory write attempts on read-only filesystems.
1413 */
1414 if (rdonly &&
1415 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1416 error = EROFS;
1417 goto bad2;
1418 }
1419 if (!wantparent) {
1420 ni_dvp_unlocked = 2;
1421 if (ndp->ni_dvp != dp)
1422 vput(ndp->ni_dvp);
1423 else
1424 vrele(ndp->ni_dvp);
1425 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1426 VOP_UNLOCK(ndp->ni_dvp);
1427 ni_dvp_unlocked = 1;
1428 }
1429
1430 if (cnp->cn_flags & AUDITVNODE1)
1431 AUDIT_ARG_VNODE1(dp);
1432 else if (cnp->cn_flags & AUDITVNODE2)
1433 AUDIT_ARG_VNODE2(dp);
1434
1435 if ((cnp->cn_flags & LOCKLEAF) == 0)
1436 VOP_UNLOCK(dp);
1437 success:
1438 /*
1439 * FIXME: for lookups which only cross a mount point to fetch the
1440 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1441 * if either WANTPARENT or LOCKPARENT is set.
1442 */
1443 /*
1444 * Because of shared lookup we may have the vnode shared locked, but
1445 * the caller may want it to be exclusively locked.
1446 */
1447 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1448 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1449 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1450 if (VN_IS_DOOMED(dp)) {
1451 error = ENOENT;
1452 goto bad2;
1453 }
1454 }
1455 success_right_lock:
1456 if (ndp->ni_vp != NULL) {
1457 if ((cnp->cn_flags & ISDOTDOT) == 0)
1458 nameicap_tracker_add(ndp, ndp->ni_vp);
1459 if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS)
1460 return (vfs_lookup_failifexists(ndp));
1461 }
1462 return (0);
1463
1464 bad2:
1465 if (ni_dvp_unlocked != 2) {
1466 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1467 vput(ndp->ni_dvp);
1468 else
1469 vrele(ndp->ni_dvp);
1470 }
1471 bad:
1472 vput(dp);
1473 bad_unlocked:
1474 ndp->ni_vp = NULL;
1475 return (error);
1476 }
1477
1478 /*
1479 * relookup - lookup a path name component
1480 * Used by lookup to re-acquire things.
1481 */
1482 int
vfs_relookup(struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,bool refstart)1483 vfs_relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1484 bool refstart)
1485 {
1486 struct vnode *dp = NULL; /* the directory we are searching */
1487 int rdonly; /* lookup read-only flag bit */
1488 int error = 0;
1489
1490 KASSERT(cnp->cn_flags & ISLASTCN,
1491 ("relookup: Not given last component."));
1492 /*
1493 * Setup: break out flag bits into variables.
1494 */
1495 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0,
1496 ("relookup: parent not wanted"));
1497 rdonly = cnp->cn_flags & RDONLY;
1498 cnp->cn_flags &= ~ISSYMLINK;
1499 dp = dvp;
1500 cnp->cn_lkflags = LK_EXCLUSIVE;
1501 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1502
1503 /*
1504 * Search a new directory.
1505 *
1506 * See a comment in vfs_lookup for cnp->cn_nameptr.
1507 *
1508 * Check for "" which represents the root directory after slash
1509 * removal.
1510 */
1511 if (cnp->cn_nameptr[0] == '\0') {
1512 /*
1513 * Support only LOOKUP for "/" because lookup()
1514 * can't succeed for CREATE, DELETE and RENAME.
1515 */
1516 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1517 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1518
1519 if (!(cnp->cn_flags & LOCKLEAF))
1520 VOP_UNLOCK(dp);
1521 *vpp = dp;
1522 /* XXX This should probably move to the top of function. */
1523 if (refstart)
1524 panic("lookup: SAVESTART");
1525 return (0);
1526 }
1527
1528 if (cnp->cn_flags & ISDOTDOT)
1529 panic ("relookup: lookup on dot-dot");
1530
1531 /*
1532 * We now have a segment name to search for, and a directory to search.
1533 */
1534 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1535 KASSERT(*vpp == NULL, ("leaf should be empty"));
1536 if (error != EJUSTRETURN)
1537 goto bad;
1538 /*
1539 * If creating and at end of pathname, then can consider
1540 * allowing file to be created.
1541 */
1542 if (rdonly) {
1543 error = EROFS;
1544 goto bad;
1545 }
1546 /* ASSERT(dvp == ndp->ni_startdir) */
1547 if (refstart)
1548 VREF(dvp);
1549 if ((cnp->cn_flags & LOCKPARENT) == 0)
1550 VOP_UNLOCK(dp);
1551 /*
1552 * We return with ni_vp NULL to indicate that the entry
1553 * doesn't currently exist, leaving a pointer to the
1554 * (possibly locked) directory vnode in ndp->ni_dvp.
1555 */
1556 return (0);
1557 }
1558
1559 dp = *vpp;
1560
1561 /*
1562 * Disallow directory write attempts on read-only filesystems.
1563 */
1564 if (rdonly &&
1565 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1566 if (dvp == dp)
1567 vrele(dvp);
1568 else
1569 vput(dvp);
1570 error = EROFS;
1571 goto bad;
1572 }
1573 /*
1574 * Set the parent lock/ref state to the requested state.
1575 */
1576 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp)
1577 VOP_UNLOCK(dvp);
1578 /*
1579 * Check for symbolic link
1580 */
1581 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1582 ("relookup: symlink found.\n"));
1583
1584 /* ASSERT(dvp == ndp->ni_startdir) */
1585 if (refstart)
1586 VREF(dvp);
1587
1588 if ((cnp->cn_flags & LOCKLEAF) == 0)
1589 VOP_UNLOCK(dp);
1590 return (0);
1591 bad:
1592 vput(dp);
1593 *vpp = NULL;
1594 return (error);
1595 }
1596
1597 #ifdef INVARIANTS
1598 /*
1599 * Validate the final state of ndp after the lookup.
1600 */
1601 static void
NDVALIDATE_impl(struct nameidata * ndp,int line)1602 NDVALIDATE_impl(struct nameidata *ndp, int line)
1603 {
1604 struct componentname *cnp;
1605
1606 cnp = &ndp->ni_cnd;
1607 if (cnp->cn_pnbuf == NULL)
1608 panic("%s: got no buf! called from %d", __func__, line);
1609 }
1610
1611 #endif
1612