In the VectorRuleQuantAMMStorage contract, the _quantAMMUnpack128Matrix function contains a critical flaw that could lead to price calculation errors in the AMM. The function double-writes the last element when unpacking odd-sized covariance matrices:
This double-write is particularly dangerous because this function is called during covariance matrix updates in QuantAMMCovarianceBasedRule:
The second write could overwrite the correct value with corrupted data, leading to incorrect covariance calculations. Since these matrices directly influence price calculations through:
A corrupted matrix element could cause the AMM to calculate incorrect prices or enter an invalid state, potentially leading to financial losses.
The implementation attempts to handle odd-sized matrices in both the main unpacking loop and with a special case at the end, leading to double-writing of the last element.
A pool is using a 3x3 covariance matrix for price calculations. During normal operation, the matrix data needs to be unpacked from storage. The unpacking function first processes all elements through its main loop, correctly writing the last element (position [2][2]) with value X.
However, because 3x3 = 9 elements (odd number), the special case handling at the end of the function triggers. It writes to position [2][2] again, but due to differences in how the int128 conversions handle the packed data in these two different code paths, the final value becomes Y instead of X.
This corrupted matrix element then causes the next price update's covariance calculations to use the wrong value, leading to incorrect prices being quoted by the AMM.
Remove the redundant last element assignment and handle odd-sized matrices in the main loop:
Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelyhood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.