Skip to content

Commit 1b412d2

Browse files
dkosmariDaniel K. O. (dkosmari)
andauthored
wutsocket fixes: select() handles timeout wrong, and poll() lacks a safety check (#428)
* - Fix select() to also update the fd_set arguments on timeout condition. - Move the nfds argument restriction to the nsysnet side. - Allow user-defined FD_SETSIZE, since newlib's fd_set allow custom sizes. - Define __socklen_t_defined when socklen_t is typedefed. * Added safety check for poll(): the nsysnet fd must fit in nsysnet_fd_set. --------- Co-authored-by: Daniel K. O. (dkosmari) <none@none>
1 parent cac70f5 commit 1b412d2

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

include/sys/select.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#ifndef FD_SETSIZE
34
#define FD_SETSIZE 32
5+
#endif
46

57
#include_next <sys/select.h>

include/sys/socket.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353
#define SO_NOSLOWSTART 0x4000 // disable slowstart
5454
#define SO_RUSRBUF 0x10000 // enable somemopt provided memory for receive buffer
5555

56-
typedef uint32_t socklen_t;
56+
#ifndef __socklen_t_defined
57+
typedef __socklen_t socklen_t;
58+
#define __socklen_t_defined
59+
#endif
60+
5761
typedef uint16_t sa_family_t;
5862

5963
struct sockaddr

libraries/wutsocket/poll.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ poll(struct pollfd *fds,
3232

3333
if ((cnv_fd + 1) > cnv_nfds) {
3434
cnv_nfds = cnv_fd + 1;
35+
if (cnv_nfds > NSYSNET_FD_SETSIZE) {
36+
errno = EINVAL;
37+
return -1;
38+
}
3539
}
3640

3741
if (fds[i].events & POLLIN) {

libraries/wutsocket/select.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ select(int nfds,
1111
nsysnet_fd_set cnv_rd, cnv_wr, cnv_ex;
1212
struct nsysnet_timeval cnv_timeout;
1313

14-
if (nfds > FD_SETSIZE) {
15-
errno = EINVAL;
16-
return -1;
17-
}
18-
1914
NSYSNET_FD_ZERO(&cnv_rd);
2015
NSYSNET_FD_ZERO(&cnv_wr);
2116
NSYSNET_FD_ZERO(&cnv_ex);
@@ -38,6 +33,10 @@ select(int nfds,
3833

3934
if ((cnv_fd + 1) > cnv_nfds) {
4035
cnv_nfds = cnv_fd + 1;
36+
if (cnv_nfds > NSYSNET_FD_SETSIZE) {
37+
errno = EINVAL;
38+
return -1;
39+
}
4140
}
4241

4342
if (rd_fd) {
@@ -67,8 +66,6 @@ select(int nfds,
6766
return rc;
6867
}
6968

70-
rc = 0;
71-
7269
if (readfds) {
7370
FD_ZERO(readfds);
7471
}
@@ -79,6 +76,11 @@ select(int nfds,
7976
FD_ZERO(exceptfds);
8077
}
8178

79+
if (rc == 0)
80+
return 0;
81+
82+
rc = 0;
83+
8284
for (i = 0; i < nfds; i++) {
8385
int cnv_fd = __wut_get_nsysnet_fd(i);
8486
if (cnv_fd == -1) {

0 commit comments

Comments
 (0)