Versions Compared

Key

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

The This managed string and SafeStr libraries were library was developed in response to the need for a string library that could improve the quality and security of newly developed C language programs while eliminating obstacles to widespread adoption and possible standardization.

...

The managed string library also provides a mechanism for dealing with data sanitization by (optionally) checking that all characters in a string belong to a predefined set of "safe" characters.

Compliant Solution 1

This compliant solution illustrates how the managed string library can be used to create a managed string and retrieve a null-terminated byte string from the managed string.

...

Note that the calls to fprintf() and printf() are C99 standard functions and not managed string functions.

Compliant Solution 2

This compliant solution illustrates how the SafeStr library can be used to create and manipulate safe strings.

Code Block

safestr_t string1;
safestr_t string2;

XXL_TRY_BEGIN {
      string1 = safestr_create("sample string", 0);
      string2 = safestr_alloc(14, 0);
      safestr_copy(&string2, string1);
      safestr_printf(string2);
}

XXL_CATCH (SAFESTR_ERROR_OUT_OF_MEMORY) {
      printf("Insufficient Memory.\n");
}

XXL_EXCEPT {
      printf("Operating failed.\n");
}

XXL_TRY_END;

Note that printf() is a C99 standard function and not a SafeStr string function.

Risk Assessment

String handling functions defined in C99 Section 7.21 and elsewhere are susceptible to common programming errors that can lead to serious, exploitable vulnerabilities. Managed strings, when used properly, can eliminate many of these errors--particularly in new development.

...

Examples of vulnerabilities resulting from the violation of this recommendation can be found on the CERT website.

References

Wiki Markup
\[[Burch 06|AA. C References#Seacord 06]\]
\[[CERT 06|AA. C References#CERT 06]\]
\[[Messier and Viega 03|http://www.zork.org/safestr/]\] "Safe C String library v1.0.3"
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.21, "String handling <string.h>"
\[[Seacord 05a|AA. C References#Seacord 05a]\] Chapter 2, "Strings"