1*f1ad81b8Schristos /* $OpenBSD: sftp-realpath.c,v 1.2 2021/09/02 21:03:54 deraadt Exp $ */
260e752b5Schristos /*
360e752b5Schristos * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
460e752b5Schristos *
560e752b5Schristos * Redistribution and use in source and binary forms, with or without
660e752b5Schristos * modification, are permitted provided that the following conditions
760e752b5Schristos * are met:
860e752b5Schristos * 1. Redistributions of source code must retain the above copyright
960e752b5Schristos * notice, this list of conditions and the following disclaimer.
1060e752b5Schristos * 2. Redistributions in binary form must reproduce the above copyright
1160e752b5Schristos * notice, this list of conditions and the following disclaimer in the
1260e752b5Schristos * documentation and/or other materials provided with the distribution.
1360e752b5Schristos * 3. The names of the authors may not be used to endorse or promote
1460e752b5Schristos * products derived from this software without specific prior written
1560e752b5Schristos * permission.
1660e752b5Schristos *
1760e752b5Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1860e752b5Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1960e752b5Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2060e752b5Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2160e752b5Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2260e752b5Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2360e752b5Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2460e752b5Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2560e752b5Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2660e752b5Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2760e752b5Schristos * SUCH DAMAGE.
2860e752b5Schristos */
29ed75d7a8Schristos #include "includes.h"
30*f1ad81b8Schristos __RCSID("$NetBSD: sftp-realpath.c,v 1.3 2021/09/27 17:03:13 christos Exp $");
3160e752b5Schristos
3260e752b5Schristos #include <sys/types.h>
3360e752b5Schristos #include <sys/stat.h>
3460e752b5Schristos
3560e752b5Schristos #include <errno.h>
3660e752b5Schristos #include <stdlib.h>
3760e752b5Schristos #include <stddef.h>
3860e752b5Schristos #include <string.h>
3960e752b5Schristos #include <unistd.h>
4060e752b5Schristos #include <limits.h>
4160e752b5Schristos
4260e752b5Schristos #ifndef SYMLOOP_MAX
4360e752b5Schristos # define SYMLOOP_MAX 32
4460e752b5Schristos #endif
4560e752b5Schristos
4660e752b5Schristos /* XXX rewrite sftp-server to use POSIX realpath and remove this hack */
4760e752b5Schristos
4860e752b5Schristos char *sftp_realpath(const char *path, char *resolved);
4960e752b5Schristos
5060e752b5Schristos /*
5160e752b5Schristos * char *realpath(const char *path, char resolved[PATH_MAX]);
5260e752b5Schristos *
5360e752b5Schristos * Find the real name of path, by removing all ".", ".." and symlink
5460e752b5Schristos * components. Returns (resolved) on success, or (NULL) on failure,
5560e752b5Schristos * in which case the path which caused trouble is left in (resolved).
5660e752b5Schristos */
5760e752b5Schristos char *
sftp_realpath(const char * path,char * resolved)5860e752b5Schristos sftp_realpath(const char *path, char *resolved)
5960e752b5Schristos {
6060e752b5Schristos struct stat sb;
6160e752b5Schristos char *p, *q, *s;
6260e752b5Schristos size_t left_len, resolved_len;
6360e752b5Schristos unsigned symlinks;
6460e752b5Schristos int serrno, slen, mem_allocated;
6560e752b5Schristos char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
6660e752b5Schristos
6760e752b5Schristos if (path[0] == '\0') {
6860e752b5Schristos errno = ENOENT;
6960e752b5Schristos return (NULL);
7060e752b5Schristos }
7160e752b5Schristos
7260e752b5Schristos serrno = errno;
7360e752b5Schristos
7460e752b5Schristos if (resolved == NULL) {
7560e752b5Schristos resolved = malloc(PATH_MAX);
7660e752b5Schristos if (resolved == NULL)
7760e752b5Schristos return (NULL);
7860e752b5Schristos mem_allocated = 1;
7960e752b5Schristos } else
8060e752b5Schristos mem_allocated = 0;
8160e752b5Schristos
8260e752b5Schristos symlinks = 0;
8360e752b5Schristos if (path[0] == '/') {
8460e752b5Schristos resolved[0] = '/';
8560e752b5Schristos resolved[1] = '\0';
8660e752b5Schristos if (path[1] == '\0')
8760e752b5Schristos return (resolved);
8860e752b5Schristos resolved_len = 1;
8960e752b5Schristos left_len = strlcpy(left, path + 1, sizeof(left));
9060e752b5Schristos } else {
9160e752b5Schristos if (getcwd(resolved, PATH_MAX) == NULL) {
9260e752b5Schristos if (mem_allocated)
9360e752b5Schristos free(resolved);
9460e752b5Schristos else
9560e752b5Schristos strlcpy(resolved, ".", PATH_MAX);
9660e752b5Schristos return (NULL);
9760e752b5Schristos }
9860e752b5Schristos resolved_len = strlen(resolved);
9960e752b5Schristos left_len = strlcpy(left, path, sizeof(left));
10060e752b5Schristos }
10160e752b5Schristos if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
10260e752b5Schristos errno = ENAMETOOLONG;
10360e752b5Schristos goto err;
10460e752b5Schristos }
10560e752b5Schristos
10660e752b5Schristos /*
10760e752b5Schristos * Iterate over path components in `left'.
10860e752b5Schristos */
10960e752b5Schristos while (left_len != 0) {
11060e752b5Schristos /*
11160e752b5Schristos * Extract the next path component and adjust `left'
11260e752b5Schristos * and its length.
11360e752b5Schristos */
11460e752b5Schristos p = strchr(left, '/');
11560e752b5Schristos s = p ? p : left + left_len;
11660e752b5Schristos if (s - left >= (ptrdiff_t)sizeof(next_token)) {
11760e752b5Schristos errno = ENAMETOOLONG;
11860e752b5Schristos goto err;
11960e752b5Schristos }
12060e752b5Schristos memcpy(next_token, left, s - left);
12160e752b5Schristos next_token[s - left] = '\0';
12260e752b5Schristos left_len -= s - left;
12360e752b5Schristos if (p != NULL)
12460e752b5Schristos memmove(left, s + 1, left_len + 1);
12560e752b5Schristos if (resolved[resolved_len - 1] != '/') {
12660e752b5Schristos if (resolved_len + 1 >= PATH_MAX) {
12760e752b5Schristos errno = ENAMETOOLONG;
12860e752b5Schristos goto err;
12960e752b5Schristos }
13060e752b5Schristos resolved[resolved_len++] = '/';
13160e752b5Schristos resolved[resolved_len] = '\0';
13260e752b5Schristos }
13360e752b5Schristos if (next_token[0] == '\0')
13460e752b5Schristos continue;
13560e752b5Schristos else if (strcmp(next_token, ".") == 0)
13660e752b5Schristos continue;
13760e752b5Schristos else if (strcmp(next_token, "..") == 0) {
13860e752b5Schristos /*
13960e752b5Schristos * Strip the last path component except when we have
14060e752b5Schristos * single "/"
14160e752b5Schristos */
14260e752b5Schristos if (resolved_len > 1) {
14360e752b5Schristos resolved[resolved_len - 1] = '\0';
14460e752b5Schristos q = strrchr(resolved, '/') + 1;
14560e752b5Schristos *q = '\0';
14660e752b5Schristos resolved_len = q - resolved;
14760e752b5Schristos }
14860e752b5Schristos continue;
14960e752b5Schristos }
15060e752b5Schristos
15160e752b5Schristos /*
15260e752b5Schristos * Append the next path component and lstat() it. If
15360e752b5Schristos * lstat() fails we still can return successfully if
15460e752b5Schristos * there are no more path components left.
15560e752b5Schristos */
15660e752b5Schristos resolved_len = strlcat(resolved, next_token, PATH_MAX);
15760e752b5Schristos if (resolved_len >= PATH_MAX) {
15860e752b5Schristos errno = ENAMETOOLONG;
15960e752b5Schristos goto err;
16060e752b5Schristos }
16160e752b5Schristos if (lstat(resolved, &sb) != 0) {
16260e752b5Schristos if (errno == ENOENT && p == NULL) {
16360e752b5Schristos errno = serrno;
16460e752b5Schristos return (resolved);
16560e752b5Schristos }
16660e752b5Schristos goto err;
16760e752b5Schristos }
16860e752b5Schristos if (S_ISLNK(sb.st_mode)) {
16960e752b5Schristos if (symlinks++ > SYMLOOP_MAX) {
17060e752b5Schristos errno = ELOOP;
17160e752b5Schristos goto err;
17260e752b5Schristos }
17360e752b5Schristos slen = readlink(resolved, symlink, sizeof(symlink) - 1);
17460e752b5Schristos if (slen < 0)
17560e752b5Schristos goto err;
17660e752b5Schristos symlink[slen] = '\0';
17760e752b5Schristos if (symlink[0] == '/') {
17860e752b5Schristos resolved[1] = 0;
17960e752b5Schristos resolved_len = 1;
18060e752b5Schristos } else if (resolved_len > 1) {
18160e752b5Schristos /* Strip the last path component. */
18260e752b5Schristos resolved[resolved_len - 1] = '\0';
18360e752b5Schristos q = strrchr(resolved, '/') + 1;
18460e752b5Schristos *q = '\0';
18560e752b5Schristos resolved_len = q - resolved;
18660e752b5Schristos }
18760e752b5Schristos
18860e752b5Schristos /*
18960e752b5Schristos * If there are any path components left, then
19060e752b5Schristos * append them to symlink. The result is placed
19160e752b5Schristos * in `left'.
19260e752b5Schristos */
19360e752b5Schristos if (p != NULL) {
19460e752b5Schristos if (symlink[slen - 1] != '/') {
19560e752b5Schristos if (slen + 1 >=
19660e752b5Schristos (ptrdiff_t)sizeof(symlink)) {
19760e752b5Schristos errno = ENAMETOOLONG;
19860e752b5Schristos goto err;
19960e752b5Schristos }
20060e752b5Schristos symlink[slen] = '/';
20160e752b5Schristos symlink[slen + 1] = 0;
20260e752b5Schristos }
20360e752b5Schristos left_len = strlcat(symlink, left, sizeof(symlink));
20460e752b5Schristos if (left_len >= sizeof(symlink)) {
20560e752b5Schristos errno = ENAMETOOLONG;
20660e752b5Schristos goto err;
20760e752b5Schristos }
20860e752b5Schristos }
20960e752b5Schristos left_len = strlcpy(left, symlink, sizeof(left));
21060e752b5Schristos }
21160e752b5Schristos }
21260e752b5Schristos
21360e752b5Schristos /*
21460e752b5Schristos * Remove trailing slash except when the resolved pathname
21560e752b5Schristos * is a single "/".
21660e752b5Schristos */
21760e752b5Schristos if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
21860e752b5Schristos resolved[resolved_len - 1] = '\0';
21960e752b5Schristos return (resolved);
22060e752b5Schristos
22160e752b5Schristos err:
22260e752b5Schristos if (mem_allocated)
22360e752b5Schristos free(resolved);
22460e752b5Schristos return (NULL);
22560e752b5Schristos }
226