ArrakisV2Router#addLiquidityPermit2 will strand ETH
The ArrakisV2Router contract's addLiquidityPermit2 function incorrectly sets a boolean flag, causing excess ETH to be refunded incorrectly and leaving ETH stranded in the contract. An attacker could potentially steal the stranded ETH, and could manipulate pools to increase the amount available to steal.
Source: https://github.com/sherlock-audit/2023-06-arrakis-judging/issues/183
Found by
0x007, 0x52, 0xpinky, BenRai, DadeKuma, Jeiwan, auditor0517, auditsea, branch_indigo, elephant_coral, kutugu, rvierdiiev, tallo
Summary
Inside ArrakisV2Router#addLiquidityPermit2, isToken0Weth is set incorrectly leading to the wrong amount of ETH being refunded to the user
Vulnerability Detail
bool isToken0Weth;
_permit2Add(params_, amount0, amount1, token0, token1);
_addLiquidity(
params_.addData.vault,
amount0,
amount1,
sharesReceived,
params_.addData.gauge,
params_.addData.receiver,
token0,
token1
);
if (msg.value > 0) {
if (isToken0Weth && msg.value > amount0) {
payable(msg.sender).sendValue(msg.value - amount0);
} else if (!isToken0Weth && msg.value > amount1) {
payable(msg.sender).sendValue(msg.value - amount1);
}
}
Above we see that excess msg.value is returned to the user at the end of the function. This uses the value of isToken0Weth to determine the amount to send back to the user. The issue is that isToken0Weth is set incorrectly and will lead to ETH being stranded in the contract. isToken0Weth is never set, it will always be false. This means that when WETH actually is token0 the incorrect amount of ETH will be sent back to the user.
This same issue can also be used to steal the ETH left in the contract by a malicious user. To make matters worse, the attacker can manipulate the underlying pools to increase the amount of ETH left in the contract so they can steal even more.
Impact
ETH will be stranded in contract and stolen
Code Snippet
Tool used
Manual Review
Recommendation
Move isToken0Weth and set it correctly:
- bool isToken0Weth;
_permit2Add(params_, amount0, amount1, token0, token1);
_addLiquidity(
params_.addData.vault,
amount0,
amount1,
sharesReceived,
params_.addData.gauge,
params_.addData.receiver,
token0,
token1
);
if (msg.value > 0) {
+ bool isToken0Weth = _isToken0Weth(address(token0), address(token1));
if (isToken0Weth && msg.value > amount0) {
payable(msg.sender).sendValue(msg.value - amount0);
} else if (!isToken0Weth && msg.value > amount1) {
payable(msg.sender).sendValue(msg.value - amount1);
}
}
Discussion
Gevarist
We consider the issue as a medium issue, some user fund can be lost. Only the stranded eth can be potentially stolen.
ctf-sec
The finding result in lose of fund, recommend maintaining high severity.
Gevarist
@ctf-sec stealing fund is only possible if loss of fund happen before. I would argue that seems like a medium severity issue.
0xpinky
@Gevarist stealing also one of the way for losing funds.. but thats not the only way.. any form of loss is loss only.
Gevarist
You can find a fix on v2-periphery repository, isToken0Weth is now correctly set using _isToken0Weth.
IAm0x52
Fix looks good. isToken0Weth is now set correctly in all cases
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
The boolean variable isToken0Weth is declared but never assigned, so it always defaults to false, causing incorrect refund logic.
Impact
ETH will be stranded in the contract and could be stolen by an attacker, who may also manipulate pools to increase the amount.
What the attacker needed
None
The fix
The fix was to set isToken0Weth correctly using the _isToken0Weth function before the refund logic.
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.
When users add liquidity to a pool, they might send more ETH than needed, and the contract should refund the excess. A variable that decides which token is WETH is never set, so the contract always thinks WETH is not the first token, leading to wrong refunds and ETH stuck in the contract. Attackers could later take that stuck ETH.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.