Solidity Storage Array Bug Announcement
This weblog publish is about two bugs related to storage arrays that are in any other case unrelated. Each have been current within the compiler for a very long time and have solely been found now though a contract containing them ought to very probably present malfunctions in checks.
Daenam Kim with assist from Nguyen Pham, each from Curvegrid found a problem the place invalid information is saved in reference to arrays of signed integers.
This bug has been current since Solidity 0.4.7 and we think about it the extra severe of the 2. If these arrays use damaging integers in a sure scenario, it can trigger information corruption and thus the bug needs to be simple to detect.
Via the Ethereum bug bounty program, we acquired a report a few flaw inside the new experimental ABI encoder (known as ABIEncoderV2). The brand new ABI encoder continues to be marked as experimental, however we nonetheless suppose that this deserves a distinguished announcement since it’s already used on mainnet.
Credit to Ming Chuan Lin (of https://www.secondstate.io) for each discovering and fixing the bug!
The 0.5.10 release comprises the fixes to the bugs.
For the time being, we don’t plan to publish a repair to the legacy 0.4.x collection of Solidity, however we’d if there’s in style demand.
Each bugs needs to be simply seen in checks that contact the related code paths.
Particulars in regards to the two bugs could be discovered under.
Signed Integer Array Bug
Who needs to be involved
When you’ve got deployed contracts which use signed integer arrays in storage and both straight assign
- a literal array with a minimum of one damaging worth in it (x = [-1, -2, -3];) or
- an present array of a totally different signed integer sort
to it, this can result in information corruption within the storage array.
Contracts that solely assign particular person array parts (i.e. with x[2] = -1;) will not be affected.
Easy methods to examine if contract is weak
When you use signed integer arrays in storage, attempt to run checks the place you utilize damaging values. The impact needs to be that the precise worth saved is constructive as a substitute of damaging.
When you’ve got a contract that meets these circumstances, and need to confirm whether or not the contract is certainly weak, you possibly can attain out to us through security@ethereum.org.
Technical particulars
Storage arrays could be assigned from arrays of various sort. Throughout this copy and project operation, a kind conversion is carried out on every of the weather. Along with the conversion, particularly if the signed integer sort is shorter than 256 bits, sure bits of the worth need to be zeroed out in preparation for storing a number of values in the identical storage slot.
Which bits to zero out was incorrectly decided from the supply and never the goal sort. This results in too many bits being zeroed out. Particularly, the signal bit will likely be zero which makes the worth constructive.
ABIEncoderV2 Array Bug
Who needs to be involved
When you’ve got deployed contracts which use the experimental ABI encoder V2, then these is perhaps affected. Which means that solely contracts which use the next directive inside the supply code could be affected:
pragma experimental ABIEncoderV2;
Moreover, there are a variety of necessities for the bug to set off. See technical particulars additional under for extra data.
Easy methods to examine if contract is weak
The bug solely manifests itself when all the following circumstances are met:
- Storage information involving arrays or structs is shipped on to an exterior perform name, to abi.encode or to occasion information with out prior project to an area (reminiscence) variable AND
- this information both comprises an array of structs or an array of statically-sized arrays (i.e. a minimum of two-dimensional).
Along with that, within the following scenario, your code is NOT affected:
- if you happen to solely return such information and don’t use it in abi.encode, exterior calls or occasion information.
Attainable penalties
Naturally, any bug can have wildly various penalties relying on this system management circulate, however we anticipate that that is extra more likely to result in malfunction than exploitability.
The bug, when triggered, will below sure circumstances ship corrupt parameters on technique invocations to different contracts.
Technical particulars
Throughout the encoding course of, the experimental ABI encoder doesn’t correctly advance to the subsequent factor in an array in case the weather occupy greater than a single slot in storage.
That is solely the case for parts which might be structs or statically-sized arrays. Arrays of dynamically-sized arrays or of elementary datatypes will not be affected.
The precise impact you will notice is that information is “shifted” within the encoded array: When you’ve got an array of sort uint[2][] and it comprises the info
[[1, 2], [3, 4], [5, 6]], then will probably be encoded as [[1, 2], [2, 3], [3, 4]] as a result of the encoder solely advances by a single slot between parts as a substitute of two.
This publish was collectively composed by @axic, @chriseth, @holiman