Skip to content

Commit 121a2d0

Browse files
committed
Fix GH-20601: ftp_connect() timeout argument overflow.
1 parent 27f17c3 commit 121a2d0

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

main/network.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,27 @@ static inline void sub_times(struct timeval a, struct timeval b, struct timeval
315315
}
316316
}
317317

318-
static inline void php_network_set_limit_time(struct timeval *limit_time,
318+
static inline zend_result php_network_set_limit_time(struct timeval *limit_time,
319319
struct timeval *timeout)
320320
{
321321
gettimeofday(limit_time, NULL);
322+
#ifndef PHP_WIN32
323+
const double timeoutmax = (double) PHP_TIMEOUT_ULL_MAX / 1000000.0;
324+
#else
325+
const double timeoutmax = (double) LONG_MAX / 1000000.0;
326+
#endif
327+
328+
if (limit_time->tv_sec > (timeoutmax - timeout->tv_sec)) {
329+
return FAILURE;
330+
}
331+
322332
limit_time->tv_sec += timeout->tv_sec;
323333
limit_time->tv_usec += timeout->tv_usec;
324334
if (limit_time->tv_usec >= 1000000) {
325335
limit_time->tv_usec -= 1000000;
326336
limit_time->tv_sec++;
327337
}
338+
return SUCCESS;
328339
}
329340
#endif
330341

@@ -391,7 +402,11 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd,
391402
if (timeout) {
392403
memcpy(&working_timeout, timeout, sizeof(working_timeout));
393404
#if HAVE_GETTIMEOFDAY
394-
php_network_set_limit_time(&limit_time, &working_timeout);
405+
if (UNEXPECTED(php_network_set_limit_time(&limit_time, &working_timeout) == FAILURE)) {
406+
error = ERANGE;
407+
ret = -1;
408+
goto ok;
409+
}
395410
#endif
396411
}
397412

@@ -849,7 +864,11 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
849864
if (timeout) {
850865
memcpy(&working_timeout, timeout, sizeof(working_timeout));
851866
#if HAVE_GETTIMEOFDAY
852-
php_network_set_limit_time(&limit_time, &working_timeout);
867+
if (UNEXPECTED(php_network_set_limit_time(&limit_time, &working_timeout) == FAILURE)) {
868+
php_network_freeaddresses(psal);
869+
zend_value_error("timeout value overflow");
870+
return -1;
871+
}
853872
#endif
854873
}
855874

0 commit comments

Comments
 (0)