Unchecked return value bug on TransferHelper::_safeTransferFrom()
The TransferHelper::_safeTransferFrom() function ignores the boolean return value from _callOptionalReturn(), so if an ERC-20 token's transferFrom() returns false instead of reverting, the transfer failure goes undetected. An attacker could exploit this to deposit collateral or liquidate positions without actually supplying tokens.
serial-coder Follow with 0x11singh99, josephdara and 5 others
Originally disclosed on Code4rena
Submitted by serial-coder, also found by 0x11singh99, josephdara, Jorgect, unix515, Mrxstrange, nonseodion, and Rhaydden
Currently, the WiseLending protocol supports several ERC-20 tokens and will also support more tokens in the future:
ERC20 in scope: WETH, WBTC, LINK, DAI, WstETH, sDAI, USDC, USDT, WISE and may others in the future. (Also corresponding Aave tokens if existing).
On some ERC-20 tokens, the transferFrom() will return false on failure instead of reverting a transaction. I noticed that the TransferHelper::_safeTransferFrom(), which is used throughout the protocol, is vulnerable to detecting the token transfer failure if the transferFrom() returns false due to the unchecked return value bug.
The following lists the functions directly invoking the vulnerable _safeTransferFrom():
WiseCore::_coreLiquidation()- [
WiseLending::depositExactAmount()](https://github.com/code-423n4/2024-02-wise-lending/blob/79186b243d8553e66358c05497e5ccfd9488b5e2/contracts/WiseLending.sol#L476-L481) WiseLending::solelyDeposit()WiseLending::paybackExactAmount()WiseLending::paybackExactShares()FeeManager::paybackBadDebtForToken()FeeManager::paybackBadDebtNoReward()PendlePowerFarm::_manuallyPaybackShares()PendlePowerManager::enterFarm()PendlePowerFarmController::exchangeRewardsForCompoundingWithIncentive()PendlePowerFarmController::exchangeLpFeesForPendleWithIncentive()PendlePowerFarmController::lockPendle()PendlePowerFarmToken::addCompoundRewards()PendlePowerFarmToken::depositExactAmount()AaveHub::depositExactAmount()AaveHub::paybackExactAmount()AaveHub::paybackExactShares()
Due to the unchecked return value bug, users or attackers can exploit these protocol’s functions without supplying a token (please refer to the Proof of Concept section for more details).
Proof of Concept
On some ERC-20 tokens, the
transferFrom()will return false on failure instead of reverting a transaction.
The WiseLending protocol implements the TransferHub::_callOptionalReturn() as a helper function for executing low-level calls. In case the transferFrom() returns false on failure, the _callOptionalReturn() will receive the returned parameters: success == true and returndata != bytes(0).
Then, the _callOptionalReturn() will decode the received returndata for the success status. If the transferFrom() returns false, the results will become false.
With the results == false, finally, the _callOptionalReturn() will return false to its function caller.
On the TransferHelper::_safeTransferFrom(), I noticed that the function does not evaluate the return value (i.e., unchecked return value bug) of the _callOptionalReturn(). Subsequently, the _safeTransferFrom() cannot detect the token transfer failure if the transferFrom() returns false.
// FILE: https://github.com/code-423n4/2024-02-wise-lending/blob/79186b243d8553e66358c05497e5ccfd9488b5e2/contracts/TransferHub/CallOptionalReturn.sol
function _callOptionalReturn(
address token,
bytes memory data
)
internal
returns (bool call)
{
(
bool success,
@1 bytes memory returndata //@audit -- On some tokens, the transferFrom() will return false instead of reverting a transaction
) = token.call(
data
);
@2 bool results = returndata.length == 0 || abi.decode(
@2 returndata, //@audit -- If the transferFrom() returns false, the results == false
@2 (bool)
@2 );
if (success == false) {
revert();
}
@3 call = success
@3 && results // @audit -- If the results == false, the _callOptionalReturn() will return false
@3 && token.code.length > 0;
}
...
// FILE: https://github.com/code-423n4/2024-02-wise-lending/blob/79186b243d8553e66358c05497e5ccfd9488b5e2/contracts/TransferHub/TransferHelper.sol
function _safeTransferFrom(
address _token,
address _from,
address _to,
uint256 _value
)
internal
{
@4 _callOptionalReturn( //@audit -- The _safeTransferFrom() cannot detect the token transfer failure if the transferFrom() returns false instead of reverting a transaction due to the unchecked return value bug
_token,
abi.encodeWithSelector(
IERC20.transferFrom.selector,
_from,
_to,
_value
)
);
}
@1 -- On some tokens, the transferFrom() will return false instead of reverting a transaction: see here.
@2 -- If the transferFrom() returns false, the results == false: see here.
@3 -- If the results == false, the _callOptionalReturn() will return false: see here.
@4 -- The _safeTransferFrom() cannot detect the token transfer failure if the transferFrom() returns false instead of reverting a transaction due to the unchecked return value bug: see here.
Note: Please refer to the Impact section for a complete list of the functions directly invoking the vulnerable _safeTransferFrom().
The following briefly analyzes 2 of the 17 functions that would affect the exploitation as examples:
- Due to the unchecked return value bug, the
WiseCore::_coreLiquidation()cannot be aware of the token transfer failure. Thus, a rogue liquidator can steal collateral from the target liquidable position without supplying any debt token (tokenToPayback). - On the
WiseLending::depositExactAmount(), a rogue depositor can increase their collateral without spending any token.
// FILE: https://github.com/code-423n4/2024-02-wise-lending/blob/79186b243d8553e66358c05497e5ccfd9488b5e2/contracts/WiseCore.sol
function _coreLiquidation(
CoreLiquidationStruct memory _data
)
internal
returns (uint256 receiveAmount)
{
...
@5 _safeTransferFrom( //@audit -- Liquidator can steal collateral (_receiveToken) from the target liquidable position
@5 _data.tokenToPayback,
@5 _data.caller,
@5 address(this),
@5 _data.paybackAmount
@5 );
...
}
...
// FILE: https://github.com/code-423n4/2024-02-wise-lending/blob/79186b243d8553e66358c05497e5ccfd9488b5e2/contracts/WiseLending.sol
function depositExactAmount(
uint256 _nftId,
address _poolToken,
uint256 _amount
)
public
syncPool(_poolToken)
returns (uint256)
{
uint256 shareAmount = _handleDeposit(
msg.sender,
_nftId,
_poolToken,
_amount
);
@6 _safeTransferFrom( //@audit -- Depositor can increase their collateral without supplying any token
@6 _poolToken,
@6 msg.sender,
@6 address(this),
@6 _amount
@6 );
return shareAmount;
}
@5 -- Liquidator can steal collateral (_receiveToken) from the target liquidable position: see here.
@6 -- Depositor can increase their collateral without supplying any token: see here.
Recommended Mitigation Steps
Improve the _safeTransferFrom() by checking the return value from the _callOptionalReturn() and reverting a transaction if it is false.
function _safeTransferFrom(
address _token,
address _from,
address _to,
uint256 _value
)
internal
{
- _callOptionalReturn(
+ bool success = _callOptionalReturn(
_token,
abi.encodeWithSelector(
IERC20.transferFrom.selector,
_from,
_to,
_value
)
);
+ require(success, "Token transfer failed");
}
Assessed type
Invalid Validation
vm06007 (Wise Lending) commented:
This then leads to some tokens unusable (like USDT, for example) and this topic was already discussed severely during hats competition where I can send links to findings, so should be scraped in my opinion. Also sufficient checks are already done in
_callOptionalReturndirectly.
vm06007 (Wise Lending) commented:
Mitigated here.
Additionally,
WiseLendingis not meant to be used with corrupted tokens that have unsupported transfer/transferFrom implementations.
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
Impact
A rogue liquidator could steal collateral without paying the debt token, and a rogue depositor could increase their collateral without spending any token; however, the protocol owner later stated that WiseLending is not meant to be used with such tokens and that the issue was mitigated.
What the attacker needed
Protocol supports a token that returns false on failed…; attacker can call deposit or liquidation functions
The fix
The _safeTransferFrom() should check the return value from _callOptionalReturn() and revert if it is false; the owner also mitigated it and stated that the protocol is not meant to be used with corrupted tokens.
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.
Some tokens signal a failed transfer by returning false instead of stopping the transaction with an error. The code that calls these tokens ignores that false result, so it thinks the transfer succeeded when it did not. This could let someone pretend to deposit or repay tokens without actually sending them.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.