Difference between revisions of "Often Misused: String Management"

From OWASP
Jump to: navigation, search
Line 1: Line 1:
 
{{Template:Vulnerability}}
 
{{Template:Vulnerability}}
 
{{Template:Fortify}}
 
{{Template:Fortify}}
{{Template:Vulnerability}}
+
 
 +
[[Category:FIXME|This is the text from the old template. This needs to be rewritten using the new template.]]
 +
 
 
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''
 
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''
  
Line 12: Line 14:
 
==Description==
 
==Description==
  
A vulnerability is a weakness in an application (frequently a broken or missing control) that enables an attack to succeed. Be sure you don't put [attacks] or [controls] in this category.
+
Functions that convert between Multibyte and Unicode strings encourage buffer overflows.
  
# Start with a one-sentence description of the vulnerability
+
Windows provides the MultiByteToWideChar(), WideCharToMultiByte(), UnicodeToBytes, and BytesToUnicode functions to convert between arbitrary multibyte (usually ANSI) character strings and Unicode (wide character) strings. The size arguments to these functions are specified in different units – one in bytes, the other in characters – making their use prone to error. In a multibyte character string, each character occupies a varying number of bytes, and therefore the size of such strings is most easily specified as a total number of bytes. In Unicode, however, characters are always a fixed size, and string lengths are typically given by the number of characters they contain. Mistakenly specifying the wrong units in a size argument can lead to a buffer overflow.
# What is the problem that creates the vulnerability?
+
# What are the attacks that target this vulnerability?
+
# What are the technical impacts of this vulnerability?
+
  
  
 
==Risk Factors==
 
==Risk Factors==
  
* Talk about the [[OWASP Risk Rating Methodology|factors]] that make this vulnerability likely or unlikely to actually happen
+
TBD
* Discuss the technical impact of a successful exploit of this vulnerability
+
* Consider the likely [business impacts] of a successful attack
+
  
  
 
==Examples==
 
==Examples==
  
===Short example name===
+
The following function takes a username specified as a multibyte string and a pointer to a structure for user information and populates the structure with information about the specified user. Since Windows authentication uses Unicode for usernames, the username argument is first converted from a multibyte string to a Unicode string.
: A short example description, small picture, or sample code with [http://www.site.com links]
+
  
===Short example name===
+
<pre>
: A short example description, small picture, or sample code with [http://www.site.com links]
+
void getUserInfo(char *username, struct _USER_INFO_2 info){
 +
WCHAR unicodeUser[UNLEN+1];
 +
MultiByteToWideChar(CP_ACP, 0, username, -1,
 +
  unicodeUser, sizeof(unicodeUser));
 +
NetUserGetInfo(NULL, unicodeUser, 2, (LPBYTE *)&info);
 +
}
 +
</pre>
  
 +
This function incorrectly passes the size of unicodeUser in bytes instead of characters. The call to MultiByteToWideChar() can therefore write up to (UNLEN+1)*sizeof(WCHAR) wide characters, or (UNLEN+1)*sizeof(WCHAR)* sizeof(WCHAR) bytes, to the unicodeUser array, which has only (UNLEN+1)*sizeof(WCHAR) bytes allocated. If the username string contains more than UNLEN characters, the call to MultiByteToWideChar() will overflow the buffer unicodeUser.
  
 
==Related [[Attacks]]==
 
==Related [[Attacks]]==
Line 44: Line 47:
 
==Related [[Vulnerabilities]]==
 
==Related [[Vulnerabilities]]==
  
* [[Vulnerability 1]]
+
* [[Buffer Overflow]]
* [[Vulnerabiltiy 2]]
+
  
 
==Related [[Controls]]==
 
==Related [[Controls]]==
Line 60: Line 62:
  
 
==References==
 
==References==
Note: A reference to related [http://cwe.mitre.org/ CWE] or [http://capec.mitre.org/ CAPEC] article should be added when exists. Eg:
 
  
* [http://cwe.mitre.org/data/definitions/79.html CWE 79].
+
TBD
* http://www.link1.com
+
* [http://www.link2.com Title for the link2]
+
  
 
[[Category:FIXME|add links
 
[[Category:FIXME|add links
Line 96: Line 95:
  
 
[[Category:OWASP ASDR Project]]
 
[[Category:OWASP ASDR Project]]
 
 
==Abstract==
 
 
Functions that convert between Multibyte and Unicode strings encourage buffer overflows.
 
 
==Description==
 
 
Windows provides the MultiByteToWideChar(), WideCharToMultiByte(), UnicodeToBytes, and BytesToUnicode functions to convert between arbitrary multibyte (usually ANSI) character strings and Unicode (wide character) strings. The size arguments to these functions are specified in different units – one in bytes, the other in characters – making their use prone to error. In a multibyte character string, each character occupies a varying number of bytes, and therefore the size of such strings is most easily specified as a total number of bytes. In Unicode, however, characters are always a fixed size, and string lengths are typically given by the number of characters they contain. Mistakenly specifying the wrong units in a size argument can lead to a buffer overflow.
 
 
==Examples ==
 
 
The following function takes a username specified as a multibyte string and a pointer to a structure for user information and populates the structure with information about the specified user. Since Windows authentication uses Unicode for usernames, the username argument is first converted from a multibyte string to a Unicode string.
 
 
<pre>
 
void getUserInfo(char *username, struct _USER_INFO_2 info){
 
WCHAR unicodeUser[UNLEN+1];
 
MultiByteToWideChar(CP_ACP, 0, username, -1,
 
  unicodeUser, sizeof(unicodeUser));
 
NetUserGetInfo(NULL, unicodeUser, 2, (LPBYTE *)&info);
 
}
 
</pre>
 
 
This function incorrectly passes the size of unicodeUser in bytes instead of characters. The call to MultiByteToWideChar() can therefore write up to (UNLEN+1)*sizeof(WCHAR) wide characters, or (UNLEN+1)*sizeof(WCHAR)* sizeof(WCHAR) bytes, to the unicodeUser array, which has only (UNLEN+1)*sizeof(WCHAR) bytes allocated. If the username string contains more than UNLEN characters, the call to MultiByteToWideChar() will overflow the buffer unicodeUser.
 
 
 
==Related Threats==
 
 
==Related Attacks==
 
 
==Related Vulnerabilities==
 
 
[[Buffer Overflow]]
 
 
==Related Countermeasures==
 
 
==Categories==
 
 
[[Category:Use of Dangerous API]]
 
[[Category:Use of Dangerous API]]
 
[[Category:Implementation]]
 
[[Category:Implementation]]

Revision as of 07:39, 27 September 2008

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


This article includes content generously donated to OWASP by Fortify.JPG.

Last revision (mm/dd/yy): 09/27/2008

Vulnerabilities Table of Contents

ASDR Table of Contents

Contents


Description

Functions that convert between Multibyte and Unicode strings encourage buffer overflows.

Windows provides the MultiByteToWideChar(), WideCharToMultiByte(), UnicodeToBytes, and BytesToUnicode functions to convert between arbitrary multibyte (usually ANSI) character strings and Unicode (wide character) strings. The size arguments to these functions are specified in different units – one in bytes, the other in characters – making their use prone to error. In a multibyte character string, each character occupies a varying number of bytes, and therefore the size of such strings is most easily specified as a total number of bytes. In Unicode, however, characters are always a fixed size, and string lengths are typically given by the number of characters they contain. Mistakenly specifying the wrong units in a size argument can lead to a buffer overflow.


Risk Factors

TBD


Examples

The following function takes a username specified as a multibyte string and a pointer to a structure for user information and populates the structure with information about the specified user. Since Windows authentication uses Unicode for usernames, the username argument is first converted from a multibyte string to a Unicode string.

	void getUserInfo(char *username, struct _USER_INFO_2 info){
		WCHAR unicodeUser[UNLEN+1];
		MultiByteToWideChar(CP_ACP, 0, username, -1,
				  unicodeUser, sizeof(unicodeUser));
		NetUserGetInfo(NULL, unicodeUser, 2, (LPBYTE *)&info);
	}

This function incorrectly passes the size of unicodeUser in bytes instead of characters. The call to MultiByteToWideChar() can therefore write up to (UNLEN+1)*sizeof(WCHAR) wide characters, or (UNLEN+1)*sizeof(WCHAR)* sizeof(WCHAR) bytes, to the unicodeUser array, which has only (UNLEN+1)*sizeof(WCHAR) bytes allocated. If the username string contains more than UNLEN characters, the call to MultiByteToWideChar() will overflow the buffer unicodeUser.

Related Attacks


Related Vulnerabilities

Related Controls


Related Technical Impacts


References

TBD