Fix denial of service - memory corruption.
authorJeremy Allison <jra@samba.org>
Sun, 27 Feb 2011 17:16:20 +0000 (18:16 +0100)
committerKarolin Seeger <kseeger@samba.org>
Sun, 27 Feb 2011 17:16:20 +0000 (18:16 +0100)
CVE-2011-0719

Fix bug #7949 (DoS in Winbind and smbd with many file descriptors open).

All current released versions of Samba are vulnerable to
a denial of service caused by memory corruption. Range
checks on file descriptors being used in the FD_SET macro
were not present allowing stack corruption. This can cause
the Samba code to crash or to loop attempting to select
on a bad file descriptor set.

A connection to a file share, or a local account is needed
to exploit this problem, either authenticated or unauthenticated
(guest connection).

Currently we do not believe this flaw is exploitable
beyond a crash or causing the code to loop, but on the
advice of our security reviewers we are releasing fixes
in case an exploit is discovered at a later date.

18 files changed:
lib/tevent/tevent_select.c
lib/tevent/tevent_standard.c
nsswitch/wb_common.c
source3/client/client.c
source3/client/dnsbrowse.c
source3/lib/events.c
source3/lib/packet.c
source3/lib/readline.c
source3/lib/select.c
source3/lib/util_sock.c
source3/lib/wbclient.c
source3/libaddns/dnssock.c
source3/libads/cldap.c
source3/libsmb/nmblib.c
source3/nmbd/nmbd_packets.c
source3/utils/smbfilter.c
source3/winbindd/winbindd.c
source3/winbindd/winbindd_dual.c

index d97418991ae3e3dd8bec6edd13d7fb5a0144b0ff..890e0311c6033be92cb8b36ba573587965b3901e 100644 (file)
@@ -111,6 +111,11 @@ static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_C
                                                           struct select_event_context);
        struct tevent_fd *fde;
 
+       if (fd < 0 || fd >= FD_SETSIZE) {
+               errno = EBADF;
+               return NULL;
+       }
+
        fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
                                   handler, private_data,
                                   handler_name, location);
@@ -143,6 +148,11 @@ static int select_event_loop_select(struct select_event_context *select_ev, stru
 
        /* setup any fd events */
        for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
+               if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       return -1;
+               }
+
                if (fde->flags & TEVENT_FD_READ) {
                        FD_SET(fde->fd, &r_fds);
                }
index c3f8b36e840faf52773d1e7c6327a23cf909c7c4..d4a00cc062a4e472a309d211f490e979cc36c134 100644 (file)
@@ -457,6 +457,11 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva
 
        /* setup any fd events */
        for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
+               if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
+                       std_ev->exit_code = EBADF;
+                       return -1;
+               }
+
                if (fde->flags & TEVENT_FD_READ) {
                        FD_SET(fde->fd, &r_fds);
                }
index d0dfcb8bbfb67326d82f2736b8110b3a9baf8688..75ce585a86349e7c4f19207ae7ec100ac0a2430b 100644 (file)
@@ -240,6 +240,12 @@ static int winbind_named_pipe_sock(const char *dir)
 
                switch (errno) {
                        case EINPROGRESS:
+
+                               if (fd < 0 || fd >= FD_SETSIZE) {
+                                       errno = EBADF;
+                                       goto error_out;
+                               }
+
                                FD_ZERO(&w_fds);
                                FD_SET(fd, &w_fds);
                                tv.tv_sec = CONNECT_TIMEOUT - wait_time;
@@ -384,6 +390,12 @@ int winbind_write_sock(void *buffer, int count, int recursing, int need_priv)
                struct timeval tv;
                fd_set r_fds;
 
+               if (winbindd_fd < 0 || winbindd_fd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       winbind_close_sock();
+                       return -1;
+               }
+
                /* Catch pipe close on other end by checking if a read()
                   call would not block by calling select(). */
 
@@ -444,6 +456,11 @@ int winbind_read_sock(void *buffer, int count)
                struct timeval tv;
                fd_set r_fds;
 
+               if (winbindd_fd < 0 || winbindd_fd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       winbind_close_sock();
+                       return -1;
+               }
                /* Catch pipe close on other end by checking if a read()
                   call would not block by calling select(). */
 
index b7065916b95676a3946f50407a81701e3b6e908d..1e5e8e1acb3cdae326b574fe062c93646e82e53f 100644 (file)
@@ -4384,8 +4384,10 @@ static void readline_callback(void)
 
  again:
 
-       if (cli->fd == -1)
+       if (cli->fd < 0 || cli->fd >= FD_SETSIZE) {
+               errno = EBADF;
                return;
+       }
 
        FD_ZERO(&fds);
        FD_SET(cli->fd,&fds);
index 5e3a4de9cff03afb16397a8a6ee03d0579d87326..a6b9360a1b0a257e8ecf83f7721c78f29cbfc012 100644 (file)
@@ -81,6 +81,11 @@ static void do_smb_resolve(struct mdns_smbsrv_result *browsesrv)
                        TALLOC_FREE(fdset);
                }
 
+               if (mdnsfd < 0 || mdnsfd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       break;
+               }
+
                fdsetsz = howmany(mdnsfd + 1, NFDBITS) * sizeof(fd_mask);
                fdset = TALLOC_ZERO(ctx, fdsetsz);
                FD_SET(mdnsfd, fdset);
@@ -181,6 +186,12 @@ int do_smb_browse(void)
                        TALLOC_FREE(fdset);
                }
 
+               if (mdnsfd < 0 || mdnsfd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       TALLOC_FREE(ctx);
+                       return 1;
+               }
+
                fdsetsz = howmany(mdnsfd + 1, NFDBITS) * sizeof(fd_mask);
                fdset = TALLOC_ZERO(ctx, fdsetsz);
                FD_SET(mdnsfd, fdset);
index 90d86c6c79feac40d34e38ee94259d0ef7b52360..3c83f81a710441731dd3b1840eade20f84b6f6d2 100644 (file)
@@ -55,6 +55,14 @@ bool event_add_to_select_args(struct tevent_context *ev,
        bool ret = false;
 
        for (fde = ev->fd_events; fde; fde = fde->next) {
+               if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
+                       /* We ignore here, as it shouldn't be
+                          possible to add an invalid fde->fd
+                          but we don't want FD_SET to see an
+                          invalid fd. */
+                       continue;
+               }
+
                if (fde->flags & EVENT_FD_READ) {
                        FD_SET(fde->fd, read_fds);
                        ret = true;
index ef28bf9f625b53b0f8ef214eaf1e0ac3fe9262d4..c14d1e51ec30a1e5edd3956288b99ccd3cb838f7 100644 (file)
@@ -106,6 +106,11 @@ NTSTATUS packet_fd_read_sync(struct packet_context *ctx)
        int res;
        fd_set r_fds;
 
+       if (ctx->fd < 0 || ctx->fd >= FD_SETSIZE) {
+               errno = EBADF;
+               return map_nt_error_from_unix(errno);
+       }
+
        FD_ZERO(&r_fds);
        FD_SET(ctx->fd, &r_fds);
 
index 34867aad9e7eaccdefcf48d0047b4e932f318d25..70a82f27ab7b5abf325af0051de2480c5ddd9d23 100644 (file)
@@ -91,6 +91,11 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void)
                timeout.tv_sec = 5;
                timeout.tv_usec = 0;
 
+               if (fd < 0 || fd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       break;
+               }
+
                FD_ZERO(&fds);
                FD_SET(fd,&fds);
 
index b5443ff20ca3c7ebdb201d77616ca52fb0d1bca2..2230b43475a476259ec80d2c302991cc959ce84a 100644 (file)
@@ -75,6 +75,17 @@ int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, s
                        return -1;
                }
 
+               if (select_pipe[0] < 0 || select_pipe[0] >= FD_SETSIZE) {
+                       DEBUG(0, ("sys_select: bad fd\n"));
+                       if (readfds != NULL)
+                               FD_ZERO(readfds);
+                       if (writefds != NULL)
+                               FD_ZERO(writefds);
+                       if (errorfds != NULL)
+                               FD_ZERO(errorfds);
+                       errno = EBADF;
+                       return -1;
+               }
                /*
                 * These next two lines seem to fix a bug with the Linux
                 * 2.0.x kernel (and probably other UNIXes as well) where
@@ -101,6 +112,7 @@ int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, s
                readfds2 = &readfds_buf;
                FD_ZERO(readfds2);
        }
+
        FD_SET(select_pipe[0], readfds2);
 
        errno = 0;
index da79aca9df55e5e0d5dbc0ca57117186f2ebca70..238daf47eeba3a8a38474aa9ff8c87fe44b20408 100644 (file)
@@ -560,6 +560,11 @@ NTSTATUS read_fd_with_timeout(int fd, char *buf,
        timeout.tv_usec = (long)(1000 * (time_out % 1000));
 
        for (nread=0; nread < mincnt; ) {
+               if (fd < 0 || fd >= FD_SETSIZE) {
+                       errno = EBADF;
+                       return map_nt_error_from_unix(EBADF);
+               }
+
                FD_ZERO(&fds);
                FD_SET(fd,&fds);
 
@@ -1294,7 +1299,7 @@ bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
 
        for (i=0; i<num_addrs; i++) {
                sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
-               if (sockets[i] < 0)
+               if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE)
                        goto done;
                set_blocking(sockets[i], false);
        }
@@ -1343,8 +1348,10 @@ bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
        FD_ZERO(&r_fds);
 
        for (i=0; i<num_addrs; i++) {
-               if (sockets[i] == -1)
+               if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE) {
+                       /* This cannot happen - ignore if so. */
                        continue;
+               }
                FD_SET(sockets[i], &wr_fds);
                FD_SET(sockets[i], &r_fds);
                if (sockets[i]>maxfd)
index 3cf992c7de9a8ec95208bcbbf5b84086c679b359..9cb250d218bedf290770be0fe8f04a097f9c4a0c 100644 (file)
@@ -120,7 +120,7 @@ static bool winbind_closed_fd(int fd)
        struct timeval tv;
        fd_set r_fds;
 
-       if (fd == -1) {
+       if (fd == -1 || fd >= FD_SETSIZE) {
                return true;
        }
 
@@ -222,6 +222,13 @@ static struct tevent_req *wb_connect_send(TALLOC_CTX *mem_ctx,
                wbc_err = map_wbc_err_from_errno(errno);
                goto post_status;
        }
+       if (wb_ctx->fd >= FD_SETSIZE) {
+               close(wb_ctx->fd);
+               wb_ctx->fd = -1;
+               errno = EBADF;
+               wbc_err = map_wbc_err_from_errno(errno);
+               goto post_status;
+       }
 
        subreq = async_connect_send(mem_ctx, ev, wb_ctx->fd,
                                    (struct sockaddr *)&sunaddr,
index 7c8bd418e5765e5dc97ccd3e85a1c0dfb8aca1f9..bf11fea9313d4343fb599680a71a0ccb6909c202 100644 (file)
@@ -219,6 +219,11 @@ static DNS_ERROR read_all(int fd, uint8 *data, size_t len)
                ssize_t ret;
                int fd_ready;
                
+               if (fd < 0 || fd >= FD_SETSIZE) {
+                       /* read timeout */
+                       return ERROR_DNS_SOCKET_ERROR;
+               }
+
                FD_ZERO( &rfds );
                FD_SET( fd, &rfds );
 
index ae087d976cf4dd30a1e14c7038e2e50c1d9c5093..ac4e2cb3012ee4f872ba6c459930007c7df623e5 100644 (file)
@@ -134,6 +134,11 @@ static int recv_cldap_netlogon(TALLOC_CTX *mem_ctx,
                return -1;
        }
 
+       if (sock < 0 || sock >= FD_SETSIZE) {
+               errno = EBADF;
+               return -1;
+       }
+
        FD_ZERO(&r_fds);
        FD_SET(sock, &r_fds);
 
index 1206d9ddb3d83191831dd6f7c66a1cbec12facd6..8230c5ac0fde822dc27fafc1eb241ae934e1195e 100644 (file)
@@ -1089,6 +1089,11 @@ struct packet_struct *receive_packet(int fd,enum packet_type type,int t)
        struct timeval timeout;
        int ret;
 
+       if (fd < 0 || fd >= FD_SETSIZE) {
+               errno = EBADF;
+               return NULL;
+       }
+
        FD_ZERO(&fds);
        FD_SET(fd,&fds);
        timeout.tv_sec = t/1000;
index f69845b346c4b6c9586ba6cee76ba6a2bd150cfa..1c570ea617dbeedfa2d86bda4f4edd1833373d9f 100644 (file)
@@ -1683,7 +1683,7 @@ static bool create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_n
        for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
                count++;
 
-       if((count*2) + 2 > FD_SETSIZE) {
+       if((count*2) + 2 >= FD_SETSIZE) {
                DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
 only use %d.\n", (count*2) + 2, FD_SETSIZE));
                SAFE_FREE(pset);
@@ -1699,24 +1699,44 @@ only use %d.\n", (count*2) + 2, FD_SETSIZE));
        FD_ZERO(pset);
 
        /* Add in the broadcast socket on 137. */
+       if (ClientNMB < 0 || ClientNMB >= FD_SETSIZE) {
+               errno = EBADF;
+               SAFE_FREE(pset);
+               return True;
+       }
+
        FD_SET(ClientNMB,pset);
        sock_array[num++] = ClientNMB;
        *maxfd = MAX( *maxfd, ClientNMB);
 
        /* Add in the 137 sockets on all the interfaces. */
        for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
+               if (subrec->nmb_sock < 0 || subrec->nmb_sock >= FD_SETSIZE) {
+                       /* We have to ignore sockets outside FD_SETSIZE. */
+                       continue;
+               }
                FD_SET(subrec->nmb_sock,pset);
                sock_array[num++] = subrec->nmb_sock;
                *maxfd = MAX( *maxfd, subrec->nmb_sock);
        }
 
        /* Add in the broadcast socket on 138. */
+       if (ClientDGRAM < 0 || ClientDGRAM >= FD_SETSIZE) {
+               errno = EBADF;
+               SAFE_FREE(pset);
+               return True;
+       }
+
        FD_SET(ClientDGRAM,pset);
        sock_array[num++] = ClientDGRAM;
        *maxfd = MAX( *maxfd, ClientDGRAM);
 
        /* Add in the 138 sockets on all the interfaces. */
        for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
+               if (subrec->dgram_sock < 0 || subrec->dgram_sock >= FD_SETSIZE) {
+                       /* We have to ignore sockets outside FD_SETSIZE. */
+                       continue;
+               }
                FD_SET(subrec->dgram_sock,pset);
                sock_array[num++] = subrec->dgram_sock;
                *maxfd = MAX( *maxfd, subrec->dgram_sock);
@@ -1767,7 +1787,7 @@ bool listen_for_packets(bool run_election)
 
 #ifndef SYNC_DNS
        dns_fd = asyncdns_fd();
-       if (dns_fd != -1) {
+       if (dns_fd >= 0 && dns_fd < FD_SETSIZE) {
                FD_SET(dns_fd, &r_fds);
                maxfd = MAX( maxfd, dns_fd);
        }
index 39a264011eed4cf90acd6d9ac47893f096d30711..a3ed657fd0c6f11162689f41619e21133bbd29f5 100644 (file)
@@ -170,8 +170,8 @@ static void filter_child(int c, struct sockaddr_storage *dest_ss)
                int num;
                
                FD_ZERO(&fds);
-               if (s != -1) FD_SET(s, &fds);
-               if (c != -1) FD_SET(c, &fds);
+               if (s >= 0 && s < FD_SETSIZE) FD_SET(s, &fds);
+               if (c >= 0 && c < FD_SETSIZE) FD_SET(c, &fds);
 
                num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
                if (num <= 0) continue;
@@ -243,6 +243,10 @@ static void start_filter(char *desthost)
                struct sockaddr_storage ss;
                socklen_t in_addrlen = sizeof(ss);
                
+               if (s < 0 || s >= FD_SETSIZE) {
+                       break;
+               }
+
                FD_ZERO(&fds);
                FD_SET(s, &fds);
 
index 0443ebfdf2cdb6362f402b3dc05d822305990660..b72d42b49183a4f046064ce1ecfeb3a0bd710da4 100644 (file)
@@ -1064,6 +1064,12 @@ static void process_loop(void)
        }
 
        for (ev = fd_events; ev; ev = ev->next) {
+               if (ev->fd < 0 || ev->fd >= FD_SETSIZE) {
+                       /* Ignore here - event_add_to_select_args
+                          should make this impossible. */
+                       continue;
+               }
+
                if (ev->flags & EVENT_FD_READ) {
                        FD_SET(ev->fd, &r_fds);
                        maxfd = MAX(ev->fd, maxfd);
index 72e5607d3f4c2a261bb7c63ee1c0cacf275d7496..3eef3d39fd0d42400bb8b8a44508469516281177 100644 (file)
@@ -1403,6 +1403,13 @@ static bool fork_domain_child(struct winbindd_child *child)
 
                FD_ZERO(&r_fds);
                FD_ZERO(&w_fds);
+
+               if (state.sock < 0 || state.sock >= FD_SETSIZE) {
+                       TALLOC_FREE(frame);
+                       perror("EBADF");
+                       _exit(1);
+               }
+
                FD_SET(state.sock, &r_fds);
                maxfd = state.sock;