Staker can perform rewards withdrawal every overdueBlocks less 1 and have zero frozenCoinAge
The reward calculation uses a time difference that depends on the staker's last reward withdrawal, allowing a staker to avoid the frozen coin age penalty by withdrawing rewards frequently. An attacker could inflate UNION token emissions and dilute honest holders' rewards.
Source: https://github.com/sherlock-audit/2023-02-union-judging/issues/25
Found by
hyh
Summary
Time difference accounted for in _getEffectiveAmounts() that is used for rewards computation is based on the lesser amount of time passed since last reward withdrawal and borrower's last repay. In order to maximize the rewards a staker can withdraw rewards every uToken.overdueBlocks() - 1, so that difference is always be less than overdue threshold and none of staker's loans be deemed overdue no matter how long ago their last payments were.
Vulnerability Detail
Effective amounts are counted to determine the reward multiplier and amount, and the frozen part of these amounts corresponds to the bad debt. However, the time period check for the inclusion of a borrower to the frozenCoinAge is performed as overdueBlocks < repayDiff, where repayDiff = block.number - _max(lastRepay, coinAge.lastWithdrawRewards), and lastWithdrawRewards = getLastWithdrawRewards[stakerAddress] is staker controlled.
So this repayDiff can be always kept below overdueBlocks via calling rewards gathering frequent enough, once per overdueBlocks - 1 or sooner.
Also notice that _getCoinAge() returned coinAge.lockedCoinAge is updated on overdue debt repayment only and is zero when no such repayments were made.
As an example, a staker can lend to themselves, pay no interest, call withdrawRewards() once per overdueBlocks - 1, gathering full rewards without any penalty. Or just observing bad debt start to do it in order to remove its impact on rewards.
In any case this provides a way to remove frozenCoinAge based bad debt penalty altogether.
Impact
Excess UNION rewards emissions dilute holdings of the honest members who do not time their withdrawRewards() calls to the points of reward multiplier maximization.
This is monetary loss for all UNION holders (as total emission is increased), and particularly for the stakers who actually use the system (as specifically new emission is increased), i.e. in result the attacker steals from all holders/stakers by inflating UNION emission.
Setting severity to be high as there are no specific preconditions for the attack, it can be carried out by any UNION staker.
Code Snippet
_getEffectiveAmounts() accumulates frozenCoinAge only when uToken.overdueBlocks() = overdueBlocks < repayDiff = block.number - _max(lastRepay, coinAge.lastWithdrawRewards), treating it as zero for that voucher otherwise:
function _getEffectiveAmounts(address stakerAddress, uint256 pastBlocks)
...
{
uint256 memberTotalFrozen = 0;
CoinAge memory coinAge = _getCoinAge(stakerAddress);
uint256 overdueBlocks = uToken.overdueBlocks();
uint256 voucheesLength = vouchees[stakerAddress].length;
// Loop through all of the stakers vouchees sum their total
// locked balance and sum their total currDefaultFrozenCoinAge
for (uint256 i = 0; i < voucheesLength; i++) {
// Get the vouchee record and look up the borrowers voucher record
// to get the locked amount and lastUpdated block number
Vouchee memory vouchee = vouchees[stakerAddress][i];
Vouch memory vouch = vouchers[vouchee.borrower][vouchee.voucherIndex];
uint256 lastRepay = uToken.getLastRepay(vouchee.borrower);
uint256 repayDiff = block.number - _max(lastRepay, coinAge.lastWithdrawRewards);
uint256 locked = uint256(vouch.locked);
if (overdueBlocks < repayDiff && (coinAge.lastWithdrawRewards != 0 || lastRepay != 0)) {
memberTotalFrozen += locked;
if (pastBlocks >= repayDiff) {
coinAge.frozenCoinAge += (locked * repayDiff);
} else {
coinAge.frozenCoinAge += (locked * pastBlocks);
}
}
uint256 lastUpdateBlock = _max(coinAge.lastWithdrawRewards, uint256(vouch.lastUpdated));
coinAge.lockedCoinAge += (block.number - lastUpdateBlock) * locked;
}
return (
// staker's total effective staked = (staked coinage - frozen coinage) / (# of blocks since last reward claiming)
coinAge.diff == 0 ? 0 : (coinAge.stakedCoinAge - coinAge.frozenCoinAge) / coinAge.diff,
// effective locked amount = (locked coinage - frozen coinage) / (# of blocks since last reward claiming)
coinAge.diff == 0 ? 0 : (coinAge.lockedCoinAge - coinAge.frozenCoinAge) / coinAge.diff,
memberTotalFrozen
);
}
_getCoinAge() returns getLastWithdrawRewards[stakerAddress] as lastWithdrawRewards, i.e. that's staker controlled, and block.number - _max(lastWithdrawRewards, uint256(staker.lastUpdated)) as diff, frozenCoinAge[stakerAddress] as frozenCoinAge, which serves as a base for frozen accumulator:
function _getCoinAge(address stakerAddress) private view returns (CoinAge memory) {
Staker memory staker = stakers[stakerAddress];
uint256 lastWithdrawRewards = getLastWithdrawRewards[stakerAddress];
uint256 diff = block.number - _max(lastWithdrawRewards, uint256(staker.lastUpdated));
CoinAge memory coinAge = CoinAge({
lastWithdrawRewards: lastWithdrawRewards,
diff: diff,
stakedCoinAge: staker.stakedCoinAge + diff * uint256(staker.stakedAmount),
lockedCoinAge: staker.lockedCoinAge,
frozenCoinAge: frozenCoinAge[stakerAddress]
});
return coinAge;
}
While across the protocol frozenCoinAge[stakerAddress] is accumulated only on onRepayBorrow():
/**
* @dev Update the frozen info by the utoken repay
* @param borrower Borrower address
*/
function onRepayBorrow(address borrower) external {
if (address(uToken) != msg.sender) revert AuthFailed();
uint256 overdueBlocks = uToken.overdueBlocks();
uint256 vouchersLength = vouchers[borrower].length;
uint256 lastRepay = 0;
uint256 diff = 0;
for (uint256 i = 0; i < vouchersLength; i++) {
Vouch memory vouch = vouchers[borrower][i];
lastRepay = uToken.getLastRepay(borrower);
diff = block.number - lastRepay;
if (overdueBlocks < diff) {
frozenCoinAge[vouch.staker] += uint256(vouch.locked) * diff;
}
}
}
onRepayBorrow() is called only by _repayBorrowFresh() and only when the debt is overdue:
function _repayBorrowFresh(address payer, address borrower, uint256 amount, uint256 interest) internal {
...
if (repayAmount >= interest) {
...
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).onRepayBorrow(borrower);
}
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;
}
}
I.e. if a staker's borrowers didn't repaid at all, or repaid without overdue for some period and then became bad debt, making no repayments after some date, then frozenCoinAge[stakerAddress] is zero as no overdue repayments were ever made.
Furthermore, staker can call withdrawRewards() to update getLastWithdrawRewards[stakerAddress] and keep diff = block.number - _max(lastWithdrawRewards, uint256(staker.lastUpdated)) below current uToken.overdueBlocks(), keeping their frozen counter zero.
Tool used
Manual Review
Recommendation
As an example approach, consider introducing lifetime effective staked, locked and frozen accumulators, i.e. do not resetting them on each rewards claim, and using the difference vs the last claim point instead.
This way total staked, locked, frozen counters will be kept since staker inception, but only the last yet unrewarded period will be used to calculate each next portion of the rewards.
Manipulation surface described will not be viable as withdrawRewards() will only determine the reward period and will not be resetting the whole reward accounting.
Discussion
kingjacob
https://github.com/unioncredit/union-v2-contracts/pull/109
And two connected prs relating to small issues with the initial fix pr:
- https://github.com/unioncredit/union-v2-contracts/pull/118
- https://github.com/unioncredit/union-v2-contracts/pull/119
dmitriia
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
The frozen coin age penalty is only applied when the time since the later of the borrower's last repayment and the staker's last reward withdrawal exceeds the overdue threshold, and the staker can reset the latter by calling withdrawRewards frequently.
Impact
Excess UNION rewards are emitted, diluting the holdings of honest stakers and causing monetary loss to all UNION holders.
What the attacker needed
Be a staker in the protocol; have an overdue borrower or self-lend
The fix
The reward accounting should use lifetime accumulators for staked, locked, and frozen amounts that are not reset on each reward claim, so that the frozen penalty cannot be avoided by frequent withdrawals.
How it was found
- A staker lends to a borrower (or to themselves) and the borrower does not repay.
- The staker calls withdrawRewards() at least once every overdueBlocks - 1 blocks.
- Because the time since the last reward withdrawal is always less than overdueBlocks, the frozenCoinAge penalty is never applied.
- The staker continues to receive full rewards without any reduction for the overdue debt.
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 lending protocol, stakers earn rewards based on how long they have staked and lent. If a borrower becomes overdue, the staker's rewards are supposed to be reduced (frozen). The bug lets a staker avoid that reduction by claiming rewards often enough, so they keep earning full rewards even when their borrowers are overdue.
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.