QuantAMM

QuantAMM
49,600 OP
View results
Submission Details
Severity: medium
Invalid

Integer Overflow in QuantAMMStorage Contract

Summary

A critical integer overflow vulnerability has been identified in the _quantAMMPackTwo128 function of the QuantAMMStorage contract. This function is designed to pack two 128-bit integers into a single 256-bit storage slot, but its implementation can lead to silent data corruption due to an unprotected left shift operation.

Description

The root cause is performing a large bit shift operation on an integer without proper type casting to preserve the bits during the shift operation. While the input validation prevents obviously invalid inputs, it doesn't protect against the mechanics of how Solidity handles large shift operations.

The vulnerability occurs in the left shift operation _leftInt << 128. While the function includes bounds checking to ensure input values are within int128 range, the shift operation itself is performed before any type casting, which can lead to bit loss during the shift operation.

In Solidity, when shifting integers, if the shift amount equals or exceeds the bit width of the type, the result is undefined. In this case, shifting a large int256 value by 128 bits can lead to data corruption, even if the original value was within the valid int128 range.

function _quantAMMPackTwo128(int256 _leftInt, int256 _rightInt) internal pure returns (int256 packed) {
require((_leftInt <= MAX128) && (_rightInt <= MAX128), "Overflow");
require((_leftInt >= MIN128) && (_rightInt >= MIN128), "Underflow");
packed = (_leftInt << 128) | int256(uint256(_rightInt << 128) >> 128);
}

As an example consider these inputs:

  • _leftInt = 2^127 - 1 (maximum valid int128)

  • When shifted left by 128 bits, bits are lost during the operation

  • The resulting packed value will not correctly represent the original number in the upper 128 bits

Impact

  • Silent data corruption in storage

  • Incorrect financial calculations in the AMM

  • Potential economic losses if the corrupted values are used in trading calculations

  • System state inconsistency

Recommendation

Cast to uint256 before the shift operation to ensures no bits are lost during the shift.

Updates

Lead Judging Commences

n0kto Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.