界: Time and State

分布式计算是关于时间和状态的。也就是说,为了让多个组件相互通信,必须共享状态,所有这些都需要时间。

大多数程序员都会将其工作拟人化。他们会让一个控制线程以同样的方式(他们必须自己完成工作时采取的方式)执行整个程序。然而,现代计算机不同任务之间切换得非常快,在多核、多 CPU 或分布式系统中,两个事件可能完全同时发生。程序员预期的程序执行过程与实际情况之间存在差距,即存在缺陷。这些缺陷与线程、流程、时间和信息之间的意外交互有关。这些交互是通过共享状态发生的:信号量、变量、文件系统,以及总而言之,可以存储信息的任何内容。

Race Condition: File System Access

Abstract
可以利用检查文件属性和使用文件之间的时间窗口发起权限提升攻击。
Explanation
在以下情况下,将发生文件访问争用情况(称为检查时间、使用时间 (TOCTOU) 争用情况):

1.程序检查文件的属性,按名称引用文件。

2.程序稍后使用相同的文件名执行文件系统操作,并假定先前检查的属性没有改变。
示例:以下程序调用 CBL_CHECK_FILE_EXIST 例程在创建文件之前检查该文件是否存在,并执行所需的操作。


CALL "CBL_CHECK_FILE_EXIST" USING
filename
file-details
RETURNING status-code
END-CALL

IF status-code NOT = 0
MOVE 3 to access-mode
MOVE 0 to deny-mode
MOVE 0 to device

CALL "CBL_CREATE_FILE" USING
filename
access-mode
deny-mode
device
file-handle
RETURNING status-code
END-CALL
END-IF


CBL_CHECK_FILE_EXIST 的调用按预期正常运行并返回非零值,表明文件不存在。然而,由于 CBL_CHECK_FILE_EXISTCBL_CREATE_FILE 均作用于文件名而非文件句柄,因此无法保证 filename 变量仍然引用传递到 CBL_CREATE_FILE(当该变量传递到 CBL_CHECK_FILE_EXIST 时其引用的文件)时磁盘上的同一文件。如果攻击者在调用 CBL_CHECK_FILE_EXIST 后创建 filename,则对 CBL_CREATE_FILE 的调用将会失败,致使程序相信文件为空,而事实上该文件包含攻击者控制的数据。

此类攻击的漏洞窗口是测试属性和使用文件之间的时间段。即使在检查后立即使用,现代操作系统也不能保证进程挂起 CPU 之前执行的代码量。攻击者有多种技术来延长机会窗口的长度,以便更容易地利用漏洞。然而,即使窗口很小,漏洞利用尝试也可以简单地重复一遍又一遍,直到成功。

此外,这种类型的漏洞可能适用于具有 root 权限的程序,该程序可代表非特权用户执行某些文件操作,并使用访问检查来确保它不会使用其 root 权限执行不应被当前用户访问的操作。通过欺骗程序执行原本不允许的操作,攻击者可能会获得提升的权限。
References
[1] J. Viega, G. McGraw Building Secure Software Addison-Wesley
[2] Standards Mapping - Common Weakness Enumeration CWE ID 362, CWE ID 367
[3] Standards Mapping - Common Weakness Enumeration Top 25 2022 [22] CWE ID 362
[4] Standards Mapping - Common Weakness Enumeration Top 25 2023 [21] CWE ID 362
[5] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-000366, CCI-003178
[6] Standards Mapping - General Data Protection Regulation (GDPR) Access Violation
[7] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 1.3
[8] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2023 Directive 5.1, Rule 1.3
[9] Standards Mapping - NIST Special Publication 800-53 Revision 4 CM-5 Access Restrictions for Change (P1), CM-6 Configuration Settings (P1), SA-11 Developer Security Testing and Evaluation (P1)
[10] Standards Mapping - NIST Special Publication 800-53 Revision 5 CM-5 Access Restrictions for Change, CM-6 Configuration Settings, SA-11 Developer Testing and Evaluation
[11] Standards Mapping - OWASP Application Security Verification Standard 4.0 1.11.2 Business Logic Architectural Requirements (L2 L3), 1.11.3 Business Logic Architectural Requirements (L3), 11.1.6 Business Logic Security Requirements (L2 L3)
[12] Standards Mapping - OWASP Top 10 2021 A04 Insecure Design
[13] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[14] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[16] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[17] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[18] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[19] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.3 - Terminal Software Attack Mitigation
[20] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.3 - Terminal Software Attack Mitigation
[21] Standards Mapping - SANS Top 25 2009 Insecure Interaction - CWE ID 362
[22] Standards Mapping - SANS Top 25 2010 Insecure Interaction - CWE ID 362
[23] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3630.1 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3630.1 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3630.1 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3630.1 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3630.1 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3630.1 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3630.1 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-001995 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-001995 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-001995 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-001995 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-001995 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-001995 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-001995 CAT II
[37] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-001995 CAT II
[38] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-001995 CAT II
[39] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-001995 CAT II
[40] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-001995 CAT II
[41] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-001995 CAT II
[42] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-001995 CAT II
[43] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-001410 CAT II, APSC-DV-001995 CAT II
[44] Standards Mapping - Security Technical Implementation Guide Version 6.1 APSC-DV-001410 CAT II, APSC-DV-001995 CAT II
desc.controlflow.cobol.file_access_race_condition