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.

if (socketpool_limit > 0 && socketpool_used >= socketpool_limit) {<br />

if (!block) {<br />

avail = 0;<br />

errno = EMFILE;<br />

} else {<br />

while (socketpool_limit > 0 && socketpool_used >= socketpool_limit)<br />

SPC_WAIT_COND(socketpool_cond, socketpool_mutex);<br />

}<br />

}<br />

if (avail && (new_sd = socket(domain, type, protocol)) != -1)<br />

socketpool_used++;<br />

SPC_CLEANUP_POP(1);<br />

return new_sd;<br />

}<br />

When a socket that was obtained using spc_socketpool_accept( ) or spc_socketpool_<br />

socket( ) is no longer needed, close it by calling spc_socketpool_close( ). Do not call<br />

spc_socketpool_close( ) with file or socket descriptors that were not obtained from<br />

one of the wrapper functions; otherwise, the socket count will become corrupted.<br />

This implementation does not keep a list of the actual descriptors that have been<br />

allocated, so it is the responsibility of the caller to do so. If a socket being closed<br />

makes room for another socket to be created, the condition that the accept( ) and<br />

socket( ) wrapper functions wait on will be signaled.<br />

int spc_socketpool_close(int sd) {<br />

if (closesocket(sd) = = SOCKET_ERROR) return -1;<br />

SPC_ACQUIRE_MUTEX(socketpool_mutex);<br />

if (socketpool_limit > 0 && socketpool_used = = socketpool_limit)<br />

SPC_SIGNAL_COND(socketpool_cond);<br />

socketpool_used--;<br />

SPC_RELEASE_MUTEX(socketpool_mutex);<br />

return 0;<br />

}<br />

13.9 Guarding Against Resource Starvation<br />

Attacks on Unix<br />

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

You need to prevent resource starvation attacks against your application.<br />

Solution<br />

The operating system does not trust the applications that it allows to run. For this<br />

reason, the operating system imposes limits on certain resources. The limitations are<br />

imposed to prevent an application from using up all of the available system<br />

resources, thus denying other running applications the ability to run. The default<br />

Guarding Against Resource Starvation Attacks on Unix | 727<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!