collectFees() updates delinquency wrongly as _writeState() is called before assets are transferred
The collectFees() function calls _writeState() before transferring assets to the fee recipient, so totalAssets() is higher than it should be when checking delinquency. This could cause the market to be incorrectly marked as not delinquent, allowing the borrower to avoid paying the delinquency fee for a period.
Submitted by MiloTruck, also found by deth, yumsec (1, 2), deepkin, and T1MOH
The collectFees() function calls _writeState() before transferring assets to the feeRecipient:
_writeState(state);
asset.safeTransfer(feeRecipient, withdrawableFees);
However, _writeState() calls totalAssets() when checking if the market is delinquent:
WildcatMarketBase.sol#L448-L453
function _writeState(MarketState memory state) internal {
bool isDelinquent = state.liquidityRequired() > totalAssets();
state.isDelinquent = isDelinquent;
_state = state;
emit StateUpdated(state.scaleFactor, isDelinquent);
}
totalAssets() returns the current asset balance of the market:
WildcatMarketBase.sol#L238-L240
function totalAssets() public view returns (uint256) {
return IERC20(asset).balanceOf(address(this));
}
Since the transfer of assets is only performed after _writeState() is called, totalAssets() will be higher than it should be in _writeState(). This could cause collectFees() to incorrectly update state.isDelinquent to false when the market is still delinquent.
For example:
-
Assume a market has the following state:
liquidityRequired() = 1050.totalAssets() = 1000.state.accruedProtocolFees = 50.
-
The market’s borrower calls
collectFees(). In_writeState():liquidityRequired() = 1050 - 50 = 1000.totalAssets() = 1000since the transfer of assets has not occurred.- As
liquidityRequired() == totalAssets(),isDelinquentis updated tofalse. - However, the market is actually still delinquent as
totalAssets()will be950after the call tocollectFees().
Impact
collectFees() will incorrectly update the market to non-delinquent when liquidityRequired() < totalAssets() + state.accuredProtocolFees.
Should this occur, the delinquency fee will not be added to scaleFactor until the market’s state is updated later on, causing a loss of yield for lenders since the borrower gets to avoid paying the penalty fee for a period of time.
Proof of Concept
The following test demonstrates how collectFees() incorrectly updates state.isDelinquent to false when the market is still delinquent:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.20;
import 'src/WildcatArchController.sol';
import 'src/WildcatMarketControllerFactory.sol';
import 'forge-std/Test.sol';
import 'test/shared/TestConstants.sol';
import 'test/helpers/MockERC20.sol';
contract CollectFeesTest is Test {
// Wildcat contracts
WildcatMarketController controller;
WildcatMarket market;
// Test contracts
MockERC20 asset = new MockERC20();
// Users
address AIKEN;
address DUEET;
function setUp() external {
// Deploy Wildcat contracts
WildcatArchController archController = new WildcatArchController();
MarketParameterConstraints memory constraints = MarketParameterConstraints({
minimumDelinquencyGracePeriod: MinimumDelinquencyGracePeriod,
maximumDelinquencyGracePeriod: MaximumDelinquencyGracePeriod,
minimumReserveRatioBips: MinimumReserveRatioBips,
maximumReserveRatioBips: MaximumReserveRatioBips,
minimumDelinquencyFeeBips: MinimumDelinquencyFeeBips,
maximumDelinquencyFeeBips: MaximumDelinquencyFeeBips,
minimumWithdrawalBatchDuration: MinimumWithdrawalBatchDuration,
maximumWithdrawalBatchDuration: MaximumWithdrawalBatchDuration,
minimumAnnualInterestBips: MinimumAnnualInterestBips,
maximumAnnualInterestBips: MaximumAnnualInterestBips
});
WildcatMarketControllerFactory controllerFactory = new WildcatMarketControllerFactory(
address(archController),
address(0),
constraints
);
// Set protocol fee to 10%
controllerFactory.setProtocolFeeConfiguration(
address(1),
address(0),
0,
1000 // protocolFeeBips
);
// Register controllerFactory in archController
archController.registerControllerFactory(address(controllerFactory));
// Setup Aiken and register him as borrower
AIKEN = makeAddr("AIKEN");
archController.registerBorrower(AIKEN);
asset.mint(AIKEN, 1000e18);
// Setup Dueet and give him some asset token
DUEET = makeAddr("DUEET");
asset.mint(DUEET, 1000e18);
// Deploy controller and market for Aiken
vm.prank(AIKEN);
(address _controller, address _market) = controllerFactory.deployControllerAndMarket(
"Market Token",
"MKT",
address(asset),
type(uint128).max,
MaximumAnnualInterestBips,
MaximumDelinquencyFeeBips,
MaximumWithdrawalBatchDuration,
MaximumReserveRatioBips,
MaximumDelinquencyGracePeriod
);
controller = WildcatMarketController(_controller);
market = WildcatMarket(_market);
}
function test_collectFeesUpdatesDelinquencyWrongly() public {
// Register Dueet as lender
address[] memory arr = new address[](1);
arr[0] = DUEET;
vm.prank(AIKEN);
controller.authorizeLenders(arr);
// Dueet becomes a lender in the market
vm.startPrank(DUEET);
asset.approve(address(market), 1000e18);
market.depositUpTo(1000e18);
vm.stopPrank();
// Some time passes, market becomes delinquent
skip(2 weeks);
market.updateState();
MarketState memory state = market.previousState();
assertTrue(state.isDelinquent);
// Aiken tops up some assets
uint256 amount = market.coverageLiquidity() - market.totalAssets() - market.accruedProtocolFees();
vm.prank(AIKEN);
asset.transfer(address(market), amount);
// Someone calls collectFees()
market.collectFees();
// Market was updated to not delinquent
state = market.previousState();
assertFalse(state.isDelinquent);
// However, it should be delinquent as liquidityRequired > totalAssets()
assertGt(market.coverageLiquidity(), market.totalAssets());
}
}
Recommended Mitigation
In collectFees(), consider calling _writeState() after assets have been transferred to the feeRecipient:
- _writeState(state);
asset.safeTransfer(feeRecipient, withdrawableFees);
+ _writeState(state);
laurenceday (Wildcat) confirmed and commented:
Mitigated here.
Note: For full discussion, see here.
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 order of operations: _writeState() is called before the asset transfer, so totalAssets() includes fees that are about to be sent out.
What the attacker needed
Market is delinquent; borrower tops up assets; collectFees() is called
The fix
The fix is to call _writeState() after the asset transfer in collectFees(); the report states this was mitigated.
How it was found
- Set up a market with a borrower and a lender, and make the market delinquent.
- Borrower tops up assets so that liquidityRequired() equals totalAssets() plus accruedProtocolFees().
- Call collectFees().
- Observe that state.isDelinquent is set to false even though liquidityRequired() > totalAssets() after the fee transfer.
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.
A smart contract market tracks whether a borrower is 'delinquent' (behind on required liquidity). When fees are collected, the contract first updates this status and then sends the fees out. Because the status check happens before the money leaves, the contract thinks there is more money than there really is, so it may wrongly say the borrower is not delinquent.
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.