2020年10月14日,Certora 开发团队的 John Toman 报告了 Solidity 代码生成器中的一个漏洞。Certora 的漏洞披露文章可以在这里找到这里.
此漏洞已在 2020年10月19日发布的 Solidity 版本 0.7.4 中修复。此漏洞存在于所有早期版本的 Solidity 中。
我们为该漏洞分配了“中等”严重性级别。
谁应该关注
此漏洞可能导致bytes 或 string 存储数组的新创建元素被非零值初始化。为了发生这种情况,以下三个操作必须发生
- 您从 memory 或 calldata 将一个空的 bytes 或 string 值复制到存储。
- 您通过修改 .length 或使用 .push()(而不是 .push(c))来扩展存储值。
- 您在第一次写入之前读取新创建的字节数组元素。
请注意,最后一步也可以通过读取整个字节数组或将其传递到其他位置来隐式完成。
读取操作可能导致新的字节数组元素为非零。
漏洞示例
contract C { bytes data; function f() public returns (bytes memory) { // Empty byte array bytes memory t; // Store something else in memory after it uint[2] memory x; x[0] = type(uint).max; // Copy the empty byte array to storage, // this will copy too much from memory. data = t; // Create a new byte array element, // this will only update the length value. data.push(); // Now, `data[0]` is `0xff` instead of `0`. return data; } }