CWE
Home > CWE List > CWE- Individual Dictionary Definition (1.0.1)  
Search by ID:

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

Individual Definition in a New Window
Incorrect Calculation of Multi-Byte String Length
Status: Draft
Weakness ID: 135 (Weakness Base)
Description
Summary

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

Potential Mitigations

Always verify the length of the string unit character.

Use length computing functions (e.g. strlen, wcslen, etc.) appropriately with their equivalent type (e.g.: byte, wchar_t, etc.)

Demonstrative Examples

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

C Example:
#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));
 
/* Very wrong for obvious reasons //
newString = (wchar_t *) malloc(strlen(wideString));
*/
 
/* Wrong because wide characters aren't 1 byte long! //
newString = (wchar_t *) malloc(wcslen(wideString));
*/
 
/* correct! */
newString = (wchar_t *) malloc(wcslen(wideString) * sizeof(wchar_t));
 
/* ... */
}

The output from the printf() statement would be: Strlen() output: 0 Wcslen() output: 53

Other Notes

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. Again, if a language subject to these flaws must be used, the most effective mitigation technique is to pay careful attention to the code at implementation time and ensure that these flaws do not occur.

Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness ClassWeakness ClassWeakness Class682Incorrect Calculation
Research Concepts (primary)1000
ChildOfCategoryCategory133String Errors
Development Concepts (primary)699
Taxonomy Mappings
Mapped Taxonomy NameMapped Node Name
CLASPImproper string length checking
Applicable Platforms
Languages
C
C++
Time of Introduction
* Implementation
Content History
Submissions
CLASP. (Externally Mined)
Modifications
Eric Dalci. Cigital. 2008-07-01. (External)
updated Potential_Mitigations, Time_of_Introduction
CWE Content Team. MITRE. 2008-09-08. (Internal)
updated Applicable_Platforms, Relationships, Other_Notes, Taxonomy_Mappings
Previous Entry Names
* Improper String Length Checking (changed 2008-04-11)
Page Last Updated: October 16, 2008