Skip to content
Smart contracts Audit finding Advanced

Due to bit-shifting errors, reserve amounts in the pump will be corrupted, resulting in wrong oracle values

A bit-shifting error in the storeBytes16 function caused the upper 128 bits of a storage slot to overwrite the lower 128 bits of another slot, corrupting reserve amounts and leading to wrong oracle values. An attacker could potentially manipulate the pump's reserve data, though the impact was limited because the incorrectly shifted bytes were never non-zero.

Trust Follow

Originally disclosed on Code4rena

Submitted by Trust

It is advised to first read finding: Due to slot confusion, reserve amounts in the pump will be corrupted, resulting in wrong oracle values, which provides all the contextual information for this separate bug.

We’ve discussed how a wrong sload() source slot leads to corruption of the reserves. In LibBytes16, another confusion occurs.

Recall the correct storage overwriting done in LibBytes:

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))))))
    )
}

Importantly, it clears the lower 128 bytes of the source slot and replaces the upper 128 bytes of the dest slot using the upper 128 bytes of the source slot: shl(128,shr(128,SOURCE))

In storeBytes16(), the shl() operation has been discarded. This means the code will use the upper 128 bytes of SOURCE to overwrite the lower 128 bytes in DEST.

if (reserves.length & 1 == 1) {
    iByte = maxI * 64;
    assembly {
        sstore(
        // @audit - overwrite SLOT+MAXI*32
            add(slot, mul(maxI, 32)),
                                                    // @audit - overwrite lower 16 bytes with upper 16 bytes?
            add(mload(add(reserves, add(iByte, 32))), shr(128, sload(add(slot, maxI))))
        )
    }
}

In other words, regardless of the SLOT being read, instead of keeping the lower 128 bits as is, it stores whatever happens to be in the upper 128 bits. Note this is a completely different error from the slot confusion, which happens to be in the same line of code.

Impact

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

PoC

Assume slot confusion bug has been corrected for clarity.

  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. Suppose previous reserves are array [P0,P1,...,P64]
  4. Reserve count is odd, so affected code block is reached
  5. SLOT[32*32] = UPPER: 64 | LOWER: UPPER(SLOT[32*32]) = 64 | P64

Recommended Mitigation Steps

Replace the affected line with the calculation below: shr(128, shl(128, SLOT)) This will use the lower 128 bytes and clear the upper 128 bytes,as intended.

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 shifted are never non-zero.

Regardless it should be fixed. Recommend changing to Medium.

alcueca (Judge) decreased severity to Medium and commented:

@publiuss, I noticed that in your fix you didn’t change any tests. May I suggest that you increase your test coverage to be certain that the wardens are not missing other storage corruption issues?

Other than that, without a clear PoC, I can’t accept this as High.

publiuss (Basin) commented:

@publiuss, I notice that in your fix you didn’t change any tests. May I suggest that you increase your test coverage to be certain that the wardens are not missing other storage corruption issues?

Other than that, without a clear PoC, I can’t accept this as High.

The test coverage didn’t change because the issue doesn’t actually impact the functionality. This issue has been addressed and fixed in all bytes libraries.


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 storeBytes16 function omitted a left-shift (shl) operation, so it used the upper 128 bits of the source slot instead of the lower 128 bits when overwriting the lower 128 bits of the destination slot.

What the attacker needed

A Well action triggers a reserve update; reserve count is odd

The fix

The affected line should be replaced with a calculation that uses the lower 128 bits and clears the upper 128 bits, and the issue was fixed in all bytes libraries.

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. Suppose previous reserves are array [P0,P1,...,P64]
  4. Reserve count is odd, so affected code block is reached
  5. SLOT[32*32] = UPPER: 64 | LOWER: UPPER(SLOT[32*32]) = 64 | P64

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.

This report describes a bug in a smart contract's code that stores data in a packed way. When updating reserves, a missing bit-shift operation caused the wrong part of a storage slot to be copied, which could corrupt the reserve amounts and produce incorrect oracle values. The bug was real but did not actually break anything because the affected bytes were always zero.

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.