Inconsistency across multiple repaying functions causing lender to pay extra fees
The report shows that some functions update the market state before pulling funds from the borrower, while repay functions pull funds first. This inconsistency causes borrowers to pay extra fees when using the state-first functions.
deadrxsezzz Follow with Bigsam, 0xNirix and 3 others
Originally disclosed on Code4rena
Submitted by deadrxsezzz, also found by Bigsam, 0xNirix, Udsen, Takarez, and Infect3d
Within functions such as repay and repayAndProcessUnpaidWithdrawalBatches, funds are first pulled from the user in order to use them towards the currently expired, but not yet unpaid batch, and then the updated state is fetched.
function repay(uint256 amount) external nonReentrant sphereXGuardExternal {
if (amount == 0) revert_NullRepayAmount();
asset.safeTransferFrom(msg.sender, address(this), amount);
emit_DebtRepaid(msg.sender, amount);
MarketState memory state = _getUpdatedState();
if (state.isClosed) revert_RepayToClosedMarket();
// Execute repay hook if enabled
hooks.onRepay(amount, state, _runtimeConstant(0x24));
_writeState(state);
}
However, this is not true for functions such as closeMarket, deposit, repayOutstandingDebt and repayDelinquentDebt, where the state is first fetched and only then funds are pulled, forcing borrower into higher fees.
function closeMarket() external onlyBorrower nonReentrant sphereXGuardExternal {
MarketState memory state = _getUpdatedState(); // fetches updated state
if (state.isClosed) revert_MarketAlreadyClosed();
uint256 currentlyHeld = totalAssets();
uint256 totalDebts = state.totalDebts();
if (currentlyHeld < totalDebts) {
// Transfer remaining debts from borrower
uint256 remainingDebt = totalDebts - currentlyHeld;
_repay(state, remainingDebt, 0x04); // pulls user funds
currentlyHeld += remainingDebt;
This inconsistency will cause borrowers to pay extra fees which they otherwise wouldn’t.
PoC:
function test_inconsistencyIssue() external {
parameters.annualInterestBips = 3650;
_deposit(alice, 1e18);
uint256 borrowAmount = market.borrowableAssets();
vm.prank(borrower);
market.borrow(borrowAmount);
vm.prank(alice);
market.queueFullWithdrawal();
fastForward(52 weeks);
asset.mint(borrower, 10e18);
vm.startPrank(borrower);
asset.approve(address(market), 10e18);
uint256 initBalance = asset.balanceOf(borrower);
asset.transfer(address(market), 10e18);
market.closeMarket();
uint256 finalBalance = asset.balanceOf(borrower);
uint256 paid = initBalance - finalBalance;
console.log(paid);
}
function test_inconsistencyIssue2() external {
parameters.annualInterestBips = 3650;
_deposit(alice, 1e18);
uint256 borrowAmount = market.borrowableAssets();
vm.prank(borrower);
market.borrow(borrowAmount);
vm.prank(alice);
market.queueFullWithdrawal();
fastForward(52 weeks);
asset.mint(borrower, 10e18);
vm.startPrank(borrower);
asset.approve(address(market), 10e18);
uint256 initBalance = asset.balanceOf(borrower);
market.closeMarket();
uint256 finalBalance = asset.balanceOf(borrower);
uint256 paid = initBalance - finalBalance;
console.log(paid);
}
and the logs:
Ran 2 tests for test/market/WildcatMarket.t.sol:WildcatMarketTest
[PASS] test_inconsistencyIssue() (gas: 656338)
Logs:
800455200405885337
[PASS] test_inconsistencyIssue2() (gas: 680537)
Logs:
967625143234433533
Recommended Mitigation Steps
Always pull the funds first and refund later if needed.
d1ll0n (Wildcat) acknowledged and commented:
The listed functions which incur higher fees all require the current state of the market to accurately calculate relevant values to the transfer. Because of that, the transfer can’t happen until after the state is updated, and it would be expensive (and too large to fit in the contract size) to redo the withdrawal payments post-transfer.
For the repay functions this is more of an issue than the others, as that represents the borrower specifically taking action to repay their debts, whereas the other functions are actions by other parties (and thus we aren’t very concerned if they fail to cure the borrower’s delinquency for them). We may end up just removing these secondary repay functions.
laurenceday (Wildcat) commented:
Resolved by wildcat-finance/v2-protocol@e7afdc9.
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 differs: some functions fetch the updated state before transferring funds, while repay functions transfer funds first, so the state used for fee calculation includes additional accrued interest.
What the attacker needed
Borrower has an active loan; borrower calls a state-first function
The fix
The issue was resolved by a commit according to the project team.
How it was found
- Set up a market with a borrower and a lender, and borrow the maximum amount.
- Queue a full withdrawal and advance time by 52 weeks.
- Give the borrower extra funds and approve the market.
- Call closeMarket after transferring funds directly to the market, or call closeMarket without a prior transfer.
- Compare the borrower's balance before and after to see the amount paid.
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 market, interest and fees depend on the current state of the market. If the state is updated before the borrower's payment is taken, the payment is applied to a later, higher-debt state, so the borrower pays more. The report shows two ways to repay and compares the fees paid.
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.