Unwhitelisted token can cause disaster
The sendCollaterals function in ActivePool.sol does not verify that the token being sent is whitelisted. If an allowed contract calls it with a non-whitelisted token, the token index defaults to zero, causing incorrect updates to the balance of the whitelisted token at index zero and allowing the non-whitelisted token to be transferred.
Originally disclosed on Code4rena
Submitted by csanuragjain
Impact
Contract instability and financial loss. This will happen if one of the allowed contract calls sendCollaterals with non whitelisted token (may happen with user input on allowed contract)
Proof of Concept
- Navigate to contract at https://github.com/code-423n4/2021-12-yetifinance/blob/main/packages/contracts/contracts/ActivePool.sol
- Assume sendCollaterals function is called by one of allowed contract with a non whitelisted token and amount as 1
function sendCollaterals(address _to, address[] memory _tokens, uint[] memory _amounts) external override returns (bool) {
_requireCallerIsBOorTroveMorTMLorSP();
require(_tokens.length == _amounts.length);
for (uint i = 0; i < _tokens.length; i++) {
_sendCollateral(_to, _tokens[i], _amounts[i]); // reverts if send fails
}
if (_needsUpdateCollateral(_to)) {
ICollateralReceiver(_to).receiveCollateral(_tokens, _amounts);
}
return true;
}
- This calls _sendCollateral with our non whitelisted token and amount as 1
function _sendCollateral(address _to, address _collateral, uint _amount) internal returns (bool) {
uint index = whitelist.getIndex(_collateral);
poolColl.amounts[index] = poolColl.amounts[index].sub(_amount);
bool sent = IERC20(_collateral).transfer(_to, _amount);
require(sent);
emit ActivePoolBalanceUpdated(_collateral, _amount);
emit CollateralSent(_collateral, _to, _amount);
}
- whitelist.getIndex(_collateral); will return 0 as our collateral is not whitelisted and will not be present in whitelist.getIndex(_collateral);. This means index will point to whitelisted collateral at index 0
- poolColl.amounts[index] will get updated for whitelisted collateral at index 0 even though this collateral was never meant to be updated
poolColl.amounts[index] = poolColl.amounts[index].sub(_amount);
- Finally our non supported token gets transferred to recipient and since _needsUpdateCollateral is true so recipient poolColl.amounts gets increased even though recipient never received the whitelisted collateral
- Finally sender pool amount will be reduced even though it has the whitelisted collateral and recipient pool amount will be increased even though it does not have whitelisted collateral
Recommended Mitigation Steps
Add a check to see if collateral to be transferred is whitelisted
kingyetifinance (Yeti finance) disputed:
@LilYeti: Thanks for the thorough run through. It is true, but this is abstracted away, all calls of sendCollateral are internal / between contracts in our codebase, and there are checks for valid collateral in whitelist before this.
Validating data integrity outside a function inside the same contract would be a low severity. Validating data integrity in an external contract is medium severity. Many things can go wrong.
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 contract does not check that the token is whitelisted before using its index from the whitelist, so a non-whitelisted token gets index zero and corrupts the balance of the first whitelisted token.
Impact
This could cause contract instability and financial loss if an allowed contract calls sendCollaterals with a non-whitelisted token, though the project disputes that such calls are possible because all calls are internal and validated beforehand.
What the attacker needed
An allowed contract calls sendCollaterals; The call includes a non-whitelisted token
The fix
The contract should check that the collateral to be transferred is whitelisted before proceeding.
How it was found
- Assume an allowed contract calls sendCollaterals with a non-whitelisted token and amount 1.
- sendCollaterals calls _sendCollateral with that token and amount.
- _sendCollateral calls whitelist.getIndex on the token, which returns 0 because the token is not whitelisted.
- The balance of the whitelisted token at index 0 is reduced by the amount.
- The non-whitelisted token is transferred to the recipient.
- If _needsUpdateCollateral is true, the recipient's balance is increased even though they did not receive the whitelisted token.
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.
The smart contract keeps a list of approved tokens (a whitelist). When it sends tokens, it looks up the token's position in that list. If the token isn't on the list, the lookup returns zero, so the contract mistakenly updates the balance of the first approved token instead of rejecting the transfer. This can mess up the contract's accounting and potentially cause financial loss.
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.