summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@cvs.openbsd.org>2017-11-25 06:46:23 +0000
committerDarren Tucker <dtucker@cvs.openbsd.org>2017-11-25 06:46:23 +0000
commit8dc408dc9c5ed5904f73b4f3c8e43cbcb3bd21fc (patch)
tree8485a6266d0cf60cc234c0212031c9143e6912a4 /usr.bin
parent11811783a1ed46f25063c9e930d70b1a181af280 (diff)
Add monotime_ts and monotime_tv that return monotonic timespec and
timeval respectively. Replace calls to gettimeofday() in packet timing with monotime_tv so that the callers will work over a clock step. Should prevent integer overflow during clock steps reported by wangle6 at huawei.com. "I like" markus@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/ssh/misc.c39
-rw-r--r--usr.bin/ssh/misc.h4
-rw-r--r--usr.bin/ssh/packet.c6
-rw-r--r--usr.bin/ssh/ssh-keyscan.c8
-rw-r--r--usr.bin/ssh/sshconnect.c4
5 files changed, 38 insertions, 23 deletions
diff --git a/usr.bin/ssh/misc.c b/usr.bin/ssh/misc.c
index aee2d030fef..547f2bd3226 100644
--- a/usr.bin/ssh/misc.c
+++ b/usr.bin/ssh/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.118 2017/10/25 00:17:08 djm Exp $ */
+/* $OpenBSD: misc.c,v 1.119 2017/11/25 06:46:22 dtucker Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@@ -1219,8 +1219,8 @@ ms_subtract_diff(struct timeval *start, int *ms)
{
struct timeval diff, finish;
- gettimeofday(&finish, NULL);
- timersub(&finish, start, &diff);
+ monotime_tv(&finish);
+ timersub(&finish, start, &diff);
*ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000);
}
@@ -1233,14 +1233,29 @@ ms_to_timeval(struct timeval *tv, int ms)
tv->tv_usec = (ms % 1000) * 1000;
}
+void
+monotime_ts(struct timespec *ts)
+{
+ if (clock_gettime(CLOCK_MONOTONIC, ts) != 0)
+ fatal("clock_gettime: %s", strerror(errno));
+}
+
+void
+monotime_tv(struct timeval *tv)
+{
+ struct timespec ts;
+
+ monotime_ts(&ts);
+ tv->tv_sec = ts.tv_sec;
+ tv->tv_usec = ts.tv_nsec / 1000;
+}
+
time_t
monotime(void)
{
struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
- fatal("clock_gettime: %s", strerror(errno));
-
+ monotime_ts(&ts);
return (ts.tv_sec);
}
@@ -1249,10 +1264,8 @@ monotime_double(void)
{
struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
- fatal("clock_gettime: %s", strerror(errno));
-
- return (ts.tv_sec + (double)ts.tv_nsec / 1000000000);
+ monotime_ts(&ts);
+ return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
}
void
@@ -1274,7 +1287,7 @@ bandwidth_limit(struct bwlimit *bw, size_t read_len)
struct timespec ts, rm;
if (!timerisset(&bw->bwstart)) {
- gettimeofday(&bw->bwstart, NULL);
+ monotime_tv(&bw->bwstart);
return;
}
@@ -1282,7 +1295,7 @@ bandwidth_limit(struct bwlimit *bw, size_t read_len)
if (bw->lamt < bw->thresh)
return;
- gettimeofday(&bw->bwend, NULL);
+ monotime_tv(&bw->bwend);
timersub(&bw->bwend, &bw->bwstart, &bw->bwend);
if (!timerisset(&bw->bwend))
return;
@@ -1316,7 +1329,7 @@ bandwidth_limit(struct bwlimit *bw, size_t read_len)
}
bw->lamt = 0;
- gettimeofday(&bw->bwstart, NULL);
+ monotime_tv(&bw->bwstart);
}
/* Make a template filename for mk[sd]temp() */
diff --git a/usr.bin/ssh/misc.h b/usr.bin/ssh/misc.h
index 9045f4ff706..8e24d5c4484 100644
--- a/usr.bin/ssh/misc.h
+++ b/usr.bin/ssh/misc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.h,v 1.67 2017/10/25 00:17:08 djm Exp $ */
+/* $OpenBSD: misc.h,v 1.68 2017/11/25 06:46:22 dtucker Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -67,6 +67,8 @@ char *tohex(const void *, size_t);
void sanitise_stdfd(void);
void ms_subtract_diff(struct timeval *, int *);
void ms_to_timeval(struct timeval *, int);
+void monotime_ts(struct timespec *);
+void monotime_tv(struct timeval *);
time_t monotime(void);
double monotime_double(void);
void lowercase(char *s);
diff --git a/usr.bin/ssh/packet.c b/usr.bin/ssh/packet.c
index e92598caa9d..8ce7db0584a 100644
--- a/usr.bin/ssh/packet.c
+++ b/usr.bin/ssh/packet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.c,v 1.266 2017/10/25 00:17:08 djm Exp $ */
+/* $OpenBSD: packet.c,v 1.267 2017/11/25 06:46:22 dtucker Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1321,7 +1321,7 @@ ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
for (;;) {
if (state->packet_timeout_ms != -1) {
ms_to_timeval(&timeout, ms_remain);
- gettimeofday(&start, NULL);
+ monotime_tv(&start);
}
if ((r = select(state->connection_in + 1, setp,
NULL, NULL, timeoutp)) >= 0)
@@ -1946,7 +1946,7 @@ ssh_packet_write_wait(struct ssh *ssh)
for (;;) {
if (state->packet_timeout_ms != -1) {
ms_to_timeval(&timeout, ms_remain);
- gettimeofday(&start, NULL);
+ monotime_tv(&start);
}
if ((ret = select(state->connection_out + 1,
NULL, setp, NULL, timeoutp)) >= 0)
diff --git a/usr.bin/ssh/ssh-keyscan.c b/usr.bin/ssh/ssh-keyscan.c
index 52191d156f7..0afc71729ba 100644
--- a/usr.bin/ssh/ssh-keyscan.c
+++ b/usr.bin/ssh/ssh-keyscan.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssh-keyscan.c,v 1.115 2017/06/30 04:17:23 dtucker Exp $ */
+/* $OpenBSD: ssh-keyscan.c,v 1.116 2017/11/25 06:46:22 dtucker Exp $ */
/*
* Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
*
@@ -357,7 +357,7 @@ conalloc(char *iname, char *oname, int keytype)
fdcon[s].c_len = 4;
fdcon[s].c_off = 0;
fdcon[s].c_keytype = keytype;
- gettimeofday(&fdcon[s].c_tv, NULL);
+ monotime_tv(&fdcon[s].c_tv);
fdcon[s].c_tv.tv_sec += timeout;
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
FD_SET(s, read_wait);
@@ -391,7 +391,7 @@ static void
contouch(int s)
{
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
- gettimeofday(&fdcon[s].c_tv, NULL);
+ monotime_tv(&fdcon[s].c_tv);
fdcon[s].c_tv.tv_sec += timeout;
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
}
@@ -525,7 +525,7 @@ conloop(void)
con *c;
int i;
- gettimeofday(&now, NULL);
+ monotime_tv(&now);
c = TAILQ_FIRST(&tq);
if (c && (c->c_tv.tv_sec > now.tv_sec ||
diff --git a/usr.bin/ssh/sshconnect.c b/usr.bin/ssh/sshconnect.c
index 4d81e7ff273..cc5c3246ef0 100644
--- a/usr.bin/ssh/sshconnect.c
+++ b/usr.bin/ssh/sshconnect.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sshconnect.c,v 1.287 2017/09/14 04:32:21 djm Exp $ */
+/* $OpenBSD: sshconnect.c,v 1.288 2017/11/25 06:46:22 dtucker Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -332,7 +332,7 @@ waitrfd(int fd, int *timeoutp)
struct timeval t_start;
int oerrno, r;
- gettimeofday(&t_start, NULL);
+ monotime_tv(&t_start);
pfd.fd = fd;
pfd.events = POLLIN;
for (; *timeoutp >= 0;) {