Halborn Logo

SDK - deBridge Cross Chain Swaps


Prepared by:

Halborn Logo

HALBORN

Last Updated 04/26/2024

Date of Engagement by: March 25th, 2022 - April 20th, 2022

Summary

80% of all REPORTED Findings have been addressed

All findings

5

Critical

0

High

0

Medium

0

Low

0

Informational

5


1. INTRODUCTION

deBridge engaged Halborn to conduct a security assessment on their smart contracts beginning on March 25th, 2022 and ending April 20th, 2022. deBridge a cross-chain interoperability and liquidity transfer protocol that allows truly decentralized transfer of assets between various blockchains. deBridge is a cross-chain interoperability and liquidity transfer protocol that allows decentralized transfer of assets between blockchains.

2. AUDIT SUMMARY

The team at Halborn was provided four weeks for the engagement and assigned one full-time security engineer to audit the security of the assets in scope. The engineer is a blockchain and smart contract security expert with advanced penetration testing, smart-contract hacking, and deep knowledge of multiple blockchain protocols.

The purpose of this audit is to achieve the following:

    • Identify potential security issues with the smart contracts.

3. TEST APPROACH & METHODOLOGY

Halborn performed a combination of manual and automated security testing to balance efficiency, timeliness, practicality, and accuracy with the scope of the smart contract audit. While manual testing is recommended to uncover flaws in logic, process, and implementation, automated testing techniques help enhance the smart contract code coverage and quickly identify items that do not follow security best practices. The following phases and associated tools were used throughout the term of the audit:

    • Research into architecture and purpose.

    • Smart contract manual code review and walkthrough

    • Graphing out functionality and contract logic/connectivity/functions(solgraph)

    • Manual testing of core functions through Hardhat and Ganache

    • Manual testing with custom scripts.

    • Static Analysis of security for scoped contract, and imported functions.(Slither)

    • Scanning of solidity files for vulnerabilities, security hotspots or bugs. (MythX)

    • Testnet deployment (Remix IDE)

4. SCOPE

The review was scoped to contracts and scripts in the following: master branch 9e3262cd63cd9d916c9de00f021543888f3568ad commit (cross-chain-swaps) master branch 340b77c551a8cde4800ebdc4665a580938816737 commit (cross-chain-sdk)

Smart contracts:

    • CrossChainForwarder.sol

    • ForwarderBase.sol

    • LPConnector.sol

    • ReceivingForwarder.sol

    • CalldataUtils.sol

    • Flags.sol

    • SignatureUtil.sol

    • SwapCalldataUtils.sol

SDK:

    • Chain.ts

    • CrossChainForwardingService.ts

    • CrossChainPathFindingService.ts

    • CrossChainResolver.ts

    • DePair.ts

    • DePairsCollection.ts

    • Icons.ts

    • RecommendExecutionFeeService.ts

    • SDK.ts

    • SDKError.ts

    • SwapBuildingService.ts

5. RISK METHODOLOGY

Vulnerabilities or issues observed by Halborn are ranked based on the risk assessment methodology by measuring the LIKELIHOOD of a security incident and the IMPACT should an incident occur. This framework works for communicating the characteristics and impacts of technology vulnerabilities. The quantitative model ensures repeatable and accurate measurement while enabling users to see the underlying vulnerability characteristics that were used to generate the Risk scores. For every vulnerability, a risk level will be calculated on a scale of 5 to 1 with 5 being the highest likelihood or impact.
RISK SCALE - LIKELIHOOD
  • 5 - Almost certain an incident will occur.
  • 4 - High probability of an incident occurring.
  • 3 - Potential of a security incident in the long term.
  • 2 - Low probability of an incident occurring.
  • 1 - Very unlikely issue will cause an incident.
RISK SCALE - IMPACT
  • 5 - May cause devastating and unrecoverable impact or loss.
  • 4 - May cause a significant level of impact or loss.
  • 3 - May cause a partial impact or loss to many.
  • 2 - May cause temporary impact or loss.
  • 1 - May cause minimal or un-noticeable impact.
The risk level is then calculated using a sum of these two values, creating a value of 10 to 1 with 10 being the highest level of security risk.
Critical
High
Medium
Low
Informational
  • 10 - CRITICAL
  • 9 - 8 - HIGH
  • 7 - 6 - MEDIUM
  • 5 - 4 - LOW
  • 3 - 1 - VERY LOW AND INFORMATIONAL

6. SCOPE

Out-of-Scope: New features/implementations after the remediation commit IDs.

7. Assessment Summary & Findings Overview

Critical

0

High

0

Medium

0

Low

0

Informational

5

Impact x Likelihood

HAL-01

HAL-02

HAL-03

HAL-04

HAL-05

Security analysisRisk levelRemediation Date
SAME PAIR OF TOKENS CAN BE USEDInformationalNot Applicable
MISSING PAUSEABLE FUNCTIONALITYInformationalNot Applicable
UNUSED RETURN VALUESInformational-
COGNITIVE COMPLEXITY OF FUNCTION IS TOO HIGHInformationalAcknowledged
POSSIBLE MISUSE OF PUBLIC FUNCTIONSInformationalSolved - 12/05/2022

8. Findings & Tech Details

8.1 SAME PAIR OF TOKENS CAN BE USED

// Informational

Description

It is observed that in forward() and swapAndSend() functions inside ReceivingForwarder.sol and in CrosschainForwarder.sol does not check if source token type equals to destination token type and accepts same pair of tokens as input.

This type of bugs may lead to critical attack vectors or griefing attacks.

Code Location

ReceivingForwarder.sol

  • forward

ReceivingForwarder.sol

    function forward(
        address _dstTokenIn,
        address _router,
        bytes memory _routerCalldata,
        address _dstTokenOut,
        address _fallbackAddress
    ) external payable override{
        if (_dstTokenIn == NATIVE_TOKEN) {
            return _forwardFromETH(
                _router,
                _routerCalldata,
                _dstTokenOut,
                _fallbackAddress
            );
        }
        else {
            return _forwardFromERC20(
                IERC20Upgradeable(_dstTokenIn),
                _router,
                _routerCalldata,
                _dstTokenOut,
                _fallbackAddress
            );

CrosschainForwarder.sol

  • swapAndSend

LPConnector.sol

    function swapAndSend(
        address _srcTokenIn,
        uint _srcAmountIn,
        bytes memory _srcTokenInPermit,
        address _srcSwapRouter,
        bytes calldata _srcSwapCalldata,
        address _srcTokenOut,
        bytes calldata _dstDetails
    ) external payable override {
        if (!supportedRouters[_srcSwapRouter]) revert NotSupportedRouter();

        uint ethBalanceBefore = address(this).balance - msg.value;
        uint srcAmountOut;

        if (_srcTokenIn == NATIVE_TOKEN) {
            _validateSrcETHIn(_srcAmountIn);
            srcAmountOut = _swapToERC20Via(
                _srcSwapRouter,
                _srcSwapCalldata,
                _srcAmountIn,
                IERC20Upgradeable(_srcTokenOut)
            );
        }
        else {
            ...

In the Proof of Concept below, we created a test script which gives same input token type for source and destination.

Proof of concept

    it('Same pair of tokens', async () => {
      await state.usdToken.instance.mint(state.user.address,usd2v(10));
      console.log(await state.usdToken.instance.balanceOf(state.user.address));

      await state.receivingForwarder.instance.connect(state.user)
        .forward(
          state.usdToken.instance.address, // _wrappedToken
          state.dex.instance.address, // _router
          SAMPLE_CALLDATA.swap.sample, // _routerCalldata
          state.usdToken.instance.address, // _targetToken
          state.user.address, // _fallbackAddress
        );

        console.log(await state.usdToken.instance.balanceOf(state.user.address));
    });
Score
Impact: 1
Likelihood: 1
Recommendation

NOT APPLICABLE: The issue is marked as not applicable by the DeBridge team with an explanation: Both CrosschainForwarder and ReceivingForwarder work as relayers: they take tokenIn from msg.sender, swap it to tokenOut via the swapRouter, then pass the resulting tokenOut along with deBridgeGate or the destination address. It seems that there is nothing wrong with the situation when both tokens are the same. Moreover, this opens broad possibilities for arbitrage.

8.2 MISSING PAUSEABLE FUNCTIONALITY

// Informational

Description

Even code that has been thoroughly audited and tested may contain bugs or defective code parts. These flaws are frequently undetected until they are employed in an attack by an opponent. Because immutability is one of the basic characteristics of the blockchain, it is difficult to correct if a critical fault is discovered. While some patterns (such as the Proxy Delegate pattern) allow for upgradeable code to some extent, these solutions normally take a long time to implement and come into action. Before the update is transmitted to the network, the attackers could continue with their malicious actions and cause harm.

Score
Impact: 1
Likelihood: 1
Recommendation

NOT APPLICABLE: The issue is marked as not applicable by the DeBridge team with an explanation: ReceivingForwarder and CrosschainForwarder communicate with the deBridgeGate contract. If we ever need to stop the contracts, we can either stop deBridgeGate or quickly upgrade the proxy to a fixed or paused implementation.

8.3 UNUSED RETURN VALUES

// Informational

Description
Finding description placeholder
Score
Impact:
Likelihood:

8.4 COGNITIVE COMPLEXITY OF FUNCTION IS TOO HIGH

// Informational

Description

Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

CrossChainPathFindingService.ts

  • refreshDst
Score
Impact: 1
Likelihood: 1
Recommendation

ACKNOWLEDGED: The DeBridge team acknowledged this finding.

8.5 POSSIBLE MISUSE OF PUBLIC FUNCTIONS

// Informational

Description

In public functions, array arguments are immediately copied to memory, while external functions can read directly from calldata. Reading calldata is cheaper than memory allocation. Public functions need to write the arguments to memory because public functions may be called internally. Internal calls are passed internally by pointers to memory. Thus, the function expects its arguments being located in memory when the compiler generates the code for an internal function. Furthermore, methods do not necessarily have to be public if they are only called within the contract-in such case they should be marked internal.

Code Location

LPConnector.sol

  • getUnwrapToken

LPConnector.sol

    function getUnwrapToken(address _wrappedToken) public view returns (address) {
        return pools[_wrappedToken].jToken;
    }
Score
Impact: 1
Likelihood: 1
Recommendation

SOLVED: The issue was solved by the DeBridge team.

Halborn strongly recommends conducting a follow-up assessment of the project either within six months or immediately following any material changes to the codebase, whichever comes first. This approach is crucial for maintaining the project’s integrity and addressing potential vulnerabilities introduced by code modifications.

© Halborn 2024. All rights reserved.