
Smart Security Practices From The Best
What do Lido, Red Stone, YieldNest, and Braintrust have in common? They’ve developed effective methods for improving security without drastically increasing costs. Top-tier protocol […]
The threat scenario covered in this article is “malicious hook owner updates the oracle parameters resulting in invalid price“.
The example illustrating such a vulnerability was based on the EulerMedianOracle
hook that can be found here.
This article is one of a series where we present some implementations of “Bad Hooks” as part of our research supported by the Uniswap Foundation Grant.
All presented bad hooks are drawn from the extensive threat modeling sessions conducted throughout our research. For a deeper understanding, refer to our previous article detailing the possible threats originating from various use cases.
The EulerMedianOracle
hook contract implements an alternative to the TWAP oracle that saves the current tick on each swap. It calculates the price similarly to the median oracle created by the Euler protocol. This approach mitigates some security risks as when using TWAP: “lower-liquidity pools are prone to manipulation”.
Additionally, the hook has a function that allows you to get the median price within a given time period. Here you can find the original implementation: EulerMedianOracle.sol
For the purpose of this article, we have created an updated custom hook implementation, BadEulerMedianOracle
, with a backdoor that allows the hook owner to update the saved tick values. Here is the updated version: BadEulerMedianOracle.sol
The malicious version of the oracle hook contract contains the updatePriceTicks
function, which can be called only by the hook owner.
As you can see in the picture, the hook function allows the owner to update the ringBuffers
storage variable. This variable is later used in the readOracle
function to calculate the median price.
That said, simply the updatePriceTicks
function is a backdoor.
The PoC of the attack has been implemented in the test_abuser_read
test and can be found here: BadEulerMedianOracle.t.sol#L59.
Let’s imagine there is a lending protocol that uses this oracle to calculate the value of provided collateral when borrowing some other assets.
The goal of the attack is to manipulate the prices and make the collateral’s value higher, resulting in borrowing more assets and leaving the lending protocol with bad debt.
Step 1: The hook must be deployed and some legitimate swaps need to be executed to populate the oracle with some ticks. This is achieved with the createSwaps
function.
Step 2: The lending protocol gets the current price using the readOracle
function during a legitimate borrow operation. The price return is equal to 15 (the unit does not matter in this scenario).
Step 3: The malicious hook owner updates the ticks with arbitrary values leading to a higher price.
Step 4: The malicious hook owner adds the collateral covered by their to the lending protocol and borrows other assets. The protocol gets the current, manipulated price using the readOracle
function (the price is not over 10x higher) and allows the borrower to get more assets.
That simply means that the malicious hook owner can control the value of their collateral in the lending protocol.
It’s crucial to adhere to specific security measures when developing or integrating Uniswap V4 hooks. Below are key guidelines to ensure robust security in these processes.
Security Guidelines for Web3 Developers Constructing Hooks:
Security Recommendations for Developers Integrating Uniswap v4 Hooks with Critical-Level Values:
Whether you are building a Uniswap V4 hook or integrating with one, let us help you to make it secure!.
Composable Security 🇵🇱⛓️ is a Polish company specializing in increasing the security of projects based on smart contracts written in Solidity. Examples of projects that have trusted us are market leaders such as FujiDAO, Enjin, Volmex Finance, DIVA Protocol or Tellor. We are creators of the Smart Contract Security Verification Standard. Speakers at various conferences such as EthCC, ETHWarsaw, or OWASP AppSec EU. Authors of numerous publications on DeFi security. Experienced auditors operating in the IT Security space since 2016.
Meet Composable Security
Get throughly tested by the creators of Smart Contract Security Verification Standard
Let us help
Get throughly tested by the creators of Smart Contract Security Verification Standard