Solidity Empty Byte Array Copy Bug

2020年10月4日, John Toman 发现该漏洞。

影响范围 version < 0.7.4, 中危。

漏洞简述

依次执行以下三个操作将造成,bytesstring被非零值初始化。

  • memory/calldata copy 空的bytes/stringstorage

  • 对该storage中的 bytes/string.push() 操作,使length增长。

  • 在向新空间写入前,直接读取

举例如下:

contract C {
    bytes data;
    function f() public returns (bytes memory) {
        uint[2] memory x;
        x[0] = type(uint).max; // 先在memory中存点别的东西
        bytes memory t;
        data = t;
        data.push();
        return data; // 这里会越界读到 x 的值,返回值会是 0xff 而非 0x00
    }
}