User to sell the last supply will make the exchange contribution forever stuck
When the last supply tokens are sold, the exit contribution is deducted from the output and remains stuck in the contract because no supply is left to redeem it. An attacker could sell the final supply and cause a potentially large amount of reserve tokens to be permanently locked.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-10-mento-update-judging/issues/17
The protocol has acknowledged this issue.
Found by
0x73696d616f
Summary
BancorExchangeProvider::_getScaledAmountOut() decreases the amount out when the token in is the supply token by the exit contribution, that is, scaledAmountOut = (scaledAmountOut * (MAX_WEIGHT - exchange.exitContribution)) / MAX_WEIGHT;.
Whenever the last supply of the token is withdrawn, it will get all the reserve and store it in scaledAmountOut, and then apply the exit contribution, leaving these funds forever stuck, as there is 0 supply to redeem it.
Root Cause
In BancorExchangeProvider:345, the exchange contribution is applied regardless of there being supply left to redeem it.
Internal pre-conditions
- All supply must be withdrawn from the exchange.
External pre-conditions
None.
Attack Path
- Users call
Broker::swapIn(), that callsGoodDollarExchangeProvider::swapIn(), which is the exchange contract that holds the token and reserve balances, selling supply tokens until the supply becomes 0.
Impact
The last exit contribution will be forever stuck. This amount is unbounded and may be very significant.
PoC
Add the following test to BancorExchangeProvider.t.sol:
function test_POC_swapIn_whenTokenInIsToken_shouldSwapIn() public {
BancorExchangeProvider bancorExchangeProvider = initializeBancorExchangeProvider();
uint256 amountIn = 300_000 * 1e18;
bytes32 exchangeId = bancorExchangeProvider.createExchange(poolExchange1);
vm.startPrank(brokerAddress);
uint256 amountOut = bancorExchangeProvider.swapIn(exchangeId, address(token), address(reserveToken), amountIn);
(, , uint256 tokenSupplyAfter, uint256 reserveBalanceAfter, , ) = bancorExchangeProvider.exchanges(exchangeId);
assertEq(amountOut, 59400e18);
assertEq(reserveBalanceAfter, 600e18);
assertEq(tokenSupplyAfter, 0);
vm.expectRevert("ERR_INVALID_SUPPLY");
bancorExchangeProvider.swapIn(exchangeId, address(token), address(reserveToken), 1e18);
}
Mitigation
The specific mitigation depends on the design.
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 exchange contribution is applied even when the token supply becomes zero, so the deducted amount cannot be redeemed.
Impact
The last exit contribution will be forever stuck, and this amount is unbounded and may be very significant.
What the attacker needed
All supply must be withdrawn from the exchange
The fix
The mitigation depends on the design, but the exchange contribution should not be applied when there is no supply left to redeem it.
How it was found
- Call Broker::swapIn() to sell supply tokens until the supply becomes 0.
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 this exchange, when you sell the last of a token, a small fee (the exit contribution) is taken from what you receive. But if no tokens are left, that fee has no owner and gets stuck in the contract forever. An attacker can trigger this by selling the very last supply, locking a potentially large amount of funds.
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.