CWE-366 单线程内的竞争条件

Race Condition within a Thread

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: Medium

基本描述

If two threads of execution use a resource simultaneously, there exists the possibility that resources may be used while invalid, in turn making the state of execution undefined.

相关缺陷

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

  • cwe_Nature: ChildOf cwe_CWE_ID: 362 cwe_View_ID: 699 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', 'Other'] ['Alter Execution Logic', 'Unexpected State'] The main problem is that -- if a lock is overcome -- data could be altered in a bad state.

可能的缓解方案

Architecture and Design

策略:

Use locking functionality. This is the recommended solution. Implement some form of locking mechanism around code which alters or reads persistent data in a multithreaded environment.

Architecture and Design

策略:

Create resource-locking sanity checks. If no inherent locking mechanisms exist, use flags and signals to enforce your own blocking scheme when resources are being used by other threads of execution.

示例代码

The following example demonstrates the weakness.

bad C

int foo = 0;
int storenum(int num) {
static int counter = 0;
counter++;
if (num > foo) foo = num;
return foo;
}

bad Java

public classRace {
static int foo = 0;
public static void main() {

new Threader().start();
foo = 1;
}
public static class Threader extends Thread {

public void run() {
System.out.println(foo);
}
}
}

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CLASP Race condition within a thread
CERT C Secure Coding CON32-C CWE More Abstract Prevent data races when accessing bit-fields from multiple threads
CERT C Secure Coding CON40-C CWE More Abstract Do not refer to an atomic variable twice in an expression
CERT C Secure Coding CON43-C Exact Do not allow data races in multithreaded code
The CERT Oracle Secure Coding Standard for Java (2011) VNA02-J Ensure that compound operations on shared variables are atomic
The CERT Oracle Secure Coding Standard for Java (2011) VNA03-J Do not assume that a group of calls to independently atomic methods is atomic
Software Fault Patterns SFP19 Missing Lock

相关攻击模式

  • CAPEC-26
  • CAPEC-29

引用