界: Errors

错误和错误处理代表一类 API。与错误处理相关的错误非常普遍,因此它们应该拥有自己的专属章节。与“API 滥用”一样,有两种情况可以引入与错误相关的安全漏洞:最常见的一种是错误处理不当(或者根本不处理)。第二种是产生错误,这些错误要么(向潜在的攻击者)泄露过多信息,要么难以处理。

Denial of Service: External Call

Abstract
函数在循环语句内调用外部合约,这可能会导致拒绝服务。
Explanation
对外部合约的调用可能会失败,如果未正确处理失败,可能会导致调用合约内部出现拒绝服务。这可能会使合约无法进一步使用。

当在循环语句内执行外部调用时,这一点尤其重要,处理支付时更是如此,在这种情况下,通常最好让用户提取资金,而不是向他们推送资金。

示例 1:以下代码使用 for 循环语句,通过使用 send 外部调用向所有涉及的地址退款。


function refundAll() public {
for(uint x; x < refundAddresses.length; x++) {
require(refundAddresses[x].send(refunds[refundAddresses[x]]));
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 2
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 703
[6] Standards Mapping - Smart Contract Weakness Classification SWC-113
desc.structural.solidity.swc113