OSSP CVS Repository

ossp - Check-in [1662]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 1662
Date: 2002-Jan-27 14:15:28 (local)
2002-Jan-27 13:15:28 (UTC)
User:rse
Branch:
Comment: Internally make sure an invalid file-descriptor (integer not between 0 and (FD_SETSIZE-1) does not lead to any segfaults or other undefined behaviour. Instead an error is returned and errno is set to EBADF, similar to what the OS functions do. Especially pth_poll() now return with this error (instead of skipping the fd) if an fd in the "struct pollfd" is invalid.

Hint by: Archie Cobbs <archie@packetdesign.com>

Tickets:
Inspections:
Files:
ossp-pkg/pth/ChangeLog      1.556 -> 1.557     8 inserted, 0 deleted
ossp-pkg/pth/pth_event.c      1.57 -> 1.58     2 inserted, 0 deleted
ossp-pkg/pth/pth_high.c      1.82 -> 1.83     22 inserted, 2 deleted
ossp-pkg/pth/pth_util.c      1.20 -> 1.21     9 inserted, 0 deleted

ossp-pkg/pth/ChangeLog 1.556 -> 1.557

--- ChangeLog    2002/01/27 12:39:10     1.556
+++ ChangeLog    2002/01/27 13:15:28     1.557
@@ -21,6 +21,14 @@
     
   Changes between 1.4.0 and 1.4.1 (24-Mar-2001 to 27-Jan-2002)
 
+   *) Internally make sure an invalid file-descriptor (integer not
+      between 0 and (FD_SETSIZE-1) does not lead to any segfaults or
+      other undefined behaviour. Instead an error is returned and errno
+      is set to EBADF, similar to what the OS functions do. Especially
+      pth_poll() now return with this error (instead of skipping the fd)
+      if an fd in the "struct pollfd" is invalid.
+      [Ralf S. Engelschall, Archie Cobbs <archie@packetdesign.com>]
+
    *) Correctly support PTH_FDMODE_NONBLOCK in pth_connect and pth_accept. 
       [Archie Cobbs <archie@packetdesign.com>]
 


ossp-pkg/pth/pth_event.c 1.57 -> 1.58

--- pth_event.c  2002/01/27 11:03:40     1.57
+++ pth_event.c  2002/01/27 13:15:28     1.58
@@ -114,6 +114,8 @@
     if (spec & PTH_EVENT_FD) {
         /* filedescriptor event */
         int fd = va_arg(ap, int);
+        if (!pth_util_fd_valid(fd))
+            return_errno(NULL, EBADF);
         ev->ev_type = PTH_EVENT_FD;
         ev->ev_goal = (int)(spec & (PTH_UNTIL_FD_READABLE|\
                                     PTH_UNTIL_FD_WRITEABLE|\


ossp-pkg/pth/pth_high.c 1.82 -> 1.83

--- pth_high.c   2002/01/27 12:39:10     1.82
+++ pth_high.c   2002/01/27 13:15:28     1.83
@@ -411,8 +411,8 @@
     FD_ZERO(&wfds);
     FD_ZERO(&efds);
     for(i = 0; i < nfd; i++) {
-        if (pfd[i].fd < 0)
-            continue;
+        if (!pth_util_fd_valid(pfd[i].fd))
+            return_errno(-1, EBADF);
         if (pfd[i].events & POLLIN)
             FD_SET(pfd[i].fd, &rfds);
         if (pfd[i].events & POLLOUT)
@@ -600,6 +600,8 @@
         /* now directly poll filedescriptor for readability
            to avoid unneccessary (and resource consuming because of context
            switches, etc) event handling through the scheduler */
+        if (!pth_util_fd_valid(fd))
+            return_errno(-1, EBADF);
         FD_ZERO(&fds);
         FD_SET(fd, &fds);
         delay.tv_sec  = 0;
@@ -668,6 +670,10 @@
         /* now directly poll filedescriptor for writeability
            to avoid unneccessary (and resource consuming because of context
            switches, etc) event handling through the scheduler */
+        if (!pth_util_fd_valid(fd)) {
+            pth_fdmode(fd, fdmode);
+            return_errno(-1, EBADF);
+        }
         FD_ZERO(&fds);
         FD_SET(fd, &fds);
         delay.tv_sec  = 0;
@@ -758,6 +764,8 @@
         /* first directly poll filedescriptor for readability
            to avoid unneccessary (and resource consuming because of context
            switches, etc) event handling through the scheduler */
+        if (!pth_util_fd_valid(fd))
+            return_errno(-1, EBADF);
         FD_ZERO(&fds);
         FD_SET(fd, &fds);
         delay.tv_sec  = 0;
@@ -900,6 +908,12 @@
         /* first directly poll filedescriptor for writeability
            to avoid unneccessary (and resource consuming because of context
            switches, etc) event handling through the scheduler */
+        if (!pth_util_fd_valid(fd)) {
+            pth_fdmode(fd, fdmode);
+            if (iovcnt > sizeof(tiov_stack))
+                free(tiov);
+            return_errno(-1, EBADF);
+        }
         FD_ZERO(&fds);
         FD_SET(fd, &fds);
         delay.tv_sec  = 0;
@@ -1177,6 +1191,8 @@
         /* now directly poll filedescriptor for readability
            to avoid unneccessary (and resource consuming because of context
            switches, etc) event handling through the scheduler */
+        if (!pth_util_fd_valid(fd))
+            return_errno(-1, EBADF);
         FD_ZERO(&fds);
         FD_SET(fd, &fds);
         delay.tv_sec  = 0;
@@ -1257,6 +1273,10 @@
         /* now directly poll filedescriptor for writeability
            to avoid unneccessary (and resource consuming because of context
            switches, etc) event handling through the scheduler */
+        if (!pth_util_fd_valid(fd)) {
+            pth_fdmode(fd, fdmode);
+            return_errno(-1, EBADF);
+        }
         FD_ZERO(&fds);
         FD_SET(fd, &fds);
         delay.tv_sec  = 0;


ossp-pkg/pth/pth_util.c 1.20 -> 1.21

--- pth_util.c   2002/01/27 11:03:41     1.20
+++ pth_util.c   2002/01/27 13:15:28     1.21
@@ -91,6 +91,15 @@
     return d;
 }
 
+/* check whether a file-descriptor is valid */
+#if cpp
+#if !defined(FD_SETSIZE)
+#define FD_SETSIZE 1024
+#endif
+#define pth_util_fd_valid(fd) \
+    ((fd) >= 0 && (fd) <= (FD_SETSIZE-1))
+#endif
+
 /* merge input fd set into output fds */
 intern void pth_util_fds_merge(int nfd,
                                fd_set *ifds1, fd_set *ofds1,

CVSTrac 2.0.1