diff options
author | Daniel Stenberg <daniel@haxx.se> | 2020-08-24 11:07:59 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2020-08-28 14:16:41 +0200 |
commit | a2c85bb8e4ba9fa94b88c7592766763e30379ed2 (patch) | |
tree | 1d13ea2d5984bbe625a1cdc33b994bfc00e6815d | |
parent | 68a51324740945f1da9758bc1d26bbe4835bd847 (diff) | |
download | curl-a2c85bb8e4ba9fa94b88c7592766763e30379ed2.tar.gz |
Curl_pgrsTime - return new time to avoid timeout integer overflow
Setting a timeout to INT_MAX could cause an immediate error to get
returned as timeout because of an overflow when different values of
'now' were used.
This is primarily fixed by having Curl_pgrsTime() return the "now" when
TIMER_STARTSINGLE is set so that the parent function will continue using
that time.
Reported-by: IonuČ›-Francisc Oancea
Fixes #5583
Closes #5847
-rw-r--r-- | lib/connect.c | 3 | ||||
-rw-r--r-- | lib/multi.c | 34 | ||||
-rw-r--r-- | lib/progress.c | 9 | ||||
-rw-r--r-- | lib/progress.h | 4 |
4 files changed, 27 insertions, 23 deletions
diff --git a/lib/connect.c b/lib/connect.c index f53266054..8ceb8db62 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -1317,10 +1317,9 @@ CURLcode Curl_connecthost(struct connectdata *conn, /* context */ const struct Curl_dns_entry *remotehost) { struct Curl_easy *data = conn->data; - struct curltime before = Curl_now(); CURLcode result = CURLE_COULDNT_CONNECT; int i; - timediff_t timeout_ms = Curl_timeleft(data, &before, TRUE); + timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE); if(timeout_ms < 0) { /* a precaution, no need to continue if time already is up */ diff --git a/lib/multi.c b/lib/multi.c index 00eb4dd69..f4448f7c4 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -1702,7 +1702,7 @@ CURLcode Curl_preconnect(struct Curl_easy *data) static CURLMcode multi_runsingle(struct Curl_multi *multi, - struct curltime now, + struct curltime *nowp, struct Curl_easy *data) { struct Curl_message *msg = NULL; @@ -1743,7 +1743,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, (data->mstate < CURLM_STATE_COMPLETED)) { /* we need to wait for the connect state as only then is the start time stored, but we must not check already completed handles */ - timeout_ms = Curl_timeleft(data, &now, + timeout_ms = Curl_timeleft(data, nowp, (data->mstate <= CURLM_STATE_DO)? TRUE:FALSE); @@ -1752,25 +1752,25 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, if(data->mstate == CURLM_STATE_WAITRESOLVE) failf(data, "Resolving timed out after %" CURL_FORMAT_TIMEDIFF_T " milliseconds", - Curl_timediff(now, data->progress.t_startsingle)); + Curl_timediff(*nowp, data->progress.t_startsingle)); else if(data->mstate == CURLM_STATE_WAITCONNECT) failf(data, "Connection timed out after %" CURL_FORMAT_TIMEDIFF_T " milliseconds", - Curl_timediff(now, data->progress.t_startsingle)); + Curl_timediff(*nowp, data->progress.t_startsingle)); else { struct SingleRequest *k = &data->req; if(k->size != -1) { failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T " milliseconds with %" CURL_FORMAT_CURL_OFF_T " out of %" CURL_FORMAT_CURL_OFF_T " bytes received", - Curl_timediff(now, data->progress.t_startsingle), + Curl_timediff(*nowp, data->progress.t_startsingle), k->bytecount, k->size); } else { failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T " milliseconds with %" CURL_FORMAT_CURL_OFF_T " bytes received", - Curl_timediff(now, data->progress.t_startsingle), + Curl_timediff(*nowp, data->progress.t_startsingle), k->bytecount); } } @@ -1795,7 +1795,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, if(!result) { /* after init, go CONNECT */ multistate(data, CURLM_STATE_CONNECT); - Curl_pgrsTime(data, TIMER_STARTOP); + *nowp = Curl_pgrsTime(data, TIMER_STARTOP); rc = CURLM_CALL_MULTI_PERFORM; } break; @@ -1812,7 +1812,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, if(result) break; - Curl_pgrsTime(data, TIMER_STARTSINGLE); + *nowp = Curl_pgrsTime(data, TIMER_STARTSINGLE); if(data->set.timeout) Curl_expire(data, data->set.timeout, EXPIRE_TIMEOUT); @@ -2220,7 +2220,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, if(Curl_pgrsUpdate(data->conn)) result = CURLE_ABORTED_BY_CALLBACK; else - result = Curl_speedcheck(data, now); + result = Curl_speedcheck(data, *nowp); if(!result) { send_timeout_ms = 0; @@ -2230,7 +2230,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, data->progress.ul_limit_size, data->set.max_send_speed, data->progress.ul_limit_start, - now); + *nowp); recv_timeout_ms = 0; if(data->set.max_recv_speed > 0) @@ -2239,11 +2239,11 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, data->progress.dl_limit_size, data->set.max_recv_speed, data->progress.dl_limit_start, - now); + *nowp); if(!send_timeout_ms && !recv_timeout_ms) { multistate(data, CURLM_STATE_PERFORM); - Curl_ratelimit(data, now); + Curl_ratelimit(data, *nowp); } else if(send_timeout_ms >= recv_timeout_ms) Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST); @@ -2265,7 +2265,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, data->progress.ul_limit_size, data->set.max_send_speed, data->progress.ul_limit_start, - now); + *nowp); /* check if over recv speed */ recv_timeout_ms = 0; @@ -2274,10 +2274,10 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, data->progress.dl_limit_size, data->set.max_recv_speed, data->progress.dl_limit_start, - now); + *nowp); if(send_timeout_ms || recv_timeout_ms) { - Curl_ratelimit(data, now); + Curl_ratelimit(data, *nowp); multistate(data, CURLM_STATE_TOOFAST); if(send_timeout_ms >= recv_timeout_ms) Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST); @@ -2557,7 +2557,7 @@ CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles) SIGPIPE_VARIABLE(pipe_st); sigpipe_ignore(data, &pipe_st); - result = multi_runsingle(multi, now, data); + result = multi_runsingle(multi, &now, data); sigpipe_restore(&pipe_st); if(result) @@ -3031,7 +3031,7 @@ static CURLMcode multi_socket(struct Curl_multi *multi, SIGPIPE_VARIABLE(pipe_st); sigpipe_ignore(data, &pipe_st); - result = multi_runsingle(multi, now, data); + result = multi_runsingle(multi, &now, data); sigpipe_restore(&pipe_st); if(CURLM_OK >= result) { diff --git a/lib/progress.c b/lib/progress.c index 895138448..eced74c9f 100644 --- a/lib/progress.c +++ b/lib/progress.c @@ -164,9 +164,13 @@ void Curl_pgrsResetTransferSizes(struct Curl_easy *data) } /* + * + * Curl_pgrsTime(). Store the current time at the given label. This fetches a + * fresh "now" and returns it. + * * @unittest: 1399 */ -void Curl_pgrsTime(struct Curl_easy *data, timerid timer) +struct curltime Curl_pgrsTime(struct Curl_easy *data, timerid timer) { struct curltime now = Curl_now(); timediff_t *delta = NULL; @@ -209,7 +213,7 @@ void Curl_pgrsTime(struct Curl_easy *data, timerid timer) * changing the t_starttransfer time. */ if(data->progress.is_t_startransfer_set) { - return; + return now; } else { data->progress.is_t_startransfer_set = true; @@ -228,6 +232,7 @@ void Curl_pgrsTime(struct Curl_easy *data, timerid timer) us = 1; /* make sure at least one microsecond passed */ *delta += us; } + return now; } void Curl_pgrsStartNow(struct Curl_easy *data) diff --git a/lib/progress.h b/lib/progress.h index 3515ac6d5..c19d966d3 100644 --- a/lib/progress.h +++ b/lib/progress.h @@ -7,7 +7,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -49,7 +49,7 @@ void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size); void Curl_ratelimit(struct Curl_easy *data, struct curltime now); int Curl_pgrsUpdate(struct connectdata *); void Curl_pgrsResetTransferSizes(struct Curl_easy *data); -void Curl_pgrsTime(struct Curl_easy *data, timerid timer); +struct curltime Curl_pgrsTime(struct Curl_easy *data, timerid timer); timediff_t Curl_pgrsLimitWaitTime(curl_off_t cursize, curl_off_t startsize, curl_off_t limit, |