CWE-478 在Switch语句中缺失缺省条件
Missing Default Case in Switch Statement
结构: Simple
Abstraction: Variant
状态: Draft
被利用可能性: unkown
基本描述
The code does not have a default case in a switch statement, which might lead to complex logical errors and resultant weaknesses.
扩展描述
This flaw represents a common problem in software development, in which not all possible values for a variable are considered or handled by a given process. Because of this, further decisions are made based on poor information, and cascading failure results. This cascading failure may result in any number of security issues, and constitutes a significant failure in the system.
相关缺陷
- cwe_Nature: ChildOf cwe_CWE_ID: 1023 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 | ['Varies by Context', 'Alter Execution Logic'] | Depending on the logical circumstances involved, any consequences may result: e.g., issues of confidentiality, authentication, authorization, availability, integrity, accountability, or non-repudiation. |
可能的缓解方案
Implementation
策略:
Ensure that there are no unaccounted for cases, when adjusting flow or values based on the value of a given variable. In switch statements, this can be accomplished through the use of the default label.
Implementation
策略:
In the case of switch style statements, the very simple act of creating a default case can mitigate this situation, if done correctly. Often however, the default case is used simply to represent an assumed option, as opposed to working as a check for invalid input. This is poor practice and in some cases is as bad as omitting a default case entirely.
示例代码
例
The following does not properly check the return code in the case where the security_check function returns a -1 value when an error occurs. If an attacker can supply data that will invoke an error, the attacker can bypass the security check:
bad C
#define PASSED 1
int result;
...
result = security_check(data);
switch (result) {
exit(-1);
//Break never reached because of exit()
break;
case PASSED:
break;
}
// program execution continues...
...
Instead a default label should be used for unaccounted conditions:
good C
#define PASSED 1
int result;
...
result = security_check(data);
switch (result) {
exit(-1);
//Break never reached because of exit()
break;
case PASSED:
break;
default:
exit(-1);
}
This label is used because the assumption cannot be made that all possible cases are accounted for. A good practice is to reserve the default case for error handling.
例
In the following Java example the method getInterestRate retrieves the interest rate for the number of points for a mortgage. The number of points is provided within the input parameter and a switch statement will set the interest rate value to be returned based on the number of points.
bad Java
public static final String INTEREST_RATE_AT_ONE_POINTS = "4.75";
public static final String INTEREST_RATE_AT_TWO_POINTS = "4.50";
...
public BigDecimal getInterestRate(int points) {
switch (points) {
break;
case 1:
break;
case 2:
break;
}
return result;
}
However, this code assumes that the value of the points input parameter will always be 0, 1 or 2 and does not check for other incorrect values passed to the method. This can be easily accomplished by providing a default label in the switch statement that outputs an error message indicating an invalid value for the points input parameter and returning a null value.
good Java
public static final String INTEREST_RATE_AT_ONE_POINTS = "4.75";
public static final String INTEREST_RATE_AT_TWO_POINTS = "4.50";
...
public BigDecimal getInterestRate(int points) {
switch (points) {
break;
case 1:
break;
case 2:
break;
default:
System.err.println("Returning null value for interest rate");
result = null;
}
return result;
}
分类映射
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
CLASP | Failure to account for default case in switch | ||
Software Fault Patterns | SFP4 | Unchecked Status Condition |
引用
文章来源于互联网:scap中文网
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论