Borrowers can avoid liquidations, if ERC777 token is configured as an emissionToken
If an ERC777 token is configured as an emission token, a malicious borrower can revert the reward transfer during liquidation, causing the entire liquidation transaction to fail. This allows the undercollateralised borrower to avoid liquidation, potentially accruing bad debt for the protocol.
Submitted by Udsen
https://github.com/code-423n4/2023-07-moonwell/blob/main/src/core/MErc20.sol#L139-L142
https://github.com/code-423n4/2023-07-moonwell/blob/main/src/core/MToken.sol#L1002
If a borrower is undercollateralized then he can be liquidated by a liquidator by calling the MErc20.liquidateBorrow function. liquidateBorrow function calls the MToken.liquidateBorrowFresh in its execution process. Inside the liquidateBorrowFresh function the MToken.repayBorrowFresh is called which verifies whether repayment of borrowed amount is allowed by calling the Comptroller.repayBorrowAllowed function. The repayBorrowAllowed function updates the borrower eligible rewards by calling the MultiRewardDistributor.updateMarketBorrowIndexAndDisburseBorrowerRewards.
The MultiRewardDistributor.updateMarketBorrowIndexAndDisburseBorrowerRewards function calls the disburseBorrowerRewardsInternal to ditribute the multi rewards to the borrower. The disburseBorrowerRewardsInternal calls the sendReward function if the _sendTokens flag is set to true.
sendReward function is called for each emissionConfig.config.emissionToken token of the MarketEmissionConfig[] array of the given _mToken market.
sendReward function transafers the rewards to the borrower using the safeTransfer function as shown below:
token.safeTransfer(_user, _amount);
Problem here is that emissionToken can be ERC777 token (which is backward compatible with ERC20) thus allowing a malicious borrower (the recipient contract of rewards in this case) to implement tokensReceived hook in its contract and revert the transaction inside the hook. This will revert the entire liquidation transaction. Hence the undercollateralized borrower can avoid the liquidation thus putting both depositors and protocol in danger.
Proof of Concept
function liquidateBorrow(address borrower, uint repayAmount, MTokenInterface mTokenCollateral) override external returns (uint) {
(uint err,) = liquidateBorrowInternal(borrower, repayAmount, mTokenCollateral);
return err;
}
https://github.com/code-423n4/2023-07-moonwell/blob/main/src/core/MErc20.sol#L139-L142
(uint repayBorrowError, uint actualRepayAmount) = repayBorrowFresh(liquidator, borrower, repayAmount);
https://github.com/code-423n4/2023-07-moonwell/blob/main/src/core/MToken.sol#L1002
if (_amount > 0 && _amount <= currentTokenHoldings) {
// Ensure we use SafeERC20 to revert even if the reward token isn't ERC20 compliant
token.safeTransfer(_user, _amount);
return 0;
} else {
Tools Used
VSCode
Recommended Mitigation Steps
Hence it is recommended to disallow any ERC777 tokens being configured as emissionToken in any MToken market and only allow ERC20 tokens as emissionTokens.
sorrynotsorry (Lookout) commented:
This logic is a bit off as the malicious user will not be having the ERC777 MToken rewards anyway, so the value extracted from this scenario doesn’t match.
Only allow ERC20 tokens as emissionTokens
Above mitigation recommendation is also matching the start of the submission. Erc777’s are Erc20 tokens as well.
alcueca (Judge) decreased severity to Medium and commented: The finding shows that rewards are distributed as part of a liquidation.
If an ERC777 token is set as an emissionsToken, which has transfer hooks, a malicious borrower can revert on receiving rewards, and avoid liquidation. Avoiding liquidation in certain situations accrues bad debt for the protocol, or can be compounded with other vulnerabilities. This would be a valid Medium.
@ElliotFriedman, do you have in your documentation anything mentioning that ERC777 tokens should not be used as emission tokens?
ElliotFriedman (Moonwell) confirmed and commented:
This is a valid finding.
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 protocol allows ERC777 tokens as emission tokens and transfers rewards during liquidation without handling the token's transfer hook, so a borrower can revert the reward transfer and thus the liquidation.
Impact
An undercollateralised borrower could avoid liquidation, which may accrue bad debt for the protocol or be compounded with other vulnerabilities.
What the attacker needed
ERC777 token configured as emission token; borrower is a contract; borrower is undercollateralised
The fix
The protocol should disallow ERC777 tokens from being configured as emission tokens, allowing only standard ERC20 tokens.
How it was found
- A borrower becomes undercollateralised.
- A liquidator calls liquidateBorrow on the MErc20 market.
- The liquidation process calls repayBorrowFresh, which triggers reward distribution via MultiRewardDistributor.
- The distributor calls sendReward, which uses safeTransfer to send the emission token to the borrower.
- If the emission token is ERC777, the borrower's contract receives the tokens and its tokensReceived hook reverts.
- The revert propagates and the entire liquidation transaction fails, so the borrower avoids liquidation.
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 lending protocol, if you borrow too much relative to your collateral, anyone can liquidate you (repay your debt and take your collateral at a discount). This report shows that if the protocol pays rewards in a special token type (ERC777) that lets the receiver run code when receiving tokens, a malicious borrower can make that code fail, which cancels the whole liquidation. So the borrower escapes being liquidated.
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.