Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Dynamically allocated buffers dynamically resize as additional memory is required. Dynamic approaches scale better and do not discard excess data. The major disadvantage is that, if inputs are not limited, they can exhaust memory on a machine and, consequently, be used in denial-of-service attacks.

Wiki Markup
Dynamic allocation is often disallowed in safety critical systems.  For example, the MISRA standard requires that "Dynamic heap memory allocation shall not be used" \[[MISRA 042004|AA. Bibliography#MISRA 04]\]. Some safety critical systems can take advantage of dynamic memory allocation during initialization, but not during operations.  For example, avionics software may dynamically allocate memory while initializing the aircraft, but not during flight.

Wiki Markup
There are a number of existing libraries available for managing string data; the library selected depends on the approach adopted for managing null-terminated byte strings.  The functions defined by C99, Section 7.21, "String handling <{{string.h}}>" \[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] are primarily intended for managing statically allocated strings.  However, these functions are problematic because many of them are insufficiently bounded.  Consequently, this standard recommends use of the ISO/IEC TR 24731-1 \[[ISO/IEC TR 24731-1:2007|AA. Bibliography#ISO/IEC TR 24731-1-2007]\] functions for use with statically allocated arrays. (seeSee [STR07-C. Use TR 24731 for remediation of existing string manipulation code].). These functions provide bounds-checking interfaces to protect against buffer overflows and other runtime constraint violations.

Wiki Markup
ISO/IEC PDTR 24731-2 (in progress) offers another approach, supplying functions that allocate enough memory for their results \[[ISO/IEC PDTR 24731-2|AA. Bibliography#ISO/IEC ISO/IEC PDTR 24731-2]\]. ISO/IEC PDTR 24731-2 provides an API that dynamically allocates the results of string functions as needed. Almost all of the APIs in this TR are also in a current International Standard.  For example, PDTR 24731-2 includes POSIX functions such as {{strdup()}} \[[ISO/IEC 9945:2003|AA. Bibliography#ISO/IEC 9945-2003]\], as well as functions from the Linux Standard Base Core Specification such as {{asprintf()}} \[[ISO/IEC 23360-1:2006|AA. Bibliography#ISO/IEC 23360-1-2006]\].

Wiki Markup
Another library that uses dynamic allocation is the CERT managed string library.  The managed string library described in \[[Burch 062006|AA. Bibliography#Burch06]\] was developed in response to the need for a string library that could improve the quality and security of newly developed C language code while eliminating obstacles to widespread adoption and possible standardization. The managed string library eliminates the possibility of unbounded copies, null-termination errors, and truncation by ensuring there is always adequate space available for the resulting string (including the terminating null character).  The primary advantage of the CERT managed string library, is that the source code is freely available so that the library can be adopted and customized as required by an organization.

...

Failing to adopt a consistent plan for managing strings within an application can lead to inconsistent decisions, which may make it difficult to ensure system properties, such as adhering to safety requirements.

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

Related Guidelines

This rule appears in the C++ Secure Coding Standard as : STR01-CPP. Adopt and implement a consistent plan for managing strings.

Bibliography

Wiki Markup
\[[Burch 062006|AA. Bibliography#Burch06]\]
\[[CERT 06c2006c|AA. Bibliography#CERT 06c]\]
\[[ISO/IEC 9945:2003|AA. Bibliography#ISO/IEC 9945-2003]\]
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 7.21, "String handling <{{string.h}}>"
\[[ISO/IEC 23360-1:2006|AA. Bibliography#ISO/IEC 23360-1-2006]\]
\[[ISO/IEC TR 24731-1:2007|AA. Bibliography#ISO/IEC TR 24731-1-2007]\]
\[[ISO/IEC PDTR 24731-2|AA. Bibliography#ISO/IEC PDTR 24731-2-2007]\]
\[[MISRA 042004|AA. Bibliography#MISRA 04]\] Rule 20.4
\[[Seacord 05a2005a|AA. Bibliography#Seacord 05a]\] Chapter 2, "Strings"

...