Difference between revisions of "Insecure Compiler Optimization"

From OWASP
Jump to: navigation, search
(Description)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Template:Vulnerability}}
 
{{Template:Vulnerability}}
 +
{{Template:Fortify}}
 +
 +
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''
 +
 +
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]
  
 
==Description==
 
==Description==
  
Security problems caused by compiler optimization.  
+
Improperly scrubbing sensitive data from memory can compromise security.
  
==Examples ==
+
Compiler optimization errors occur when:
  
* "Dead store removal"
+
* Secret data is stored in memory.
 +
* The secret data is scrubbed from memory by overwriting its contents.
 +
* The source code is compiled using an optimizing compiler, which identifies and removes the function that overwrites the contents as a dead store because the memory is not used subsequently.
 +
 
 +
==Risk Factors==
 +
 
 +
TBD
 +
 
 +
==Examples==
 +
 
 +
===Example: "Dead store removal"===
  
 
Memory overwriting code is removed by optimizing compiler, which causes sensitive information left in the memory after its usage.  
 
Memory overwriting code is removed by optimizing compiler, which causes sensitive information left in the memory after its usage.  
  
==Related Threats==
+
The following code reads a password from the user, uses the password to connect to a back-end mainframe, and then attempts to scrub the password from memory using memset().
 +
 
 +
<pre>
 +
void GetData(char *MFAddr) {
 +
char pwd[64];
 +
if (GetPasswordFromUser(pwd, sizeof(pwd))) {
 +
  if (ConnectToMainframe(MFAddr, pwd)) {
 +
  // Interaction with mainframe
 +
}
 +
  }
 +
  memset(pwd, 0, sizeof(pwd));
 +
}
 +
</pre>
 +
 
 +
The code in the example will behave correctly if it is executed verbatim, but if the code is compiled using an optimizing compiler, such as Microsoft Visual C++® .NET or GCC 3.x, then the call to memset() will be removed as a dead store because the buffer pwd is not used after its value is overwritten [1]. Because the buffer pwd contains a sensitive value, the application may be vulnerable to attack if the data is left memory resident. If attackers are able to access the correct region of memory, they may use the recovered password to gain control of the system.
 +
 
 +
It is common practice to overwrite sensitive data manipulated in memory, such as passwords or cryptographic keys, in order to prevent attackers from learning system secrets. However, with the advent of optimizing compilers, programs do not always behave as their source code alone would suggest. In the example, the compiler interprets the call to memset() as dead code because the memory being written to is not subsequently used, despite the fact that there is clearly a security motivation for the operation to occur. The problem here is that many compilers, and in fact many programming languages, do not take this and other security concerns into consideration in their efforts to improve efficiency.
 +
 
 +
Attackers typically exploit this type of vulnerability by using a core dump or runtime mechanism to access the memory used by a particular application and recover the secret information. Once an attacker has access to the secret information, it is relatively straightforward to further exploit the system and possibly compromise other resources with which the application interacts.
 +
 
 +
==Related [[Attacks]]==
 +
 
 +
* [[Attack 1]]
 +
* [[Attack 2]]
 +
 
 +
 
 +
==Related [[Vulnerabilities]]==
 +
 
 +
* [[Vulnerability 1]]
 +
* [[Vulnerabiltiy 2]]
 +
 
 +
 
 +
==Related [[Controls]]==
 +
 
 +
* [[Control 1]]
 +
* [[Control 2]]
 +
 
 +
 
 +
==Related [[Technical Impacts]]==
 +
 
 +
* [[Technical Impact 1]]
 +
* [[Technical Impact 2]]
 +
 
 +
 
 +
==References==
 +
 
 +
* [1] M. Howard and D. LeBlanc. Writing Secure Code, Second Edition. Microsoft Press, 2003.
 +
 
 +
[[Category:FIXME|add links
 +
 
 +
In addition, one should classify vulnerability based on the following subcategories: Ex:<nowiki>[[Category:Error Handling Vulnerability]]</nowiki>
 +
 
 +
Availability Vulnerability
 +
 
 +
Authorization Vulnerability
 +
 
 +
Authentication Vulnerability
 +
 
 +
Concurrency Vulnerability
 +
 
 +
Configuration Vulnerability
 +
 
 +
Cryptographic Vulnerability
 +
 
 +
Encoding Vulnerability
 +
 
 +
Error Handling Vulnerability
  
==Related Attacks==
+
Input Validation Vulnerability
  
==Related Vulnerabilities==
+
Logging and Auditing Vulnerability
  
==Related Countermeasures==
+
Session Management Vulnerability]]
  
==Related Technologies==
+
__NOTOC__
[[:Technology:Compiler]]
+
  
==Categories==
 
  
{{Template:Stub}}
+
[[Category:OWASP ASDR Project]]
 +
[[Category:Environmental Vulnerability]]
 +
[[Category:C]]
 +
[[Category:Implementation]]
 +
[[Category:Code Snippet]]
 +
[[category:Compiler]]
 +
[[Category:Vulnerability]]

Latest revision as of 05:59, 26 February 2009

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): 02/26/2009

Vulnerabilities Table of Contents

Description

Improperly scrubbing sensitive data from memory can compromise security.

Compiler optimization errors occur when:

  • Secret data is stored in memory.
  • The secret data is scrubbed from memory by overwriting its contents.
  • The source code is compiled using an optimizing compiler, which identifies and removes the function that overwrites the contents as a dead store because the memory is not used subsequently.

Risk Factors

TBD

Examples

Example: "Dead store removal"

Memory overwriting code is removed by optimizing compiler, which causes sensitive information left in the memory after its usage.

The following code reads a password from the user, uses the password to connect to a back-end mainframe, and then attempts to scrub the password from memory using memset().

	 void GetData(char *MFAddr) { 
	 char pwd[64];
	 if (GetPasswordFromUser(pwd, sizeof(pwd))) {
	  if (ConnectToMainframe(MFAddr, pwd)) {
			  // Interaction with mainframe
		 }
	   }
	   memset(pwd, 0, sizeof(pwd));
	}

The code in the example will behave correctly if it is executed verbatim, but if the code is compiled using an optimizing compiler, such as Microsoft Visual C++® .NET or GCC 3.x, then the call to memset() will be removed as a dead store because the buffer pwd is not used after its value is overwritten [1]. Because the buffer pwd contains a sensitive value, the application may be vulnerable to attack if the data is left memory resident. If attackers are able to access the correct region of memory, they may use the recovered password to gain control of the system.

It is common practice to overwrite sensitive data manipulated in memory, such as passwords or cryptographic keys, in order to prevent attackers from learning system secrets. However, with the advent of optimizing compilers, programs do not always behave as their source code alone would suggest. In the example, the compiler interprets the call to memset() as dead code because the memory being written to is not subsequently used, despite the fact that there is clearly a security motivation for the operation to occur. The problem here is that many compilers, and in fact many programming languages, do not take this and other security concerns into consideration in their efforts to improve efficiency.

Attackers typically exploit this type of vulnerability by using a core dump or runtime mechanism to access the memory used by a particular application and recover the secret information. Once an attacker has access to the secret information, it is relatively straightforward to further exploit the system and possibly compromise other resources with which the application interacts.

Related Attacks


Related Vulnerabilities


Related Controls


Related Technical Impacts


References

  • [1] M. Howard and D. LeBlanc. Writing Secure Code, Second Edition. Microsoft Press, 2003.