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