Skip to content
Business logic Audit finding Intermediate

YieldStakingBase.stake() cannot append borrowAmount

A new restriction in yieldSetERC721TokenData prevents the function from being called more than once for the same NFT, which breaks the ability to add to an existing loan in YieldStakingBase.stake(). As a result, users cannot borrow additional funds without first repaying the loan, incurring extra fees.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

Code reference: YieldLogic.sol#L150

In this PR, yieldSetERC721TokenData() adds the following restrictions:

    function executeYieldSetERC721TokenData(InputTypes.ExecuteYieldSetERC721TokenDataParams memory params) internal {
...
        if (params.isLock) {
            require(ymData.yieldCap > 0, Errors.YIELD_EXCEED_STAKER_CAP_LIMIT);

@>          require(tokenData.lockerAddr == address(0), Errors.ASSET_ALREADY_LOCKED_IN_USE);

            VaultLogic.erc721SetTokenLockerAddr(nftAssetData, params.tokenId, lockerAddr);
        } else {
            require(tokenData.lockerAddr == lockerAddr, Errors.YIELD_TOKEN_LOCKED_BY_OTHER);

            VaultLogic.erc721SetTokenLockerAddr(nftAssetData, params.tokenId, address(0));
        }

This causes yieldSetERC721TokenData() to be called only once. This is fine for YieldWUSDStaking.sol. But for the other YieldStakingBase.sol, there is a problem, because it is not possible to increase borrowing again (Health Factor is still enough) as before.

In YieldStakingBase.stake()

    function _stake(uint32 poolId, address nft, uint256 tokenId, uint256 borrowAmount) internal virtual {
...
        YieldStakeData storage sd = stakeDatas[nft][tokenId];
        if (sd.yieldAccount == address(0)) {
            require(vars.nftLockerAddr == address(0), Errors.YIELD_ETH_NFT_ALREADY_USED);

            vars.totalDebtAmount = borrowAmount;

            sd.yieldAccount = address(vars.yieldAccout);
            sd.poolId = poolId;
            sd.state = Constants.YIELD_STATUS_ACTIVE;
        } else {
            require(vars.nftLockerAddr == address(this), Errors.YIELD_ETH_NFT_NOT_USED_BY_ME);
            require(sd.state == Constants.YIELD_STATUS_ACTIVE, Errors.YIELD_ETH_STATUS_NOT_ACTIVE);
            require(sd.poolId == poolId, Errors.YIELD_ETH_POOL_NOT_SAME);

            vars.totalDebtAmount = convertToDebtAssets(poolId, sd.debtShare) + borrowAmount;
        }
....
@>      poolYield.yieldSetERC721TokenData(poolId, nft, tokenId, true, address(underlyingAsset));

        // check hf
        uint256 hf = calculateHealthFactor(nft, nc, sd);
        require(hf >= nc.unstakeHeathFactor, Errors.YIELD_ETH_HEATH_FACTOR_TOO_LOW);

        emit Stake(msg.sender, nft, tokenId, borrowAmount);
    }

Impact

Users can’t borrow additional funds and have to pay back the loan first, losing a certain amount of handling fee.

Recommended Mitigation

Two possible modifications

  1. _stake() does not call poolYield.yieldSetERC721TokenData() if appending.

    function _stake(uint32 poolId, address nft, uint256 tokenId, uint256 borrowAmount) internal virtual {
    ...
            require(vars.nftLockerAddr == address(this), Errors.YIELD_ETH_NFT_NOT_USED_BY_ME);
            require(sd.state == Constants.YIELD_STATUS_ACTIVE, Errors.YIELD_ETH_STATUS_NOT_ACTIVE);
            require(sd.poolId == poolId, Errors.YIELD_ETH_POOL_NOT_SAME);
    
            vars.totalDebtAmount = convertToDebtAssets(poolId, sd.debtShare) + borrowAmount;
        }
    ....
    
  2. poolYield.yieldSetERC721TokenData(poolId, nft, tokenId, true, address(underlyingAsset));

  3. if (vars.nftLockerAddr != address(this))

  4. poolYield.yieldSetERC721TokenData(poolId, nft, tokenId, true, address(underlyingAsset));

  5. }

2. `yieldSetERC721TokenData()` to allow the current `nftLockerAddr` to execute.
```diff
    function executeYieldSetERC721TokenData(InputTypes.ExecuteYieldSetERC721TokenDataParams memory params) internal {
...
        if (params.isLock) {
            require(ymData.yieldCap > 0, Errors.YIELD_EXCEED_STAKER_CAP_LIMIT);

-           require(tokenData.lockerAddr == address(0), Errors.ASSET_ALREADY_LOCKED_IN_USE);
+           require(tokenData.lockerAddr == address(0) || tokenData.lockerAddr == lockerAddr), Errors.ASSET_ALREADY_LOCKED_IN_USE);

            VaultLogic.erc721SetTokenLockerAddr(nftAssetData, params.tokenId, lockerAddr);
        } else {
            require(tokenData.lockerAddr == lockerAddr, Errors.YIELD_TOKEN_LOCKED_BY_OTHER);

            VaultLogic.erc721SetTokenLockerAddr(nftAssetData, params.tokenId, address(0));
        }

It is recommended to choose option (2).

thorseldon (BendDAO) confirmed and commented:

Fixed in commit eef87bf


The original report as published on Code4rena, by bin2chen. 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 new requirement that the NFT's locker address must be zero prevents re-locking by the same locker, which is needed when increasing a loan.

Impact

Users cannot borrow additional funds without first repaying the loan, losing a certain amount of handling fee.

What the attacker needed

Existing active loan on the NFT; sufficient health factor to borrow more

The fix

The project fixed it in commit eef87bf, as confirmed by the team.

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.

This is a smart contract bug where a new safety check stops users from adding more debt to an existing loan. The check was meant to stop the same NFT being locked twice, but it also blocks the normal top-up action. So users must repay and start over, paying extra fees.

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 bin2chen

View all

139 more disclosed reports, best first.

More from BendDAO Invitational

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.