Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WP-H22] Attacker can deposit from and to the same chainId to steal from the incentivePool #144

Closed
code423n4 opened this issue Mar 16, 2022 · 2 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate This issue or pull request already exists

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-03-biconomy/blob/db8a1fdddd02e8cc209a4c73ffbb3de210e4a81a/contracts/hyphen/LiquidityPool.sol#L149-L173

Vulnerability details

https://github.com/code-423n4/2022-03-biconomy/blob/db8a1fdddd02e8cc209a4c73ffbb3de210e4a81a/contracts/hyphen/LiquidityPool.sol#L149-L173

function depositErc20(
    uint256 toChainId,
    address tokenAddress,
    address receiver,
    uint256 amount,
    string memory tag
) public tokenChecks(tokenAddress) whenNotPaused nonReentrant {
    require(
        tokenManager.getDepositConfig(toChainId, tokenAddress).min <= amount &&
            tokenManager.getDepositConfig(toChainId, tokenAddress).max >= amount,
        "Deposit amount not in Cap limit"
    );
    require(receiver != address(0), "Receiver address cannot be 0");
    require(amount != 0, "Amount cannot be 0");
    address sender = _msgSender();

    uint256 rewardAmount = getRewardAmount(amount, tokenAddress);
    if (rewardAmount != 0) {
        incentivePool[tokenAddress] = incentivePool[tokenAddress] - rewardAmount;
    }
    liquidityProviders.increaseCurrentLiquidity(tokenAddress, amount);
    SafeERC20Upgradeable.safeTransferFrom(IERC20Upgradeable(tokenAddress), sender, address(this), amount);
    // Emit (amount + reward amount) in event
    emit Deposit(sender, tokenAddress, receiver, toChainId, amount + rewardAmount, rewardAmount, tag);
}

https://github.com/code-423n4/2022-03-biconomy/blob/db8a1fdddd02e8cc209a4c73ffbb3de210e4a81a/contracts/hyphen/LiquidityPool.sol#L242-L261

function depositNative(
    address receiver,
    uint256 toChainId,
    string memory tag
) external payable whenNotPaused nonReentrant {
    require(
        tokenManager.getDepositConfig(toChainId, NATIVE).min <= msg.value &&
            tokenManager.getDepositConfig(toChainId, NATIVE).max >= msg.value,
        "Deposit amount not in Cap limit"
    );
    require(receiver != address(0), "Receiver address cannot be 0");
    require(msg.value != 0, "Amount cannot be 0");

    uint256 rewardAmount = getRewardAmount(msg.value, NATIVE);
    if (rewardAmount != 0) {
        incentivePool[NATIVE] = incentivePool[NATIVE] - rewardAmount;
    }
    liquidityProviders.increaseCurrentLiquidity(NATIVE, msg.value);
    emit Deposit(_msgSender(), NATIVE, receiver, toChainId, msg.value + rewardAmount, rewardAmount, tag);
}

In the current implementation, users are allowed to set toChainId when depositErc20() or depositNative(), when there are some incentive rewards, the user can get more tokens on toChainId.

However, there is no check that prevents the toChainId to be the current chainId, so that when the incentive rewards are high enough, an attacker or a malicious user can set the toChainId to the current chainId and steal the incentive rewards without helping the protocol to liquidity to the current chain, instead, it actually lowers the liquidity.

PoC

Given:

  • Balance of contract on Mainnet = 10 ETH
  • incentivePool[NATIVE] = 1 ETH
  • rewardAmount for 1 ETH is 0.01 ETH
  1. Alice depositNative() on Mainnet with amount = 1 ETH, toChainId = 1, receiver = Alice
  • event Deposit(alice, NATIVE, alice, 1, 1.01 ETH, 0.01 ETH, tag) emitted
  • Balance of contract on Mainnet = 11 ETH
  • Alice paid 1 ETH
  1. Executor call sendFundsToUser() on Mainnet, amount = 1.01 ETH
  • Balance of contract on Mainnet = 9.99 ETH
  • Alice got 1.01 ETH back

Recommendation

Consider adding checks to require toChainId != current chainId.

@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Mar 16, 2022
code423n4 added a commit that referenced this issue Mar 16, 2022
@ankurdubey521
Copy link
Collaborator

Duplicate of #87

@ankurdubey521 ankurdubey521 marked this as a duplicate of #87 Mar 30, 2022
@ankurdubey521 ankurdubey521 added the duplicate This issue or pull request already exists label Mar 30, 2022
@pauliax
Copy link
Collaborator

pauliax commented Apr 28, 2022

#87

@pauliax pauliax added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants