Dr. Dobb's Digest March 2009

The Safe C Library

Promoting safer, more secure programming


Editor's Note: Since this article first appeared, Bo Berry has released the Safe Math Library which complements the Safe C Library by catching arithmetic overflows.

The recently released Safe C Library implements a subset of the functions defined in the ISO TR24731 specification which is designed to provide alternative functions for the C Library (as defined in ISO/IEC 9899:1999) that promotes safer, more secure programming in C.

The Safe C Library (available for download here) provides bound checking memory and string functions per ISO/IEC TR24731. These functions are alternative functions to the existing Standard C Library. The functions verify that output buffers are large enough for the intended result, and return a failure indicator if they are not. Optionally, failing functions call a "runtime-constraint handler" to report the error. Data is never written past the end of an array. All string results are null terminated. In addition, the functions in ISO/IEC TR 24731-1:2007 are re-entrant: they never return pointers to static objects owned by the function.

Included in the Safe C Library are extensions to the specification to provide a complementary set of functions with like behavior. The library can be used to mitigate (that is, lessen) software security problems. When used properly, these functions decrease the danger of buffer overruns and the vulnerability of cyber attacks.

The ISO TR24731 Bounds Checking Interface documents indicate that the key motivation for the new specification is to help mitigate the ever increasing security attacks -- specifically the buffer overrun. According to the rationale document:

Buffer overrun attacks continue to be a security problem. Roughly 10% of vulnerability reports cataloged by CERT from 01/01/2005 to 07/01/2005 involved buffer overflows. Preventing buffer overruns is the primary, but not the only, motivation for this technical report.

The rationale document goes on to say that:

...these only mitigate, that is lessen, security problems. When used properly, these functions decrease the danger buffer overrun attacks. Source code may remain vulnerable due to other bugs and security issues. The highest level of security is achieved by building in layers of security utilizing multiple strategies.

The rationale document also lists the following key points for TR24731:

The Safe C Library only implements a subset of the functions defined in the specification. Included in the library are extensions to the specification to provide a complementary set of functions with like behavior.

Among the design considerations for Safe C are:

Acknowledgements

Thanks to Safe C Project Adminstrator Bo Berry who prepared the readme file on which this short description is based.