21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.13 Preventing File Descriptor Overflows When<br />

Using select( )<br />

<strong>Problem</strong><br />

Your program uses the select( ) system call to determine when sockets are ready for<br />

writing, have data waiting to be read, or have an exceptional condition (e.g., out-ofband<br />

data has arrived). Using select( ) requires the use of the fd_set data type,<br />

which typically entails the use of the FD_*( ) family of macros. In most implementations,<br />

FD_SET( ) and FD_CLR( ), in particular, are susceptible to an array overrun.<br />

Solution<br />

Do not use the FD_*( ) family of macros. Instead, use the macros that are provided in<br />

this recipe. The FD_SET( ) and FD_CLR( ) macros will modify an fd_set object without<br />

performing any bounds checking. The macros we provide will do proper bounds<br />

checking.<br />

Discussion<br />

The select( ) system call is normally used to multiplex sockets. In a single-threaded<br />

environment, select( ) allows you to build sets of socket descriptors for which you<br />

wish to wait for data to become available or that you wish to have available to write<br />

data to. The fd_set data type is used to hold a list of the socket descriptors, and several<br />

standard macros are used to manipulate objects of this type.<br />

Normally, fd_set is defined as a structure with a single member that is a statically<br />

allocated array of long integers. Because socket descriptors are always numbered<br />

starting with 0 and ending with the highest allowable descriptor, the array of integers<br />

in an fd_set is actually treated as a bitmask with a one-to-one correspondence<br />

between bits and socket descriptors.<br />

The size of the array in the fd_set structure is determined by the FD_SETSIZE macro.<br />

Most often, the size of the array is sufficiently large to be able to handle any possible<br />

file descriptor, but the problem is that most implementations of the FD_SET( ) and<br />

FD_CLR( ) macros (which are used to set and clear socket descriptors in an fd_set<br />

object) do not perform any bounds checking and will happily overrun the array if<br />

asked to do so.<br />

If FD_SETSIZE is defined to be sufficiently large, why is this a problem? Consider the<br />

situation in which a server program is compiled with FD_SETSIZE defined to be 256,<br />

which is normally the maximum number of file and socket descriptors allowed in a<br />

Unix process. Everything works just fine for a while, but eventually the number of<br />

allowed file descriptors is increased to 512 because 256 are no longer enough for all<br />

112 | Chapter 3: Input Validation<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!