Miscalculated null termination

Revision as of 15:04, 4 August 2006 by Weilin Zhong (Talk | contribs)

Jump to: navigation, search

This is a Vulnerability. To view all vulnerabilities, please see the Vulnerability Category page.


Miscalculated null termination occurs when the placement of a null character at the end of a buffer of characters (or string) is misplaced or omitted.


  • Confidentiality: Information disclosure may occur if strings with misplaced or omitted null characters are printed.
  • Availability: A randomly placed null character may put the system into an undefined state, and therefore make it prone to crashing.
  • Integrity: A misplaced null character may corrupt other data in memory
  • Access Control: Should the null character corrupt the process flow, or affect a flag controlling access, it may lead to logical errors which allow for the execution of arbitrary code.

Exposure period

  • Requirements specification: The choice could be made to use a language that is not susceptible to these issues.
  • Implementation: Precise knowledge of string manipulation functions may prevent this issue

Required resources




Likelihood of exploit


Avoidance and mitigation

  • Requirements specification: The choice could be made to use a language that is not susceptible to these issues.
  • Implementation: Ensure that all string functions used are understood fully as to how they append null characters. Also, be wary of off-by-one errors when appending nulls to the end of strings.


Miscalculated null termination is a common issue, and often difficult to detect. The most common symptoms occur infrequently (in the case of problems resulting from "safe" string functions), or in odd ways characterized by data corruption (when caused by off-by-one errors).

The case of an omitted null character is the most dangerous of the possible issues. This will almost certainly result in information disclosure, and possibly a buffer overflow condition, which may be exploited to execute arbitrary code.

As for misplaced null characters, the biggest issue is a subset of buffer overflow, and write-what-where conditions, where data corruption occurs from the writing of a null character over valid data, or even instructions. These logic issues may result in any number of security flaws.


While the following example is not exploitable, it provides a good example of how nulls can be omitted or misplaced, even when "safe" functions are used:

#include <stdio.h>
#include <string.h>

int main() {   
  char longString[] = "Cellular bananular phone";    
  char shortString[16];    

  strncpy(shortString, longString, 16);    
  printf("The last character in shortString is: %c %1$x\n", 
  return (0);

The above code gives the following output:

The last character in shortString is: l 6c

So, the shortString array does not end in a NULL character, even though the "safe" string function strncpy() was used.

Related problems

  • Write-what-where condition: A subset of the problem in some cases, in which an attacker may write a null character to a small range of possible addresses.