21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

See Also<br />

• Microsoft Security Bulletin MS02-071: http://www.Microsoft.com/technet/<br />

treeview/?url=/technet/security/bulletin/MS02-071.asp<br />

• “Shatter Attacks—How to Break Windows” by Chris Paget: http://security.<br />

tombom.co.uk/shatter.html<br />

13.7 Guarding Against Spawning Too Many<br />

Threads<br />

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

You need to prevent too many threads from being spawned, a problem that could<br />

potentially result in a denial of service owing to exhausted system resources.<br />

Solution<br />

A common mistake in writing multithreaded programs is to create a new thread<br />

every time a new task is initiated; this is often overkill. Often, a “pool” of threads can<br />

be used to perform simple tasks. A set number of threads are created when the program<br />

initializes, and these threads exist for the lifetime of the process. Whenever a<br />

task needs to be performed on another thread, the task can be queued. When a<br />

thread is available, it can perform the task, then go back to waiting for another task<br />

to perform.<br />

On Windows 2000 and greater, there is a new API function called<br />

QueueUserWorkItem( ) that essentially implements the same functionality as that presented<br />

in this recipe. Unfortunately, that function does not exist on older versions of<br />

Windows. Our solution has the advantage of being portable to such older systems.<br />

However, if you are writing code that is guaranteed always to be running on a system<br />

that supports the API, you may wish to use it instead. Regardless of whether you<br />

use the API or the code we present in this recipe, the concepts are the same, and the<br />

bulk of our discussion still applies.<br />

Discussion<br />

Suppose that the program using thread spawns is a network server, and it spawns a<br />

new thread for each connection it receives, an attacker can quickly flood the server<br />

with false or incomplete connections. The result is either that the server runs out of<br />

available threads and cannot create any more, or that it cannot create them fast<br />

enough to service the incoming requests. Either way, legitimate connections can no<br />

longer get through, and system resources are exhausted.<br />

718 | Chapter 13: Other Topics<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!