summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.in4
-rw-r--r--sftp-realpath.c224
-rw-r--r--sftp-server.c6
-rw-r--r--ssh-keygen.c3
4 files changed, 231 insertions, 6 deletions
diff --git a/Makefile.in b/Makefile.in
index b84ccaa1a..6abb60fd0 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -116,7 +116,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o \
116 monitor.o monitor_wrap.o auth-krb5.o \ 116 monitor.o monitor_wrap.o auth-krb5.o \
117 auth2-gss.o gss-serv.o gss-serv-krb5.o \ 117 auth2-gss.o gss-serv.o gss-serv-krb5.o \
118 loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \ 118 loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
119 sftp-server.o sftp-common.o \ 119 sftp-server.o sftp-common.o sftp-realpath.o \
120 sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \ 120 sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
121 sandbox-seccomp-filter.o sandbox-capsicum.o sandbox-pledge.o \ 121 sandbox-seccomp-filter.o sandbox-capsicum.o sandbox-pledge.o \
122 sandbox-solaris.o uidswap.o 122 sandbox-solaris.o uidswap.o
@@ -196,7 +196,7 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11
196ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o 196ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
197 $(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS) 197 $(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
198 198
199sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o 199sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-realpath.o sftp-server-main.o
200 $(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) 200 $(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
201 201
202sftp$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-client.o sftp-common.o sftp-glob.o progressmeter.o 202sftp$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-client.o sftp-common.o sftp-glob.o progressmeter.o
diff --git a/sftp-realpath.c b/sftp-realpath.c
new file mode 100644
index 000000000..0ed808f46
--- /dev/null
+++ b/sftp-realpath.c
@@ -0,0 +1,224 @@
1/* $OpenBSD: sftp-realpath.c,v 1.1 2019/07/05 04:55:40 djm Exp $ */
2/*
3 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/types.h>
31#include <sys/param.h>
32#include <sys/stat.h>
33
34#include <errno.h>
35#include <stdlib.h>
36#include <stddef.h>
37#include <string.h>
38#include <unistd.h>
39#include <limits.h>
40
41#ifndef SYMLOOP_MAX
42# define SYMLOOP_MAX 32
43#endif
44
45/* XXX rewrite sftp-server to use POSIX realpath and remove this hack */
46
47char *sftp_realpath(const char *path, char *resolved);
48
49/*
50 * char *realpath(const char *path, char resolved[PATH_MAX]);
51 *
52 * Find the real name of path, by removing all ".", ".." and symlink
53 * components. Returns (resolved) on success, or (NULL) on failure,
54 * in which case the path which caused trouble is left in (resolved).
55 */
56char *
57sftp_realpath(const char *path, char *resolved)
58{
59 struct stat sb;
60 char *p, *q, *s;
61 size_t left_len, resolved_len;
62 unsigned symlinks;
63 int serrno, slen, mem_allocated;
64 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
65
66 if (path[0] == '\0') {
67 errno = ENOENT;
68 return (NULL);
69 }
70
71 serrno = errno;
72
73 if (resolved == NULL) {
74 resolved = malloc(PATH_MAX);
75 if (resolved == NULL)
76 return (NULL);
77 mem_allocated = 1;
78 } else
79 mem_allocated = 0;
80
81 symlinks = 0;
82 if (path[0] == '/') {
83 resolved[0] = '/';
84 resolved[1] = '\0';
85 if (path[1] == '\0')
86 return (resolved);
87 resolved_len = 1;
88 left_len = strlcpy(left, path + 1, sizeof(left));
89 } else {
90 if (getcwd(resolved, PATH_MAX) == NULL) {
91 if (mem_allocated)
92 free(resolved);
93 else
94 strlcpy(resolved, ".", PATH_MAX);
95 return (NULL);
96 }
97 resolved_len = strlen(resolved);
98 left_len = strlcpy(left, path, sizeof(left));
99 }
100 if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
101 errno = ENAMETOOLONG;
102 goto err;
103 }
104
105 /*
106 * Iterate over path components in `left'.
107 */
108 while (left_len != 0) {
109 /*
110 * Extract the next path component and adjust `left'
111 * and its length.
112 */
113 p = strchr(left, '/');
114 s = p ? p : left + left_len;
115 if (s - left >= (ptrdiff_t)sizeof(next_token)) {
116 errno = ENAMETOOLONG;
117 goto err;
118 }
119 memcpy(next_token, left, s - left);
120 next_token[s - left] = '\0';
121 left_len -= s - left;
122 if (p != NULL)
123 memmove(left, s + 1, left_len + 1);
124 if (resolved[resolved_len - 1] != '/') {
125 if (resolved_len + 1 >= PATH_MAX) {
126 errno = ENAMETOOLONG;
127 goto err;
128 }
129 resolved[resolved_len++] = '/';
130 resolved[resolved_len] = '\0';
131 }
132 if (next_token[0] == '\0')
133 continue;
134 else if (strcmp(next_token, ".") == 0)
135 continue;
136 else if (strcmp(next_token, "..") == 0) {
137 /*
138 * Strip the last path component except when we have
139 * single "/"
140 */
141 if (resolved_len > 1) {
142 resolved[resolved_len - 1] = '\0';
143 q = strrchr(resolved, '/') + 1;
144 *q = '\0';
145 resolved_len = q - resolved;
146 }
147 continue;
148 }
149
150 /*
151 * Append the next path component and lstat() it. If
152 * lstat() fails we still can return successfully if
153 * there are no more path components left.
154 */
155 resolved_len = strlcat(resolved, next_token, PATH_MAX);
156 if (resolved_len >= PATH_MAX) {
157 errno = ENAMETOOLONG;
158 goto err;
159 }
160 if (lstat(resolved, &sb) != 0) {
161 if (errno == ENOENT && p == NULL) {
162 errno = serrno;
163 return (resolved);
164 }
165 goto err;
166 }
167 if (S_ISLNK(sb.st_mode)) {
168 if (symlinks++ > SYMLOOP_MAX) {
169 errno = ELOOP;
170 goto err;
171 }
172 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
173 if (slen < 0)
174 goto err;
175 symlink[slen] = '\0';
176 if (symlink[0] == '/') {
177 resolved[1] = 0;
178 resolved_len = 1;
179 } else if (resolved_len > 1) {
180 /* Strip the last path component. */
181 resolved[resolved_len - 1] = '\0';
182 q = strrchr(resolved, '/') + 1;
183 *q = '\0';
184 resolved_len = q - resolved;
185 }
186
187 /*
188 * If there are any path components left, then
189 * append them to symlink. The result is placed
190 * in `left'.
191 */
192 if (p != NULL) {
193 if (symlink[slen - 1] != '/') {
194 if (slen + 1 >=
195 (ptrdiff_t)sizeof(symlink)) {
196 errno = ENAMETOOLONG;
197 goto err;
198 }
199 symlink[slen] = '/';
200 symlink[slen + 1] = 0;
201 }
202 left_len = strlcat(symlink, left, sizeof(symlink));
203 if (left_len >= sizeof(symlink)) {
204 errno = ENAMETOOLONG;
205 goto err;
206 }
207 }
208 left_len = strlcpy(left, symlink, sizeof(left));
209 }
210 }
211
212 /*
213 * Remove trailing slash except when the resolved pathname
214 * is a single "/".
215 */
216 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
217 resolved[resolved_len - 1] = '\0';
218 return (resolved);
219
220err:
221 if (mem_allocated)
222 free(resolved);
223 return (NULL);
224}
diff --git a/sftp-server.c b/sftp-server.c
index e7dd33b2f..359204fa7 100644
--- a/sftp-server.c
+++ b/sftp-server.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sftp-server.c,v 1.116 2019/06/28 13:35:04 deraadt Exp $ */ 1/* $OpenBSD: sftp-server.c,v 1.117 2019/07/05 04:55:40 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
4 * 4 *
@@ -51,6 +51,8 @@
51#include "sftp.h" 51#include "sftp.h"
52#include "sftp-common.h" 52#include "sftp-common.h"
53 53
54char *sftp_realpath(const char *, char *); /* sftp-realpath.c */
55
54/* Our verbosity */ 56/* Our verbosity */
55static LogLevel log_level = SYSLOG_LEVEL_ERROR; 57static LogLevel log_level = SYSLOG_LEVEL_ERROR;
56 58
@@ -1174,7 +1176,7 @@ process_realpath(u_int32_t id)
1174 } 1176 }
1175 debug3("request %u: realpath", id); 1177 debug3("request %u: realpath", id);
1176 verbose("realpath \"%s\"", path); 1178 verbose("realpath \"%s\"", path);
1177 if (realpath(path, resolvedname) == NULL) { 1179 if (sftp_realpath(path, resolvedname) == NULL) {
1178 send_status(id, errno_to_portable(errno)); 1180 send_status(id, errno_to_portable(errno));
1179 } else { 1181 } else {
1180 Stat s; 1182 Stat s;
diff --git a/ssh-keygen.c b/ssh-keygen.c
index 3aa4f5125..d00ce494f 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh-keygen.c,v 1.333 2019/06/28 13:35:04 deraadt Exp $ */ 1/* $OpenBSD: ssh-keygen.c,v 1.334 2019/07/05 04:55:40 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -43,7 +43,6 @@
43#include "xmalloc.h" 43#include "xmalloc.h"
44#include "sshkey.h" 44#include "sshkey.h"
45#include "authfile.h" 45#include "authfile.h"
46#include "uuencode.h"
47#include "sshbuf.h" 46#include "sshbuf.h"
48#include "pathnames.h" 47#include "pathnames.h"
49#include "log.h" 48#include "log.h"