Staker can manipulate lockedCoinAge and earn rewards in excess of the allowed maximum
A staker's lockedCoinAge accumulator can be inflated by repeatedly triggering borrows when a voucher's trust is maxed, because the voucher's lastUpdated timestamp is not refreshed. This allows an attacker to obtain an arbitrarily large rewards multiplier and claim all UNION rewards from the contract.
Source: https://github.com/sherlock-audit/2023-02-union-judging/issues/26
Found by
hyh
Summary
Voucher can be counted arbitrary many times in staker's lockedCoinAge. If a voucher has maximized its trust then its locked is added to the lockedCoinAge each time fully as its lastUpdated is kept intact. This provides a surface to grow lockedCoinAge as big as an attacker wants, increasing it by current_block_difference * vouch.locked on each transaction.
Vulnerability Detail
vouch.lastUpdated is used for stakers[vouch.staker].lockedCoinAge accumulator and should be updated whenever its current state is recorded there. However, now it is not always the case as lockedCoinAge is updated in updateLocked() for every vouch until remaining is hit, while for the vouches, whose trust is maxxed in the borrow case (lock == true), such lastUpdated renewal doesn't take place.
Impact
An attacker will be able to steal all UNION rewards at once as with lockedCoinAge arbitrary big the reward multiplier, which isn't controlled to the upside, will be arbitrary big as well and an attacker will claim all available UNION in the contract.
Code Snippet
updateLocked() accumulates stakers[vouch.staker].lockedCoinAge before vouch logic is run.
However, lastUpdated isn't always updated, which leads to ability to grow the accumulator when lock == true and vouch.trust == vouch.locked:
uint256 lastWithdrawRewards = getLastWithdrawRewards[vouch.staker];
stakers[vouch.staker].lockedCoinAge +=
(block.number - _max(lastWithdrawRewards, uint256(vouch.lastUpdated))) *
uint256(vouch.locked);
if (lock) {
// Look up the staker and determine how much unlock stake they
// have available for the borrower to borrow. If there is 0
// then continue to the next voucher in the array
uint96 stakerLocked = stakers[vouch.staker].locked;
uint96 stakerStakedAmount = stakers[vouch.staker].stakedAmount;
uint96 availableStake = stakerStakedAmount - stakerLocked;
uint96 lockAmount = _min(availableStake, vouch.trust - vouch.locked);
if (lockAmount == 0) continue;
// Calculate the amount to add to the lock then
// add the extra amount to lock to the stakers locked amount
// and also update the vouches locked amount and lastUpdated block
innerAmount = _min(remaining, lockAmount);
stakers[vouch.staker].locked = stakerLocked + innerAmount;
vouch.locked += innerAmount;
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);
}
updateLocked( ..., true) is called on each borrow() with amount > minBorrow:
function borrow(address to, uint256 amount) external override onlyMember(msg.sender) whenNotPaused nonReentrant {
IAssetManager assetManagerContract = IAssetManager(assetManager);
if (amount < minBorrow) revert AmountLessMinBorrow();
if (amount > getRemainingDebtCeiling()) revert AmountExceedGlobalMax();
...
// 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 borrow amount. ie
// the borrower is trying to borrow more than is able to be underwritten
IUserManager(userManager).updateLocked(msg.sender, (actualAmount + fee).toUint96(), true);
So, whenever it's lock == true, vouch.trust == vouch.locked, the stakers[vouch.staker].lockedCoinAge += (block.number - _max(lastWithdrawRewards, uint256(vouch.lastUpdated))) * uint256(vouch.locked) will be counted for such a voucher with old vouch.lastUpdated as many times as updateLocked() run, which can be orchestrated easily enough.
Suppose Bob the staker has a vouch with trust maxxed, i.e. vouch.trust = vouch.locked = 10k DAI. He can setup a second borrower being his own account, some minimal trust, then can run min borrow many, many times, gaining huge stakers[vouch.staker].lockedCoinAge as vouch.lastUpdated aren't updated and lockedCoinAge grows with a positive some_number_of_blocks * 10k DAI number each time Bob borrows 1 DAI via his second borrower.
some_number_of_blocks here is (block.number - _max(lastWithdrawRewards, uint256(vouch.lastUpdated))) and it can be any as attack is viable as long as it is positive. Bob can wait for some time before proceeding to grow this number of blocks to make the attack bit more effective.
With effectiveLocked arbitrary big Bob will be able to obtain arbitrary big lendingRatio and _getRewardsMultiplier() result:
function _getRewardsMultiplier(UserManagerAccountState memory user) internal pure returns (uint256) {
if (user.isMember) {
if (user.effectiveStaked == 0) {
return memberRatio;
}
uint256 lendingRatio = user.effectiveLocked.wadDiv(user.effectiveStaked);
return lendingRatio + memberRatio;
} else {
return nonMemberRatio;
}
}
This will issue an arbitrary big amount of rewards to him, so Bob can effectively steal all UNION from the reward contract:
function _calculateRewardsByBlocks(
address account,
address token,
uint256 pastBlocks,
Info memory userInfo,
uint256 totalStaked,
UserManagerAccountState memory user
) internal view returns (uint256) {
uint256 startInflationIndex = users[account][token].inflationIndex;
if (user.effectiveStaked == 0 || totalStaked == 0 || startInflationIndex == 0 || pastBlocks == 0) {
return 0;
}
uint256 rewardMultiplier = _getRewardsMultiplier(user);
uint256 curInflationIndex = _getInflationIndexNew(totalStaked, pastBlocks);
if (curInflationIndex < startInflationIndex) revert InflationIndexTooSmall();
return
userInfo.accrued +
(curInflationIndex - startInflationIndex).wadMul(user.effectiveStaked).wadMul(rewardMultiplier);
}
Tool used
Manual Review
Recommendation
Consider updating vouch.lastUpdated every time it participated in lockedCoinAge accumulator:
uint256 lastWithdrawRewards = getLastWithdrawRewards[vouch.staker];
stakers[vouch.staker].lockedCoinAge +=
(block.number - _max(lastWithdrawRewards, uint256(vouch.lastUpdated))) *
uint256(vouch.locked);
+ vouch.lastUpdated = uint64(block.number);
if (lock) {
// Look up the staker and determine how much unlock stake they
// have available for the borrower to borrow. If there is 0
// then continue to the next voucher in the array
uint96 stakerLocked = stakers[vouch.staker].locked;
uint96 stakerStakedAmount = stakers[vouch.staker].stakedAmount;
uint96 availableStake = stakerStakedAmount - stakerLocked;
uint96 lockAmount = _min(availableStake, vouch.trust - vouch.locked);
if (lockAmount == 0) continue;
// Calculate the amount to add to the lock then
// add the extra amount to lock to the stakers locked amount
// and also update the vouches locked amount and lastUpdated block
innerAmount = _min(remaining, lockAmount);
stakers[vouch.staker].locked = stakerLocked + innerAmount;
vouch.locked += innerAmount;
- 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);
}
It is used only in _getEffectiveAmounts() as an addition to coinAge.lockedCoinAge already recorded:
uint256 lastUpdateBlock = _max(coinAge.lastWithdrawRewards, uint256(vouch.lastUpdated));
coinAge.lockedCoinAge += (block.number - lastUpdateBlock) * locked;
}
Discussion
kingjacob
https://github.com/unioncredit/union-v2-contracts/pull/111
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
Likely the vouch.lastUpdated timestamp is not updated when a voucher with maxed trust participates in the lockedCoinAge accumulator during a borrow, so the same locked amount is counted multiple times.
What the attacker needed
Staker with a maxed-trust voucher; second borrower account with minimal trust; ability to call borrow repeatedly
The fix
The fix is to update vouch.lastUpdated every time it participates in the lockedCoinAge accumulator, as shown in the recommendation.
How it was found
- Set up a staker account with a voucher whose trust is maxed (trust equals locked).
- Create a second borrower account with minimal trust.
- Repeatedly call borrow with a small amount (e.g., 1 DAI) from the second borrower, each time triggering updateLocked with lock=true.
- Observe that lockedCoinAge increases by (block difference * locked) each time without resetting lastUpdated.
- After accumulating a large lockedCoinAge, claim rewards to receive an excessive amount of UNION.
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 earn rewards based on how much they have locked and for how long. The code tracks a value called lockedCoinAge that grows over time. The bug is that under certain conditions, the time since last update is not reset, so the same locked amount gets counted again and again, making the reward multiplier huge. An attacker can exploit this to take all the rewards.
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.