depositYBLendSGLLockXchainTOLP function of the MagnetarAssetXChainModule will not work because it transfers Singularity tokens to the user before _withdrawToChain
The depositYBLendSGLLockXchainTOLP function wraps Singularity tokens into TOFT tokens and transfers them to the user, but then tries to withdraw those same tokens from the contract's YieldBox balance, which is empty. As a result, the function always reverts and the cross-chain deposit and lock feature is broken.
Originally disclosed on Code4rena
Submitted by KIntern_NA
Description
In depositYBLendSGLLockXchainTOLP function of the MagnetarAssetXChainModule, after lending to SGL, it will wrap the received Singularity tokens into the TOFT wrapping token of it. Afterward, it attempts to transfer those wrapped tokens cross-chain, then unwrap them in the destination chain to lock and participate.
function depositYBLendSGLLockXchainTOLP(DepositAndSendForLockingData memory data) public payable {
...
uint256 fraction =
_depositYBLendSGL(data.depositData, data.singularity, IYieldBox(yieldBox), data.user, data.lendAmount);
// wrap SGL receipt into tReceipt
// ! User should approve `address(this)` for `IERC20(data.singularity)` !
uint256 toftAmount = _wrapSglReceipt(IYieldBox(yieldBox), data.singularity, data.user, fraction, data.assetId);
data.lockAndParticipateSendParams.lzParams.sendParam.amountLD = toftAmount;
// decode `composeMsg` and re-encode it with updated params
(uint16 msgType_,, uint16 msgIndex_, bytes memory tapComposeMsg_, bytes memory nextMsg_) =
TapiocaOmnichainEngineCodec.decodeToeComposeMsg(data.lockAndParticipateSendParams.lzParams.sendParam.composeMsg);
LockAndParticipateData memory lockData = abi.decode(tapComposeMsg_, (LockAndParticipateData));
lockData.fraction = toftAmount;
data.lockAndParticipateSendParams.lzParams.sendParam.composeMsg =
TapiocaOmnichainEngineCodec.encodeToeComposeMsg(abi.encode(lockData), msgType_, msgIndex_, nextMsg_);
// send on another layer for lending
_withdrawToChain(
MagnetarWithdrawData({
yieldBox: yieldBox,
assetId: data.assetId,
unwrap: false,
lzSendParams: data.lockAndParticipateSendParams.lzParams,
sendGas: data.lockAndParticipateSendParams.lzSendGas,
composeGas: data.lockAndParticipateSendParams.lzComposeGas,
sendVal: data.lockAndParticipateSendParams.lzSendVal,
composeVal: data.lockAndParticipateSendParams.lzComposeVal,
composeMsg: data.lockAndParticipateSendParams.lzParams.sendParam.composeMsg,
composeMsgType: data.lockAndParticipateSendParams.lzComposeMsgType,
withdraw: true
})
);
}
After lending into the Singularity contract, _wrapSglReceipt is used to wrap the received Singularity tokens into TOFT tokens. Thus, they will be able to be sent cross-chain by using _withdrawToChain with composed messages including the lockAndParticipate option to perform these actions after receiving tokens
The _withdrawToChain function attempts to withdraw YieldBox shares of this contract (address(this)) to obtain tokens before sending them cross-chain (see this code snippet). However, _wrapSglReceipt has sent tokens to the user after wrapping. Therefore, there are no tokens or YieldBox shares existing in this contract, resulting in _withdrawToChain reverting afterward.
function _wrapSglReceipt(IYieldBox yieldBox, address sgl, address user, uint256 fraction, uint256 assetId)
internal
returns (uint256 toftAmount)
{
IERC20(sgl).safeTransferFrom(user, address(this), fraction);
(, address tReceiptAddress,,) = yieldBox.assets(assetId);
IERC20(sgl).approve(tReceiptAddress, fraction);
toftAmount = ITOFT(tReceiptAddress).wrap(address(this), address(this), fraction);
IERC20(tReceiptAddress).safeTransfer(user, toftAmount);
}
Impact
depositYBLendSGLLockXchainTOLP function of MagnetarAssetXChainModule will be broken.
Recommended Mitigation Steps
Should deposit into YieldBox for address(this) after wrapping Singularity tokens in the _wrapSglReceipt function as following:
function _wrapSglReceipt(IYieldBox yieldBox, address sgl, address user, uint256 fraction, uint256 assetId)
internal
returns (uint256 toftAmount)
{
IERC20(sgl).safeTransferFrom(user, address(this), fraction);
(, address tReceiptAddress,,) = yieldBox.assets(assetId);
IERC20(sgl).approve(tReceiptAddress, fraction);
toftAmount = ITOFT(tReceiptAddress).wrap(address(this), address(this), fraction);
//deposit to YieldBox for this
IERC20(tReceiptAddress).safeApprove(address(yieldBox), toftAmount);
yieldBox.depositAsset(assetId, address(this), address(this), toftAmount, 0);
}
cryptotechmaker (Tapioca) confirmed and commented:
PR here.
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 _wrapSglReceipt helper transfers the wrapped tokens directly to the user instead of depositing them into YieldBox for the contract, so the subsequent _withdrawToChain call has no shares to withdraw.
Impact
The depositYBLendSGLLockXchainTOLP function is broken and cannot be used.
What the attacker needed
None
The fix
The _wrapSglReceipt function should deposit the wrapped TOFT tokens into YieldBox for the contract instead of transferring them to the user; the project confirmed and linked a fix PR.
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 moves tokens to the user too early. Later, the same function tries to take those tokens from the contract's own balance to send them to another blockchain, but the balance is zero, so the whole transaction fails. The feature simply does not work.
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.