You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.1 KiB

  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. the progressmeter formatting outside of signal handler context and have the
  6. atomicio callback called for EINTR too. bz#2434 with contributions from djm
  7. and jjelen at redhat.com, ok djm@
  8. OpenBSD-Commit-ID: 1af61c1f70e4f3bd8ab140b9f1fa699481db57d8
  9. CVE-2019-6109
  10. Origin: backport, https://anongit.mindrot.org/openssh.git/commit/?id=8976f1c4b2721c26e878151f52bdf346dfe2d54c
  11. Bug-Debian: https://bugs.debian.org/793412
  12. Last-Update: 2019-02-08
  13. Patch-Name: sanitize-scp-filenames-via-snmprintf.patch
  14. ---
  15. atomicio.c | 20 ++++++++++++++-----
  16. progressmeter.c | 53 ++++++++++++++++++++++---------------------------
  17. progressmeter.h | 3 ++-
  18. scp.c | 1 +
  19. sftp-client.c | 16 ++++++++-------
  20. 5 files changed, 51 insertions(+), 42 deletions(-)
  21. diff --git a/atomicio.c b/atomicio.c
  22. index f854a06f5..d91bd7621 100644
  23. --- a/atomicio.c
  24. +++ b/atomicio.c
  25. @@ -1,4 +1,4 @@
  26. -/* $OpenBSD: atomicio.c,v 1.28 2016/07/27 23:18:12 djm Exp $ */
  27. +/* $OpenBSD: atomicio.c,v 1.29 2019/01/23 08:01:46 dtucker Exp $ */
  28. /*
  29. * Copyright (c) 2006 Damien Miller. All rights reserved.
  30. * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
  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. @@ -1,4 +1,4 @@
  70. -/* $OpenBSD: progressmeter.c,v 1.45 2016/06/30 05:17:05 dtucker Exp $ */
  71. +/* $OpenBSD: progressmeter.c,v 1.46 2019/01/23 08:01:46 dtucker Exp $ */
  72. /*
  73. * Copyright (c) 2003 Nils Nordman. All rights reserved.
  74. *
  75. @@ -31,6 +31,7 @@
  76. #include <errno.h>
  77. #include <signal.h>
  78. +#include <stdarg.h>
  79. #include <stdio.h>
  80. #include <string.h>
  81. #include <time.h>
  82. @@ -39,6 +40,7 @@
  83. #include "progressmeter.h"
  84. #include "atomicio.h"
  85. #include "misc.h"
  86. +#include "utf8.h"
  87. #define DEFAULT_WINSIZE 80
  88. #define MAX_WINSIZE 512
  89. @@ -61,7 +63,7 @@ static void setscreensize(void);
  90. void refresh_progress_meter(void);
  91. /* signal handler for updating the progress meter */
  92. -static void update_progress_meter(int);
  93. +static void sig_alarm(int);
  94. static double start; /* start progress */
  95. static double last_update; /* last progress update */
  96. @@ -74,6 +76,7 @@ static long stalled; /* how long we have been stalled */
  97. static int bytes_per_second; /* current speed in bytes per second */
  98. static int win_size; /* terminal window size */
  99. static volatile sig_atomic_t win_resized; /* for window resizing */
  100. +static volatile sig_atomic_t alarm_fired;
  101. /* units for format_size */
  102. static const char unit[] = " KMGT";
  103. @@ -126,9 +129,17 @@ refresh_progress_meter(void)
  104. off_t bytes_left;
  105. int cur_speed;
  106. int hours, minutes, seconds;
  107. - int i, len;
  108. int file_len;
  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. /* filename */
  123. buf[0] = '\0';
  124. - file_len = win_size - 35;
  125. + file_len = win_size - 36;
  126. if (file_len > 0) {
  127. - len = snprintf(buf, file_len + 1, "\r%s", file);
  128. - if (len < 0)
  129. - len = 0;
  130. - if (len >= file_len + 1)
  131. - len = file_len;
  132. - for (i = len; i < file_len; i++)
  133. - buf[i] = ' ';
  134. - buf[file_len] = '\0';
  135. + buf[0] = '\r';
  136. + snmprintf(buf+1, sizeof(buf)-1 , &file_len, "%*s",
  137. + file_len * -1, file);
  138. }
  139. /* percent of transfer done */
  140. @@ -228,22 +234,11 @@ refresh_progress_meter(void)
  141. /*ARGSUSED*/
  142. static void
  143. -update_progress_meter(int ignore)
  144. +sig_alarm(int ignore)
  145. {
  146. - int save_errno;
  147. -
  148. - save_errno = errno;
  149. -
  150. - if (win_resized) {
  151. - setscreensize();
  152. - win_resized = 0;
  153. - }
  154. - if (can_output())
  155. - refresh_progress_meter();
  156. -
  157. - signal(SIGALRM, update_progress_meter);
  158. + signal(SIGALRM, sig_alarm);
  159. + alarm_fired = 1;
  160. alarm(UPDATE_INTERVAL);
  161. - errno = save_errno;
  162. }
  163. void
  164. @@ -259,10 +254,9 @@ start_progress_meter(const char *f, off_t filesize, off_t *ctr)
  165. bytes_per_second = 0;
  166. setscreensize();
  167. - if (can_output())
  168. - refresh_progress_meter();
  169. + refresh_progress_meter();
  170. - signal(SIGALRM, update_progress_meter);
  171. + signal(SIGALRM, sig_alarm);
  172. signal(SIGWINCH, sig_winch);
  173. alarm(UPDATE_INTERVAL);
  174. }
  175. @@ -286,6 +280,7 @@ stop_progress_meter(void)
  176. static void
  177. sig_winch(int sig)
  178. {
  179. + signal(SIGWINCH, sig_winch);
  180. win_resized = 1;
  181. }
  182. diff --git a/progressmeter.h b/progressmeter.h
  183. index bf179dca6..8f6678060 100644
  184. --- a/progressmeter.h
  185. +++ b/progressmeter.h
  186. @@ -1,4 +1,4 @@
  187. -/* $OpenBSD: progressmeter.h,v 1.3 2015/01/14 13:54:13 djm Exp $ */
  188. +/* $OpenBSD: progressmeter.h,v 1.4 2019/01/23 08:01:46 dtucker Exp $ */
  189. /*
  190. * Copyright (c) 2002 Nils Nordman. All rights reserved.
  191. *
  192. @@ -24,4 +24,5 @@
  193. */
  194. void start_progress_meter(const char *, off_t, off_t *);
  195. +void refresh_progress_meter(void);
  196. void stop_progress_meter(void);
  197. diff --git a/scp.c b/scp.c
  198. index 7163d33dc..80308573c 100644
  199. --- a/scp.c
  200. +++ b/scp.c
  201. @@ -593,6 +593,7 @@ scpio(void *_cnt, size_t s)
  202. off_t *cnt = (off_t *)_cnt;
  203. *cnt += s;
  204. + refresh_progress_meter();
  205. if (limit_kbps > 0)
  206. bandwidth_limit(&bwlimit, s);
  207. return 0;
  208. diff --git a/sftp-client.c b/sftp-client.c
  209. index 4986d6d8d..2bc698f86 100644
  210. --- a/sftp-client.c
  211. +++ b/sftp-client.c
  212. @@ -101,7 +101,9 @@ sftpio(void *_bwlimit, size_t amount)
  213. {
  214. struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
  215. - bandwidth_limit(bwlimit, amount);
  216. + refresh_progress_meter();
  217. + if (bwlimit != NULL)
  218. + bandwidth_limit(bwlimit, amount);
  219. return 0;
  220. }
  221. @@ -121,8 +123,8 @@ send_msg(struct sftp_conn *conn, struct sshbuf *m)
  222. iov[1].iov_base = (u_char *)sshbuf_ptr(m);
  223. iov[1].iov_len = sshbuf_len(m);
  224. - if (atomiciov6(writev, conn->fd_out, iov, 2,
  225. - conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
  226. + if (atomiciov6(writev, conn->fd_out, iov, 2, sftpio,
  227. + conn->limit_kbps > 0 ? &conn->bwlimit_out : NULL) !=
  228. sshbuf_len(m) + sizeof(mlen))
  229. fatal("Couldn't send packet: %s", strerror(errno));
  230. @@ -138,8 +140,8 @@ get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
  231. if ((r = sshbuf_reserve(m, 4, &p)) != 0)
  232. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  233. - if (atomicio6(read, conn->fd_in, p, 4,
  234. - conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
  235. + if (atomicio6(read, conn->fd_in, p, 4, sftpio,
  236. + conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL) != 4) {
  237. if (errno == EPIPE || errno == ECONNRESET)
  238. fatal("Connection closed");
  239. else
  240. @@ -157,8 +159,8 @@ get_msg_extended(struct sftp_conn *conn, struct sshbuf *m, int initial)
  241. if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
  242. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  243. - if (atomicio6(read, conn->fd_in, p, msg_len,
  244. - conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
  245. + if (atomicio6(read, conn->fd_in, p, msg_len, sftpio,
  246. + conn->limit_kbps > 0 ? &conn->bwlimit_in : NULL)
  247. != msg_len) {
  248. if (errno == EPIPE)
  249. fatal("Connection closed");