hats-finance / AlephZeroAMM-0x0d88a9ece90994ecb3ba704730819d71c139f60f

Apache License 2.0
1 stars 0 forks source link

Limited Functionality Due to Default Reentrancy Lock in ink! Pair Contract. #35

Open hats-bug-reporter[bot] opened 9 months ago

hats-bug-reporter[bot] commented 9 months ago

Github username: @0xmahdirostami Twitter username: 0xmahdirostami Submission hash (on-chain): 0x8ab813928c2f33a6c444369efda328c6b30d66e814b93504197b0d89437057c8 Severity: medium

Description:

Description

The ink! implementation has a default reentrancy lock, resulting in the automatic locking of all functions within the pair contract. In the Solidity version of UniswapV2, reentrancy is selectively applied to certain functions (mint, swap, skim, sync, burn), while other functions remain callable. However, in the Rust implementation, all functions are locked by default.

Impact

The reentrancy lock imposes limitations on the functionality of the pair contract, leading to temporary inaccessibility and a potential Denial of Service (DOS) scenario.

Affected Functions

Functions that change the state and might be impacted include:

Getter functions that may be affected:

Proof of Concept

In the swap function, there is a callback to the to address, causing all subsequent calls to the pair contract to be locked.

This renders some functionalities temporarily unusable.

if let Some(data) = data {
    // Call the callback.
    let mut swap_callee: contract_ref!(SwapCallee) = to.into();
    swap_callee.swap_call(self.env().caller(), amount_0_out, amount_1_out, data);
}

Scenario

For readonly functions, Bob wants to get a flash loan from the contract. In the callback, he might need the price or balance of one of the tokens, but the transaction will revert. Bob is forced to read all states before the callback (which is not the case in the Solidity implementation).

For other functions, if Bob wants to get one of the tokens in the contract and then use that token with the pair token for a farming strategy or other strategies, the transaction reverts. Users aren't able to use their LP token in the callback.

Revised Code File (Optional)

Consider allowing reentrancy in the contract and selectively lock only specific functions (mint, swap, skim, sync, burn) to address the issue.

deuszx commented 9 months ago

Thank you for participation. After carefully reviewing the submission we've concluded that this issue is INVALID.

The submission correctly points out the lack of reentrancy into the underlying LP token. This was a conscious design choice - we trade flexibility of the contract (under very particular scenario) for overall security. Reentrancy has been a source of nasty bugs and we agree with ink!'s design choice to disallow for it by default.

We hope you participate in the future audits of ink!.