xref: /netbsd-src/sys/kern/vfs_getcwd.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* $NetBSD: vfs_getcwd.c,v 1.60 2020/05/16 18:31:50 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1999, 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Sommerfeld.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.60 2020/05/16 18:31:50 christos Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/proc.h>
45 #include <sys/uio.h>
46 #include <sys/kmem.h>
47 #include <sys/dirent.h>
48 #include <sys/kauth.h>
49 
50 #include <ufs/ufs/dir.h>	/* XXX only for DIRBLKSIZ */
51 
52 #include <sys/syscallargs.h>
53 
54 /*
55  * Vnode variable naming conventions in this file:
56  *
57  * rvp: the current root we're aiming towards.
58  * lvp, *lvpp: the "lower" vnode
59  * uvp, *uvpp: the "upper" vnode.
60  *
61  * Since all the vnodes we're dealing with are directories, and the
62  * lookups are going *up* in the filesystem rather than *down*, the
63  * usual "pvp" (parent) or "dvp" (directory) naming conventions are
64  * too confusing.
65  */
66 
67 /*
68  * XXX Will infinite loop in certain cases if a directory read reliably
69  *	returns EINVAL on last block.
70  * XXX is EINVAL the right thing to return if a directory is malformed?
71  */
72 
73 /*
74  * XXX Untested vs. mount -o union; probably does the wrong thing.
75  */
76 
77 /*
78  * Find parent vnode of *lvpp, return in *uvpp
79  *
80  * If we care about the name, scan it looking for name of directory
81  * entry pointing at lvp.
82  *
83  * Place the name in the buffer which starts at bufp, immediately
84  * before *bpp, and move bpp backwards to point at the start of it.
85  *
86  * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
87  * On exit, *uvpp is either NULL or is a locked vnode reference.
88  */
89 static int
90 getcwd_scandir(struct vnode *lvp, struct vnode **uvpp, char **bpp,
91     char *bufp, struct lwp *l)
92 {
93 	int     error = 0;
94 	int     eofflag;
95 	off_t   off;
96 	int     tries;
97 	struct uio uio;
98 	struct iovec iov;
99 	char   *dirbuf = NULL;
100 	int	dirbuflen;
101 	ino_t   fileno;
102 	struct vattr va;
103 	struct vnode *uvp = NULL;
104 	kauth_cred_t cred = l->l_cred;
105 	struct componentname cn;
106 	int len, reclen;
107 	tries = 0;
108 
109 	/* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
110 	KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
111 
112 	/*
113 	 * If we want the filename, get some info we need while the
114 	 * current directory is still locked.
115 	 */
116 	if (bufp != NULL) {
117 		error = VOP_GETATTR(lvp, &va, cred);
118 		if (error) {
119 			VOP_UNLOCK(lvp);
120 			*uvpp = NULL;
121 			return error;
122 		}
123 	}
124 
125 	/*
126 	 * Ok, we have to do it the hard way..
127 	 * Next, get parent vnode using lookup of ..
128 	 */
129 	cn.cn_nameiop = LOOKUP;
130 	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
131 	cn.cn_cred = cred;
132 	cn.cn_nameptr = "..";
133 	cn.cn_namelen = 2;
134 	cn.cn_consume = 0;
135 
136 	/* At this point, lvp is locked  */
137 	error = VOP_LOOKUP(lvp, uvpp, &cn);
138 	VOP_UNLOCK(lvp);
139 	if (error) {
140 		*uvpp = NULL;
141 		return error;
142 	}
143 	uvp = *uvpp;
144 	/* If we don't care about the pathname, we're done */
145 	if (bufp == NULL) {
146 		return 0;
147 	}
148 
149 	fileno = va.va_fileid;
150 
151 	/* I guess UFS_DIRBLKSIZ is a good guess at a good size to use? */
152 	dirbuflen = UFS_DIRBLKSIZ;
153 	if (dirbuflen < va.va_blocksize)
154 		dirbuflen = va.va_blocksize;
155 	dirbuf = kmem_alloc(dirbuflen, KM_SLEEP);
156 
157 	/* Now lvp is unlocked, try to lock uvp */
158 	error = vn_lock(uvp, LK_SHARED);
159 	if (error) {
160 		vrele(uvp);
161 		*uvpp = NULL;
162 		return error;
163 	}
164 
165 #if 0
166 unionread:
167 #endif
168 	off = 0;
169 	do {
170 		/* call VOP_READDIR of parent */
171 		iov.iov_base = dirbuf;
172 		iov.iov_len = dirbuflen;
173 
174 		uio.uio_iov = &iov;
175 		uio.uio_iovcnt = 1;
176 		uio.uio_offset = off;
177 		uio.uio_resid = dirbuflen;
178 		uio.uio_rw = UIO_READ;
179 		UIO_SETUP_SYSSPACE(&uio);
180 
181 		eofflag = 0;
182 
183 		error = VOP_READDIR(uvp, &uio, cred, &eofflag, 0, 0);
184 
185 		off = uio.uio_offset;
186 
187 		/*
188 		 * Try again if NFS tosses its cookies.
189 		 * XXX this can still loop forever if the directory is busted
190 		 * such that the second or subsequent page of it always
191 		 * returns EINVAL
192 		 */
193 		if ((error == EINVAL) && (tries < 3)) {
194 			off = 0;
195 			tries++;
196 			continue;	/* once more, with feeling */
197 		}
198 
199 		if (!error) {
200 			char   *cpos;
201 			struct dirent *dp;
202 
203 			cpos = dirbuf;
204 			tries = 0;
205 
206 			/* scan directory page looking for matching vnode */
207 			for (len = (dirbuflen - uio.uio_resid); len > 0;
208 			    len -= reclen) {
209 				dp = (struct dirent *) cpos;
210 				reclen = dp->d_reclen;
211 
212 				/* check for malformed directory.. */
213 				if (reclen < _DIRENT_MINSIZE(dp) ||
214 				    reclen > len) {
215 					error = EINVAL;
216 					goto out;
217 				}
218 				/*
219 				 * XXX should perhaps do VOP_LOOKUP to
220 				 * check that we got back to the right place,
221 				 * but getting the locking games for that
222 				 * right would be heinous.
223 				 */
224 				if ((dp->d_type != DT_WHT) &&
225 				    (dp->d_fileno == fileno)) {
226 					char *bp = *bpp;
227 
228 					bp -= dp->d_namlen;
229 					if (bp <= bufp) {
230 						error = ERANGE;
231 						goto out;
232 					}
233 					memcpy(bp, dp->d_name, dp->d_namlen);
234 					error = 0;
235 					*bpp = bp;
236 					goto out;
237 				}
238 				cpos += reclen;
239 			}
240 		} else
241 			goto out;
242 	} while (!eofflag);
243 #if 0
244 	/*
245 	 * Deal with mount -o union, which unions only the
246 	 * root directory of the mount.
247 	 */
248 	if ((uvp->v_vflag & VV_ROOT) &&
249 	    (uvp->v_mount->mnt_flag & MNT_UNION)) {
250 		struct vnode *tvp = uvp;
251 
252 		uvp = uvp->v_mount->mnt_vnodecovered;
253 		vput(tvp);
254 		vref(uvp);
255 		*uvpp = uvp;
256 		vn_lock(uvp, LK_SHARED | LK_RETRY);
257 		goto unionread;
258 	}
259 #endif
260 	error = ENOENT;
261 
262 out:
263 	VOP_UNLOCK(uvp);
264 	kmem_free(dirbuf, dirbuflen);
265 	return error;
266 }
267 
268 /*
269  * common routine shared by sys___getcwd() and vn_isunder()
270  */
271 int
272 getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
273     int limit, int flags, struct lwp *l)
274 {
275 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
276 	kauth_cred_t cred = l->l_cred;
277 	struct vnode *uvp = NULL;
278 	char *bp = NULL;
279 	int error;
280 	accmode_t accmode = VEXEC;
281 
282 	error = 0;
283 	if (rvp == NULL) {
284 		rvp = cwdi->cwdi_rdir;
285 		if (rvp == NULL)
286 			rvp = rootvnode;
287 	}
288 
289 	vref(rvp);
290 	vref(lvp);
291 
292 	/*
293 	 * Error handling invariant:
294 	 * Before a `goto out':
295 	 *	lvp is either NULL, or held.
296 	 *	uvp is either NULL, or held.
297 	 */
298 
299 	if (bufp)
300 		bp = *bpp;
301 
302 	/*
303 	 * this loop will terminate when one of the following happens:
304 	 *	- we hit the root
305 	 *	- getdirentries or lookup fails
306 	 *	- we run out of space in the buffer.
307 	 */
308 	if (lvp == rvp) {
309 		if (bp)
310 			*(--bp) = '/';
311 		goto out;
312 	}
313 	do {
314 		/*
315 		 * access check here is optional, depending on
316 		 * whether or not caller cares.
317 		 */
318 		int chkaccess = (flags & GETCWD_CHECK_ACCESS);
319 		bool locked = false;
320 
321 		/*
322 		 * step up if we're a covered vnode..
323 		 * check access on the first vnode only.
324 		 */
325 		if (lvp->v_vflag & VV_ROOT) {
326 			vn_lock(lvp, LK_SHARED | LK_RETRY);
327 			if (chkaccess) {
328 				error = VOP_ACCESS(lvp, accmode, cred);
329 				if (error) {
330 					VOP_UNLOCK(lvp);
331 					goto out;
332 				}
333 				chkaccess = 0;
334 			}
335 			while (lvp->v_vflag & VV_ROOT) {
336 				struct vnode *tvp;
337 
338 				if (lvp == rvp) {
339 					VOP_UNLOCK(lvp);
340 					goto out;
341 				}
342 
343 				tvp = lvp->v_mount->mnt_vnodecovered;
344 				/*
345 				 * hodie natus est radici frater
346 				 */
347 				if (tvp == NULL) {
348 					VOP_UNLOCK(lvp);
349 					error = ENOENT;
350 					goto out;
351 				}
352 				vref(tvp);
353 				vput(lvp);
354 				lvp = tvp;
355 				if (lvp->v_vflag & VV_ROOT)
356 					vn_lock(lvp, LK_SHARED | LK_RETRY);
357 			}
358 		}
359 
360 		/* Do we need to check access to the directory? */
361 		if (chkaccess && !cache_have_id(lvp)) {
362 			/* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
363 			vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
364 			error = VOP_ACCESS(lvp, accmode, cred);
365 			if (error) {
366 				VOP_UNLOCK(lvp);
367 				goto out;
368 			}
369 			chkaccess = 0;
370 			locked = true;
371 		}
372 
373 		/*
374 		 * Look in the name cache; if that fails, look in the
375 		 * directory..
376 		 */
377 		error = cache_revlookup(lvp, &uvp, &bp, bufp, chkaccess,
378 		    accmode);
379 		if (error == -1) {
380 			if (!locked) {
381 				locked = true;
382 				vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
383 			}
384 			if (lvp->v_type != VDIR) {
385 				VOP_UNLOCK(lvp);
386 				error = ENOTDIR;
387 				goto out;
388 			}
389 			error = getcwd_scandir(lvp, &uvp, &bp, bufp, l);
390 			/* lvp now unlocked */
391 		} else if (locked) {
392 			VOP_UNLOCK(lvp);
393 		}
394 		if (error)
395 			goto out;
396 #if DIAGNOSTIC
397 		if (bufp && (bp <= bufp)) {
398 			panic("getcwd: oops, went back too far");
399 		}
400 #endif
401 		accmode = VEXEC | VREAD;
402 		if (bp)
403 			*(--bp) = '/';
404 		vrele(lvp);
405 		lvp = uvp;
406 		uvp = NULL;
407 		limit--;
408 	} while ((lvp != rvp) && (limit > 0));
409 
410 out:
411 	if (bpp)
412 		*bpp = bp;
413 	if (uvp)
414 		vrele(uvp);
415 	if (lvp)
416 		vrele(lvp);
417 	vrele(rvp);
418 	return error;
419 }
420 
421 /*
422  * Check if one directory can be found inside another in the directory
423  * hierarchy.
424  *
425  * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
426  * chroot() actually means something.
427  */
428 int
429 vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
430 {
431 	int error;
432 
433 	error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
434 
435 	if (!error)
436 		return 1;
437 	else
438 		return 0;
439 }
440 
441 /*
442  * Returns true if proc p1's root directory equal to or under p2's
443  * root directory.
444  *
445  * Intended to be used from ptrace/procfs sorts of things.
446  */
447 
448 int
449 proc_isunder(struct proc *p1, struct lwp *l2)
450 {
451 	struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
452 	struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
453 
454 	if (r1 == NULL)
455 		return (r2 == NULL);
456 	else if (r2 == NULL)
457 		return 1;
458 	else
459 		return vn_isunder(r1, r2, l2);
460 }
461 
462 /*
463  * Find pathname of process's current directory.
464  *
465  * Use vfs vnode-to-name reverse cache; if that fails, fall back
466  * to reading directory contents.
467  */
468 
469 int
470 sys___getcwd(struct lwp *l, const struct sys___getcwd_args *uap, register_t *retval)
471 {
472 	/* {
473 		syscallarg(char *) bufp;
474 		syscallarg(size_t) length;
475 	} */
476 
477 	int     error;
478 	char   *path;
479 	char   *bp, *bend;
480 	int     len = SCARG(uap, length);
481 	int	lenused;
482 	struct	cwdinfo *cwdi;
483 
484 	if (len > MAXPATHLEN * 4)
485 		len = MAXPATHLEN * 4;
486 	else if (len < 2)
487 		return ERANGE;
488 
489 	path = kmem_alloc(len, KM_SLEEP);
490 	bp = &path[len];
491 	bend = bp;
492 	*(--bp) = '\0';
493 
494 	/*
495 	 * 5th argument here is "max number of vnodes to traverse".
496 	 * Since each entry takes up at least 2 bytes in the output buffer,
497 	 * limit it to N/2 vnodes for an N byte buffer.
498 	 */
499 	cwdi = l->l_proc->p_cwdi;
500 	rw_enter(&cwdi->cwdi_lock, RW_READER);
501 	error = getcwd_common(cwdi->cwdi_cdir, NULL, &bp, path,
502 	    len/2, GETCWD_CHECK_ACCESS, l);
503 	rw_exit(&cwdi->cwdi_lock);
504 
505 	if (error)
506 		goto out;
507 	lenused = bend - bp;
508 	*retval = lenused;
509 	/* put the result into user buffer */
510 	error = copyout(bp, SCARG(uap, bufp), lenused);
511 
512 out:
513 	kmem_free(path, len);
514 	return error;
515 }
516 
517 /*
518  * Try to find a pathname for a vnode.  Since there is no mapping vnode ->
519  * parent directory, this needs the namecache to succeed.  Caller holds a
520  * reference to the vnode.
521  */
522 int
523 vnode_to_path(char *path, size_t len, struct vnode *vp, struct lwp *curl,
524     struct proc *p)
525 {
526 	struct proc *curp = curl->l_proc;
527 	int error, lenused, elen;
528 	char *bp, *bend;
529 	struct vnode *dvp;
530 
531 	KASSERT(vrefcnt(vp) > 0);
532 
533 	bp = bend = &path[len];
534 	*(--bp) = '\0';
535 
536 	error = cache_revlookup(vp, &dvp, &bp, path, false, 0);
537 	if (error != 0)
538 		return (error == -1 ? ENOENT : error);
539 
540 	*(--bp) = '/';
541 	error = getcwd_common(dvp, NULL, &bp, path, len / 2,
542 	    GETCWD_CHECK_ACCESS, curl);
543 	vrele(dvp);
544 	if (error != 0)
545 		return error;
546 
547 	/*
548 	 * Strip off emulation path for emulated processes looking at
549 	 * the maps file of a process of the same emulation. (Won't
550 	 * work if /emul/xxx is a symlink..)
551 	 */
552 	if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
553 		elen = strlen(curp->p_emul->e_path);
554 		if (!strncmp(bp, curp->p_emul->e_path, elen))
555 			bp = &bp[elen];
556 	}
557 
558 	lenused = bend - bp;
559 
560 	memcpy(path, bp, lenused);
561 	path[lenused] = '\0';
562 
563 	return 0;
564 }
565