Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: NTBS -> string

Wiki Markup
Null-terminated byte strings (NTBS)Strings must contain a null-termination character at or before the address of the last element of the array before they can be safely passed as arguments to standard string-handling functions, such as {{strcpy()}} or {{strlen()}}. This is because these functions, as well as other string-handling functions defined by C99 \[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\], depend on the existence of a null-termination character to determine the length of a string. Similarly, NTBSstrings must be null -terminated before iterating on a character array where the termination condition of the loop depends on the existence of a null-termination character within the memory allocated for the string, as in the following example:

Code Block
size_t i;
char ntbsstr[16];
/* ... */
for (i = 0; i < sizeof(ntbsstr); ++i) {
  if (ntbsstr[i] == '\0') break;
  /* ... */
}

Failure to properly terminate null-terminated byte strings can result in buffer overflows and other undefined behavior.

Noncompliant Code Example (strncpy())

Wiki Markup
The standard {{strncpy()}} function does not guarantee that the resulting string is null -terminated \[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]. If there is no null character in the first {{n}} characters of the {{source}} array, the result could not be null -terminated.

In the first noncompliant code example, ntbs str is null-terminated before the call to strncpy(). However, the subsequent execution of strncpy() can overwrite the null-termination character.

Code Block
bgColor#FFcccc
char ntbsstr[NTBSSTR_SIZE];

ntbsstr[sizeof(ntbsstr)-1] = '\0';
strncpy(ntbsstr, source, sizeof(ntbsstr));

Wiki Markup
In the second noncompliant code example, {{memset()}} is used to clear the destination buffer; unfortunately, the third argument incorrectly specifies the size of the destination array \[[Schwarz 2005|AA. Bibliography#Schwarz 05]\].

Code Block
bgColor#FFcccc
char ntbsstr[NTBSSTR_SIZE];

memset(ntbsstr, 0, sizeof(ntbsstr)-1);
strncpy(ntbsstr, source, sizeof(ntbsstr)-1);

Compliant Solution (Truncation)

The correct solution depends on the programmer's intent. If the intent was to truncate a string while ensuring that the result remains a null-terminated string, this solution can be used:

Code Block
bgColor#ccccff
char ntbsstr[NTBSSTR_SIZE];

strncpy(ntbsstr, source, sizeof(ntbsstr)-1);
ntbsstr[sizeof(ntbsstr)-1] = '\0';

Compliant Solution (Copy without Truncation)

If the intent is to copy without truncation, this example copies the data and guarantees that the resulting null-terminated byte string is null-terminated. If the string cannot be copied, it is handled as an error condition.

Code Block
bgColor#ccccff
char *source = "0123456789abcdef";
char ntbsstr[NTBSSTR_SIZE];
/* ... */
if (source) {
  if (strlen(source) < sizeof(ntbsstr)) {
    strcpy(ntbsstr, source);
  }
  else {
    /* handle string too large condition */
  }
}
else {
  /* handle NULL string condition */
}

Compliant Solution (strncpy_s())

Wiki Markup
The {{strncpy_s()}} function copies up to {{n}} characters from the source array to a destination array \[[TR 24731|AA. Bibliography#ISO/IEC TR 24731-1-2007]\]. If no null character was copied from the source array, then the {{n{}}}th position in the destination array is set to a null character, guaranteeing that the resulting string is null-terminated.

Code Block
bgColor#ccccff
char *source;
char a[NTBSSTR_SIZE];
/* ... */
if (source) {
  errno_t err = strncpy_s(a, sizeof(a), source, 5);
  if (err != 0) {
    /* Handle error */
  }
}
else {
  /* handle NULL string condition */
}

Noncompliant Code Example (realloc())

One method to decrease memory usage in critical situations when all available memory has been exhausted is to use the realloc() function to halve the size of message strings. The standard realloc() function has no concept of null-terminated byte strings. As a result, if realloc() is called to decrease the memory allocated for a null-terminated byte string, the null-termination character may be truncated.

The following noncompliant code example fails to ensure that cur_msg is properly null-terminated:

Code Block
bgColor#ffcccc
char *cur_msg = NULL;
size_t cur_msg_size = 1024;

/* ... */

void lessen_memory_usage(void) {
  char *temp;
  size_t temp_size;

  /* ... */

  if (cur_msg != NULL) {
    temp_size = cur_msg_size/2 + 1;
    temp = realloc(cur_msg, temp_size);
    if (temp == NULL) {
      /* Handle error condition */
    }
    cur_msg = temp;
    cur_msg_size = temp_size;
  }
}

/* ... */

Because realloc() does not guarantee that the string is properly null-terminated, any subsequent operation on cur_msg that assumes a null-termination character may result in undefined behavior.

Compliant Solution (realloc())

In this compliant solution, the lessen_memory_usage() function ensures that the resulting string is always properly null-terminated.

Code Block
bgColor#ccccff
char *cur_msg = NULL;
size_t cur_msg_size = 1024;

/* ... */

void lessen_memory_usage(void) {
  char *temp;
  size_t temp_size;

  /* ... */

  if (cur_msg != NULL) {
    temp_size = cur_msg_size/2 + 1;
    temp = realloc(cur_msg, temp_size);
    if (temp == NULL) {
      /* Handle error condition */
    }
    cur_msg = temp;
    cur_msg_size = temp_size;

    /* ensure string is null-terminated */
    cur_msg[cur_msg_size - 1] = '\0';
  }
}

/* ... */

Risk Assessment

Failure to properly null-terminate strings can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process. Null-termination errors can also result in unintended information disclosure.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

STR32-C

high

probable

medium

P12

L1

Automated Detection

Tool

Version

Checker

Description

Section

Compass/ROSE

 

 

Section

can detect some violations of this rule

Section

Klocwork

Include Page
c:Klocwork_V
c:Klocwork_V
Section

NNTS

 

Related Vulnerabilities

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

Related Guidelines

CERT C++ Secure Coding Standard: STR32-CPP. Null-terminate character arrays as required

...

MITRE CWE: CWE-170, "Improper Null Termination"

Bibliography

Wiki Markup
\[[Schwarz 2005|AA. Bibliography#Schwarz 05]\]
\[[Seacord 2005a|AA. Bibliography#Seacord 05]\] Chapter 2, "Strings"
\[[Viega 2005|AA. Bibliography#Viega 05]\] Section 5.2.14, "Miscalculated NULL termination"

...