Bonding.sol _unbondAndBreak does not account for edge case where no tokens are returned
The bonding contract calls the DEX handler to remove liquidity but does not handle the case where the handler returns the LP tokens instead of the underlying assets. If that edge case occurs, the user's LP tokens become stuck in the bonding contract and the user loses their value.
Originally disclosed on Code4rena
Submitted by harleythedog
In Bonding.sol, the internal function _unbondAndBreak transfers a user’s stake tokens to the dexHandler and then calls removeLiquidity on the dexHandler. Within the Uniswap handler (which is the only handler so far) removeLiquidity takes special care in the edge case where router.removeLiquidity returns zero tokens. Specifically, the Uniswap handler has this code:
if (amountMalt == 0 || amountReward == 0) {
liquidityBalance = lpToken.balanceOf(address(this));
lpToken.safeTransfer(msg.sender, liquidityBalance);
return (amountMalt, amountReward);
}
If this edge case does indeed happen (i.e. if something is preventing the Uniswap router from removing liquidity at the moment), then the Uniswap handler will transfer the LP tokens back to Bonding.sol. However, Bonding.sol does not have any logic to recognize that this happened, so the LP tokens will become stuck in the contract and the user will never get any of their value back. This could be very bad if the user unbonds a lot of LP and they don’t get any of it back.
Proof of Concept
See _unbondAndBreak here: https://github.com/code-423n4/2021-11-malt/blob/c3a204a2c0f7c653c6c2dda9f4563fd1dc1cecf3/src/contracts/Bonding.sol#L226
Notice how the edge case where amountMalt == 0 || amountReward == 0 is not considered in this function, but it is considered in the Uniswap handler’s removeLiquidity here: https://github.com/code-423n4/2021-11-malt/blob/c3a204a2c0f7c653c6c2dda9f4563fd1dc1cecf3/src/contracts/DexHandlers/UniswapHandler.sol#L240
Recommended Mitigation Steps
Add a similar edge case check to _unbondAndBreak. In the case where LP tokens are transferred back to Bonding.sol instead of malt/reward, these LP tokens should be forwarded back to the user since the value is rightfully theirs.
Alex the Entreprenerd (judge) commented:
In the
thencase the tokens will be sent back to theBonding.solcontract, which has no way of rescuing or returning the tokens, probably reverting would be a better solution.Because the warden identified a way for tokens to get stucked, I think Medium Severity to be appropriate
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 bonding contract does not check for the zero-amount edge case that the DEX handler handles by returning LP tokens, so it has no path to forward those tokens to the user.
Impact
A user who unbonds a large amount of LP during the edge case could lose all of that value, as the tokens remain stuck in the contract.
What the attacker needed
Uniswap router returns zero tokens; user unbonds LP tokens
The fix
The bonding function should handle the edge case by forwarding the returned LP tokens back to the user, or by reverting instead.
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 you put money into a contract, you expect to get it back when you withdraw. Here, a special situation makes the contract hand back a different kind of token (the liquidity pool token) instead of the normal payout, and the main contract has no code to pass that token on to the user. So the user's value sits in the contract forever.
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.