Skip to content
Smart contracts Audit finding Advanced

Due to slot confusion, reserve amounts in the pump will be corrupted, resulting in wrong oracle values

The MultiFlowPump contract stores reserve counts using libraries that pack bytes16 values, but in two functions the wrong storage slot is read when the number of reserves is odd, causing the lower 128 bits of one slot to be overwritten with a value from a different slot. This corrupts reserve amounts in the pump, leading to wrong oracle values.

Trust Follow with kutugu

Originally disclosed on Code4rena

Submitted by Trust, also found by kutugu

https://github.com/code-423n4/2023-07-basin/blob/c1b72d4e372a6246e0efbd57b47fb4cbb5d77062/src/libraries/LibBytes16.sol#L45 https://github.com/code-423n4/2023-07-basin/blob/c1b72d4e372a6246e0efbd57b47fb4cbb5d77062/src/libraries/LibLastReserveBytes.sol#L58

Description

The MultiFlowPump contract stores reserve counts on every update, using the libraries LibBytes16 and LibLastReserveBytes. Those libs pack bytes16 values efficiently with the storeBytes16() and storeLastReserves functions. In case of an odd number of items, the last storage slot will be half full. Care must be taken to not step over the previous value in that slot. This is done correctly in LibBytes:

if (reserves.length & 1 == 1) {
    require(reserves[reserves.length - 1] <= type(uint128).max, "ByteStorage: too large");
    iByte = maxI * 64;
    assembly {
        sstore(
            // @audit - overwrite SLOT+MAXI*32
            add(slot, mul(maxI, 32)),
                                                       // @audit - read SLOT+MAXI*32 (2nd entry)
            add(mload(add(reserves, add(iByte, 32))), shl(128, shr(128, sload(add(slot, mul(maxI, 32))))))
        )
    }
}

As can be seen, it overwrites the slot with the previous 128 bits in the upper half of the slot, only setting the lower 128 bytes.

However, the wrong slot is read in the other two libraries. For example, in storeLastReserves():

if (reserves.length & 1 == 1) {
    iByte = maxI * 64;
    assembly {
        sstore(
            // @audit - overwrite SLOT+MAXI*32
            add(slot, mul(maxI, 32)),
                                                      // @audit - read SLOT+MAXI (2nd entry)??
            add(mload(add(reserves, add(iByte, 32))), shr(128, shl(128, sload(add(slot, maxI)))))
        )
    }
}

The error is not multiplying maxI before adding it to slot. This means that the reserves count encoded in lower 16 bytes in add(slot, mul(maxI, 32)) will have the value of a reserve in a much lower index. Slots are used in 32 byte increments, i.e. S, S+32, S+64… When maxI==0, the intended slot and the actual slot overlap. When maxI is 1..31, the read slot happens to be zero (unused), so the first actual corruption occurs on maxI==32. By substitution, we get: SLOT[32*32] = correct reserve | SLOT[32] In other words, the 4rd reserve (stored in lower 128 bits of SLOT[32]) will be written to the 64th reserve.

The Basin pump is intended to support an arbitrary number of reserves safely, therefore the described storage corruption impact is in scope.

Impact

Reserve amounts in the pump will be corrupted, resulting in wrong oracle values.

PoC

  1. A reserve update is triggered on the pump when some Well action occurs.
  2. Suppose reserves are array [0,1,2,...,63,64]
  3. Reserve count is odd, so affected code block is reached
  4. SLOT[32*32] = UPPER: 64 | LOWER: SLOT[32] = 64 | 3

Recommended Mitigation Steps

Change the sload() operation in both affected functions to sload(add(slot, mul(maxI, 32)

publiuss (Basin) confirmed, but disagreed with severity and commented:

This is a valid issue as the function incorrectly stores bytes, but it doesn’t break anything of the Pump as the bytes that are incorrectly stored are never read.

Regardless it should be fixed. Recommend changing to Medium.

alcueca (Judge) decreased severity to Medium and commented:

The bug doesn’t negatively impact the code in scope, only future code.


The original report as published on Code4rena, by Trust. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the code fails to multiply maxI by 32 when computing the slot offset in the sload call, so it reads from a slot at a much lower index than intended.

Impact

Reserve amounts in the pump could be corrupted, resulting in wrong oracle values, though the project confirmed the incorrectly stored bytes are never read and the bug does not negatively impact the code in scope.

What the attacker needed

A Well action triggers a reserve update; The number of reserves is odd; maxI is 32 or more

The fix

The sload operation in both affected functions should be changed to use the correct slot offset by multiplying maxI by 32.

How it was found

  1. A reserve update is triggered on the pump when some Well action occurs.
  2. Suppose reserves are array [0,1,2,...,63,64]
  3. Reserve count is odd, so affected code block is reached
  4. SLOT[32*32] = UPPER: 64 | LOWER: SLOT[32] = 64 | 3

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract stores data in slots, each 32 bytes. When packing many small values, the last slot may be half full. The code mistakenly reads from the wrong slot when combining the new value with the old one, so it overwrites part of a slot with data from a different slot. This corrupts the stored reserve amounts, which are used to calculate oracle values.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Trust

View all

77 more disclosed reports, best first.

More from Basin Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.