Fix select() on Linux

This fixes a bug where the caller's timeval will be clobbered on Linux.
The Kernel ABI *always* modifies the timeout argument but POSIX says it
should be a const parameter. The wrapper now handles the difference and
sys_select() may be used if obtaining the remainder on Linux is needed.
This commit is contained in:
Justine Tunney 2023-02-23 06:03:06 -08:00
parent dfabcd84c1
commit 0f1ead8943
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 17 additions and 6 deletions

View file

@ -43,26 +43,37 @@
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout) {
int rc;
struct timeval tv, *tvp;
POLLTRACE("select(%d, %p, %p, %p, %s) → ...", nfds, readfds, writefds,
exceptfds, DescribeTimeval(0, timeout));
// the linux kernel modifies timeout
if (timeout) {
if (IsAsan() && !__asan_is_valid(timeout, sizeof(*timeout))) {
return efault();
}
tv = *timeout;
tvp = &tv;
} else {
tvp = 0;
}
BEGIN_CANCELLATION_POINT;
if (nfds < 0) {
rc = einval();
} else if (IsAsan() &&
((readfds && !__asan_is_valid(readfds, FD_SIZE(nfds))) ||
(writefds && !__asan_is_valid(writefds, FD_SIZE(nfds))) ||
(exceptfds && !__asan_is_valid(exceptfds, FD_SIZE(nfds))) ||
(timeout && !__asan_is_valid(timeout, sizeof(*timeout))))) {
(exceptfds && !__asan_is_valid(exceptfds, FD_SIZE(nfds))))) {
rc = efault();
} else if (!IsWindows()) {
rc = sys_select(nfds, readfds, writefds, exceptfds, timeout);
rc = sys_select(nfds, readfds, writefds, exceptfds, tvp);
} else {
rc = sys_select_nt(nfds, readfds, writefds, exceptfds, timeout, 0);
rc = sys_select_nt(nfds, readfds, writefds, exceptfds, tvp, 0);
}
END_CANCELLATION_POINT;
POLLTRACE("select(%d, %p, %p, %p, [%s]) → %d% m", nfds, readfds, writefds,
exceptfds, DescribeTimeval(rc, timeout), rc);
exceptfds, DescribeTimeval(rc, tvp), rc);
return rc;
}