Skip to content

Commit

Permalink
Address finding 5
Browse files Browse the repository at this point in the history
  • Loading branch information
lastperson committed Sep 9, 2024
1 parent 9114518 commit f971a2b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
7 changes: 5 additions & 2 deletions contracts/handlers/DepositDataHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import "../interfaces/ISygmaMessageReceiver.sol";
@author ChainSafe Systems.
*/
contract DepositDataHelper is ERCHandlerHelpers {
using SanityChecks for *;

address public immutable _defaultMessageReceiver;
uint16 internal constant maxReturnBytes = 256;
address internal constant transformRecipient = address(0);
Expand Down Expand Up @@ -54,13 +56,14 @@ contract DepositDataHelper is ERCHandlerHelpers {
uint256 lenDestinationRecipientAddress;

(amount, lenDestinationRecipientAddress) = abi.decode(data, (uint256, uint256));
address recipientAddress = address(bytes20(bytes(data[64:64 + lenDestinationRecipientAddress])));
lenDestinationRecipientAddress.mustBe(20);
address recipientAddress = address(bytes20(bytes(data[64:84])));

address tokenAddress = _resourceIDToTokenContractAddress[resourceID];
uint256 externalAmount = convertToExternalBalance(tokenAddress, amount);

// Optional message recipient transformation.
uint256 pointer = 64 + lenDestinationRecipientAddress;
uint256 pointer = 84;
uint256 gas;
uint256 messageLength;
bytes memory message;
Expand Down
1 change: 1 addition & 0 deletions contracts/handlers/ERC1155Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ contract ERC1155Handler is IHandler, ERCHandlerHelpers, ERC1155Safe, ERC1155Hold

(tokenIDs, amounts, recipient, transferData) = abi.decode(data, (uint[], uint[], bytes, bytes));

recipient.length.mustBe(20);
bytes20 recipientAddress;

assembly {
Expand Down
3 changes: 2 additions & 1 deletion contracts/handlers/ERC721Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ contract ERC721Handler is IHandler, ERCHandlerHelpers, ERC721Safe {
bytes memory metaData;

(tokenID, lenDestinationRecipientAddress) = abi.decode(data, (uint, uint));
offsetMetaData = 64 + lenDestinationRecipientAddress;
lenDestinationRecipientAddress.mustBe(20);
offsetMetaData = 84;
destinationRecipientAddress = bytes(data[64:offsetMetaData]);
lenMetaData = abi.decode(data[offsetMetaData:], (uint));
metaData = bytes(data[offsetMetaData + 32:offsetMetaData + 32 + lenMetaData]);
Expand Down
6 changes: 6 additions & 0 deletions contracts/handlers/GmpHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
pragma solidity 0.8.11;

import "../interfaces/IHandler.sol";
import "../utils/SanityChecks.sol";

/**
@title Handles generic deposits and deposit executions.
@author ChainSafe Systems.
@notice This contract is intended to be used with the Bridge contract.
*/
contract GmpHandler is IHandler {
using SanityChecks for *;

uint256 public constant MAX_FEE = 1000000;

address public immutable _bridgeAddress;
Expand Down Expand Up @@ -165,10 +168,13 @@ contract GmpHandler is IHandler {

maxFee = uint256(bytes32(data[:pointer += 32]));
lenExecuteFuncSignature = uint16(bytes2(data[pointer:pointer += 2]));
lenExecuteFuncSignature.mustBe(4);
executeFuncSignature = bytes4(data[pointer:pointer += lenExecuteFuncSignature]);
lenExecuteContractAddress = uint8(bytes1(data[pointer:pointer += 1]));
lenExecuteContractAddress.mustBe(20);
executeContractAddress = address(uint160(bytes20(data[pointer:pointer += lenExecuteContractAddress])));
lenExecutionDataDepositor = uint8(bytes1(data[pointer:pointer += 1]));
lenExecutionDataDepositor.mustBe(20);
executionDataDepositor = address(uint160(bytes20(data[pointer:pointer += lenExecutionDataDepositor])));
executionData = bytes(data[pointer:]);

Expand Down
3 changes: 2 additions & 1 deletion contracts/handlers/XC20Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ contract XC20Handler is IHandler, ERCHandlerHelpers, XC20Safe {
bytes memory destinationRecipientAddress;

(amount, lenDestinationRecipientAddress) = abi.decode(data, (uint, uint));
destinationRecipientAddress = bytes(data[64:64 + lenDestinationRecipientAddress]);
lenDestinationRecipientAddress.mustBe(20);
destinationRecipientAddress = bytes(data[64:84]);

bytes20 recipientAddress;
address tokenAddress = _resourceIDToTokenContractAddress[resourceID];
Expand Down
6 changes: 6 additions & 0 deletions contracts/utils/SanityChecks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ pragma solidity 0.8.11;
*/
library SanityChecks {
error ZeroAddress();
error UnexpectedValue(uint256 actual, uint256 expected);

function mustNotBeZero(address addr) internal pure returns(address) {
if (addr == address(0)) revert ZeroAddress();
return addr;
}

function mustBe(uint256 actual, uint256 expected) internal pure returns(uint256) {
if (actual != expected) revert UnexpectedValue(actual, expected);
return actual;
}
}

0 comments on commit f971a2b

Please sign in to comment.