usbmuxd: Update to latest git
[feed/packages.git] / net / openssh / patches / 0003-sanitize-scp-filenames-via-snmprintf.patch
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
5
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@
9
10 OpenBSD-Commit-ID: 1af61c1f70e4f3bd8ab140b9f1fa699481db57d8
11
12 CVE-2019-6109
13
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
17
18 Patch-Name: sanitize-scp-filenames-via-snmprintf.patch
19 ---
20 atomicio.c | 20 ++++++++++++++-----
21 progressmeter.c | 53 ++++++++++++++++++++++---------------------------
22 progressmeter.h | 3 ++-
23 scp.c | 1 +
24 sftp-client.c | 16 ++++++++-------
25 5 files changed, 51 insertions(+), 42 deletions(-)
26
27 diff --git a/atomicio.c b/atomicio.c
28 index f854a06f5..d91bd7621 100644
29 --- a/atomicio.c
30 +++ b/atomicio.c
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);
33 switch (res) {
34 case -1:
35 - if (errno == EINTR)
36 + if (errno == EINTR) {
37 + /* possible SIGALARM, update callback */
38 + if (cb != NULL && cb(cb_arg, 0) == -1) {
39 + errno = EINTR;
40 + return pos;
41 + }
42 continue;
43 - if (errno == EAGAIN || errno == EWOULDBLOCK) {
44 + } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
45 #ifndef BROKEN_READ_COMPARISON
46 (void)poll(&pfd, 1, -1);
47 #endif
48 @@ -122,9 +127,14 @@ atomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
49 res = (f) (fd, iov, iovcnt);
50 switch (res) {
51 case -1:
52 - if (errno == EINTR)
53 + if (errno == EINTR) {
54 + /* possible SIGALARM, update callback */
55 + if (cb != NULL && cb(cb_arg, 0) == -1) {
56 + errno = EINTR;
57 + return pos;
58 + }
59 continue;
60 - if (errno == EAGAIN || errno == EWOULDBLOCK) {
61 + } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
62 #ifndef BROKEN_READV_COMPARISON
63 (void)poll(&pfd, 1, -1);
64 #endif
65 diff --git a/progressmeter.c b/progressmeter.c
66 index fe9bf52e4..add462dde 100644
67 --- a/progressmeter.c
68 +++ b/progressmeter.c
69 @@ -31,6 +31,7 @@
70
71 #include <errno.h>
72 #include <signal.h>
73 +#include <stdarg.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <time.h>
77 @@ -39,6 +40,7 @@
78 #include "progressmeter.h"
79 #include "atomicio.h"
80 #include "misc.h"
81 +#include "utf8.h"
82
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);
87
88 /* signal handler for updating the progress meter */
89 -static void update_progress_meter(int);
90 +static void sig_alarm(int);
91
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;
99
100 /* units for format_size */
101 static const char unit[] = " KMGT";
102 @@ -126,9 +129,17 @@ refresh_progress_meter(void)
103 off_t bytes_left;
104 int cur_speed;
105 int hours, minutes, seconds;
106 - int i, len;
107 int file_len;
108
109 + if ((!alarm_fired && !win_resized) || !can_output())
110 + return;
111 + alarm_fired = 0;
112 +
113 + if (win_resized) {
114 + setscreensize();
115 + win_resized = 0;
116 + }
117 +
118 transferred = *counter - (cur_pos ? cur_pos : start_pos);
119 cur_pos = *counter;
120 now = monotime_double();
121 @@ -158,16 +169,11 @@ refresh_progress_meter(void)
122
123 /* filename */
124 buf[0] = '\0';
125 - file_len = win_size - 35;
126 + file_len = win_size - 36;
127 if (file_len > 0) {
128 - len = snprintf(buf, file_len + 1, "\r%s", file);
129 - if (len < 0)
130 - len = 0;
131 - if (len >= file_len + 1)
132 - len = file_len;
133 - for (i = len; i < file_len; i++)
134 - buf[i] = ' ';
135 - buf[file_len] = '\0';
136 + buf[0] = '\r';
137 + snmprintf(buf+1, sizeof(buf)-1 , &file_len, "%*s",
138 + file_len * -1, file);
139 }
140
141 /* percent of transfer done */
142 @@ -228,22 +234,11 @@ refresh_progress_meter(void)
143
144 /*ARGSUSED*/
145 static void
146 -update_progress_meter(int ignore)
147 +sig_alarm(int ignore)
148 {
149 - int save_errno;
150 -
151 - save_errno = errno;
152 -
153 - if (win_resized) {
154 - setscreensize();
155 - win_resized = 0;
156 - }
157 - if (can_output())
158 - refresh_progress_meter();
159 -
160 - signal(SIGALRM, update_progress_meter);
161 + signal(SIGALRM, sig_alarm);
162 + alarm_fired = 1;
163 alarm(UPDATE_INTERVAL);
164 - errno = save_errno;
165 }
166
167 void
168 @@ -259,10 +254,9 @@ start_progress_meter(const char *f, off_t filesize, off_t *ctr)
169 bytes_per_second = 0;
170
171 setscreensize();
172 - if (can_output())
173 - refresh_progress_meter();
174 + refresh_progress_meter();
175
176 - signal(SIGALRM, update_progress_meter);
177 + signal(SIGALRM, sig_alarm);
178 signal(SIGWINCH, sig_winch);
179 alarm(UPDATE_INTERVAL);
180 }
181 @@ -286,6 +280,7 @@ stop_progress_meter(void)
182 static void
183 sig_winch(int sig)
184 {
185 + signal(SIGWINCH, sig_winch);
186 win_resized = 1;
187 }
188
189 diff --git a/progressmeter.h b/progressmeter.h
190 index bf179dca6..8f6678060 100644
191 --- a/progressmeter.h
192 +++ b/progressmeter.h
193 @@ -24,4 +24,5 @@
194 */
195
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
201 --- a/scp.c
202 +++ b/scp.c
203 @@ -593,6 +593,7 @@ scpio(void *_cnt, size_t s)
204 off_t *cnt = (off_t *)_cnt;
205
206 *cnt += s;
207 + refresh_progress_meter();
208 if (limit_kbps > 0)
209 bandwidth_limit(&bwlimit, s);
210 return 0;
211 diff --git a/sftp-client.c b/sftp-client.c
212 index 4986d6d8d..2bc698f86 100644
213 --- a/sftp-client.c
214 +++ b/sftp-client.c
215 @@ -101,7 +101,9 @@ sftpio(void *_bwlimit, size_t amount)
216 {
217 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
218
219 - bandwidth_limit(bwlimit, amount);
220 + refresh_progress_meter();
221 + if (bwlimit != NULL)
222 + bandwidth_limit(bwlimit, amount);
223 return 0;
224 }
225
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);
229
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));
236
237 @@ -138,8 +140,8 @@ get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
238
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");
247 else
248 @@ -157,8 +159,8 @@ get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
249
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)
256 != msg_len) {
257 if (errno == EPIPE)
258 fatal("Connection closed");