结构: Simple
Abstraction: Base
状态: Draft
被利用可能性: Medium
Sending non-cloned mutable data as a return value may result in that data being altered or deleted by the calling function.
In situations where functions return references to mutable data, it is possible that the external code which called the function may make changes to the data sent. If this data was not previously cloned, the class will then be using modified data which may violate assumptions about its internal state.
Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C#', 'cwe_Prevalence': 'Undetermined'}]
范围 | 影响 | 注释 |
---|---|---|
['Access Control', 'Integrity'] | Modify Memory | Potentially data could be tampered with by another function which should not have been tampered with. |
策略:
Declare returned data which should not be altered as constant or immutable.
策略:
Clone all mutable data before returning references to it. This is the preferred mitigation. This way, regardless of what changes are made to the data, a valid copy is retained for use by the class.
This class has a private list of patients, but provides a way to see the list :
bad Java
While this code only means to allow reading of the patient list, the getPatients() method returns a reference to the class's original patient list instead of a reference to a copy of the list. Any caller of this method can arbitrarily modify the contents of the patient list even though it is a private member of the class.
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
CLASP | Mutable object returned | ||
The CERT Oracle Secure Coding Standard for Java (2011) | OBJ04-J | Provide mutable classes with copy functionality to safely allow passing instances to untrusted code | |
The CERT Oracle Secure Coding Standard for Java (2011) | OBJ05-J | Defensively copy private mutable class members before returning their references | |
SEI CERT Perl Coding Standard | EXP34-PL | Imprecise | Do not modify $_ in list or sorting functions |
Software Fault Patterns | SFP23 | Exposed Data |