CWE-135: Incorrect Calculation of Multi-Byte String Length

Export to Word

Description

The product does not correctly calculate the length of strings that can contain wide or multi-byte characters.

Extended Description

N/A


ThreatScore

Threat Mapped score: 0.0

Industry: Finiancial

Threat priority: Unclassified


Observed Examples (CVEs)

Related Attack Patterns (CAPEC)

N/A


Attack TTPs

N/A

Modes of Introduction

Phase Note
Implementation There are several ways in which improper string length checking may result in an exploitable condition. All of these, however, involve the introduction of buffer overflow conditions in order to reach an exploitable state. The first of these issues takes place when the output of a wide or multi-byte character string, string-length function is used as a size for the allocation of memory. While this will result in an output of the number of characters in the string, note that the characters are most likely not a single byte, as they are with standard character strings. So, using the size returned as the size sent to new or malloc and copying the string to this newly allocated memory will result in a buffer overflow. Another common way these strings are misused involves the mixing of standard string and wide or multi-byte string functions on a single string. Invariably, this mismatched information will result in the creation of a possibly exploitable buffer overflow condition.

Common Consequences

Potential Mitigations

Applicable Platforms


Demonstrative Examples

Intro: The following example would be exploitable if any of the commented incorrect malloc calls were used.

Body: The output from the printf() statement would be:

#include <stdio.h> #include <strings.h> #include <wchar.h> int main() { wchar_t wideString[] = L"The spazzy orange tiger jumped " \ "over the tawny jaguar."; wchar_t *newString; printf("Strlen() output: %d\nWcslen() output: %d\n", strlen(wideString), wcslen(wideString)); /* Wrong because the number of chars in a string isn't related to its length in bytes // newString = (wchar_t *) malloc(strlen(wideString)); */ /* Wrong because wide characters aren't 1 byte long! // newString = (wchar_t *) malloc(wcslen(wideString)); */ /* Wrong because wcslen does not include the terminating null */ newString = (wchar_t *) malloc(wcslen(wideString) * sizeof(wchar_t)); /* correct! */ newString = (wchar_t *) malloc((wcslen(wideString) + 1) * sizeof(wchar_t)); /* ... */ }

Notes

← Back to CWE list