TOFT removeCollateral can be used to steal all the balance
The removeCollateral message pathway in TOFT allows an attacker to supply a malicious marketHelper contract and an arbitrary withdrawLzFeeAmount, which are not validated on the sending side. When the message is received on the remote chain, the TOFT contract calls withdrawToChain on the attacker's contract with the TOFT's entire native token balance as the fee amount, enabling theft of all such funds.
windhustler Follow with 0x73696d616f
Originally disclosed on Code4rena
Submitted by windhustler, also found by 0x73696d616f
removeCollateral -> remove message pathway can be used to steal all the balance of the TapiocaOFT and mTapiocaOFT tokens in case when their underlying tokens is native. TOFTs that hold native tokens are deployed with erc20 address set to address zero, so while minting you need to transfer value.
Proof of Concept
The attack needs to be executed by invoking the removeCollateral function from any chain to chain on which the underlying balance resides, e.g. host chain of the TOFT. When the message is received on the remote chain, I have placed in the comments below what are the params that need to be passed to execute the attack.
function remove(bytes memory _payload) public {
(
,
,
address to,
,
ITapiocaOFT.IRemoveParams memory removeParams,
ICommonData.IWithdrawParams memory withdrawParams,
ICommonData.IApproval[] memory approvals
) = abi.decode(
_payload,
(
uint16,
address,
address,
bytes32,
ITapiocaOFT.IRemoveParams,
ICommonData.IWithdrawParams,
ICommonData.IApproval[]
)
);
// approvals can be an empty array so this is skipped
if (approvals.length > 0) {
_callApproval(approvals);
}
// removeParams.market and removeParams.share don't matter
approve(removeParams.market, removeParams.share);
// removeParams.market just needs to be deployed by the attacker and do nothing, it is enough to implement IMarket interface
IMarket(removeParams.market).removeCollateral(
to,
to,
removeParams.share
);
// withdrawParams.withdraw = true to enter the if block
if (withdrawParams.withdraw) {
// Attackers removeParams.market contract needs to have yieldBox() function and it can return any address
address ybAddress = IMarket(removeParams.market).yieldBox();
// Attackers removeParams.market needs to have collateralId() function and it can return any uint256
uint256 assetId = IMarket(removeParams.market).collateralId();
// removeParams.marketHelper is a malicious contract deployed by the attacker which is being transferred all the balance
// withdrawParams.withdrawLzFeeAmount needs to be precomputed by the attacker to match the balance of TapiocaOFT
IMagnetar(removeParams.marketHelper).withdrawToChain{
value: withdrawParams.withdrawLzFeeAmount // This is not validated on the sending side so it can be any value
}(
ybAddress,
to,
assetId,
withdrawParams.withdrawLzChainId,
LzLib.addressToBytes32(to),
IYieldBoxBase(ybAddress).toAmount(
assetId,
removeParams.share,
false
),
removeParams.share,
withdrawParams.withdrawAdapterParams,
payable(to),
withdrawParams.withdrawLzFeeAmount
);
}
}
Neither removeParams.marketHelper or withdrawParams.withdrawLzFeeAmount are validated on the sending side so the former can be the address of a malicious contract and the latter can be the TOFT’s balance of gas token.
This type of attack is possible because the msg.sender in IMagnetar(removeParams.marketHelper).withdrawToChain is the address of the TOFT contract which holds all the balances.
This is because:
- Relayer submits the message to
lzReceiveso he is themsg.sender. - Inside the
_blockingLzReceivethere is a call into its own public function so themsg.senderis the address of the contract. - Inside the
_nonBlockingLzReceivethere is delegatecall into a corresponding module which preserves themsg.senderwhich is the address of the TOFT. - Inside the module there is a call to withdrawToChain and here the
msg.senderis the address of the TOFT contract, so we can maliciously transfer all the balance of the TOFT.
Tools Used
Foundry
Recommended Mitigation Steps
It’s hard to recommend a simple fix since as I pointed out in my other issues the airdropping logic has many flaws. One of the ways of tackling this issue is during the removeCollateral to:
- Do not allow
adapterParamsparams to be passed as bytes but rather asgasLimitandairdroppedAmount, from which you would encode eitheradapterParamsV1oradapterParamsV2. - And then on the receiving side check and send with value only the amount the user has airdropped.
0xRektora (Tapioca) confirmed and commented:
Related to https://github.com/code-423n4/2023-07-tapioca-findings/issues/1290
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 receiving logic trusts user-supplied marketHelper and withdrawLzFeeAmount without validating them against the sender's actual deposit, allowing the TOFT contract itself to be tricked into forwarding its entire native balance.
Impact
An attacker could steal all native token balance held by TapiocaOFT and mTapiocaOFT contracts on the destination chain.
What the attacker needed
Ability to call removeCollateral on any chain; attacker-controlled contracts implementing IMarket and…; TOFT holds native tokens on destination chain
The fix
The removeCollateral flow should not accept raw adapterParams; instead it should take gasLimit and airdroppedAmount, encode adapterParams from them, and on the receiving side send only the amount the user actually airdropped.
How it was found
- Deploy a malicious contract that implements the IMarket interface and a malicious marketHelper contract.
- Call removeCollateral on any chain, targeting the chain where the TOFT holds native tokens, with removeParams.market set to the malicious IMarket contract and removeParams.marketHelper set to the malicious helper.
- Set withdrawParams.withdraw to true and withdrawParams.withdrawLzFeeAmount to the TOFT's native token balance.
- When the message is received on the remote chain, the TOFT calls withdrawToChain on the malicious helper with value equal to the entire balance.
- The malicious helper receives the funds, completing the theft.
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 bug in a cross-chain token contract that holds native tokens (like ETH). When a user asks to remove collateral on another chain, the contract later sends a message that includes a helper address and a fee amount. The contract does not check that the helper is legitimate or that the fee is reasonable, so an attacker can set the helper to their own contract and the fee to the contract's entire balance. The contract then calls the attacker's helper with all its native tokens, and the attacker keeps them.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.