1 From 11b88754cadcad0ba79b4ffcc127223248dccb54 Mon Sep 17 00:00:00 2001
2 From: "dtucker@openbsd.org" <dtucker@openbsd.org>
3 Date: Wed, 23 Jan 2019 08:01:46 +0000
4 Subject: upstream: Sanitize scp filenames via snmprintf. To do this we move
6 the progressmeter formatting outside of signal handler context and have the
7 atomicio callback called for EINTR too. bz#2434 with contributions from djm
8 and jjelen at redhat.com, ok djm@
10 OpenBSD-Commit-ID: 1af61c1f70e4f3bd8ab140b9f1fa699481db57d8
14 Origin: backport, https://anongit.mindrot.org/openssh.git/commit/?id=8976f1c4b2721c26e878151f52bdf346dfe2d54c
15 Bug-Debian: https://bugs.debian.org/793412
16 Last-Update: 2019-02-08
18 Patch-Name: sanitize-scp-filenames-via-snmprintf.patch
20 atomicio.c | 20 ++++++++++++++-----
21 progressmeter.c | 53 ++++++++++++++++++++++---------------------------
22 progressmeter.h | 3 ++-
24 sftp-client.c | 16 ++++++++-------
25 5 files changed, 51 insertions(+), 42 deletions(-)
27 diff --git a/atomicio.c b/atomicio.c
28 index f854a06f5..d91bd7621 100644
31 @@ -65,9 +65,14 @@ atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n,
32 res = (f) (fd, s + pos, n - pos);
36 + if (errno == EINTR) {
37 + /* possible SIGALARM, update callback */
38 + if (cb != NULL && cb(cb_arg, 0) == -1) {
43 - if (errno == EAGAIN || errno == EWOULDBLOCK) {
44 + } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
45 #ifndef BROKEN_READ_COMPARISON
46 (void)poll(&pfd, 1, -1);
48 @@ -122,9 +127,14 @@ atomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
49 res = (f) (fd, iov, iovcnt);
53 + if (errno == EINTR) {
54 + /* possible SIGALARM, update callback */
55 + if (cb != NULL && cb(cb_arg, 0) == -1) {
60 - if (errno == EAGAIN || errno == EWOULDBLOCK) {
61 + } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
62 #ifndef BROKEN_READV_COMPARISON
63 (void)poll(&pfd, 1, -1);
65 diff --git a/progressmeter.c b/progressmeter.c
66 index fe9bf52e4..add462dde 100644
78 #include "progressmeter.h"
83 #define DEFAULT_WINSIZE 80
84 #define MAX_WINSIZE 512
85 @@ -61,7 +63,7 @@ static void setscreensize(void);
86 void refresh_progress_meter(void);
88 /* signal handler for updating the progress meter */
89 -static void update_progress_meter(int);
90 +static void sig_alarm(int);
92 static double start; /* start progress */
93 static double last_update; /* last progress update */
94 @@ -74,6 +76,7 @@ static long stalled; /* how long we have been stalled */
95 static int bytes_per_second; /* current speed in bytes per second */
96 static int win_size; /* terminal window size */
97 static volatile sig_atomic_t win_resized; /* for window resizing */
98 +static volatile sig_atomic_t alarm_fired;
100 /* units for format_size */
101 static const char unit[] = " KMGT";
102 @@ -126,9 +129,17 @@ refresh_progress_meter(void)
105 int hours, minutes, seconds;
109 + if ((!alarm_fired && !win_resized) || !can_output())
118 transferred = *counter - (cur_pos ? cur_pos : start_pos);
120 now = monotime_double();
121 @@ -158,16 +169,11 @@ refresh_progress_meter(void)
125 - file_len = win_size - 35;
126 + file_len = win_size - 36;
128 - len = snprintf(buf, file_len + 1, "\r%s", file);
131 - if (len >= file_len + 1)
133 - for (i = len; i < file_len; i++)
135 - buf[file_len] = '\0';
137 + snmprintf(buf+1, sizeof(buf)-1 , &file_len, "%*s",
138 + file_len * -1, file);
141 /* percent of transfer done */
142 @@ -228,22 +234,11 @@ refresh_progress_meter(void)
146 -update_progress_meter(int ignore)
147 +sig_alarm(int ignore)
151 - save_errno = errno;
158 - refresh_progress_meter();
160 - signal(SIGALRM, update_progress_meter);
161 + signal(SIGALRM, sig_alarm);
163 alarm(UPDATE_INTERVAL);
164 - errno = save_errno;
168 @@ -259,10 +254,9 @@ start_progress_meter(const char *f, off_t filesize, off_t *ctr)
169 bytes_per_second = 0;
173 - refresh_progress_meter();
174 + refresh_progress_meter();
176 - signal(SIGALRM, update_progress_meter);
177 + signal(SIGALRM, sig_alarm);
178 signal(SIGWINCH, sig_winch);
179 alarm(UPDATE_INTERVAL);
181 @@ -286,6 +280,7 @@ stop_progress_meter(void)
185 + signal(SIGWINCH, sig_winch);
189 diff --git a/progressmeter.h b/progressmeter.h
190 index bf179dca6..8f6678060 100644
191 --- a/progressmeter.h
192 +++ b/progressmeter.h
196 void start_progress_meter(const char *, off_t, off_t *);
197 +void refresh_progress_meter(void);
198 void stop_progress_meter(void);
199 diff --git a/scp.c b/scp.c
200 index 7163d33dc..80308573c 100644
203 @@ -593,6 +593,7 @@ scpio(void *_cnt, size_t s)
204 off_t *cnt = (off_t *)_cnt;
207 + refresh_progress_meter();
209 bandwidth_limit(&bwlimit, s);
211 diff --git a/sftp-client.c b/sftp-client.c
212 index 4986d6d8d..2bc698f86 100644
215 @@ -101,7 +101,9 @@ sftpio(void *_bwlimit, size_t amount)
217 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
219 - bandwidth_limit(bwlimit, amount);
220 + refresh_progress_meter();
221 + if (bwlimit != NULL)
222 + bandwidth_limit(bwlimit, amount);
226 @@ -121,8 +123,8 @@ send_msg(struct sftp_conn *conn, struct sshbuf *m)
227 iov[1].iov_base = (u_char *)sshbuf_ptr(m);
228 iov[1].iov_len = sshbuf_len(m);
230 - if (atomiciov6(writev, conn->fd_out, iov, 2,
231 - conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
232 + if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
233 + conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
234 sshbuf_len(m) + sizeof(mlen))
235 fatal("Couldn't send packet: %s", strerror(errno));
237 @@ -138,8 +140,8 @@ get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
239 if ((r = sshbuf_reserve(m, 4, &p)) != 0)
240 fatal("%s: buffer error: %s", __func__, ssh_err(r));
241 - if (atomicio6(read, conn->fd_in, p, 4,
242 - conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
243 + if (atomicio6(read, conn->fd_in, p, 4, sftpio,
244 + conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
245 if (errno == EPIPE || errno == ECONNRESET)
246 fatal("Connection closed");
248 @@ -157,8 +159,8 @@ get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
250 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
251 fatal("%s: buffer error: %s", __func__, ssh_err(r));
252 - if (atomicio6(read, conn->fd_in, p, msg_len,
253 - conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
254 + if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
255 + conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
258 fatal("Connection closed");