CWE-495 从公开方法中返回私有的数组类型数据域

Private Data Structure Returned From A Public Method

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: unkown

基本描述

The product has a method that is declared public, but returns a reference to a private data structure, which could then be modified in unexpected ways.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 664 cwe_View_ID: 1000 cwe_Ordinal: Primary

适用平台

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'}]

常见的影响

范围 影响 注释
Integrity Modify Application Data The contents of the data structure can be modified from outside the intended scope.

可能的缓解方案

Implementation

策略:

Declare the method private.

Implementation

策略:

Clone the member data and keep an unmodified version of the data private to the object.

Implementation

策略:

Use public setter methods that govern how a private member can be modified.

示例代码

Here, a public method in a Java class returns a reference to a private array. Given that arrays in Java are mutable, any modifications made to the returned reference would be reflected in the original private array.

bad Java

private String[] colors;
public String[] getColors() {
return colors;
}

In this example, the Color class defines functions that return non-const references to private members (an array type and an integer type), which are then arbitrarily altered from outside the control of the class.

bad C++

class Color
{
private:
int[2] colorArray;
int colorValue;
public:
Color () : colorArray { 1, 2 }, colorValue (3) { };
int[2] & fa () { return colorArray; } // return reference to private array
int & fv () { return colorValue; } // return reference to private integer
};

int main ()
{
Color c;

c.fa () [1] = 42; // modifies private array element
c.fv () = 42; // modifies private int

return 0;
}

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms Private Array-Typed Field Returned From A Public Method
Software Fault Patterns SFP23 Exposed Data