Minimum borrow amount can be surpassed and borrower can be treated as being overdue earlier than their actual overdue time
The borrow function checks the minimum borrow amount before withdrawing from the asset manager, so if the asset manager can only return a dust amount or nothing, the borrow still succeeds with less than the minimum. This can set the borrower's last repayment timestamp early, marking them as overdue before the actual overdue time, which blocks them from borrowing or vouching and reduces lenders' staking rewards.
Source: https://github.com/sherlock-audit/2024-06-union-finance-update-2-judging/issues/114
Found by
Bigsam, hyh
Summary
It is possible to borrow less than _minBorrow and preliminary be marked as overdue when assetManager have temporary fund access limitations.
Vulnerability Detail
UToken's borrow() can be effectively run with lesser amount than _minBorrow when it is a liquidity shortage in the asset manager's underlying markets and they can return only some dust amount or nothing at all. In these cases borrow() call will still be concluded. Particularly, it is possible to run it with zero amount when assetManager cannot access liquidity.
In that case the borrower, if they borrow for the first time after full repay, will not have their lastRepay field reset on a subsequent material borrow operations as it will already be set on zero amount borrow before. As a result such borrowers can be effectively overdue for the system way before the actual overdue time passes for them.
Impact
_minBorrow threshold can be violated when market conditions restrict assetManager withdrawals. A user can have lastRepay set earlier than time of obtaining the funds, which will mark them overdue before the actual overdue time comes by. This will have a material adverse impact both on such a borrower (for them checkIsOverdue will be true, so they won't be able to borrow or create vouches) and their lenders (for them stakerFrozen and frozenCoinAge will be increased and staking rewards diminished).
Code Snippet
If current market conditions don't allow any material withdrawal then borrow() still can happen and lastRepay be set on any dust or even zero amount being lent out:
function borrow(address to, uint256 amount) external override onlyMember(msg.sender) whenNotPaused nonReentrant {
IAssetManager assetManagerContract = IAssetManager(assetManager);
uint256 actualAmount = decimalScaling(amount, underlyingDecimal);
>> if (actualAmount < _minBorrow) revert AmountLessMinBorrow();
// Calculate the origination fee
uint256 fee = calculatingFee(actualAmount);
if (_borrowBalanceView(msg.sender) + actualAmount + fee > _maxBorrow) revert AmountExceedMaxBorrow();
if (checkIsOverdue(msg.sender)) revert MemberIsOverdue();
if (amount > assetManagerContract.getLoanableAmount(underlying)) revert InsufficientFundsLeft();
if (!accrueInterest()) revert AccrueInterestFailed();
uint256 borrowedAmount = borrowBalanceStoredInternal(msg.sender);
// Initialize the last repayment date to the current block timestamp
>> if (getLastRepay(msg.sender) == 0) {
accountBorrows[msg.sender].lastRepay = getTimestamp();
}
// Withdraw the borrowed amount of tokens from the assetManager and send them to the borrower
>> uint256 remaining = assetManagerContract.withdraw(underlying, to, amount);
>> if (remaining > amount) revert WithdrawFailed();
>> actualAmount -= decimalScaling(remaining, underlyingDecimal);
If market is such that assetManagerContract.withdraw can only withdraw dust or can't withdraw anything, a user can request to borrow an amount bigger than minimal, but borrow() will be executed with some dust or even zero amount effectively borrowed. This isn't fully covered by the getLoanableAmount() check since it measures total funds invested via getSupplyView() calls to the underlying markets.
As _minBorrow is for amount effectively borrowed, and not just for amount requested, it will be in a violation:
/**
>> * @dev Min amount that can be borrowed by a single member
*/
uint256 private _minBorrow;
Also, it will have a side effect of resetting lastRepay even with zero amount borrowed when the borrower had no debt as of time of the call. This will effectively mark a borrower as an overdue when time since they obtained any material debt is in fact much less than overdueTime:
function checkIsOverdue(address account) public view override returns (bool isOverdue) {
if (_getBorrowed(account) != 0) {
>> uint256 lastRepay = getLastRepay(account);
>> uint256 diff = getTimestamp() - lastRepay;
>> isOverdue = overdueTime < diff;
}
}
function getLastRepay(address account) public view override returns (uint256) {
return accountBorrows[account].lastRepay;
}
This can happen as subsequent borrow() calls will not set lastRepay as the logic is based on having empty lastRepay:
if (getLastRepay(msg.sender) == 0) {
accountBorrows[msg.sender].lastRepay = getTimestamp();
}
Tool used
Manual Review
Recommendation
Consider controlling the effective amount being borrowed, e.g.:
function borrow(address to, uint256 amount) external override onlyMember(msg.sender) whenNotPaused nonReentrant {
IAssetManager assetManagerContract = IAssetManager(assetManager);
uint256 actualAmount = decimalScaling(amount, underlyingDecimal);
- if (actualAmount < _minBorrow) revert AmountLessMinBorrow();
// Calculate the origination fee
uint256 fee = calculatingFee(actualAmount);
if (_borrowBalanceView(msg.sender) + actualAmount + fee > _maxBorrow) revert AmountExceedMaxBorrow();
if (checkIsOverdue(msg.sender)) revert MemberIsOverdue();
if (amount > assetManagerContract.getLoanableAmount(underlying)) revert InsufficientFundsLeft();
if (!accrueInterest()) revert AccrueInterestFailed();
uint256 borrowedAmount = borrowBalanceStoredInternal(msg.sender);
// Initialize the last repayment date to the current block timestamp
if (getLastRepay(msg.sender) == 0) {
accountBorrows[msg.sender].lastRepay = getTimestamp();
}
// Withdraw the borrowed amount of tokens from the assetManager and send them to the borrower
uint256 remaining = assetManagerContract.withdraw(underlying, to, amount);
if (remaining > amount) revert WithdrawFailed();
actualAmount -= decimalScaling(remaining, underlyingDecimal);
+ if (actualAmount < _minBorrow) revert AmountLessMinBorrow();
Discussion
maxweng
I think this issue only happens when the underlying market protocol we integrate doesn't implement the withdraw() function correctly, which it succeeded and returned true but the withdrawal amount was less than what the caller requested.
But to tight things up on our end, I think we can add another check on the actual withdrawal amount.
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/unioncredit/union-v2-contracts/pull/175
dmitriia
The protocol team fixed this issue in the following PRs/commits: unioncredit/union-v2-contracts#175
Fix looks ok
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 minimum borrow check is performed on the requested amount before the actual withdrawal, and the last repayment timestamp is set even when the effective borrowed amount is below the minimum or zero.
Impact
A borrower can be marked overdue earlier than their actual overdue time, preventing them from borrowing or creating vouches, and their lenders may have increased stakerFrozen and frozenCoinAge, reducing staking rewards.
What the attacker needed
AssetManager has temporary fund access limitations; borrower has no existing debt (lastRepay is zero)
The fix
The protocol team fixed this issue by moving the minimum borrow check to after the withdrawal, so it applies to the effective amount borrowed.
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 lets users borrow tokens, but it has a rule that you must borrow at least a certain minimum amount. The contract checks this minimum before it actually gets the tokens from a separate pool, so if that pool can only give a tiny amount or nothing, the borrow still goes through with less than the minimum. This also sets a timestamp that is used to decide if you are late on repayment, so you can be marked as overdue too early.
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.