Solidity Storage Array Bug Announcement
This weblog submit is about two bugs linked 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 doubtless present malfunctions in assessments.
Daenam Kim with assist from Nguyen Pham, each from Curvegrid found a problem the place invalid knowledge is saved in reference to arrays of signed integers.
This bug has been current since Solidity 0.4.7 and we contemplate it the extra critical of the 2. If these arrays use unfavourable integers in a sure state of affairs, it can trigger knowledge corruption and thus the bug must be straightforward to detect.
By 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 launch accommodates the fixes to the bugs.
In the mean time, we don’t plan to publish a repair to the legacy 0.4.x sequence of Solidity, however we’d if there may be standard demand.
Each bugs must be simply seen in assessments that contact the related code paths.
Particulars concerning the two bugs will be discovered under.
Signed Integer Array Bug
Who must be involved
When you have deployed contracts which use signed integer arrays in storage and both instantly assign
- a literal array with not less than one unfavourable worth in it (x = [-1, -2, -3];) or
- an current array of a totally different signed integer sort
to it, this may result in knowledge corruption within the storage array.
Contracts that solely assign particular person array components (i.e. with x[2] = -1;) should not affected.
test if contract is weak
When you use signed integer arrays in storage, attempt to run assessments the place you employ unfavourable values. The impact must be that the precise worth saved is constructive as a substitute of unfavourable.
When you have a contract that meets these circumstances, and wish to confirm whether or not the contract is certainly weak, you possibly can attain out to us through safety@ethereum.org.
Technical particulars
Storage arrays will be assigned from arrays of various sort. Throughout this copy and task 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 must 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. Specifically, the signal bit will likely be zero which makes the worth constructive.
ABIEncoderV2 Array Bug
Who must be involved
When you have 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 will be affected:
pragma experimental ABIEncoderV2;
Moreover, there are a selection of necessities for the bug to set off. See technical particulars additional under for extra data.
test if contract is weak
The bug solely manifests itself when all the following circumstances are met:
- Storage knowledge involving arrays or structs is shipped on to an exterior perform name, to abi.encode or to occasion knowledge with out prior task to a neighborhood (reminiscence) variable AND
- this knowledge both accommodates an array of structs or an array of statically-sized arrays (i.e. not less than two-dimensional).
Along with that, within the following state of affairs, your code is NOT affected:
- should you solely return such knowledge and don’t use it in abi.encode, exterior calls or occasion knowledge.
Potential penalties
Naturally, any bug can have wildly various penalties relying on this system management circulate, however we anticipate that that is extra prone 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
In the course of 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 components which might be structs or statically-sized arrays. Arrays of dynamically-sized arrays or of elementary datatypes should not affected.
The particular impact you will note is that knowledge is “shifted” within the encoded array: When you have an array of sort uint[2][] and it accommodates the information
[[1, 2], [3, 4], [5, 6]], then it will likely be encoded as [[1, 2], [2, 3], [3, 4]] as a result of the encoder solely advances by a single slot between components as a substitute of two.
This submit was collectively composed by @axic, @chriseth, @holiman