{"@ID": "733", "@Name": "Compiler Optimization Removal or Modification of Security-critical Code", "@Abstraction": "Base", "@Structure": "Simple", "@Status": "Incomplete", "Description": "The developer builds a security-critical protection mechanism into the software, but the compiler optimizes the program such that the mechanism is removed or modified.", "Related_Weaknesses": {"Related_Weakness": {"@Nature": "ChildOf", "@CWE_ID": "1038", "@View_ID": "1000", "@Ordinal": "Primary"}}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": [{"@Name": "C", "@Prevalence": "Often"}, {"@Name": "C++", "@Prevalence": "Often"}, {"@Class": "Compiled", "@Prevalence": "Undetermined"}]}, "Modes_Of_Introduction": {"Introduction": {"Phase": "Build and Compilation"}}, "Common_Consequences": {"Consequence": {"Scope": ["Access Control", "Other"], "Impact": ["Bypass Protection Mechanism", "Alter Execution Logic"]}}, "Detection_Methods": {"Detection_Method": [{"Method": "Black Box", "Description": "This specific weakness is impossible to detect using black box methods. While an analyst could examine memory to see that it has not been scrubbed, an analysis of the executable would not be successful. This is because the compiler has already removed the relevant code. Only the source code shows whether the programmer intended to clear the memory or not, so this weakness is indistinguishable from others.", "Effectiveness": "Limited"}, {"Method": "White Box", "Description": "This weakness is only detectable using white box methods (see black box detection factor). Careful analysis is required to determine if the code is likely to be removed by the compiler."}]}, "Demonstrative_Examples": {"Demonstrative_Example": {"@Demonstrative_Example_ID": "DX-200", "Intro_Text": "The following code reads a password from the\n              user, uses the password to connect to a back-end\n              mainframe, and then attempts to scrub the password from\n              memory using memset().", "Example_Code": {"@Nature": "Bad", "@Language": "C", "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": [null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"@style": "margin-left:1em;", "xhtml:i": "// Interaction with mainframe"}, "#text": "if (ConnectToMainframe(MFAddr, pwd)) {\n\t\t\t\t\t \n\t\t\t\t\t }"}, "#text": "char pwd[64];\n\t\t\t\t   if (GetPasswordFromUser(pwd, sizeof(pwd))) {\n\t\t\t\t   \n\t\t\t\t   }\n\t\t\t\t   memset(pwd, 0, sizeof(pwd));"}, "#text": "void GetData(char *MFAddr) {\n\t\t\t\t \n\t\t\t\t }"}, "Body_Text": ["The code in the example will behave\n               correctly if it is executed verbatim, but if the code\n               is compiled using an optimizing compiler, then the call to\n               memset() might be removed as a dead store, because the\n               buffer pwd is not used after its value is overwritten.\n               Because the buffer pwd contains a sensitive\n               value, the application may be vulnerable to attack if\n               the data are left memory resident. If attackers are\n               able to access the correct region of memory, they may\n               use the recovered password to gain control of the\n               system.", "It is common practice to overwrite sensitive\n               data manipulated in memory, such as passwords or\n               cryptographic keys, in order to prevent attackers from\n               learning system secrets. However, with the advent of\n               optimizing compilers, programs do not always behave as\n               their source code alone would suggest. In the example,\n               the compiler interprets the call to memset() as dead\n               code because the memory being written to is not\n               subsequently used, despite the fact that there is\n               clearly a security motivation for the operation to\n               occur. The problem here is that many compilers, and in\n               fact many programming languages, do not take this and\n               other security concerns into consideration in their\n               efforts to improve efficiency.", "Attackers typically exploit this type of\n               vulnerability by using a core dump or runtime mechanism\n               to access the memory used by a particular application\n               and recover the secret information. Once an attacker\n               has access to the secret information, it is relatively\n               straightforward to further exploit the system and\n               possibly compromise other resources with which the\n               application interacts."]}}, "Observed_Examples": {"Observed_Example": [{"Reference": "CVE-2008-1685", "Description": "C compiler optimization, as allowed by specifications, removes code that is used to perform checks to detect integer overflows.", "Link": "https://www.cve.org/CVERecord?id=CVE-2008-1685"}, {"Reference": "CVE-2019-1010006", "Description": "Chain: compiler optimization (CWE-733) removes or modifies code used to detect integer overflow (CWE-190), allowing out-of-bounds write (CWE-787).", "Link": "https://www.cve.org/CVERecord?id=CVE-2019-1010006"}]}, "Related_Attack_Patterns": {"Related_Attack_Pattern": [{"@CAPEC_ID": "10"}, {"@CAPEC_ID": "24"}, {"@CAPEC_ID": "46"}, {"@CAPEC_ID": "8"}, {"@CAPEC_ID": "9"}]}, "References": {"Reference": {"@External_Reference_ID": "REF-7", "@Section": "Chapter 9, \"A Compiler Optimization Caveat\" Page 322"}}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.", "Comments": "Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.", "Reasons": {"Reason": {"@Type": "Acceptable-Use"}}}, "Content_History": {"Submission": {"Submission_Name": "CWE Content Team", "Submission_Organization": "MITRE", "Submission_Date": "2008-10-01", "Submission_Version": "1.0.1", "Submission_ReleaseDate": "2008-10-14", "Submission_Comment": "new weakness-focused entry for Research view closes the gap between 14 and 435."}, "Modification": [{"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-11-24", "Modification_Version": "1.1", "Modification_ReleaseDate": "2008-11-25", "Modification_Comment": "updated Detection_Factors"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-03-10", "Modification_Version": "1.3", "Modification_ReleaseDate": "2009-03-10", "Modification_Comment": "updated Applicable_Platforms, Observed_Examples, Related_Attack_Patterns, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2010-02-16", "Modification_Version": "1.8", "Modification_ReleaseDate": "2010-02-16", "Modification_Comment": "updated References"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-06-01", "Modification_Version": "1.13", "Modification_ReleaseDate": "2011-06-01", "Modification_Comment": "updated Common_Consequences"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-05-11", "Modification_Version": "2.2", "Modification_ReleaseDate": "2012-05-15", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2014-07-30", "Modification_Version": "2.8", "Modification_ReleaseDate": "2014-07-31", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-01-19", "Modification_Version": "2.10", "Modification_ReleaseDate": "2017-01-19", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-11-08", "Modification_Version": "3.0", "Modification_ReleaseDate": "2017-11-08", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2018-03-27", "Modification_Version": "3.1", "Modification_ReleaseDate": "2018-03-27", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-02-24", "Modification_Version": "4.0", "Modification_ReleaseDate": "2020-02-24", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-06-25", "Modification_Version": "4.1", "Modification_ReleaseDate": "2020-06-25", "Modification_Comment": "updated Observed_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-04-27", "Modification_Version": "4.11", "Modification_ReleaseDate": "2023-04-27", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-06-29", "Modification_Version": "4.12", "Modification_ReleaseDate": "2023-06-29", "Modification_Comment": "updated Mapping_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2024-02-29", "Modification_Version": "4.14", "Modification_ReleaseDate": "2024-02-29", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-12-11", "Modification_Version": "4.19", "Modification_ReleaseDate": "2025-12-11", "Modification_Comment": "updated Common_Consequences, Demonstrative_Examples, Detection_Factors, Time_of_Introduction, Weakness_Ordinalities"}]}}
