Weakness · High
CWE-170: Improper Null Termination
What it is
The product does not terminate or incorrectly terminates a string or array with a null character or equivalent terminator.
- Severity (our rating)
- High
- Likelihood of exploit
- Medium
- Introduced during
- Implementation
- Exploited in the wild
- No CVE in CISA's catalog
In more detail
Null termination errors frequently occur in two different ways. An off-by-one error could cause a null to be written out of bounds, leading to an overflow. Or, a program could use a strncpy() function call incorrectly, which prevents a null terminator from being added at all. Other scenarios are possible.
What an attacker can do
- Read Memory (Confidentiality, Integrity, Availability)
- Execute Unauthorized Code or Commands (Confidentiality, Integrity, Availability, Access Control, Other)
- DoS: Crash, Exit, or Restart (Confidentiality, Integrity, Availability)
- DoS: Resource Consumption (CPU) (Confidentiality, Integrity, Availability)
- DoS: Resource Consumption (Memory) (Confidentiality, Integrity, Availability)
- Modify Memory (Integrity, Availability)
- Alter Execution Logic (Integrity, Confidentiality, Availability, Access Control, Other)
How to prevent it
Requirements
- Use a language that is not susceptible to these issues. However, be careful of null byte interaction errors (CWE-626) with lower-level constructs that may be written in a language that is susceptible.
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.
- If performance constraints permit, special code can be added that validates null-termination of string buffers, this is a rather naive and error-prone solution.
- Switch to bounded string manipulation functions. Inspect buffer lengths involved in the buffer overrun trace reported with the defect.
- Add code that fills buffers with nulls (however, the length of buffers still needs to be inspected, to ensure that the non null-terminated string is not written at the physical end of the buffer).
How to find it
- Automated Static Analysis. Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
H2 Security
Found it in your code?
We review where it appears, fix it with your team, and check it was not already used against you.
Talk to our teamT Academy
Stop writing it in the first place
Learn to build software and systems without weaknesses like this one, with practice on real hosts in your browser, every objective checked on the machine.
Start freeSource: MITRE Common Weakness Enumeration, CWE version 4.19.1. CWE is a trademark of The MITRE Corporation.