_sendToken implementation in Balancer.sol is wrong which will make the underlying erc20 be send to a random address and lost
The _sendToken function in Balancer.sol incorrectly encodes the destination address using abi.encode instead of abi.encodePacked, causing the address to be padded with zeros. When the cross-chain message is received, the padded address is misinterpreted, leading to tokens being sent to a wrong address and lost.
Submitted by Vagner
The function _sendToken is called on rebalance to perform the rebalance operation by the owner which will transfer native token or the underlying ERC20 for a specific tOFT token to other chains. This function uses the router from Stargate to transfer the tokens, but the implementation of the swap is done wrong which will make the tokens to be lost.
Proof of Concept
_sendToken calls Stargate’s router swap function with the all the parameters needed as can be seen here https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/Balancer.sol#L322-L332, but the problem relies that the destination address is computed by calling abi.encode(connectedOFTs[_oft][_dstChainId].dstOft) instead of the abi.encodePacked(connectedOFTs[_oft][_dstChainId].dstOft) https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/Balancer.sol#L316-L318. Per Stargate documentation https://stargateprotocol.gitbook.io/stargate/developers/how-to-swap , the address of the swap need to casted to bytes by using abi.encodePacked and not abi.encode, casting which is done correctly in the _sendNative function https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/Balancer.sol#L291 . The big difference between abi.encodePacked and abi.encode is that abi.encode will fill the remaining 12 bytes of casting a 20 bytes address with 0 values. Here is an example of casting the address 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
bytes normalAbi = 0x0000000000000000000000005b38da6a701c568545dcfcb03fcb875f56beddc4;
bytes packedAbi = 0x5b38da6a701c568545dcfcb03fcb875f56beddc4;
This will hurt the whole logic of the swap since when the lzReceive function on the Bridge.sol contract from Startgate will be called, the address where the funds will be sent will be a wrong address. As you can see here the lzReceive on Bridge.sol for Abitrum for example uses assembly to load 20 bytes of the payload to the toAddress https://arbiscan.io/address/0x352d8275aae3e0c2404d9f68f6cee084b5beb3dd#code#F1#L88 which in our case, for the address that I provided as an example it would be
toAddress = 0x0000000000000000000000005b38Da6A701c5685;
because abi.encode was used instead of abi.ecnodePacked. Then it will try to swap the tokens to this address, by calling sgReceive on it, which will not exist in most of the case and the assets will be lost, as specified by Stargate documentation https://stargateprotocol.gitbook.io/stargate/composability-stargatecomposed.sol
Recommended Mitigation Steps
Use abi.encodePacked instead of abi.encode on _sendToken, same as the protocol does in _sendNative, so the assumptions will be correct.
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 use of abi.encode instead of abi.encodePacked for encoding the destination address, which adds padding zeros and causes the address to be misinterpreted on the destination chain.
Impact
The underlying ERC20 tokens could be sent to a random address and lost.
What the attacker needed
Owner calls rebalance; cross-chain transfer to destination chain
The fix
Use abi.encodePacked instead of abi.encode in _sendToken, as done in _sendNative.
How it was found
- The owner calls rebalance, which invokes _sendToken.
- _sendToken calls Stargate's router swap with the destination address encoded using abi.encode.
- The cross-chain message is received on the destination chain, and lzReceive loads 20 bytes from the payload as the toAddress.
- Because abi.encode padded the address with zeros, the loaded address is incorrect.
- The tokens are sent to the wrong address and lost.
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.
When sending data between blockchains, addresses must be packed tightly without extra zeros. This code added extra zeros, so the receiving contract read the wrong address and sent tokens to a place where they could not be recovered.
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.