Skip to content
Business logic Audit finding Intermediate

Staker rewards can be gathered with maximal multiplier no matter how borrowers are overdue

The frozen coin age used to compute staker rewards was reset whenever a borrower took out any new loan, even a tiny one, so a staker could have their borrowers borrow a minimal amount to clear the overdue penalty and claim rewards at the maximum multiplier. This let a staker obtain inflated UNION token issuance and dilute other holders.

hyh Follow with cccz

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-union-finance-judging/issues/74

Found by

hyh, cccz

Summary

Frozen coin age of an account is based on the vouch.lastUpdated, which is set to the current block not only on initial borrowing and debt full interest payment or notional repayment, but also on any repetitive borrowing from a vouch.

Using this a staker can collude with their borrowers, asking them to borrow a minimal amount, so staker's memberTotalFrozen end up being zero and maximal rewards can be obtained immediately with UserManager’s withdrawRewards().

Vulnerability Detail

Bob the staker whose borrowers are overdue can ask them to borrow min amount as soon as they can (1 DAI), then claim the rewards, which will be with maximal 2x multiplier.

Bob can do it each time he wants to gather the rewards (say before he unstakes), effectively surpassing the reward multiplier logic altogether.

Another theoretical way to game the rewards logic is to lend to self, but this require paying interest, that isn't low. On the other side minimal borrow amount is kept low, now staying at 1.0 DAI, to promote protocol adoption.

This way, Bob and his borrowers excess costs are negligible (gas and min borrowings), while benefit of obtaining max rewards are material.

I.e. Bob, being a rational user, on each reward withdrawal can ask the vouchees to borrow minimal amount (Bob will update trust for that end, if needed) instead of repaying anything, this way removing the penalty. Bob will obtain the rewards with the maximal multiplier.

Impact

Bob gains bloated reward issuance and steals from all UNION token holders by having its supply diluted.

As this can be done without any major prerequisites, setting the severity to be high.

Code Snippet

UNION tokens issuance is the main staker profit mechanics, while stake utilization is the core metric for reward issuance rate calculations:

https://docs.union.finance/protocol-overview/plain-english-overview#stakers-earning-union-from-comptroller

MARKDOWN
Stake utilization is meant to give control of the protocol to those who vouch for people who borrow and dont default. And the 0.75 is to allow non-members a non financial means but still sybil resistant way to acquire the UNION. The curve is upgradeable without requiring a fork or a withdrawal of the dai staked. All of this is handled by the {Comptroller.sol Contract}

UserManager’s updateLocked() sets vouchers[borrower][i].lastUpdated to the current block both on borrowing and repaying:

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/user/UserManager.sol#L800-L841

Solidity
    function updateLocked(
        address borrower,
        uint96 amount,
        bool lock
    ) external onlyMarket {
        uint96 remaining = amount;

        for (uint256 i = 0; i < vouchers[borrower].length; i++) {
            Vouch storage vouch = vouchers[borrower][i];
            uint96 innerAmount;

            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);
            }

getFrozenInfo() uses vouchers[vouchee.borrower][vouchee.voucherIndex].lastUpdated for calculating what share of the total staker’s lending business with the system is overdue:

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/user/UserManager.sol#L438-L454

Solidity
    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;

I.e. as a result getFrozenInfo() will treat constantly borrowing account as not frozen.

Tool used

Manual Review

Recommendation

Consider either basing total frozen on borrower's lastRepay instead of voucher's lastUpdate (as it described in another issue) or removing the lastUpdate renewal on a repetitive borrow.

I.e. update of the indicator that is used in the total frozen calculation on a repetitive borrow is the issue here as it neither loan start nor full interest / partial notional prepayment and shouldn't reset the frozen counter.

In other words frozen age is overdue analogue with regard to lender's account health. Repetitive borrow do not reduce this overdue, but is counted towards it.

As lastUpdate is used in account frozen statistics calculation only, the simplest solution is to change lastUpdate on the notional repay and initial borrowing from the vouch only:

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/user/UserManager.sol#L811-L828

Solidity
            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 vouchLocked = vouch.locked;
-               uint96 lockAmount = _min(availableStake, vouch.trust - vouch.locked);
+               uint96 lockAmount = _min(availableStake, vouch.trust - vouchLocked);
                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 = vouchLocked + innerAmount;
+               if (vouchLocked == 0) vouch.lastUpdated = uint64(block.number);
-               vouch.locked += innerAmount;
-               vouch.lastUpdated = uint64(block.number);
            }

Notice, that this example solution will suffice if the issue is treated in isolation, but given other issues using lastRepay is preferred.

Discussion

kingjacob

Dupe of #99

The original report as published on Sherlock, by hyh. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 vouch's lastUpdated timestamp, which drives the frozen coin age calculation, was refreshed on every borrow rather than only on initial borrowing or repayment.

Impact

The staker gains bloated reward issuance and dilutes all UNION token holders; the report rates this high severity as it needs no major prerequisites.

What the attacker needed

Staker with vouched borrowers; borrowers willing to borrow a minimal amount; small gas and borrow costs

The fix

The lastUpdated timestamp should only be refreshed on initial borrowing from a vouch and on notional repayment, not on repetitive borrowing; the report also suggests basing frozen totals on the borrower's lastRepay instead.

How it was found

  1. The staker's borrowers become overdue on their loans.
  2. The staker asks the borrowers to take out a minimal new loan.
  3. The new borrow updates the vouch's lastUpdated to the current block.
  4. The staker calls withdrawRewards() and receives rewards at the maximal multiplier.

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 staker puts up tokens to back other people's loans and earns rewards based on how much of their backing is tied up in loans that are not overdue. The system measured 'overdue' from the last time the borrower's loan record changed, and any new borrowing changed that timestamp. So a borrower could take a tiny new loan to make an overdue record look fresh, and the staker would get full 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.

More by hyh

View all

137 more disclosed reports, best first.

More from Union Finance

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.