界: API Abuse
API 是调用方和被调用方之间的约定。最常见的 API 滥用是由于调用方未能遵守此约定的终止导致的。例如,如果某个程序在调用 chroot() 后未能调用 chdir(),则违反了用于指定如何安全地更改活动根目录的约定。库滥用的另一个典型示例是期望被调用方向调用方返回可信的 DNS 信息。在这种情况下,调用方通过对被调用方行为做出某种假设(返回值可用于身份验证目的)滥用其 API。另一方也可能违反调用方-被调用方约定。例如,如果编码器子类化 SecureRandom 并返回一个非随机值,则将违反此约定。
Directory Restriction
Abstract
对
chroot()
系统调用的使用不当会让攻击者从 chroot 监牢中逃脱出来。Explanation
chroot()
系统调用允许程序修改其 file system 根目录的含义。适当地调用 chroot()
后,程序无法访问在新的根目录下定义的目录树之外的任何文件。这样的环境称为 chroot 监牢,通常用来防止攻击者破坏进程,继而访问未经授权的文件。例如,在 chroot 监牢环境中运行多个 FTP 服务器,可以防止攻击者发现新服务器漏洞后下载密码文件或者其他系统中的敏感文件。对
chroot()
的使用不当可能会让攻击者从 chroot 监牢中逃脱出来。因为 chroot()
函数的调用不会改变进程当前的工作目录,所以在调用 chroot()
之后,相对路径可能仍然会引用 chroot 监牢之外的 file system 资源。 例 1:考虑以下这段来自(假设的)FTP 服务器的源代码:
chroot("/var/ftproot");
...
fgets(filename, sizeof(filename), network);
localfile = fopen(filename, "r");
while ((len = fread(buf, 1, sizeof(buf), localfile)) != EOF) {
fwrite(buf, 1, sizeof(buf), network);
}
fclose(localfile);
这段代码负责从网络中读取文件名,然后将相应的文件在本地机器上打开,并通过网络传送内容。这段代码可用来执行 FTP
GET
命令。FTP 服务器在其初始化例程中调用 chroot()
,试图阻止对 /var/ftproot
之外的文件的访问。但因为服务器没有通过调用 chdir("/")
来更改当前的工作目录,所以攻击者可以请求文件的 "../../../../../etc/passwd
”,并获取该系统密码文件的副本。References
[1] J. Viega, G. McGraw Building Secure Software Addison-Wesley
[2] A. Chuvakin Using Chroot Securely
[3] Standards Mapping - Common Weakness Enumeration CWE ID 243
[4] Standards Mapping - General Data Protection Regulation (GDPR) Access Violation
[5] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2023 Directive 4.14
[6] Standards Mapping - OWASP Mobile 2014 M4 Unintended Data Leakage
[7] Standards Mapping - OWASP Mobile Application Security Verification Standard 2.0 MASVS-STORAGE-2
[8] Standards Mapping - Web Application Security Consortium Version 2.00 Insufficient Authorization (WASC-02)
[9] Standards Mapping - Web Application Security Consortium 24 + 2 Insufficient Authorization
desc.semantic.cpp.directory_restriction