repay(), liquidate() and liquidateWLp() receive shares as argument, which may revert if from approval to tx settled blocks have passed
The repay, liquidate and liquidateWLp functions take a share amount as their argument, but the debt owed grows with time because interest accrues by timestamp. If a user approves exactly the amount needed and the transaction settles a few blocks later, the transfer can fail because the debt has grown beyond the approval.
Originally disclosed on Code4rena
Submitted by 0x73696d616f
https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/InitCore.sol#L151
https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/InitCore.sol#L282
https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/InitCore.sol#L317
repay(), liquidate() and liquidateWLp() transactions revert if users approve the exact repay amount they need in the frontend and only after some blocks have passed is the transaction settled. This happens because the interest accrual is by timestamp, so the debt would have increased since the approval, when the transaction settles.
Proof of Concept
A test when repaying debt was carried out in TestInitCore.sol. The timestamp increased just 1 second, but it was enough to make the transaction revert. It may be possible to request a bigger alowance than expected, but this has other implications.
function test_POC_TransferFromFails_DueToDebtAccrual() public {
uint256 _wbtcAmount = 3e8;
uint256 _borrowAmount = 1e8;
address _user = makeAddr("user");
deal(WBTC, _user, _wbtcAmount);
uint256 _posId = _createPos(_user, _user, 2);
uint256 shares_ = _mintPool(_user, address(lendingPools[WBTC]), _wbtcAmount, "");
vm.startPrank(_user);
lendingPools[WBTC].transfer(address(positionManager), shares_);
initCore.collateralize(_posId, address(lendingPools[WBTC]));
vm.stopPrank();
uint256 _debtShares = _borrow(_user, _posId, address(lendingPools[WBTC]), _borrowAmount, "");
uint256 _userDebtBalance = lendingPools[WBTC].debtShareToAmtCurrent(_debtShares);
vm.prank(_user);
IERC20(WBTC).approve(address(initCore), _userDebtBalance);
skip(1);
vm.prank(_user);
vm.expectRevert("ERC20: transfer amount exceeds balance");
initCore.repay(address(lendingPools[WBTC]), _debtShares, _posId);
}
Tools Used
Vscode, Foundry
Recommended Mitigation Steps
Receive the amount in InitCore as argument instead of the shares on the repay(), liquidate() and liquidateWLp() functions.
fez-init (INIT) acknowledged and commented:
The issue should be mitigated with the introduction of hooks, where such additional logic of amount to share conversion can be implemented.
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 functions accept a share amount rather than a token amount, so the token amount needed is recalculated at settlement time after interest has accrued, exceeding the earlier approval.
Impact
Repayments and liquidations may revert when the approval was for the exact amount and any time passed before settlement, so users must approve more than needed.
What the attacker needed
A position with debt; approval of the exact debt amount; time passes before settlement
The fix
The project acknowledged the issue and said it should be mitigated by introducing hooks where the amount-to-share conversion logic can be implemented.
How it was found
- Create a position and supply collateral.
- Borrow tokens so the position has debt.
- Read the current debt balance and approve exactly that amount to the core contract.
- Advance time by one second.
- Call repay with the debt shares and observe the transaction revert.
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 lending contract lets people borrow tokens and pay them back later with interest. Interest keeps growing every second, so the amount owed when you click approve is slightly smaller than the amount owed when your repayment finally goes through. If you approved only the exact amount, the contract tries to take a little more than you allowed and the repayment fails.
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.