{"@ID": "14", "@Name": "Compiler Removal of Code to Clear Buffers", "@Abstraction": "Variant", "@Structure": "Simple", "@Status": "Draft", "Description": "Sensitive memory is cleared according to the source code, but compiler optimizations leave the memory untouched when it is not read from again, aka \"dead store removal.\"", "Extended_Description": {"xhtml:p": "This compiler optimization error occurs when:", "xhtml:ol": {"xhtml:li": ["Secret data are stored in memory.", "The secret data are 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."]}}, "Related_Weaknesses": {"Related_Weakness": {"@Nature": "ChildOf", "@CWE_ID": "733", "@View_ID": "1000", "@Ordinal": "Primary"}}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": [{"@Name": "C", "@Prevalence": "Undetermined"}, {"@Name": "C++", "@Prevalence": "Undetermined"}, {"@Class": "Compiled", "@Prevalence": "Undetermined"}]}, "Modes_Of_Introduction": {"Introduction": [{"Phase": "Implementation"}, {"Phase": "Build and Compilation"}]}, "Common_Consequences": {"Consequence": {"Scope": ["Confidentiality", "Access Control"], "Impact": ["Read Memory", "Bypass Protection Mechanism"], "Note": "This weakness will allow data that has not been cleared from memory to be read. If this data contains sensitive password information, then an attacker can read the password and use the information to bypass protection mechanisms."}}, "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."}]}, "Potential_Mitigations": {"Mitigation": [{"Phase": "Implementation", "Description": "Store the sensitive data in a \"volatile\" memory location if available."}, {"Phase": "Build and Compilation", "Description": "If possible, configure your compiler so that it does not remove dead stores."}, {"Phase": "Architecture and Design", "Description": "Where possible, encrypt sensitive data that are used by a software system."}]}, "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."]}}, "Affected_Resources": {"Affected_Resource": "Memory"}, "Taxonomy_Mappings": {"Taxonomy_Mapping": [{"@Taxonomy_Name": "7 Pernicious Kingdoms", "Entry_Name": "Insecure Compiler Optimization"}, {"@Taxonomy_Name": "PLOVER", "Entry_Name": "Sensitive memory uncleared by compiler optimization"}, {"@Taxonomy_Name": "OWASP Top Ten 2004", "Entry_ID": "A8", "Entry_Name": "Insecure Storage", "Mapping_Fit": "CWE More Specific"}, {"@Taxonomy_Name": "CERT C Secure Coding", "Entry_ID": "MSC06-C", "Entry_Name": "Be aware of compiler optimization when dealing with sensitive data"}, {"@Taxonomy_Name": "Software Fault Patterns", "Entry_ID": "SFP23", "Entry_Name": "Exposed Data"}]}, "References": {"Reference": [{"@External_Reference_ID": "REF-6"}, {"@External_Reference_ID": "REF-7", "@Section": "Chapter 9, \"A Compiler Optimization Caveat\" Page 322"}, {"@External_Reference_ID": "REF-124"}, {"@External_Reference_ID": "REF-125"}, {"@External_Reference_ID": "REF-126"}]}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Variant 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": "7 Pernicious Kingdoms", "Submission_Date": "2006-07-19", "Submission_Version": "Draft 3", "Submission_ReleaseDate": "2006-07-19"}, "Modification": [{"Modification_Name": "Eric Dalci", "Modification_Organization": "Cigital", "Modification_Date": "2008-07-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Time_of_Introduction"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-09-08", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Relationships, Other_Notes, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-10-14", "Modification_Version": "1.0.1", "Modification_ReleaseDate": "2008-10-14", "Modification_Comment": "updated Relationships"}, {"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 Applicable_Platforms, Description, Detection_Factors, Other_Notes, Potential_Mitigations, Relationships, Taxonomy_Mappings, Time_of_Introduction"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-05-27", "Modification_Version": "1.4", "Modification_ReleaseDate": "2009-05-27", "Modification_Comment": "updated Demonstrative_Examples"}, {"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": "2011-09-13", "Modification_Version": "2.1", "Modification_ReleaseDate": "2011-09-13", "Modification_Comment": "updated Relationships, Taxonomy_Mappings"}, {"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 Common_Consequences, References, 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, Taxonomy_Mappings"}, {"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, Taxonomy_Mappings"}, {"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, Type"}, {"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 References, Relationships"}, {"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 References, 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, Description"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-09-09", "Modification_Version": "4.18", "Modification_ReleaseDate": "2025-09-09", "Modification_Comment": "updated References"}, {"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 Applicable_Platforms, Demonstrative_Examples, Detection_Factors, Weakness_Ordinalities"}], "Previous_Entry_Name": {"@Date": "2008-04-11", "#text": "Insecure Compiler Optimization"}}}
