When calls to external contracts are made, we write the calldata starting at byte 28, and allocate the return buffer to start at byte 0 (overlapping with the calldata). When checking RETURNDATASIZE
for dynamic types, the size is compared only to the minimum allowed size for that type, and not to the returned value's length
. As a result, malformed return data can cause the contract to mistake its own calldata for returndata.
When arguments are packed for an external call, we create a buffer of size max(args, return_data) + 32
. The calldata is placed in this buffer (starting at byte 28), and the return buffer is allocated to start at byte 0. The assumption is that we can reuse the memory becase we will not be able to read past RETURNDATASIZE
.
When data is returned, we unpack the return data by starting at byte 0. We check that RETURNDATASIZE
is greater than the minimum allowed for the returned type:
This check ensures that any dynamic types returned will have a size of at least 64. However, it does not verify that RETURNDATASIZE
is as large as the length
word of the dynamic type.
As a result, if a contract expects a dynamic type to be returned, and the part of the return data that is read as length
includes a size that is larger than the actual RETURNDATASIZE
, the return data read from the buffer will overrun the actual return data size and read from the calldata.
This contract calls an external contract with two arguments. As the call is made, the buffer includes:
byte 28: method_id
byte 32: first argument (0)
byte 64: second argument (hash)
The return data buffer begins at byte 0, and will return the returned bytestring, up to a maximum length of 96 bytes.
On the other side, imagine a simple contract that does not, in fact, return a bytestring, but instead returns two uint256s. I've implemented it in Solidity for ease of use with Foundry:
The return data will be parsed as a bytestring. The first 32 will point us to byte 32 to read the length. The second 32 will be perceived as the length. It will then read the next 32 bytes from the return data buffer, even though those weren't a part of the return data.
Since these bytes will come from byte 64, we can see above that the hash was placed there in the calldata.
If we run the following Foundry test, we can see that this does in fact happen:
Malicious or mistaken contracts returning the malformed data can result in overrunning the returned data and reading return data from the calldata buffer.
Manual Review, Foundry
If we want to continue to use the same buffer for calldata and return data, add an additional safety check for dynamic return types that the RETURNDATASIZE
is checked against the bytes that will be unpacked as the length.
Alternatively, allocate the return data buffer separately in memory.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.