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 Recipe 3.2). The format-string protection also requires access to the frame<br />

pointer because it attempts to filter out arguments that are not pointers into the heap<br />

or the local variables on the stack.<br />

See Also<br />

• MSDN reference for strsafe.h: http://msdn.microsoft.com/library/en-us/winui/<br />

winui/windowsuserinterface/resources/strings/usingstrsafefunctions.asp<br />

• SafeStr from Zork: http://www.zork.org/safestr/<br />

• StackGuard from Immunix: http://www.immunix.org/stackguard.html<br />

• ProPolice from IBM: http://www.trl.ibm.com/projects/security/ssp/<br />

• LibSafe from Avaya Labs: http://www.research.avayalabs/project/libsafe/<br />

• Netstrings by Dan J. Bernstein: http://cr.yp.to/proto/netstrings.txt<br />

• Recipes 3.2, 3.4<br />

3.4 Using the SafeStr Library<br />

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

You want an alternative to using the standard C string-manipulation functions to<br />

help avoid buffer overflows (see Recipe 3.3), format-string problems (see Recipe 3.2),<br />

and the use of unchecked external input.<br />

Solution<br />

Use the SafeStr library, which is available from http://www.zork.org/safestr/.<br />

Discussion<br />

The SafeStr library provides an implementation of dynamically sizable strings in C.<br />

In addition, the library also performs reference counting and accounting of the allocated<br />

and actual sizes of each string. Any attempt to increase the actual size of a<br />

string beyond its allocated size causes the library to increase the allocated size of the<br />

string to a size at least as large. Because strings managed by SafeStr (“safe strings”)<br />

are dynamically sized, safe strings are not a source of potential buffer overflows. (See<br />

Recipe 3.3.)<br />

Safe strings use the type safestr_t, which can actually be cast to the normal C-style<br />

string type, char *, though we strongly recommend against doing so where it can be<br />

avoided. In fact, the only time you should ever cast a safe string to a normal C-style<br />

string is for read-only purposes. This is also the only reason why the safestr_t type<br />

was designed in a way that allows casting to normal C-style strings.<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Using the SafeStr Library | 85

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

Saved successfully!

Ooh no, something went wrong!