Calculation for lender withdrawals in _applyWithdrawalBatchPayment() should not round up
The withdrawal batch payment calculation rounds up the asset amount, which can make totalDebts() exceed the market's asset balance and incorrectly mark the market as delinquent. This can cause the borrower to pay a higher interest rate or make a closed market delinquent.
Submitted by MiloTruck
After a lender calls queueWithdrawal(), the amount of assets allocated to a withdrawal batch is calculated in _applyWithdrawalBatchPayment() as shown:
WildcatMarketBase.sol#L510-L518
uint104 scaledAmountBurned = uint104(MathUtils.min(scaledAvailableLiquidity, scaledAmountOwed));
uint128 normalizedAmountPaid = state.normalizeAmount(scaledAmountBurned).toUint128();
batch.scaledAmountBurned += scaledAmountBurned;
batch.normalizedAmountPaid += normalizedAmountPaid;
state.scaledPendingWithdrawals -= scaledAmountBurned;
// Update normalizedUnclaimedWithdrawals so the tokens are only accessible for withdrawals.
state.normalizedUnclaimedWithdrawals += normalizedAmountPaid;
The calculation relies on normalizeAmount() to convert the amount of market tokens in a batch into assets.
However, as normalizeAmount() rounds up, it might cause the amount of assets allocated to a batch to be 1 higher than the correct amount. For example, if availableLiquidity is 77e6 USDC, normalizedAmount could become 77e6 + 1 after calculation due to rounding.
This is problematic, as it causes totalDebts() to increase, causing the market to incorrectly become delinquent after _applyWithdrawalBatchPayment() is called although the borrower has transferred sufficient assets.
More specifically, if a market’s asset balance is equal to totalDebts(), it should never be delinquent, regardless of what function is called, as the market has sufficient assets to cover the amount owed. However, due to the bug shown above, this could occur after a function such as queueWithdrawal() is called.
Impact
A market could incorrectly become delinquent after _applyWithdrawalBatchPayment() is called.
This could cause a borrower to wrongly pay a higher interest rate; for example:
- Borrower calls
totalDebts()to get the amount of assets owed. - Borrower transfers the assets to the market and calls
updateState(). - Borrower assumes that the market is no longer delinquent.
- However, due to the bug above, the market becomes delinquent after a lender calls
queueWithdrawal(). - This activates the delinquency fee, causing the borrower to pay a higher interest rate.
Additionally, this bug also makes it possible for a market to become delinquent after it is closed through closeMarket(), which should not be possible.
Proof of Concept
The code below contains two tests:
test_queueWithdrawalRoundingAffectsDelinquency()demonstrates how rounding up in_applyWithdrawalBatchPayment()could make the market delinquent even when it should not be.test_marketCanBecomeDelinquentAfterClosing()shows how a market can become delinquent even aftercloseMarket()is called.
// 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 WithdrawalRoundingTest 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: 0,
maximumReserveRatioBips: MaximumReserveRatioBips,
minimumDelinquencyFeeBips: 0,
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 users
AIKEN = makeAddr("AIKEN");
DUEET = makeAddr("DUEET");
asset.mint(AIKEN, 1000e18);
asset.mint(DUEET, 1000e18);
// Deploy controller and market for Aiken
archController.registerBorrower(AIKEN);
vm.prank(AIKEN);
(address _controller, address _market) = controllerFactory.deployControllerAndMarket(
"Market Token",
"MKT",
address(asset),
type(uint128).max,
50, // annual interest rate = 5%
300, // delinquency fee = 30%
3 weeks,
300, // reserve ratio = 30%
MaximumDelinquencyGracePeriod
);
controller = WildcatMarketController(_controller);
market = WildcatMarket(_market);
// Register Dueet as lender
address[] memory arr = new address[](1);
arr[0] = DUEET;
vm.prank(AIKEN);
controller.authorizeLenders(arr);
}
function test_queueWithdrawalRoundingAffectsDelinquency() public {
// Dueet deposits 1000e18 tokens
vm.startPrank(DUEET);
asset.approve(address(market), 1000e18);
market.depositUpTo(1000e18);
vm.stopPrank();
// Aiken borrows all assets
uint256 amount = market.borrowableAssets();
vm.prank(AIKEN);
market.borrow(amount);
// 1 day and 1 second passes
skip(1 days + 1);
// Aiken transfers assets so that market won't be delinquent even after full withdrawal
amount = market.currentState().totalDebts() - market.totalAssets();
vm.prank(AIKEN);
asset.transfer(address(market), amount);
// Collect fees
market.collectFees();
// Save snapshot before withdrawals
uint256 snapshot = vm.snapshot();
// Market won't be delinquent if Dueet withdraws all tokens at once
amount = market.balanceOf(DUEET);
vm.prank(DUEET);
market.queueWithdrawal(amount);
assertFalse(market.currentState().isDelinquent);
// Revert state to before withdrawals
vm.revertTo(snapshot);
// Dueet withdraws 710992167266111033190 tokens
vm.prank(DUEET);
market.queueWithdrawal(710992167266111033190);
// Dueet withdraws the rest of his tokens
amount = market.balanceOf(DUEET);
vm.prank(DUEET);
market.queueWithdrawal(amount);
// Market is now delinquent although same amount of tokens was withdrawn
assertTrue(market.currentState().isDelinquent);
}
function test_marketCanBecomeDelinquentAfterClosing() public {
// Dueet deposits 1000e18 tokens
vm.startPrank(DUEET);
asset.approve(address(market), 1000e18);
market.depositUpTo(1000e18);
vm.stopPrank();
// Aiken borrows all assets
uint256 amount = market.borrowableAssets();
vm.prank(AIKEN);
market.borrow(amount);
// 1 day and 1 second passes
skip(1 days + 1);
// Aiken closes the market
amount = market.currentState().totalDebts();
vm.prank(AIKEN);
asset.approve(address(market), amount);
vm.prank(address(controller));
market.closeMarket();
// Collect fees
market.collectFees();
// Dueet withdraws 710992167266111033190 tokens
vm.prank(DUEET);
market.queueWithdrawal(710992167266111033190);
// Dueet withdraws the rest of his tokens
amount = market.balanceOf(DUEET);
vm.prank(DUEET);
market.queueWithdrawal(amount);
// Market is now delinquent, even though it has been closed
assertTrue(market.currentState().isDelinquent);
}
}
Recommended Mitigation
In _applyWithdrawalBatchPayment(), consider rounding down when calculating the amount of assets allocated to a batch:
WildcatMarketBase.sol#L510-L511
uint104 scaledAmountBurned = uint104(MathUtils.min(scaledAvailableLiquidity, scaledAmountOwed));
- uint128 normalizedAmountPaid = state.normalizeAmount(scaledAmountBurned).toUint128();
+ uint128 normalizedAmountPaid = MathUtils.mulDiv(scaledAmountBurned, state.scaleFactor, MathUtils.RAY).toUint128();
Assessed type
Math
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 market could incorrectly become delinquent, causing the borrower to pay a higher interest rate, and a closed market could also become delinquent.
What the attacker needed
A market with a lender and borrower; borrower has transferred sufficient assets; lender queues withdrawals in multiple parts
The fix
The calculation should round down when computing the asset amount allocated to a batch.
How it was found
- A lender deposits assets into the market.
- The borrower borrows all available assets.
- Time passes, and the borrower transfers assets to the market to cover total debts.
- The lender queues a withdrawal for part of their tokens, then queues another withdrawal for the rest.
- The market becomes delinquent even though the same total amount was withdrawn as in a single withdrawal that did not cause delinquency.
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 a lender withdraws, the system calculates how much of the borrower's debt is paid off. A rounding mistake can make it look like slightly more is owed than actually is, so the market wrongly thinks the borrower is late. That can trigger extra fees even though the borrower paid enough.