Skip to content

Commit 40fc0ee

Browse files
author
Alejandro Perez Pestana
committed
waitio: Fix timeout integer overflow
Fix integer overflow in apr_wait_for_io_or_timeout by performing the microseconds to milliseconds conversion before assigning to 32-bit timeout.
1 parent d6bb3f9 commit 40fc0ee

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

support/unix/waitio.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ apr_status_t apr_wait_for_io_or_timeout(apr_file_t *f, apr_socket_t *s,
4040
int for_read)
4141
{
4242
struct pollfd pfd;
43+
apr_interval_time_t raw_timeout;
4344
int rc, timeout;
4445

45-
timeout = f ? f->timeout : s->timeout;
46+
raw_timeout = f ? f->timeout : s->timeout;
47+
if (raw_timeout > ((apr_interval_time_t)INT_MAX) * 1000) {
48+
/* timeout value exceeds maximum allowed (~25 days in microseconds)
49+
* capping to INT_MAX milliseconds to avoid overflow */
50+
timeout = INT_MAX;
51+
} else {
52+
/* convert microseconds to milliseconds (round up) */
53+
timeout = raw_timeout > 0 ? (int)((raw_timeout + 999) / 1000) : (int)raw_timeout;
54+
}
55+
4656
pfd.fd = f ? f->filedes : s->socketdes;
4757
pfd.events = for_read ? POLLIN : POLLOUT;
4858

49-
if (timeout > 0) {
50-
timeout = (timeout + 999) / 1000;
51-
}
5259
do {
5360
rc = poll(&pfd, 1, timeout);
5461
} while (rc == -1 && errno == EINTR);

0 commit comments

Comments
 (0)