Basin - MohammedRizwan's results

A composable EVM-native decentralized exchange protocol.

General Information

Platform: Code4rena

Start Date: 03/07/2023

Pot Size: $40,000 USDC

Total HM: 14

Participants: 74

Period: 7 days

Judge: alcueca

Total Solo HM: 9

Id: 259

League: ETH

Basin

Findings Distribution

Researcher Performance

Rank: 19/74

Findings: 2

Award: $169.01

QA:
grade-b

🌟 Selected for report: 0

šŸš€ Solo Findings: 0

Findings Information

🌟 Selected for report: tonisives

Also found by: Inspecktor, MohammedRizwan, Qeew, peanuts, sces60107

Labels

bug
2 (Med Risk)
satisfactory
duplicate-181

Awards

162.9427 USDC - $162.94

External Links

Lines of code

https://github.com/code-423n4/2023-07-basin/blob/9403cf973e95ef7219622dbbe2a08396af90b64c/src/Aquifer.sol#L34-L52

Vulnerability details

Impact

Aquifer.sol contract is a permissionless Well registry and factory. Aquifer.sol deploys Wells by cloning a pre-deployed Well implementation. It has boreWell() which is used to clone the well implementation by using Solady LibClone.sol functions like clone() and cloneDeterministic(). The conditions for use of these functions as commented in contract.

     * Use `salt == 0` to deploy a new Well with `create`
     * Use `salt > 0` to deploy a new Well with `create2`

boreWell() in Aquifer.sol is given as,

File: src/Aquifer.sol

34    function boreWell(
35        address implementation,
36        bytes calldata immutableData,
37        bytes calldata initFunctionCall,
38        bytes32 salt
39    ) external nonReentrant returns (address well) {
40        if (immutableData.length > 0) {
41            if (salt != bytes32(0)) {
42                well = implementation.cloneDeterministic(immutableData, salt);
43            } else {
44                well = implementation.clone(immutableData);
45            }
46        } else {
47            if (salt != bytes32(0)) {
48                well = implementation.cloneDeterministic(salt);
49            } else {
50                well = implementation.clone();
51            }
52        }

        // Some code

cloneDeterministic() function under the hood uses create2. This function has used salt which means that a malicious actor can prevent a user from deploying a boreWell creation by frontrunning it with the same "salt".

Additionally, In future if this protocol is being deployed on Multichains like optimism, arbitrum, polygon additional, more-critical vulnerabilities become possible via reorg attacks.

Proof of Concept

https://github.com/code-423n4/2023-07-basin/blob/9403cf973e95ef7219622dbbe2a08396af90b64c/src/Aquifer.sol#L34-L52

The "salt" is a bytes32 value that is used in boreWell creation call by the caller. This enables frontrunning to occur in the following way:

  • When the boreWell creation is called by user. An attacker monitors the mempool for pending transactions that involve cloning a contract with a provided "salt".
  • Upon spotting such a transaction, the attacker extracts the "salt" value.
  • The attacker quickly submits their own transaction with a higher gas price, attempting to clone the contract with the same "salt" before the original transaction is mined.
  • If the transaction got successful, the attacker's transaction is mined first, and the contract clone is created at the expected address.
  • The original transaction will likely fail, as the contract with the expected address has already been deployed.

Tools Used

Manual review

Use a salt that includes the msg.sender. That way it is not possible to front-run the transaction.




    function boreWell(
        address implementation,
        bytes calldata immutableData,
        bytes calldata initFunctionCall,
        bytes32 salt
    ) external nonReentrant returns (address well) {
        if (immutableData.length > 0) {
            if (salt != bytes32(0)) {
-                well = implementation.cloneDeterministic(immutableData, salt);
+                well = implementation.cloneDeterministic(keccak256(abi.encode(immutableData, salt, msg.sender)));

            } else {
                well = implementation.clone(immutableData);
            }
        } else {
            if (salt != bytes32(0)) {
-                well = implementation.cloneDeterministic(salt);
+                well = implementation.cloneDeterministic(keccak256(abi.encode(salt,msg.sender)));

            } else {
                well = implementation.clone();
            }
        }

       // Some code

Assessed type

Other

#0 - c4-pre-sort

2023-07-11T15:09:06Z

141345 marked the issue as duplicate of #217

#1 - c4-pre-sort

2023-07-12T16:03:01Z

141345 marked the issue as duplicate of #221

#2 - c4-pre-sort

2023-07-12T16:12:18Z

141345 marked the issue as duplicate of #181

#3 - 141345

2023-07-12T16:20:43Z

like https://github.com/code-423n4/2023-07-basin-findings/issues/159

no clear path of losing and how reorg attacks can cause harm

maybe partial

#4 - c4-judge

2023-08-04T05:57:34Z

alcueca marked the issue as satisfactory

Summary

Low Risk Issues

NumberIssueInstances
[L‑01]draft-ERC20PermitUpgradeable is deprecated by openzeppelin1
[L‑02]Violation of Checks, Effect and Interactions pattern in Well.sol1
[L‑03]Use latest version of openzeppelin library1

[L‑01] draft-ERC20PermitUpgradeable is deprecated by openzeppelin

Openzeppelin has deprecated the use of draft-ERC20PermitUpgradeable in v4.9.0 release. Check out the deprecations here.

There is 1 instance of this issue:

Use ERC20PermitUpgradeable.sol.

[L‑02] Violation of Checks, Effect and Interactions pattern

It is always recommended to follow Checks, Effect and Interactions(CEI) pattern in contracts to prevent re-entrancy attacks.

There is 1 instance of this issue:

In Well.sol at L-371.

For example:


        if (amountOut >= minAmountOut) {
-           tokenOut.safeTransfer(recipient, amountOut);
            reserves[j] -= amountOut;
+           tokenOut.safeTransfer(recipient, amountOut);

        // Some code

[L‑03] Use latest version of openzeppelin library

The contracts has used old version of OZ library. Library contracts security should be upto date. Use the latest version of OZ library i.e v4.9.2.

#0 - c4-pre-sort

2023-07-13T14:50:30Z

141345 marked the issue as high quality report

#1 - c4-sponsor

2023-08-02T20:43:06Z

publiuss marked the issue as sponsor confirmed

#2 - c4-judge

2023-08-04T21:19:55Z

alcueca marked the issue as grade-b

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax Ā© 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter