Closed c4-bot-6 closed 8 months ago
Picodes marked the issue as primary issue
othernet-global (sponsor) confirmed
Picodes marked the issue as satisfactory
Picodes marked the issue as selected for report
This report shows how in certain conditions depositLiquidityAndIncreaseShare
with zapping reverts due to an underflow. Although the impact remains minimal this is a DoS.
Picodes marked the issue as duplicate of #232
Picodes marked the issue as not selected for report
Lines of code
https://github.com/code-423n4/2024-01-salty/blob/main/src/pools/PoolMath.sol#L192 https://github.com/code-423n4/2024-01-salty/blob/main/src/pools/PoolMath.sol#L212-L227 https://github.com/code-423n4/2024-01-salty/blob/main/src/pools/PoolMath.sol#L152-L207
Vulnerability details
Impact
When a user would try to add liquidity to a pool with zapping (having unbalanced amounts), he can get his transaction reverted due to an underflow in certain scenarios. User will not lose anything but a functionality of the system can get broken in some situations.
For a user to add liquidity with unbalanced amounts of assets, have to call the function
depositLiquidityAndIncreaseShare
enabling theuseZapping
feature. This will gonna compute the amount that needs to be swapped from one token to the other in order to have a balanced ratio of the tokens compared to the pool. This computation is done in this function:The only line that is capable of underflowing is this one:
However, there is a comment above that tells us that it is checked before in a previous function.
Specifically here:
We can see that this condition is checked in order to not underflow. However, since in the computation for the swap amount normalizes the different reserves and amounts to have less bits, if one of the reserves/amounts was greater than 0 and gets shifted to 0, this condition can reverse and it would result in an underflow.
To have a clear image, consider the following example:
The user wants to add 0.01 * 10**18 of the tokenA and have some dust amount of tokenB.
When it computes the condition in
_determineZapSwapAmount
function, it will achieve the following:However, when these amounts and reserves are normalized, they get shifted 4 bits (just for this situation and for these amounts). So this condition will not be met with the normalized amounts:
Basically this will underflow and revert the whole transaction.
Notice that this just happens when one of the amounts in this condition is positive before normalization and gets 0 after bit shifting.
Proof of Concept
Check this foundry test
Setup:
Proof of Concept:
Output traces:
Tools Used
Fuzzing
Recommended Mitigation Steps
Do not use normalization of the reserves and amounts because that is the root cause of this issue
Assessed type
Under/Overflow