xref: /illumos-gate/usr/src/uts/common/syscall/getcwd.c (revision 09fbbb7d1b5a956a9b81304070dcff318a4a158e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
26  */
27 
28 #include <sys/copyops.h>
29 #include <sys/errno.h>
30 #include <sys/kmem.h>
31 #include <sys/param.h>
32 #include <sys/pathname.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/vnode.h>
37 
38 int
39 getcwd(char *buf, size_t buflen)
40 {
41 	int err;
42 	char *kbuf;
43 	size_t kbuflen;
44 
45 	/*
46 	 * If the buffer cannot accommodate one character and nul terminator,
47 	 * it is too small.
48 	 */
49 	if (buflen < 2)
50 		return (set_errno(ERANGE));
51 
52 	/*
53 	 * The user should be able to specify any size buffer, but we don't want
54 	 * to arbitrarily allocate huge kernel buffers just because the user
55 	 * requests it.  So we'll start with MAXPATHLEN (which should hold any
56 	 * normal path), and only increase it if we fail with
57 	 * ERANGE / ENAMETOOLONG.
58 	 *
59 	 * To protect against unbounded memory usage, cap to kmem_max_cached.
60 	 * This is far bigger than the length of any practical path on the
61 	 * system, and avoids allocating memory from the kmem_oversized arena.
62 	 */
63 	kbuflen = MIN(buflen, MAXPATHLEN);
64 
65 	while (kbuflen <= kmem_max_cached) {
66 		kbuf = kmem_alloc(kbuflen, KM_SLEEP);
67 
68 		if (((err = dogetcwd(kbuf, kbuflen)) == 0) &&
69 		    (copyout(kbuf, buf, strlen(kbuf) + 1) != 0)) {
70 			err = EFAULT;
71 		}
72 
73 		kmem_free(kbuf, kbuflen);
74 
75 		/*
76 		 * dogetcwd() inconsistently returns ERANGE or ENAMETOOLONG
77 		 * depending on whether it calls dirtopath() and then whether
78 		 * the subsequent operations run out of space whilst
79 		 * evaluating a cached vnode path or otherwise.
80 		 */
81 		if (err == ENAMETOOLONG || err == ERANGE) {
82 			/* For some reason, getcwd() uses ERANGE. */
83 			err = ERANGE;
84 			/*
85 			 * If the user's buffer really was too small, give up.
86 			 */
87 			if (kbuflen == buflen)
88 				break;
89 			kbuflen = MIN(kbuflen * 2, buflen);
90 		} else {
91 			break;
92 		}
93 	}
94 
95 	return ((err != 0) ? set_errno(err) : 0);
96 }
97