`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.
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,magnetarand themarketcontracts an approval is made to the Magnetar contract. MagnetarCollateralModule::depositAddCollateralAndBorrowFromMarketget called with the passed parameters.- If the
data.depositis true, the Magnetar contract will call_extractTokenswith the following params:from = msg_.user,token = collateralAddressandamount = msg_.collateralAmount.
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._userhas 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
addCollateralis called on the Market contract. I've inlined the internal function to make it easier to follow:
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
userCollateralSharemapping is updatedpearlmit.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
transferFromERC1155function when the allowance is being checked.
Other occurrences
TOFT::mintLendXChainSGLXChainLockAndParticipateReceiverhas a similar issue as:
- Extract the bbCollateral from the user, sets approval for the BigBang contract through YieldBox.
- But then inside the
BBCollateral::addCollateralthe_addTokensagain expects an allowance through the Pearlmit contract.
TOFT::lockAndParticipateReceivercalls theMagnetar:lockAndParticipatewhere:
## 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
- https://github.com/sherlock-audit/2024-02-tapioca/blob/dc2464f420927409a67763de6ec60fe5c028ab0e/Tapioca-bar/contracts/markets/bigBang/BBCommon.sol#L133
- https://github.com/sherlock-audit/2024-02-tapioca/blob/dc2464f420927409a67763de6ec60fe5c028ab0e/TapiocaZ/contracts/tOFT/modules/TOFTMarketReceiverModule.sol#L108
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.
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
- The marketBorrowReceiver function checks whitelisting and approves the Magnetar contract.
- MagnetarCollateralModule::depositAddCollateralAndBorrowFromMarket is called.
- If deposit is true, Magnetar calls _extractTokens to transfer collateral from the user.
- Magnetar calls _setApprovalForYieldBox for the Market contract.
- Magnetar calls addCollateral on the Market contract.
- 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.