Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Integral Security


SafeInt

SafeInt is a C++ template class written by David LeBlanc. SafeInt throws an exception if the results of an integer operation cannot be represented in the specified type. The class is declared as a template, so it can be used with any integer type. Every operator has been overridden except for the subscript operator[] so that operators can be used in normal arithmetic expressions.

SafeInt fails to provide correct integer promotion behavior, but the greatest limitation of SafeInt is that it cannot be used with C applications. Because operators cannot be overloaded in C, safe integer solutions are not as easy to use.

CERT Integer Library

The CERT Coordination Center has recently released a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page

The purpose of the library is to provide a collection of utility functions that can assist software developers in writing C programs that are free from integer problems such as integer overflow, integer truncation, and sign errors that are a common source of software vulnerabilities.

Functions have been provided for all integer operations subject to overflow such as addition, subtraction, multiplication, division, unary negation, and so on) for int, long, long long, and size_t integers. The following example illustrates how the library can be used to add two signed long integer values:

long retsl, xsl, ysl;
	xsl = LONG_MAX;
	ysl = 0;
	retsl = addsl(xsl,ysl);

For short integer types (char and short) it is necessary to truncate the result of the addition using one of the safe conversion functions provided. For example:

char retsc, xsc, ysc;
	xsc = SCHAR_MAX;
	ysc = 0;
	retsc = si2sc(addsi(xsc, ysc));

For error handling, the secure integer library uses the mechanism for runtime-constraint handling defined by ISO/IEC TR 24731.

The implementation uses the high-performance algorithms defined by Henry S. Warren in the book Hacker's Delight.

Conclusions

Integer vulnerabilities result from lost or misrepresented data. The key to preventing these vulnerabilities is to understand the nuances of integer behavior in C and C++ and carefully apply this knowledge in the design and implementation of your systems.

Consider using safe integer operations to eliminate exception conditions, focusing on integers used as indices (or other pointer arithmetic), lengths, sizes, and loop counters.

If integer type range checking is properly applied and safe integer operations are used for values that can pass out of range (particularly due to external manipulation), it is possible to prevent vulnerabilities resulting from integer range errors.


Robert Seacord is a Senior Vulnerability Analyst for CERT/CC, where he leads the secure coding initiative, including the development of secure coding standards for C and C++. He is also the author of three books, including Secure Coding in C and C++ .


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.