3a2fd7
diff --color -ruN a/Makefile.in b/Makefile.in
3a2fd7
--- a/Makefile.in	2022-06-23 11:31:10.168186838 +0200
3a2fd7
+++ b/Makefile.in	2022-06-23 11:32:19.146513347 +0200
3a2fd7
@@ -125,7 +125,7 @@
3a2fd7
 	monitor.o monitor_wrap.o auth-krb5.o \
3a2fd7
 	auth2-gss.o gss-serv.o gss-serv-krb5.o kexgsss.o \
3a2fd7
 	loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
3a2fd7
-	sftp-server.o sftp-common.o \
3a2fd7
+	sftp-server.o sftp-common.o sftp-realpath.o \
3a2fd7
 	sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
3a2fd7
 	sandbox-seccomp-filter.o sandbox-capsicum.o sandbox-pledge.o \
3a2fd7
 	sandbox-solaris.o uidswap.o
3a2fd7
@@ -217,8 +217,8 @@
3a2fd7
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
3a2fd7
 	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
3a2fd7
 
3a2fd7
-sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
3a2fd7
-	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3a2fd7
+sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-realpath.o sftp-server.o sftp-server-main.o
3a2fd7
+	$(LD) -o $@ sftp-server.o sftp-common.o sftp-realpath.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3a2fd7
 
3a2fd7
 sftp$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-client.o sftp-common.o sftp-glob.o progressmeter.o
3a2fd7
 	$(LD) -o $@ progressmeter.o sftp.o sftp-client.o sftp-common.o sftp-glob.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) $(LIBEDIT)
3a2fd7
diff --color -ruN a/sftp-realpath.c b/sftp-realpath.c
3a2fd7
--- a/sftp-realpath.c	1970-01-01 01:00:00.000000000 +0100
3a2fd7
+++ b/sftp-realpath.c	2022-06-23 11:35:33.193244873 +0200
3a2fd7
@@ -0,0 +1,225 @@
3a2fd7
+/*	$OpenBSD: sftp-realpath.c,v 1.2 2021/09/02 21:03:54 deraadt Exp $ */
3a2fd7
+/*
3a2fd7
+ * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
3a2fd7
+ *
3a2fd7
+ * Redistribution and use in source and binary forms, with or without
3a2fd7
+ * modification, are permitted provided that the following conditions
3a2fd7
+ * are met:
3a2fd7
+ * 1. Redistributions of source code must retain the above copyright
3a2fd7
+ *    notice, this list of conditions and the following disclaimer.
3a2fd7
+ * 2. Redistributions in binary form must reproduce the above copyright
3a2fd7
+ *    notice, this list of conditions and the following disclaimer in the
3a2fd7
+ *    documentation and/or other materials provided with the distribution.
3a2fd7
+ * 3. The names of the authors may not be used to endorse or promote
3a2fd7
+ *    products derived from this software without specific prior written
3a2fd7
+ *    permission.
3a2fd7
+ *
3a2fd7
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3a2fd7
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3a2fd7
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3a2fd7
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3a2fd7
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3a2fd7
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3a2fd7
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3a2fd7
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3a2fd7
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3a2fd7
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3a2fd7
+ * SUCH DAMAGE.
3a2fd7
+ */
3a2fd7
+
3a2fd7
+#include "includes.h"
3a2fd7
+
3a2fd7
+#include <sys/types.h>
3a2fd7
+#include <sys/stat.h>
3a2fd7
+
3a2fd7
+#include <errno.h>
3a2fd7
+#include <stdlib.h>
3a2fd7
+#include <stddef.h>
3a2fd7
+#include <string.h>
3a2fd7
+#include <unistd.h>
3a2fd7
+#include <limits.h>
3a2fd7
+
3a2fd7
+#ifndef SYMLOOP_MAX
3a2fd7
+# define SYMLOOP_MAX 32
3a2fd7
+#endif
3a2fd7
+
3a2fd7
+/* XXX rewrite sftp-server to use POSIX realpath and remove this hack */
3a2fd7
+
3a2fd7
+char *sftp_realpath(const char *path, char *resolved);
3a2fd7
+
3a2fd7
+/*
3a2fd7
+ * char *realpath(const char *path, char resolved[PATH_MAX]);
3a2fd7
+ *
3a2fd7
+ * Find the real name of path, by removing all ".", ".." and symlink
3a2fd7
+ * components.  Returns (resolved) on success, or (NULL) on failure,
3a2fd7
+ * in which case the path which caused trouble is left in (resolved).
3a2fd7
+ */
3a2fd7
+char *
3a2fd7
+sftp_realpath(const char *path, char *resolved)
3a2fd7
+{
3a2fd7
+	struct stat sb;
3a2fd7
+	char *p, *q, *s;
3a2fd7
+	size_t left_len, resolved_len;
3a2fd7
+	unsigned symlinks;
3a2fd7
+	int serrno, slen, mem_allocated;
3a2fd7
+	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
3a2fd7
+
3a2fd7
+	if (path[0] == '\0') {
3a2fd7
+		errno = ENOENT;
3a2fd7
+		return (NULL);
3a2fd7
+	}
3a2fd7
+
3a2fd7
+	serrno = errno;
3a2fd7
+
3a2fd7
+	if (resolved == NULL) {
3a2fd7
+		resolved = malloc(PATH_MAX);
3a2fd7
+		if (resolved == NULL)
3a2fd7
+			return (NULL);
3a2fd7
+		mem_allocated = 1;
3a2fd7
+	} else
3a2fd7
+		mem_allocated = 0;
3a2fd7
+
3a2fd7
+	symlinks = 0;
3a2fd7
+	if (path[0] == '/') {
3a2fd7
+		resolved[0] = '/';
3a2fd7
+		resolved[1] = '\0';
3a2fd7
+		if (path[1] == '\0')
3a2fd7
+			return (resolved);
3a2fd7
+		resolved_len = 1;
3a2fd7
+		left_len = strlcpy(left, path + 1, sizeof(left));
3a2fd7
+	} else {
3a2fd7
+		if (getcwd(resolved, PATH_MAX) == NULL) {
3a2fd7
+			if (mem_allocated)
3a2fd7
+				free(resolved);
3a2fd7
+			else
3a2fd7
+				strlcpy(resolved, ".", PATH_MAX);
3a2fd7
+			return (NULL);
3a2fd7
+		}
3a2fd7
+		resolved_len = strlen(resolved);
3a2fd7
+		left_len = strlcpy(left, path, sizeof(left));
3a2fd7
+	}
3a2fd7
+	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
3a2fd7
+		errno = ENAMETOOLONG;
3a2fd7
+		goto err;
3a2fd7
+	}
3a2fd7
+
3a2fd7
+	/*
3a2fd7
+	 * Iterate over path components in `left'.
3a2fd7
+	 */
3a2fd7
+	while (left_len != 0) {
3a2fd7
+		/*
3a2fd7
+		 * Extract the next path component and adjust `left'
3a2fd7
+		 * and its length.
3a2fd7
+		 */
3a2fd7
+		p = strchr(left, '/');
3a2fd7
+		s = p ? p : left + left_len;
3a2fd7
+		if (s - left >= (ptrdiff_t)sizeof(next_token)) {
3a2fd7
+			errno = ENAMETOOLONG;
3a2fd7
+			goto err;
3a2fd7
+		}
3a2fd7
+		memcpy(next_token, left, s - left);
3a2fd7
+		next_token[s - left] = '\0';
3a2fd7
+		left_len -= s - left;
3a2fd7
+		if (p != NULL)
3a2fd7
+			memmove(left, s + 1, left_len + 1);
3a2fd7
+		if (resolved[resolved_len - 1] != '/') {
3a2fd7
+			if (resolved_len + 1 >= PATH_MAX) {
3a2fd7
+				errno = ENAMETOOLONG;
3a2fd7
+				goto err;
3a2fd7
+			}
3a2fd7
+			resolved[resolved_len++] = '/';
3a2fd7
+			resolved[resolved_len] = '\0';
3a2fd7
+		}
3a2fd7
+		if (next_token[0] == '\0')
3a2fd7
+			continue;
3a2fd7
+		else if (strcmp(next_token, ".") == 0)
3a2fd7
+			continue;
3a2fd7
+		else if (strcmp(next_token, "..") == 0) {
3a2fd7
+			/*
3a2fd7
+			 * Strip the last path component except when we have
3a2fd7
+			 * single "/"
3a2fd7
+			 */
3a2fd7
+			if (resolved_len > 1) {
3a2fd7
+				resolved[resolved_len - 1] = '\0';
3a2fd7
+				q = strrchr(resolved, '/') + 1;
3a2fd7
+				*q = '\0';
3a2fd7
+				resolved_len = q - resolved;
3a2fd7
+			}
3a2fd7
+			continue;
3a2fd7
+		}
3a2fd7
+
3a2fd7
+		/*
3a2fd7
+		 * Append the next path component and lstat() it. If
3a2fd7
+		 * lstat() fails we still can return successfully if
3a2fd7
+		 * there are no more path components left.
3a2fd7
+		 */
3a2fd7
+		resolved_len = strlcat(resolved, next_token, PATH_MAX);
3a2fd7
+		if (resolved_len >= PATH_MAX) {
3a2fd7
+			errno = ENAMETOOLONG;
3a2fd7
+			goto err;
3a2fd7
+		}
3a2fd7
+		if (lstat(resolved, &sb) != 0) {
3a2fd7
+			if (errno == ENOENT && p == NULL) {
3a2fd7
+				errno = serrno;
3a2fd7
+				return (resolved);
3a2fd7
+			}
3a2fd7
+			goto err;
3a2fd7
+		}
3a2fd7
+		if (S_ISLNK(sb.st_mode)) {
3a2fd7
+			if (symlinks++ > SYMLOOP_MAX) {
3a2fd7
+				errno = ELOOP;
3a2fd7
+				goto err;
3a2fd7
+			}
3a2fd7
+			slen = readlink(resolved, symlink, sizeof(symlink) - 1);
3a2fd7
+			if (slen < 0)
3a2fd7
+				goto err;
3a2fd7
+			symlink[slen] = '\0';
3a2fd7
+			if (symlink[0] == '/') {
3a2fd7
+				resolved[1] = 0;
3a2fd7
+				resolved_len = 1;
3a2fd7
+			} else if (resolved_len > 1) {
3a2fd7
+				/* Strip the last path component. */
3a2fd7
+				resolved[resolved_len - 1] = '\0';
3a2fd7
+				q = strrchr(resolved, '/') + 1;
3a2fd7
+				*q = '\0';
3a2fd7
+				resolved_len = q - resolved;
3a2fd7
+			}
3a2fd7
+
3a2fd7
+			/*
3a2fd7
+			 * If there are any path components left, then
3a2fd7
+			 * append them to symlink. The result is placed
3a2fd7
+			 * in `left'.
3a2fd7
+			 */
3a2fd7
+			if (p != NULL) {
3a2fd7
+				if (symlink[slen - 1] != '/') {
3a2fd7
+					if (slen + 1 >=
3a2fd7
+					    (ptrdiff_t)sizeof(symlink)) {
3a2fd7
+						errno = ENAMETOOLONG;
3a2fd7
+						goto err;
3a2fd7
+					}
3a2fd7
+					symlink[slen] = '/';
3a2fd7
+					symlink[slen + 1] = 0;
3a2fd7
+				}
3a2fd7
+				left_len = strlcat(symlink, left, sizeof(symlink));
3a2fd7
+				if (left_len >= sizeof(symlink)) {
3a2fd7
+					errno = ENAMETOOLONG;
3a2fd7
+					goto err;
3a2fd7
+				}
3a2fd7
+			}
3a2fd7
+			left_len = strlcpy(left, symlink, sizeof(left));
3a2fd7
+		}
3a2fd7
+	}
3a2fd7
+
3a2fd7
+	/*
3a2fd7
+	 * Remove trailing slash except when the resolved pathname
3a2fd7
+	 * is a single "/".
3a2fd7
+	 */
3a2fd7
+	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
3a2fd7
+		resolved[resolved_len - 1] = '\0';
3a2fd7
+	return (resolved);
3a2fd7
+
3a2fd7
+err:
3a2fd7
+	if (mem_allocated)
3a2fd7
+		free(resolved);
3a2fd7
+	return (NULL);
3a2fd7
+}
3a2fd7
diff --color -ruN a/sftp-server.c b/sftp-server.c
3a2fd7
--- a/sftp-server.c	2022-06-23 11:31:10.147186434 +0200
3a2fd7
+++ b/sftp-server.c	2022-06-23 11:32:19.147513366 +0200
3a2fd7
@@ -51,6 +51,8 @@
3a2fd7
 #include "sftp.h"
3a2fd7
 #include "sftp-common.h"
3a2fd7
 
3a2fd7
+char *sftp_realpath(const char *, char *); /* sftp-realpath.c */
3a2fd7
+
3a2fd7
 /* Our verbosity */
3a2fd7
 static LogLevel log_level = SYSLOG_LEVEL_ERROR;
3a2fd7
 
3a2fd7
@@ -1185,7 +1187,7 @@
3a2fd7
 	}
3a2fd7
 	debug3("request %u: realpath", id);
3a2fd7
 	verbose("realpath \"%s\"", path);
3a2fd7
-	if (realpath(path, resolvedname) == NULL) {
3a2fd7
+	if (sftp_realpath(path, resolvedname) == NULL) {
3a2fd7
 		send_status(id, errno_to_portable(errno));
3a2fd7
 	} else {
3a2fd7
 		Stat s;