Solidity Storage Array Bugs

Solidity Storage Array Bug Announcement

This weblog publish 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 although a contract containing them ought to very possible present malfunctions in exams.

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 take into account it the extra critical of the 2. If these arrays use damaging integers in a sure scenario, it is going to trigger knowledge corruption and thus the bug ought to be simple to detect.

By the Ethereum bug bounty program, we obtained a report a few flaw inside the new experimental ABI encoder (known as ABIEncoderV2). The brand new ABI encoder remains to be marked as experimental, however we however suppose that this deserves a distinguished announcement since it’s already used on mainnet.
Credit to Ming Chuan Lin (of for each discovering and fixing the bug!

The 0.5.10 release accommodates the fixes to the bugs.
In the intervening time, we don’t plan to publish a repair to the legacy 0.4.x collection of Solidity, however we would if there’s widespread demand.

Each bugs ought to be simply seen in exams that contact the related code paths.

Particulars in regards to the two bugs might be discovered under.

Signed Integer Array Bug

Who ought to be involved

In case you have deployed contracts which use signed integer arrays in storage and both instantly assign

  • a literal array with at the very least one damaging 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;) are usually not affected.

Methods to test if contract is weak

For those who use signed integer arrays in storage, attempt to run exams the place you utilize damaging values. The impact ought to be that the precise worth saved is constructive as a substitute of damaging.

In case you have 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

Technical particulars

Storage arrays might be assigned from arrays of various sort. Throughout this copy and project operation, a sort 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 might be zero which makes the worth constructive.

ABIEncoderV2 Array Bug

Who ought to be involved

In case you have deployed contracts which use the experimental ABI encoder V2, then these is perhaps affected. Because of this solely contracts which use the next directive inside the supply code might 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 info.

Methods to 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 operate name, to abi.encode or to occasion knowledge with out prior project to an area (reminiscence) variable AND
  • this knowledge both accommodates an array of structs or an array of statically-sized arrays (i.e. at the very least two-dimensional).

Along with that, within the following scenario, your code is NOT affected:

  • when you solely return such knowledge and don’t use it in abi.encode, exterior calls or occasion knowledge.

Attainable penalties

Naturally, any bug can have wildly various penalties relying on this system management stream, however we count on that that is extra more likely to result in malfunction than exploitability.

The bug, when triggered, will underneath 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 following ingredient in an array in case the weather occupy greater than a single slot in storage.

That is solely the case for components which can be structs or statically-sized arrays. Arrays of dynamically-sized arrays or of elementary datatypes are usually not affected.

The particular impact you will notice is that knowledge is “shifted” within the encoded array: In case you have an array of sort uint[2][] and it accommodates the info
[[1, 2], [3, 4], [5, 6]], then it is going to 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 publish was collectively composed by @axic, @chriseth, @holiman

DailyBlockchain.News Admin

Our Mission is to bridge the knowledge gap and foster an informed blockchain community by presenting clear, concise, and reliable information every single day. Join us on this exciting journey into the future of finance, technology, and beyond. Whether you’re a blockchain novice or an enthusiast, is here for you.
Back to top button