Wrong design of swap() results in unexpected and unfavorable outputs
The swap formula in VaderMath.sol causes the output amount to decrease when the input amount exceeds the reserve, meaning a user who pays more receives less. An attacker could exploit this by swapping a larger amount to receive a smaller output, potentially causing financial loss or manipulating the pool.
Submitted by WatchPug
The current formula to calculate the amountOut for a swap is:
function calculateSwap(
uint256 amountIn,
uint256 reserveIn,
uint256 reserveOut
) public pure returns (uint256 amountOut) {
// x * Y * X
uint256 numerator = amountIn * reserveIn * reserveOut;
// (x + X) ^ 2
uint256 denominator = pow(amountIn + reserveIn);
amountOut = numerator / denominator;
}
We believe the design (the formula) is wrong and it will result in unexpected and unfavorable outputs.
Specifically, if the amountIn is larger than the reserveIn, the amountOut starts to decrease.
Proof of Concept
Given:
- A USDV-BTC Vader pool with the reserves of
200,000 USDVand2 BTC. - If Alice swap
2 BTCfor USDV, will get50000 USDVas output; - If Bob swap
2.1 BTCfor USDV, will only get49970.25 USDVas output; - If Carol swap
2.2 BTCfor USDV, will only get49886.62 USDVas output.
For the same pool reserves, paying more for less output token is unexpected and unfavorable.
This is the intended design of the Thorchain CLP model. Can the warden provide a tangible attack vector in the form of a test?
It is true that the effect will be surprising to the user, and the issue is acknowledged by the sponsor.
@alcueca We do not acknowledge the issue. This is the intended design of the CLP model and the amount supplied for a trade is meant to be safeguarded off-chain. It is an inherent trait of the model.
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 swap formula uses a denominator that grows faster than the numerator when amountIn exceeds reserveIn, causing the output to decrease.
Impact
Users could receive less output when paying more, leading to unexpected losses, though the sponsor claims this is intended design and the amount is safeguarded off-chain.
What the attacker needed
Access to a Vader pool; ability to swap tokens
How it was found
- Set up a USDV-BTC Vader pool with reserves of 200,000 USDV and 2 BTC.
- Swap 2 BTC for USDV and observe an output of 50,000 USDV.
- Swap 2.1 BTC for USDV and observe an output of 49,970.25 USDV.
- Swap 2.2 BTC for USDV and observe an output of 49,886.62 USDV.
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.
In a swap, you expect that paying more gives you more output. Here, the formula is designed so that if you pay more than the pool's reserve, you actually get less output. This is surprising and could be used to trick users or cause losses.
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.