Spearbit Profile picture
Jun 14 20 tweets 7 min read Twitter logo Read on Twitter
“clones-with-immutable-args” is a sleek proxy pattern but…

What would a critical bug involving it look like?

@SpearbitDAO engaged @sudoswap V1 in 2022 for a security assessment and identified a critical bug in its’ contract pair validation process.

Let's break it down ⬇️
This vulnerability arises from the inadequate validation of smart contract clones in Sudoswap's "isPair()" function shown below.

The function performs the inadequate validation of contract clones via calls to the "isETH20PairClone()" and "isERC20PairClone" functions: Image
In "isETHPairClone()":

The `extcodecopy` function is used to get the bytecode of the queried contract. The first 54 bytes (`0x36` in hex) of the contract at the `query` address are copied to `other` which is located at `ptr + 0x40`: Image
The `result` is computed as the logical AND of the equality of two parts of the data. If the bytecode and the additional data stored in `ptr` match the corresponding data in `other`, `result` will be true, meaning that the queried contract is a valid clone: Image
What is wrong with this validation?

The vulnerability arises from the fact that the function is only checking the first `0x36` (or 54) bytes of the contract code. While this is intended to verify the 'signature' of the contract, it opens a potential attack vector.
The parameters provided in `cloneETHPair()` are directly fed into the contract.

If malicious values are supplied, these can bypass the validation process.
The cloned contract may appear legitimate but can possess a malicious payload.

Let's take a look at an example exploit ⬇️
First, we create a malicious pair by making a copy of the source of "cloneETHPair()" with malicious values for `factory`, `bondingCurve`, `nft` and `poolType` using a valid template for the connected contract and the proxy code.

However, the remaining parameters are malicious: Image
The attacker the initializes the new `Pair` via a copy of "initialize()" which calls "__Ownable_init()" thereby setting a malicious owner: Image
The malicious owner then calls the "call()" function, with the router as the target and the `calldata` for the "pairTransferERC20From()" function.

Since the checks for `onlyOwner` and `require` pass, "pairTransferERC20From()" is called with the malicious `Pair` as msg.sender: Image
The above function first gets the factory contract, checks if calling the target is allowed (it is because of insufficient check), and then calls the target. The router as the target contract will check if it is called from a valid pair via "isPair()": Image
Now, in the `pairTransferERC20From()` function, there is a check if it's being called from a valid pair by calling the `isPair()` function. The malicious pair passes this test due to the insufficient check of the first 54 bytes, allowing transfer of tokens.
Here, "isPair()" checks if the potential pair is an `enumerableETHTemplate` clone by comparing only the first 54 bytes (runtime code including implementation address).

As a result, it does not check for extra parameters `factory`, `bondingCurve`, `nft` or `poolType`: Image
Why is this critical?

Due to the malicious pair now being considered valid, the `require` statement in "pairTransferERC20From()" has passed and tokens can be transferred to the attacker from anyone who has set an allowance for the router.
This issue is now fixed.

Spearbit recommended Sudoswap to verify more values when checking if a pair is valid - especially the factory value and also recommend to consider the removal of all trust between pairs and routers, as well as the function "call()".
In the "isPair()" function within LSSVMPairFactory.sol the factory value is now passed into the validation functions as shown below in the following commit: Image
The changes in the validation code reflect an addition of the `factory` address parameter in the `isETHPairClone` and `isERC20PairClone` for functions for legitimacy and a subsequent modification in the assembly code for proper validation beyond 54 bytes of malicious parameters: Image
Previously, the `extcodecopy` function copied only the first 54 bytes of the contract's bytecode for comparison. Now, it copies the first 74 bytes, checking an additional 20 bytes of the bytecode to prevent an attacker from sneaking in a malicious payload.
The full security review was performed by:

- @gpersoon (LSR)
- @Mudit__Gupta (LSR)
- Max Goodman (SR)

As always, fantastic work to all involved. Keep up the great work! 🫡
This is only one of the many interesting vulnerabilities we have outlined in our report. You may view the full report located in our audits repository below:

github.com/spearbit/portf…
Interested in a security review by Spearbit?

Fill out our request form below to start working with the best security minded individuals our industry has to offer.

- Spearbit Security Review Request Form -
airtable.com/shrkxrtMKYJkLa…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Spearbit

Spearbit Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SpearbitDAO

Jun 16
Looking for some weekend learning alpha?

Spearbit's got you covered.

Here's a thread of content geared towards security researchers that want something more beyond the standard auditor roadmap, top 10 vulns, or basic find-the-bugs.

Let's dive in 🤿🧵
Our approach to security reviews is to leave no stone unturned. We encourage everyone to do the same in this space.

Check out this breakdown of a critical finding based on a dependency our researchers identified:

See our blog on Arbiter for a breakdown on an incredibly cool framework from @0xjepsen and @Autoparallel

Read 7 tweets
Jun 16
Verification - a core function of web3 development.

While simple, we should still ask ourselves-

Am I using proper verification patterns for:

- Senders?
- Signatures?
- Hashes?
- Tokens?
- ZKPs?

Let's dive into Spearbit Tip #4 - Verification Patterns w/ @NoahMarconi

🧵⬇️
For blockchain developers, verification is a key aspect in the creation of secure products and can be applied in various ways, such as the verification of:

• Balances
• Transaction History
• State
• Actions
• Attestations
• and much more
Public functions within smart contracts expose all of these verification aspects. However, for advanced applications, developers often employ a set of unique verification patterns.

Let's review some of these patterns below.

👇
Read 24 tweets
May 25
As part of the tip series, we will also be introducing tips for developers.

We believe education goes beyond just researchers and must extend to the core developer community to foster a security-first culture within our industry.

Today's tip is provided by @NoahMarconi

🧵⬇️ Image
Overview (1/2)

We will highlight the importance of proper code specification for developers and break down these key areas of focus before implementation:

1. Plain English
2. State Machines
3. Diagramming
4. Interfaces
5. Placeholder Tests
6. Formal Definitions (Optional)
Overview (2/2)

In common practice, stages one through five are iterated numerous times during the system design process. This iterative approach promotes efficiency relative to starting directly with implementation, and also aids in structuring a protocol for an efficient audit.
Read 20 tweets
May 23
Introducing the Spearbit Tip series:

We will be introducing weekly tips for security researchers in order to support knowledge sharing across the web3 security ecosystem and continue raising the bar in our industry.

Let's begin.

🧵⬇️ Image
When reviewing solidity code, oftentimes it can be helpful to take a peak under the hood to grasp a stronger understanding of what is occurring.

While this can be achieved through bytecode - it's much simpler to parse through optimized Yul assembly.
To illustrate this let's take a look at a simple function: Image
Read 10 tweets
May 16
Recently @SpearbitDAO engaged @MorphoLabs in a security assessment:

Spearbit researcher @StErMi, with guidance from @MorphoLabs, identified a 𝐜𝐫𝐢𝐭𝐢𝐜𝐚𝐥 bug in Morpho's implementation of Aave v3 and a lower risk 𝐧𝐨𝐯𝐞𝐥 bug in Aave v3 itself.

Let's break it down ⬇️🧵 Image
𝐂𝐨𝐧𝐭𝐞𝐱𝐭

It is important to understand that:

Atokens where LTV=0 cannot be borrowed against for any amount.

As a result, Aave inherently restricts the usage of certain operations when Atokens with LTV=0 are used as collateral. Image
Let's say a standard user (A) of Aave has an Atoken called SpearToken (SPT) in their account.

We will also assume the following:

- They have an arbitrary amount of SPT
- SPT has LTV=0
- SPT is set as collateral
Read 22 tweets
Mar 10
🎉 It's with great excitement that we announce our first-ever promotions within our community of security researchers at Spearbit!

Here's a story about how we got here 🧵
🗓️ In September 2022, we proposed a promotion path within our community of security researchers. We outlined four roles to display expertise in web3 security: JSR, ASR, SR, and LSR.
🔍 Junior Security Researcher (JSR): Someone with experience in web3 security and an IT background seeking to upskill through real-time audit experience by being paired with more advanced SRs and LSRs.
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(