xref: /freebsd/lib/libc/stdlib/realpath.c (revision fa38579f317d5c2ff2926fab9b12ee6d429bd155)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The names of the authors may not be used to endorse or promote
15  *    products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <libsys.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <ssp/ssp.h>
41 
42 /*
43  * Find the real name of path, by removing all ".", ".." and symlink
44  * components.  Returns (resolved) on success, or (NULL) on failure,
45  * in which case the path which caused trouble is left in (resolved).
46  */
47 static char * __noinline
48 realpath1(const char *path, char *resolved)
49 {
50 	struct stat sb;
51 	char *p, *q;
52 	size_t left_len, resolved_len, next_token_len;
53 	unsigned symlinks;
54 	ssize_t slen;
55 	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
56 
57 	symlinks = 0;
58 	if (path[0] == '/') {
59 		resolved[0] = '/';
60 		resolved[1] = '\0';
61 		if (path[1] == '\0')
62 			return (resolved);
63 		resolved_len = 1;
64 		left_len = strlcpy(left, path + 1, sizeof(left));
65 	} else {
66 		if (getcwd(resolved, PATH_MAX) == NULL) {
67 			resolved[0] = '.';
68 			resolved[1] = '\0';
69 			return (NULL);
70 		}
71 		resolved_len = strlen(resolved);
72 		left_len = strlcpy(left, path, sizeof(left));
73 	}
74 	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
75 		errno = ENAMETOOLONG;
76 		return (NULL);
77 	}
78 
79 	/*
80 	 * Iterate over path components in `left'.
81 	 */
82 	while (left_len != 0) {
83 		/*
84 		 * Extract the next path component and adjust `left'
85 		 * and its length.
86 		 */
87 		p = strchr(left, '/');
88 
89 		next_token_len = p != NULL ? (size_t)(p - left) : left_len;
90 		memcpy(next_token, left, next_token_len);
91 		next_token[next_token_len] = '\0';
92 
93 		if (p != NULL) {
94 			left_len -= next_token_len + 1;
95 			memmove(left, p + 1, left_len + 1);
96 		} else {
97 			left[0] = '\0';
98 			left_len = 0;
99 		}
100 
101 		if (resolved[resolved_len - 1] != '/') {
102 			if (resolved_len + 1 >= PATH_MAX) {
103 				errno = ENAMETOOLONG;
104 				return (NULL);
105 			}
106 			resolved[resolved_len++] = '/';
107 			resolved[resolved_len] = '\0';
108 		}
109 		if (next_token[0] == '\0') {
110 			/* Handle consequential slashes. */
111 			continue;
112 		} else if (strcmp(next_token, ".") == 0) {
113 			continue;
114 		} else if (strcmp(next_token, "..") == 0) {
115 			/*
116 			 * Strip the last path component except when we have
117 			 * single "/"
118 			 */
119 			if (resolved_len > 1) {
120 				resolved[resolved_len - 1] = '\0';
121 				q = strrchr(resolved, '/') + 1;
122 				*q = '\0';
123 				resolved_len = q - resolved;
124 			}
125 			continue;
126 		}
127 
128 		/*
129 		 * Append the next path component and lstat() it.
130 		 */
131 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
132 		if (resolved_len >= PATH_MAX) {
133 			errno = ENAMETOOLONG;
134 			return (NULL);
135 		}
136 		if (lstat(resolved, &sb) != 0)
137 			return (NULL);
138 		if (S_ISLNK(sb.st_mode)) {
139 			if (symlinks++ > MAXSYMLINKS) {
140 				errno = ELOOP;
141 				return (NULL);
142 			}
143 			slen = readlink(resolved, symlink, sizeof(symlink));
144 			if (slen < 0)
145 				return (NULL);
146 			if (slen == 0) {
147 				errno = ENOENT;
148 				return (NULL);
149 			}
150 			if ((size_t)slen >= sizeof(symlink)) {
151 				errno = ENAMETOOLONG;
152 				return (NULL);
153 			}
154 			symlink[slen] = '\0';
155 			if (symlink[0] == '/') {
156 				resolved[1] = 0;
157 				resolved_len = 1;
158 			} else {
159 				/* Strip the last path component. */
160 				q = strrchr(resolved, '/') + 1;
161 				*q = '\0';
162 				resolved_len = q - resolved;
163 			}
164 
165 			/*
166 			 * If there are any path components left, then
167 			 * append them to symlink. The result is placed
168 			 * in `left'.
169 			 */
170 			if (p != NULL) {
171 				if (symlink[slen - 1] != '/') {
172 					if ((size_t)slen + 1 >= sizeof(symlink)) {
173 						errno = ENAMETOOLONG;
174 						return (NULL);
175 					}
176 					symlink[slen] = '/';
177 					symlink[slen + 1] = 0;
178 				}
179 				left_len = strlcat(symlink, left,
180 				    sizeof(symlink));
181 				if (left_len >= sizeof(symlink)) {
182 					errno = ENAMETOOLONG;
183 					return (NULL);
184 				}
185 			}
186 			left_len = strlcpy(left, symlink, sizeof(left));
187 		} else if (!S_ISDIR(sb.st_mode) && p != NULL) {
188 			errno = ENOTDIR;
189 			return (NULL);
190 		}
191 	}
192 
193 	/*
194 	 * Remove trailing slash except when the resolved pathname
195 	 * is a single "/".
196 	 */
197 	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
198 		resolved[resolved_len - 1] = '\0';
199 	return (resolved);
200 }
201 
202 char *
203 __ssp_real(realpath)(const char * __restrict path, char * __restrict resolved)
204 {
205 	char *m, *res;
206 
207 	if (path == NULL) {
208 		errno = EINVAL;
209 		return (NULL);
210 	}
211 	if (path[0] == '\0') {
212 		errno = ENOENT;
213 		return (NULL);
214 	}
215 	if (resolved != NULL) {
216 		m = NULL;
217 	} else {
218 		m = resolved = malloc(PATH_MAX);
219 		if (resolved == NULL)
220 			return (NULL);
221 	}
222 	if (__sys___realpathat(AT_FDCWD, path, resolved, PATH_MAX, 0) == 0) {
223 		return (resolved);
224 	}
225 	res = realpath1(path, resolved);
226 	if (res == NULL)
227 		free(m);
228 	return (res);
229 }
230