Skip to content
Business logic Audit finding Intermediate

`TOFTMarketReceiverModule::marketBorrowReceiver` flow is broken

The marketBorrowReceiver flow in TOFTMarketReceiverModule is broken because the Magnetar contract does not grant the Market contract an allowance through the Pearlmit contract before attempting to transfer ERC1155 tokens. This causes the transaction to revert when the Market contract tries to pull the tokens.

GiuseppeDeLaZara Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-02-tapioca-judging/issues/137

Found by

GiuseppeDeLaZara

Summary

The TOFTMarketReceiverModule::marketBorrowReceiver flow is broken and will revert when the Magnetar contract tries to transfer the ERC1155 tokens to the Market contract.

Vulnerability Detail

TOFTMarketReceiverModule::marketBorrowReceiver flow is broken.

Let's examine it more closely:

  • After checking the whitelisting status for the marketHelper, magnetar and the market contracts an approval is made to the Magnetar contract.
  • MagnetarCollateralModule::depositAddCollateralAndBorrowFromMarket get called with the passed parameters.
  • If the data.deposit is true, the Magnetar contract will call _extractTokens with the following params: from = msg_.user, token = collateralAddress and amount = msg_.collateralAmount.
Solidity
    function _extractTokens(address _from, address _token, uint256 _amount) internal returns (uint256) {
        uint256 balanceBefore = IERC20(_token).balanceOf(address(this));
        // IERC20(_token).safeTransferFrom(_from, address(this), _amount);
        pearlmit.transferFromERC20(_from, address(this), address(_token), _amount);
        uint256 balanceAfter = IERC20(_token).balanceOf(address(this));
        if (balanceAfter <= balanceBefore) revert Magnetar_ExtractTokenFail();
        return balanceAfter - balanceBefore;
    }
  • The collateral gets transferred into the Magnetar contract in case the msg._user has given sufficient allowance to the Magnetar contract through the Pearlmit contract.
  • After this _setApprovalForYieldBox(data.market, yieldBox_); is called that sets the allowance of the Magnetar contract to the Market contract.
  • Then addCollateral is called on the Market contract. I've inlined the internal function to make it easier to follow:
Solidity
    function _addCollateral(address from, address to, bool skim, uint256 amount, uint256 share) internal {
        if (share == 0) {
            share = yieldBox.toShare(collateralId, amount, false);
        }
        uint256 oldTotalCollateralShare = totalCollateralShare;
        userCollateralShare[to] += share;
        totalCollateralShare = oldTotalCollateralShare + share;

        // yieldBox.transfer(from, address(this), _assetId, share);
        bool isErr = pearlmit.transferFromERC1155(from, address(this), address(yieldBox), collateralId, share);
        if (isErr) {
            revert TransferFailed();
        }
    }
  • After the userCollateralShare mapping is updated pearlmit.transferFromERC1155(from, address(this), address(yieldBox), collateralId, share); gets called.
  • This is critical as now the Magnetar is supposed to transfer the ERC1155 tokens(Yieldbox) to the Market contract.
  • In order to do this the Magnetar contract should have given the allowance to the Market contract through the Pearlmit contract.
  • This is not the case, the Magnetar has only executed _setApprovalForYieldBox(data.market, yieldBox_);, nothing else.
  • It will revert inside the Pearlmit contract transferFromERC1155 function when the allowance is being checked.

Other occurrences

  1. TOFT::mintLendXChainSGLXChainLockAndParticipateReceiver has a similar issue as:
  • Extract the bbCollateral from the user, sets approval for the BigBang contract through YieldBox.
  • But then inside the BBCollateral::addCollateral the _addTokens again expects an allowance through the Pearlmit contract.
  1. TOFT::lockAndParticipateReceiver calls the Magnetar:lockAndParticipate where:
Solidity

## MagnetarMintCommonModule.sol

function _lockOnTOB(
        IOptionsLockData memory lockData,
        IYieldBox yieldBox_,
        uint256 fraction,
        bool participate,
        address user,
        address singularityAddress
    ) internal returns (uint256 tOLPTokenId) {
    ....
        _setApprovalForYieldBox(lockData.target, yieldBox_);
        tOLPTokenId = ITapiocaOptionLiquidityProvision(lockData.target).lock(
             participate ? address(this) : user, singularityAddress, lockData.lockDuration, lockData.amount
        );
}

## TapiocaOptionLiquidityProvision.sol

function lock(address _to, IERC20 _singularity, uint128 _lockDuration, uint128 _ybShares)
    external
    nonReentrant
    returns (uint256 tokenId)
{
    // Transfer the Singularity position to this contract
    // yieldBox.transfer(msg.sender, address(this), sglAssetID, _ybShares);
    {
        bool isErr =
            pearlmit.transferFromERC1155(msg.sender, address(this), address(yieldBox), sglAssetID, _ybShares);
        if (isErr) {
            revert TransferFailed();
        }
    }
  • The same issue where approval through the Pearlmit contract is expected.

Impact

The TOFTMarketReceiverModule::marketBorrowReceiver flow is broken and will revert when the Magnetar contract tries to transfer the ERC1155 tokens to the Market contract. There are also other instances of similar issues.

Code Snippet

Tool used

Manual Review

Recommendation

Review all the allowance mechanisms and ensure that they are correct.

Discussion

sherlock-admin3

1 comment(s) were left on this issue during the judging contest.

takarez commented:

the cause of the revert wasn't mentioned

sherlock-admin4

The protocol team fixed this issue in PR/commit https://github.com/Tapioca-DAO/tapioca-periph/commit/0a03bbbd04b30bcac183f1bae24d7f9fe9fd4103#diff-4a6decd451580f83dfe716ed16851529590c8349b1ba9bff97b42248c75e5430.

The original report as published on Sherlock, by GiuseppeDeLaZara. 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 Magnetar contract sets approval for the YieldBox contract but not for the Market contract, so the subsequent transferFromERC1155 call fails due to missing allowance.

Impact

The marketBorrowReceiver flow is broken and will revert, preventing users from borrowing from the market; similar issues exist in other functions.

What the attacker needed

User has collateral and allowance; whitelisted contracts

The fix

The protocol team fixed this issue in a commit.

How it was found

  1. The marketBorrowReceiver function checks whitelisting and approves the Magnetar contract.
  2. MagnetarCollateralModule::depositAddCollateralAndBorrowFromMarket is called.
  3. If deposit is true, Magnetar calls _extractTokens to transfer collateral from the user.
  4. Magnetar calls _setApprovalForYieldBox for the Market contract.
  5. Magnetar calls addCollateral on the Market contract.
  6. Inside addCollateral, pearlmit.transferFromERC1155 is called, which reverts because Magnetar did not approve the Market contract.

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 function that is supposed to move tokens between contracts fails because one contract (Magnetar) forgot to give another contract (Market) permission to take the tokens. As a result, the whole transaction fails and any user trying to use this feature would see an error.

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 GiuseppeDeLaZara

View all

7 more disclosed reports, best first.

More from Tapioca

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.