xref: /freebsd/lib/libc/stdlib/realpath.c (revision dc36d6f9)
1d915a14eSPedro F. Giffuni /*-
2d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3d915a14eSPedro F. Giffuni  *
4226a0f0fSMax Khon  * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
5d01a28e2SPeter Wemm  *
6d01a28e2SPeter Wemm  * Redistribution and use in source and binary forms, with or without
7d01a28e2SPeter Wemm  * modification, are permitted provided that the following conditions
8d01a28e2SPeter Wemm  * are met:
9d01a28e2SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
10d01a28e2SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
11d01a28e2SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
12d01a28e2SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
13d01a28e2SPeter Wemm  *    documentation and/or other materials provided with the distribution.
14226a0f0fSMax Khon  * 3. The names of the authors may not be used to endorse or promote
15226a0f0fSMax Khon  *    products derived from this software without specific prior written
16226a0f0fSMax Khon  *    permission.
17d01a28e2SPeter Wemm  *
18226a0f0fSMax Khon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19d01a28e2SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20d01a28e2SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21226a0f0fSMax Khon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22d01a28e2SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23d01a28e2SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24d01a28e2SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25d01a28e2SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26d01a28e2SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27d01a28e2SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28d01a28e2SPeter Wemm  * SUCH DAMAGE.
29d01a28e2SPeter Wemm  */
30d01a28e2SPeter Wemm 
31d201fe46SDaniel Eischen #include "namespace.h"
32d01a28e2SPeter Wemm #include <sys/param.h>
33d01a28e2SPeter Wemm #include <sys/stat.h>
34d01a28e2SPeter Wemm 
35d01a28e2SPeter Wemm #include <errno.h>
36839e119eSMax Khon #include <stdlib.h>
37d01a28e2SPeter Wemm #include <string.h>
38d01a28e2SPeter Wemm #include <unistd.h>
390573d0a9SMateusz Guzik #include <fcntl.h>
40d201fe46SDaniel Eischen #include "un-namespace.h"
410573d0a9SMateusz Guzik #include "libc_private.h"
420573d0a9SMateusz Guzik 
430573d0a9SMateusz Guzik extern int __realpathat(int fd, const char *path, char *buf, size_t size,
440573d0a9SMateusz Guzik     int flags);
45d01a28e2SPeter Wemm 
46d01a28e2SPeter Wemm /*
47d01a28e2SPeter Wemm  * Find the real name of path, by removing all ".", ".." and symlink
48d01a28e2SPeter Wemm  * components.  Returns (resolved) on success, or (NULL) on failure,
49d01a28e2SPeter Wemm  * in which case the path which caused trouble is left in (resolved).
50d01a28e2SPeter Wemm  */
510573d0a9SMateusz Guzik static char * __noinline
realpath1(const char * path,char * resolved)52d933a4c0SKonstantin Belousov realpath1(const char *path, char *resolved)
53d01a28e2SPeter Wemm {
54839e119eSMax Khon 	struct stat sb;
55f81e5b2dSKonstantin Belousov 	char *p, *q;
56f81e5b2dSKonstantin Belousov 	size_t left_len, resolved_len, next_token_len;
57839e119eSMax Khon 	unsigned symlinks;
58f81e5b2dSKonstantin Belousov 	ssize_t slen;
59839e119eSMax Khon 	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
60d01a28e2SPeter Wemm 
61839e119eSMax Khon 	symlinks = 0;
62226a0f0fSMax Khon 	if (path[0] == '/') {
63839e119eSMax Khon 		resolved[0] = '/';
64839e119eSMax Khon 		resolved[1] = '\0';
65226a0f0fSMax Khon 		if (path[1] == '\0')
66839e119eSMax Khon 			return (resolved);
67226a0f0fSMax Khon 		resolved_len = 1;
68d0509082SJacques Vidrine 		left_len = strlcpy(left, path + 1, sizeof(left));
69226a0f0fSMax Khon 	} else {
70839e119eSMax Khon 		if (getcwd(resolved, PATH_MAX) == NULL) {
714e738f5aSKonstantin Belousov 			resolved[0] = '.';
724e738f5aSKonstantin Belousov 			resolved[1] = '\0';
73839e119eSMax Khon 			return (NULL);
74d01a28e2SPeter Wemm 		}
75839e119eSMax Khon 		resolved_len = strlen(resolved);
76d0509082SJacques Vidrine 		left_len = strlcpy(left, path, sizeof(left));
77e53211ceSPoul-Henning Kamp 	}
78839e119eSMax Khon 	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
79d01a28e2SPeter Wemm 		errno = ENAMETOOLONG;
80839e119eSMax Khon 		return (NULL);
81d01a28e2SPeter Wemm 	}
82d01a28e2SPeter Wemm 
83839e119eSMax Khon 	/*
84839e119eSMax Khon 	 * Iterate over path components in `left'.
85839e119eSMax Khon 	 */
86839e119eSMax Khon 	while (left_len != 0) {
87839e119eSMax Khon 		/*
88839e119eSMax Khon 		 * Extract the next path component and adjust `left'
89839e119eSMax Khon 		 * and its length.
90839e119eSMax Khon 		 */
91839e119eSMax Khon 		p = strchr(left, '/');
92f81e5b2dSKonstantin Belousov 
93df5e3924SAlex Richardson 		next_token_len = p != NULL ? (size_t)(p - left) : left_len;
94f81e5b2dSKonstantin Belousov 		memcpy(next_token, left, next_token_len);
95f81e5b2dSKonstantin Belousov 		next_token[next_token_len] = '\0';
96f81e5b2dSKonstantin Belousov 
97f81e5b2dSKonstantin Belousov 		if (p != NULL) {
98f81e5b2dSKonstantin Belousov 			left_len -= next_token_len + 1;
99f81e5b2dSKonstantin Belousov 			memmove(left, p + 1, left_len + 1);
100f81e5b2dSKonstantin Belousov 		} else {
101f81e5b2dSKonstantin Belousov 			left[0] = '\0';
102f81e5b2dSKonstantin Belousov 			left_len = 0;
103839e119eSMax Khon 		}
104f81e5b2dSKonstantin Belousov 
105839e119eSMax Khon 		if (resolved[resolved_len - 1] != '/') {
106be6a158eSMax Khon 			if (resolved_len + 1 >= PATH_MAX) {
107226a0f0fSMax Khon 				errno = ENAMETOOLONG;
108839e119eSMax Khon 				return (NULL);
109d01a28e2SPeter Wemm 			}
110839e119eSMax Khon 			resolved[resolved_len++] = '/';
111839e119eSMax Khon 			resolved[resolved_len] = '\0';
112226a0f0fSMax Khon 		}
113fdbe55fcSKonstantin Belousov 		if (next_token[0] == '\0') {
114310c8f3cSJilles Tjoelker 			/* Handle consequential slashes. */
115226a0f0fSMax Khon 			continue;
116cf5cedd7SKonstantin Belousov 		} else if (strcmp(next_token, ".") == 0) {
117226a0f0fSMax Khon 			continue;
118cf5cedd7SKonstantin Belousov 		} else if (strcmp(next_token, "..") == 0) {
119839e119eSMax Khon 			/*
120839e119eSMax Khon 			 * Strip the last path component except when we have
121839e119eSMax Khon 			 * single "/"
122839e119eSMax Khon 			 */
123226a0f0fSMax Khon 			if (resolved_len > 1) {
124839e119eSMax Khon 				resolved[resolved_len - 1] = '\0';
125f4203da8SMax Khon 				q = strrchr(resolved, '/') + 1;
126226a0f0fSMax Khon 				*q = '\0';
127839e119eSMax Khon 				resolved_len = q - resolved;
128226a0f0fSMax Khon 			}
129226a0f0fSMax Khon 			continue;
130226a0f0fSMax Khon 		}
131226a0f0fSMax Khon 
132839e119eSMax Khon 		/*
133fdbe55fcSKonstantin Belousov 		 * Append the next path component and lstat() it.
134839e119eSMax Khon 		 */
135d0509082SJacques Vidrine 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
136be6a158eSMax Khon 		if (resolved_len >= PATH_MAX) {
137226a0f0fSMax Khon 			errno = ENAMETOOLONG;
138839e119eSMax Khon 			return (NULL);
139226a0f0fSMax Khon 		}
140d933a4c0SKonstantin Belousov 		if (lstat(resolved, &sb) != 0)
141839e119eSMax Khon 			return (NULL);
142839e119eSMax Khon 		if (S_ISLNK(sb.st_mode)) {
143839e119eSMax Khon 			if (symlinks++ > MAXSYMLINKS) {
144226a0f0fSMax Khon 				errno = ELOOP;
145839e119eSMax Khon 				return (NULL);
146226a0f0fSMax Khon 			}
147f81e5b2dSKonstantin Belousov 			slen = readlink(resolved, symlink, sizeof(symlink));
148df5e3924SAlex Richardson 			if (slen <= 0 || slen >= (ssize_t)sizeof(symlink)) {
149cf5cedd7SKonstantin Belousov 				if (slen < 0)
150cf5cedd7SKonstantin Belousov 					; /* keep errno from readlink(2) call */
151cf5cedd7SKonstantin Belousov 				else if (slen == 0)
152f81e5b2dSKonstantin Belousov 					errno = ENOENT;
153cf5cedd7SKonstantin Belousov 				else
154f81e5b2dSKonstantin Belousov 					errno = ENAMETOOLONG;
155839e119eSMax Khon 				return (NULL);
1569d79ec20SKonstantin Belousov 			}
157226a0f0fSMax Khon 			symlink[slen] = '\0';
158226a0f0fSMax Khon 			if (symlink[0] == '/') {
159839e119eSMax Khon 				resolved[1] = 0;
160226a0f0fSMax Khon 				resolved_len = 1;
161f81e5b2dSKonstantin Belousov 			} else {
162839e119eSMax Khon 				/* Strip the last path component. */
163f4203da8SMax Khon 				q = strrchr(resolved, '/') + 1;
164226a0f0fSMax Khon 				*q = '\0';
165839e119eSMax Khon 				resolved_len = q - resolved;
166226a0f0fSMax Khon 			}
167226a0f0fSMax Khon 
168839e119eSMax Khon 			/*
169839e119eSMax Khon 			 * If there are any path components left, then
170839e119eSMax Khon 			 * append them to symlink. The result is placed
171839e119eSMax Khon 			 * in `left'.
172839e119eSMax Khon 			 */
173057e4034SMax Khon 			if (p != NULL) {
174057e4034SMax Khon 				if (symlink[slen - 1] != '/') {
175df5e3924SAlex Richardson 					if (slen + 1 >= (ssize_t)sizeof(symlink)) {
176226a0f0fSMax Khon 						errno = ENAMETOOLONG;
177839e119eSMax Khon 						return (NULL);
178226a0f0fSMax Khon 					}
179226a0f0fSMax Khon 					symlink[slen] = '/';
180226a0f0fSMax Khon 					symlink[slen + 1] = 0;
181226a0f0fSMax Khon 				}
18224a92ae0SEd Schouten 				left_len = strlcat(symlink, left,
18324a92ae0SEd Schouten 				    sizeof(symlink));
184f81e5b2dSKonstantin Belousov 				if (left_len >= sizeof(symlink)) {
185226a0f0fSMax Khon 					errno = ENAMETOOLONG;
186839e119eSMax Khon 					return (NULL);
187226a0f0fSMax Khon 				}
188057e4034SMax Khon 			}
189d0509082SJacques Vidrine 			left_len = strlcpy(left, symlink, sizeof(left));
190310c8f3cSJilles Tjoelker 		} else if (!S_ISDIR(sb.st_mode) && p != NULL) {
191310c8f3cSJilles Tjoelker 			errno = ENOTDIR;
192310c8f3cSJilles Tjoelker 			return (NULL);
193226a0f0fSMax Khon 		}
194226a0f0fSMax Khon 	}
195226a0f0fSMax Khon 
196839e119eSMax Khon 	/*
197839e119eSMax Khon 	 * Remove trailing slash except when the resolved pathname
198839e119eSMax Khon 	 * is a single "/".
199839e119eSMax Khon 	 */
200839e119eSMax Khon 	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
201839e119eSMax Khon 		resolved[resolved_len - 1] = '\0';
202839e119eSMax Khon 	return (resolved);
203d01a28e2SPeter Wemm }
204d933a4c0SKonstantin Belousov 
205d933a4c0SKonstantin Belousov char *
realpath(const char * __restrict path,char * __restrict resolved)206d933a4c0SKonstantin Belousov realpath(const char * __restrict path, char * __restrict resolved)
207d933a4c0SKonstantin Belousov {
208d933a4c0SKonstantin Belousov 	char *m, *res;
209d933a4c0SKonstantin Belousov 
210d933a4c0SKonstantin Belousov 	if (path == NULL) {
211d933a4c0SKonstantin Belousov 		errno = EINVAL;
212d933a4c0SKonstantin Belousov 		return (NULL);
213d933a4c0SKonstantin Belousov 	}
214d933a4c0SKonstantin Belousov 	if (path[0] == '\0') {
215d933a4c0SKonstantin Belousov 		errno = ENOENT;
216d933a4c0SKonstantin Belousov 		return (NULL);
217d933a4c0SKonstantin Belousov 	}
218d933a4c0SKonstantin Belousov 	if (resolved != NULL) {
219d933a4c0SKonstantin Belousov 		m = NULL;
220d933a4c0SKonstantin Belousov 	} else {
221d933a4c0SKonstantin Belousov 		m = resolved = malloc(PATH_MAX);
222d933a4c0SKonstantin Belousov 		if (resolved == NULL)
223d933a4c0SKonstantin Belousov 			return (NULL);
224d933a4c0SKonstantin Belousov 	}
2250573d0a9SMateusz Guzik 	if (__getosreldate() >= 1300080) {
2260573d0a9SMateusz Guzik 		if (__realpathat(AT_FDCWD, path, resolved, PATH_MAX, 0) == 0)
2270573d0a9SMateusz Guzik 			return (resolved);
2280573d0a9SMateusz Guzik 	}
229d933a4c0SKonstantin Belousov 	res = realpath1(path, resolved);
230d933a4c0SKonstantin Belousov 	if (res == NULL)
231d933a4c0SKonstantin Belousov 		free(m);
232d933a4c0SKonstantin Belousov 	return (res);
233d933a4c0SKonstantin Belousov }
234