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