Stakers will lose their rewards as updateLocked() updates only the first active vouches until there is a prepayment
When a borrower pays only interest without prepayment, the updateLocked() function updates only the first active vouch's lastUpdated timestamp, causing all other stakers to lose rewards because their vouches appear overdue. An attacker could exploit this by making a small interest payment to harm other stakers' rewards.
Source: https://github.com/sherlock-audit/2022-10-union-finance-judging/issues/135
Found by
hyh
Summary
Whenever Bob the borrower has several lenders and pays only interest without prepayments, only the first of his stakers will have good health of his loan accounted for by keeping vouch.lastUpdated current, all the rest will lose the portion of rewards associated with Bob's loan as it will be counted as overdue, despite the fact that Bob has paid the full total interest without a single delay.
Vulnerability Detail
When a borrower pays interest in full, UserManager's updateLocked() is called by UToken's _repayBorrowFresh() to mark the active vouches in the Bob's vouchers array with vouch.lastUpdated = uint64(block.number). However, updateLocked() logic performs iterations over the array only up to the notional prepayment accounting point.
I.e. if Bob paid only full interest, not performing any notional prepayments, updateLocked() updates only the very first active vouch, as updateLocked() was called with amount = repayAmount - interest = 0.
Moreover, if Bob does some prepayment, the cycle continues only to mark enough vouches to account for that prepayment, i.e. it stops once the prepayment is done.
The core issue is that updateLocked() logic is made for this prepayments accounting, while it is also used for marking non-overdue vouches.
For that matter the if (remaining <= 0) break is in fact incorrect as the condition for cycle ending as for marking of the timely payment it doesn't matter how much Bob has prepaid, and all his active vouches need to be updated as Bob had payed interest for all of them, which was tracked in _repayBorrowFresh() by calculatingInterest(Bob), which is total current interest for all Bob's loans.
Impact
All Bob's lenders except the first one (or maybe first ones if there was some prepayment) will lose the corresponding part of their rewards despite the fact that Bob keeps his loan in the perfect health, not missing a single payment.
As UNION rewards is the main venue of staker profit from the system, this is a material loss for all of them. There are no any additional conditions, i.e. the loss of rewards happens in a going concern state, while even full interest payments without any prepayments is one of the base use cases for any borrower.
The total impact is massive reward loss without low probability preconditions, so setting the severity to be high.
Code Snippet
Bob pays the interest by calling repayBorrow() -> _repayBorrowFresh():
function repayBorrow(address borrower, uint256 repayAmount) external override whenNotPaused nonReentrant {
_repayBorrowFresh(msg.sender, borrower, repayAmount);
}
_repayBorrowFresh() calculates the total interest = calculatingInterest(borrower) due to Bob and calls IUserManager(userManager).updateLocked(borrower, uint96(repayAmount - interest), false) if he has paid this interest in full:
function _repayBorrowFresh(
address payer,
address borrower,
uint256 amount
) internal {
if (!accrueInterest()) revert AccrueInterestFailed();
uint256 interest = calculatingInterest(borrower);
uint256 borrowedAmount = borrowBalanceStoredInternal(borrower);
uint256 repayAmount = amount > borrowedAmount ? borrowedAmount : amount;
if (repayAmount == 0) revert AmountZero();
uint256 toReserveAmount;
uint256 toRedeemableAmount;
if (repayAmount >= interest) {
// If the repayment amount is greater than the interest (min payment)
bool isOverdue = checkIsOverdue(borrower);
// Interest is split between the reserves and the uToken minters based on
// the reserveFactorMantissa When set to WAD all the interest is paid to teh reserves.
// any interest that isn't sent to the reserves is added to the redeemable amount
// and can be redeemed by uToken minters.
toReserveAmount = (interest * reserveFactorMantissa) / WAD;
toRedeemableAmount = interest - toReserveAmount;
// Update the total borrows to reduce by the amount of principal that has
// been paid off
totalBorrows -= (repayAmount - interest);
// Update the account borrows to reflect the repayment
accountBorrows[borrower].principal = borrowedAmount - repayAmount;
accountBorrows[borrower].interest = 0;
if (getBorrowed(borrower) == 0) {
// If the principal is now 0 we can reset the last repaid block to 0.
// which indicates that the borrower has no outstanding loans.
accountBorrows[borrower].lastRepay = 0;
} else {
// Save the current block number as last repaid
accountBorrows[borrower].lastRepay = getBlockNumber();
}
// Call update locked on the userManager to lock this borrowers stakers. This function
// will revert if the account does not have enough vouchers to cover the repay amount. ie
// the borrower is trying to repay more than is locked (owed)
IUserManager(userManager).updateLocked(borrower, uint96(repayAmount - interest), false);
if (isOverdue) {
// For borrowers that are paying back overdue balances we need to update their
// frozen balance and the global total frozen balance on the UserManager
IUserManager(userManager).updateFrozenInfo(borrower, 0);
}
}
If Bob has many vouches and pays full interest, but nothing else, before minimum payment period runs out every time, only his first vouch will be marked as payed by updating vouch.lastUpdated = uint64(block.number):
} else {
// Look up how much this vouch has locked. If it is 0 then
// continue to the next voucher. Then calculate the amount to
// unlock which is the min of the vouches lock and what is
// remaining to unlock
uint96 locked = vouch.locked;
if (locked == 0) continue;
innerAmount = _min(locked, remaining);
// Update the stored locked values and last updated block
stakers[vouch.staker].locked -= innerAmount;
vouch.locked -= innerAmount;
vouch.lastUpdated = uint64(block.number);
}
remaining -= innerAmount;
// If there is no remaining amount to lock/unlock
// we can stop looping through vouchers
if (remaining <= 0) break;
I.e. the if (remaining <= 0) break condition is preventing all other Bob's vouches to be updated as healthy, damaging the corresponding lenders.
Reward multiplier is based on the frozen coin age, which is calculated with the help of vouch.lastUpdated of all staker's vouches:
/**
* @dev Get frozen coin age
* @param staker Address of staker
* @param pastBlocks Number of blocks past to calculate coin age from
* coin age = min(block.number - lastUpdated, pastBlocks) * amount
*/
function getFrozenInfo(address staker, uint256 pastBlocks)
public
view
returns (uint256 memberTotalFrozen, uint256 memberFrozenCoinAge)
{
uint256 overdueBlocks = uToken.overdueBlocks();
uint256 voucheesLength = vouchees[staker].length;
// Loop through all of the stakers vouchees sum their total
// locked balance and sum their total memberFrozenCoinAge
for (uint256 i = 0; i < voucheesLength; i++) {
// Get the vouchee record and look up the borrowers voucher record
// to get the locked amount and lastUpdate block number
Vouchee memory vouchee = vouchees[staker][i];
Vouch memory vouch = vouchers[vouchee.borrower][vouchee.voucherIndex];
uint256 lastUpdated = vouch.lastUpdated;
uint256 diff = block.number - lastUpdated;
if (overdueBlocks < diff) {
uint96 locked = vouch.locked;
memberTotalFrozen += locked;
if (pastBlocks >= diff) {
memberFrozenCoinAge += (locked * diff);
} else {
memberFrozenCoinAge += (locked * pastBlocks);
}
}
}
}
Tool used
Manual Review
Recommendation
One straightforward way to circumvent this is to run the full cycle across all Bob's vouches, setting vouch.lastUpdated = uint64(block.number) for all of them and not exiting when if (remaining <= 0) break. Current partial redemption logic remains as it is, only doing nothing when remaining is exhausted, i.e. to be placed into a if (remaining > 0) block.
Another option is to base the reward multiplier calculation not on the vouch.lastUpdated, but on the health of each borrower of the staker, i.e. to base total frozen on borrower's lastRepay instead of voucher's lastUpdate.
Borrower's lastRepay is the only indicator for the checkIsOverdue() and debtWriteOff():
checkIsOverdue() and debtWriteOff() use getLastRepay():
/**
* @dev Check if the member's loan is overdue
* @param account Member address
* @return isOverdue
*/
function checkIsOverdue(address account) public view override returns (bool isOverdue) {
if (getBorrowed(account) != 0) {
uint256 lastRepay = getLastRepay(account);
uint256 diff = getBlockNumber() - lastRepay;
isOverdue = overdueBlocks < diff;
}
}
function debtWriteOff(
address staker,
address borrower,
uint96 amount
) external {
if (amount == 0) revert AmountZero();
uint256 overdueBlocks = uToken.overdueBlocks();
uint256 lastRepay = uToken.getLastRepay(borrower);
// This function is only callable by the public if the loan is overdue by
// overdue blocks + maxOverdueBlocks. This stops the system being left with
// debt that is overdue indefinitely and no ability to do anything about it.
if (block.number <= lastRepay + overdueBlocks + maxOverdueBlocks) {
if (staker != msg.sender) revert AuthFailed();
}
getLastRepay() gets accountBorrows[].lastRepay:
function getLastRepay(address account) public view override returns (uint256) {
return accountBorrows[account].lastRepay;
}
To unify the logic and simplify vouch workflow, lastRepay of staker's borrowers can be also used for the frozen age calculations, for example:
/**
* @dev Get frozen coin age
* @param staker Address of staker
* @param pastBlocks Number of blocks past to calculate coin age from
* coin age = min(block.number - lastUpdated, pastBlocks) * amount
*/
function getFrozenInfo(address staker, uint256 pastBlocks)
public
view
returns (uint256 memberTotalFrozen, uint256 memberFrozenCoinAge)
{
uint256 overdueBlocks = uToken.overdueBlocks();
uint256 voucheesLength = vouchees[staker].length;
// Loop through all of the stakers vouchees sum their total
// locked balance and sum their total memberFrozenCoinAge
for (uint256 i = 0; i < voucheesLength; i++) {
// Get the vouchee record and look up the borrowers voucher record
// to get the locked amount and lastUpdate block number
Vouchee memory vouchee = vouchees[staker][i];
Vouch memory vouch = vouchers[vouchee.borrower][vouchee.voucherIndex];
- uint256 lastUpdated = vouch.lastUpdated;
+ uint256 lastUpdated = uToken.getLastRepay(vouchee);
+ if (vouch.locked == 0 || lastUpdated == 0) continue; // should be zero simultaneously, but we need to skip either way
uint256 diff = block.number - lastUpdated;
if (overdueBlocks < diff) {
uint96 locked = vouch.locked;
memberTotalFrozen += locked;
if (pastBlocks >= diff) {
memberFrozenCoinAge += (locked * diff);
} else {
memberFrozenCoinAge += (locked * pastBlocks);
}
}
}
}
Discussion
kingjacob
Dupe of #99 ?
dmitriia
Dupe of #99 ?
Looks like #99 and #74 are dupes, this one is actually a bit more systematic, with similar kind of outcome without the need of any direct attack
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 updateLocked() function breaks out of its loop when the prepayment amount is exhausted, so it fails to update lastUpdated for all active vouches when only interest is paid.
Impact
All stakers except the first (or first few if prepayment occurred) lose their rewards despite the borrower making full interest payments on time, causing material loss with no special conditions.
What the attacker needed
Borrower has multiple stakers; borrower pays only interest, no prepayment
The fix
The updateLocked() function should update lastUpdated for all active vouches regardless of prepayment amount, or reward calculation should use the borrower's lastRepay instead of vouch.lastUpdated.
How it was found
- Borrower has multiple stakers vouching for their loan.
- Borrower pays only the full interest amount, with no prepayment.
- updateLocked() is called with amount = repayAmount - interest = 0.
- The loop updates only the first vouch's lastUpdated and then breaks.
- Other stakers' vouches remain with old lastUpdated, making them appear overdue.
- Those stakers lose rewards for the borrower's loan.
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 this system, stakers vouch for borrowers and earn rewards if the borrower repays on time. The code that updates the 'last paid' time for each vouch stops early when the borrower only pays interest, so most stakers' vouches look overdue and they lose rewards. An attacker could make a tiny interest payment to cause this loss for other stakers.
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.