Skip to content
Smart contracts Audit finding Intermediate

distribute() won’t update epoch[tigAsset] when totalShares[tigAsset]==0 which can cause later created bond for this tigAsset to have wrong mint epoch

The distribute() function in BondNFT returns early when totalShares for an asset is zero, so the asset's epoch is never advanced. A later bond created for that asset gets an outdated mint epoch, letting the attacker lock tokens for a short real time while receiving shares and rewards as if locked for much longer.

unforgiven Follow

Originally disclosed on Code4rena

Submitted by unforgiven

https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/BondNFT.sol#L206-L228

https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/BondNFT.sol#L48-L86

Impact

Function BondNFT.createLock() creates a bond and it sets bond’s mint epoch as epoch[asset]. Function Lock.lock() first calls claimGovFees() which calls BondNFT.distribute() for all assets and updates the epoch[assets] for all assets. So during normal bond creation, the value of epoch[asset] would be updated and bond would be created from today epoch to today+period epoch. But if totalShares[tigAsset] == 0 for an asset, then distribute() won’t update epoch[asset] for that asset and epoch[asset] will be some old epoch(will be the start time where asset is added or the time where totalShares[_tigAsset] != 0). This would make createLock() set very wrong values for bond’s mint epoch when totalShares[tigAsset] == 0.

This would happen for the first bond that has been created for that asset always and it will happen again if for some period totalShares[asset] become 0, then the next bond would have wrong mint epoch. or setAllowedAsset(asset, false) has been called for that asset.

Proof of Concept

This is distribute() code in BondNFT contract:

function distribute(
        address _tigAsset,
        uint _amount
    ) external {
        if (totalShares[_tigAsset] == 0 || !allowedAsset[_tigAsset]) return;
        IERC20(_tigAsset).transferFrom(_msgSender(), address(this), _amount);
        unchecked {
            uint aEpoch = block.timestamp / DAY;
            if (aEpoch > epoch[_tigAsset]) {
                for (uint i=epoch[_tigAsset]; i<aEpoch; i++) {
                    epoch[_tigAsset] += 1;
                    accRewardsPerShare[_tigAsset][i+1] = accRewardsPerShare[_tigAsset][i];
                }
            }
            accRewardsPerShare[_tigAsset][aEpoch] += _amount * 1e18 / totalShares[_tigAsset];
        }
        emit Distribution(_tigAsset, _amount);
    }

As you can see when totalShares[_tigAsset] == 0, then the value of epoch[_tigAsset] won’t get updated to today. And there is no other logic in the code to update epoch[tigAsset]. So when totalShares[_tigAsset] == 0, then the value of the epoch[tigAsset] would be outdated. this would happen when an asset is recently added to the BondNFT assets or when there is no bond left.

When this condition happens and a user calls Lock.lock() to create a bond, the lock() function would call claimGovFees() to update rewards in BondNFT but because for that asset the value of totalShares are 0, that asset epoch[] won’t get updated and in the BondNFT.createLock(), the wrong value would set as bond’s mint epoch.

This is Lock.lock() code:

    function lock(
        address _asset,
        uint _amount,
        uint _period
    ) public {
        require(_period <= maxPeriod, "MAX PERIOD");
        require(_period >= minPeriod, "MIN PERIOD");
        require(allowedAssets[_asset], "!asset");

        claimGovFees();

        IERC20(_asset).transferFrom(msg.sender, address(this), _amount);
        totalLocked[_asset] += _amount;

        bondNFT.createLock( _asset, _amount, _period, msg.sender);
    }

And this is BondNFT.createLock() code:

    function createLock(
        address _asset,
        uint _amount,
        uint _period,
        address _owner
    ) external onlyManager() returns(uint id) {
        require(allowedAsset[_asset], "!Asset");
        unchecked {
            uint shares = _amount * _period / 365;
            uint expireEpoch = epoch[_asset] + _period;
            id = ++totalBonds;
            totalShares[_asset] += shares;
            Bond memory _bond = Bond(
                id,             // id
                address(0),     // owner
                _asset,         // tigAsset token
                _amount,        // tigAsset amount
                epoch[_asset],  // mint epoch
                block.timestamp,// mint timestamp
                expireEpoch,    // expire epoch
                0,              // pending
                shares,         // linearly scaling share of rewards
                _period,        // lock period
                false           // is expired boolean
            );
            _idToBond[id] = _bond;
            _mint(_owner, _bond);
        }
        emit Lock(_asset, _amount, _period, _owner, id);
    }

If a bond gets wrong value for mint epoch, it would have wrong value for expired epoch and user would get a lot of shares by lock for small time.

For example this scenario:

  1. Let’s assume epoch[asset1] is outdated and it shows 30 days ago epoch. (allowedAsset[asset1] was false so locking was not possible and then is set as true after 30 days)
  2. During this time, because totalShare[asset1] was 0, the distribute() function won’t update epoch[asset1] and epoch[asset1] would show 30 days ago.
  3. Attacker would create a lock for 32 days by calling Lock.lock(asset1). Code would call BondNFT.createLock() and would create a bond for attacker which epoch start time is 30 days ago and epoch expire time is 2 days later and attacker receives shares for 32 days.
  4. Some reward would get distributed into the BondNFT for the asset1.
  5. Other users would create lock too.
  6. Attacker would claim his rewards and his rewards would be for 32 day locking but attacker lock his tokens for 2 days in reality.

So attacker was able to create lock for a long time and get shares and rewards based on that, but attacker can release lock after short time.

Tools Used

VIM

Recommended Mitigation Steps

Update epoch[asset] in distribute() function even when totalShares[_tigAsset] is equal to 0. Only the division by zero and fund transfer should be prevented when totalShare is zero and epoch[asset] index should be updated.

TriHaz (Tigris Trade) confirmed

Alex the Entreprenerd (judge) commented:

The Warden has shown a set of circumstances that would allow a locker to lock their tokens for a relatively short period of time, while gaining extra rewards for up to one Epoch.

Because the finding is limited to a theft of yield, I believe it to be of Medium Severity.

GainsGoblin (Tigris Trade) resolved:

Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419176233


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

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 early return in distribute() when totalShares is zero skips the epoch update, leaving epoch stale for assets with no current shares.

Impact

An attacker could gain extra reward shares and yield for a lock period longer than the real lock time; the judge rated this a theft of yield at medium severity.

What the attacker needed

Asset has zero total shares for a period; attacker can call Lock.lock

The fix

The epoch index should be updated in distribute() even when totalShares is zero, while still preventing the division by zero and the fund transfer.

How it was found

  1. An asset's epoch is stale because totalShares for it was zero for a period.
  2. The attacker calls Lock.lock for that asset with a chosen period.
  3. createLock uses the stale epoch as the bond's mint epoch, so the expire epoch is also wrong.
  4. The attacker receives shares for the full period even though the real lock time is short.
  5. Rewards are distributed and the attacker claims rewards based on the inflated shares.

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 bond contract tracks time in 'epochs' (day-long periods) to work out how many reward shares a lock earns. When nobody holds shares in an asset, the code skips updating that asset's current epoch, so it stays stuck in the past. A user who then creates a bond is credited from that old date, so they get more shares and rewards than their real lock time deserves.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by unforgiven

View all

50 more disclosed reports, best first.

More from Tigris Trade contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.