Incorrect flag results to _hasFallbackToggled always set to false on createMultipleSettlement .
The function that builds a multi-asset settlement payload used a bitwise AND that always produced the same flag byte, so the user's fallback preference was ignored. As a result, the destination chain always saw the fallback as disabled, which could cause unexpected behaviour and integration issues.
Submitted by kodyvim, also found by minhtrng, QiuhaoLi, ast3ros, bin2chen, 0xnev, nobody2018, jasonxiale, Kow, ayden, chaduke, and SpicyMeatball
Function _hasFallbackToggled would be set to false on createMultipleSettlement regardless of user intentions.
Proof of Concept
Users can specify if they want a fallback on their transaction, which prevents the transaction from reverting in case of failure. But due to an incorrect flag, this would always be set to false.
https://github.com/code-423n4/2023-09-maia/blob/main/src/RootBridgeAgent.sol#L1090
function _createSettlementMultiple(
uint32 _settlementNonce,
address payable _refundee,
address _recipient,
uint16 _dstChainId,
address[] memory _globalAddresses,
uint256[] memory _amounts,
uint256[] memory _deposits,
bytes memory _params,
bool _hasFallbackToggled
) internal returns (bytes memory _payload) {
...SNIP
// Prepare data for call with settlement of multiple assets
_payload = abi.encodePacked(
@> _hasFallbackToggled ? bytes1(0x02) & 0x0F : bytes1(0x02),
_recipient,
uint8(hTokens.length),
_settlementNonce,
hTokens,
tokens,
_amounts,
_deposits,
_params
);
...SNIP
}
The variable _hasFallbackToggled can be set to true or false depending whether the user wants a fallback or not.
If true, the value at the payload index 0 (payload [0]) would be set to bytes1(0x02) & 0x0F but this would still results to bytes1(0x02); otherwise, false this would also results to bytes1(0x02).
On the destination chain, to check for the fallback status of a transaction:
https://github.com/code-423n4/2023-09-maia/blob/main/src/BranchBridgeAgent.sol#L651
function lzReceiveNonBlocking(address _endpoint, bytes calldata _srcAddress, bytes calldata _payload)
public
override
requiresEndpoint(_endpoint, _srcAddress)
{
...SNIP
// DEPOSIT FLAG: 2 (Multiple Settlement)
} else if (flag == 0x02) {
// Parse recipient
address payable recipient = payable(address(uint160(bytes20(_payload[PARAMS_START:PARAMS_START_SIGNED]))));
// Parse deposit nonce
nonce = uint32(bytes4(_payload[22:26]));
//Check if tx has already been executed
if (executionState[nonce] != STATUS_READY) revert AlreadyExecutedTransaction();
//Try to execute remote request
// Flag 2 - BranchBridgeAgentExecutor(bridgeAgentExecutorAddress).executeWithSettlementMultiple(recipient, localRouterAddress, _payload)
_execute(
@> _payload[0] == 0x82,
nonce,
recipient,
abi.encodeWithSelector(
BranchBridgeAgentExecutor.executeWithSettlementMultiple.selector,
recipient,
localRouterAddress,
_payload
)
);
...SNIP
}
_payload[0] == 0x82 would always be false; irrespective of the fallback status chosen by the user.
A simple test with chisel:
➜ function checkToggle(bool hastoggle) public returns(bytes memory _payload) {
_payload = abi.encodePacked(hastoggle ? bytes1(0x02) & 0x0F : bytes1(0x02));
}
➜ function test() public returns(bool) {
bytes memory payload = checkToggle(true);
return payload[0] == 0x82;
}
➜ bool check = test()
➜ check
Type: bool
└ Value: false//<@ should be true
➜ function test2() public returns(bool) {
bytes memory payload = checkToggle(false);
return payload[0] == 0x82;
}
➜ check = test2()
Type: bool
└ Value: false//<@ always false
This would result to unexpected behaviors and issues with integrations.
Recommended Mitigation Steps
Change the following line to:
function _createSettlementMultiple(
uint32 _settlementNonce,
address payable _refundee,
address _recipient,
uint16 _dstChainId,
address[] memory _globalAddresses,
uint256[] memory _amounts,
uint256[] memory _deposits,
bytes memory _params,
bool _hasFallbackToggled
) internal returns (bytes memory _payload) {
// Check if valid length
if (_globalAddresses.length > MAX_TOKENS_LENGTH) revert InvalidInputParamsLength();
// Check if valid length
if (_globalAddresses.length != _amounts.length) revert InvalidInputParamsLength();
if (_amounts.length != _deposits.length) revert InvalidInputParamsLength();
//Update Settlement Nonce
settlementNonce = _settlementNonce + 1;
// Create Arrays
address[] memory hTokens = new address[](_globalAddresses.length);
address[] memory tokens = new address[](_globalAddresses.length);
for (uint256 i = 0; i < hTokens.length;) {
// Populate Addresses for Settlement
hTokens[i] = IPort(localPortAddress).getLocalTokenFromGlobal(_globalAddresses[i], _dstChainId);
tokens[i] = IPort(localPortAddress).getUnderlyingTokenFromLocal(hTokens[i], _dstChainId);
// Avoid stack too deep
uint16 destChainId = _dstChainId;
// Update State to reflect bridgeOut
_updateStateOnBridgeOut(
msg.sender, _globalAddresses[i], hTokens[i], tokens[i], _amounts[i], _deposits[i], destChainId
);
unchecked {
++i;
}
}
// Prepare data for call with settlement of multiple assets
_payload = abi.encodePacked(
- _hasFallbackToggled ? bytes1(0x02) & 0x0F : bytes1(0x02),
+ _hasFallbackToggled ? bytes1(0x82) : bytes1(0x02),
_recipient,
uint8(hTokens.length),
_settlementNonce,
hTokens,
tokens,
_amounts,
_deposits,
_params
);
// Create and Save Settlement
// Get storage reference
Settlement storage settlement = getSettlement[_settlementNonce];
// Update Settlement
settlement.owner = _refundee;
settlement.recipient = _recipient;
settlement.hTokens = hTokens;
settlement.tokens = tokens;
settlement.amounts = _amounts;
settlement.deposits = _deposits;
settlement.dstChainId = _dstChainId;
settlement.status = STATUS_SUCCESS;
}
Assessed type
Error
Addressed 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 a bitwise AND with 0x0F was used instead of a direct assignment, so the ternary always produced the same byte value.
Impact
The fallback flag was always false on the destination chain, which could lead to unexpected behaviour and issues with integrations.
What the attacker needed
User calls createMultipleSettlement; user sets fallback to true
The fix
The ternary should set the flag byte to 0x82 when the fallback is toggled, and the report notes the issue was addressed in a later commit.
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.
A fallback option lets a user say 'if my transaction fails, don't revert it'. The code that builds the message to the other chain was supposed to set a flag byte to a special value when the user wanted a fallback, but a mistake meant the byte was always the same. So the other chain never knew the user had asked for a fallback.
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.