Platform: Code4rena
Start Date: 27/11/2023
Pot Size: $60,500 USDC
Total HM: 7
Participants: 72
Period: 7 days
Judge: Picodes
Total Solo HM: 2
Id: 309
League: ETH
Rank: 35/72
Findings: 1
Award: $54.88
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: Sathish9098
Also found by: 0xAadi, 0xHelium, 0xSmartContract, Bulletprime, K42, Raihan, ZanyBonzy, catellatech, fouzantanveer, foxb868, tala7985
The panoptic protocol is a decentralized and permissionless options trading protocol built on Uniswap v3, not the conventional type that involves trading contracts that give the buyer the right, but not the obligation, to buy or sell an underlying asset at a specified price on or before a specified date. The underlying asset can be a stock, a bond, a commodity, a currency, a market index, or an interest rate. The panoptic protocol,uses Liquidity Provider (LP) positions in Uniswap v3 as a fundamental building block for trading long and short options. The protocol is basically a gas eficient alternative to Uniswap’s NonFungiblePositionManager
that manages complex, multi-leg Uniswap positions encoded in ERC1155 tokenIds
, performs swaps allowing n' users such as; "Pro-tail",Retail Users,Market Makers and Uniswap v3 Liquidity Providers,Institutions, DAOs and protocol treasuries to mint positions with only one type of token, by enabling leveraged options trading on this token.
1.2 Core Contracts
SemiFungiblePositionManager.sol
The 'engine' of Panoptic - manages all Uniswap V3 positions in the protocol as well as being a more advanced, gas-efficient alternative to NFPM for Uniswap LPs, it supports the minting of both typical LP positions where liquidity is added to Uniswap and long options positions where Uniswap liquidity is burnt.
TokenId.sol
Implementation for the custom data type used in the SFPM and Panoptic to encode position data in 256-bit ERC1155 tokenIds - holds a pool identifier and up to four full position legs
Math.sol
Library containing all of the generic math functions like abs(), mulDiv, etc
ERC1155Minimal.sol
An implementation of the ERC1155 token standard, library from openzeppelin.
1.3 Special Features of the Panoptic Protocol Panoptic allows users to access new and improved features when options trading:
Codebase Approach Consistently following my pattern that has kept helping me detect low hanging fruits in terms of exposing vulnerabilities. 2.1 Audit Documentation and Scope
Initial step involved, was thoroghly examining audit documentation and scope
to grasp the audit’s functionalities and boundaries, so as to prioritise my efforts. It is worth highlighting the good quality of the README
for this audit, as it provided valuable insights and actionable guidance that greatly facilitated the onboarding process.
2.2 General insight on Options Trading Funtionality
A good amount of hour was put in gaining knowledge on how conventional options trading actually works, as the contrats code is basically an improved form of options trade, this helped me understand fully the problem panoptic protocol is trying to solve, the gap its trying to bridge, their goals and how the methods implemented not just work, but to what degree better than the conventional, it was important to review the long
and short
options proper implementation, what and where things can go wrong inorder to expose potential vulnerabilities.
2.3 Setup and Testing
Setting up test environment to execute forge test great,althoug fuzzing was difficult due to the complex nature of the code, though asked more questions and found ways to still run test as it greatly enhancing the efficiency of the auditing process. With a fully functional test tap at my disposal, which not only accelerate the testing of intricate concepts and potential vulnerabilities but i also gain insights into the developer’s expectations regarding all implementations.
2.4 Code review
The code review commenced with understanding the pattern” used to manage authorization and functionality accross the system. Thoroughly understanding this pattern made understanding the protocol contracts and its relations much smoother. Throughout this stage, I documented observations and raised questions concerning potential exploits without going too deep while uncovering the codes intent.
2.5 Threat Modelling
At this point I began formulating specific assumptions that, if compromised, could pose security risks to the system. This process aids me in determining the most effective exploitation strategies. Thoroughly examining and marking any doubtful or vulnerable areas within the protocol, diving deep into these areas, performing in-depth examinations, and subjecting them to rigorous testing to report these issues if they dont checkout.
2.6 Known Finding Read old audits and already known findings. Went through the bot races findings checking what was found so as to not report these issues again, while bearing in mind invariants and critial parts of functions that can lead to users lossing funds.
2.7 Report Issues I started with auditing the code base indepthly this way I started understanding line by line code and took the necessary notes to ask questions, marking out vulnerabilities and how they can be exploited, This assessment helps in evaluating whether these exploits can be strategically combined to create a more significant impact on the system’s security. In some cases, seemingly minor and moderate issues can compound to form a critical vulnerability when leveraged wisely. This has to be balanced with any risks that users may face.
Architecture & Recommendations This platform supports a well planned implementation,and as the team understands this, i must commend the considered various security measures and roles to mitigate potential risks. To highlight a few,
NonFungiblePositionManager.sol
(NFPM) smart contract from v3-periphery with the SemiFungiblePositionMananger.sol
(SFPM), which uses the ERC1155
interface to track positions within Uniswap v3. This approach reduces gas costs and makes the protocol more accessible to a wider range of users.While Panoptic's architecture is innovative and capital-efficient, it does have a few potential disadvantages to consider:
Codebase Commentary In as much as the documentation of this protocol provides a very streamlined understanding of its functionality, the general code base still needs to be totally comprehensive to strike the balance. Here are some suggestions as to things to consider so as to maximally improve the code; Code Organization: The code could be organized into smaller, more manageable contracts. This would make the code easier to understand, maintain, and audit. Commenting: While the code is generally well-commented, it could benefit from more comments explaining the logic behind certain operations, especially those involving complex mathematical operations or interactions with external contracts. Use of Assembly: The code uses inline assembly for certain operations. This is a more advanced feature of Solidity and should be used with caution, as incorrect use of assembly can lead to security vulnerabilities. Part of the code do not exactly follow the Solidity Style Guide, all these points highlighted really would make the codebase geerally stand out. Basically i think Code quality and documentation is very mature with an excuisite code commenting style.The test suite is pretty comprehensive and fuzz tests are a great way to complement the static tests.
Centralization risks
None, as the architecture is fully permissionless.
System Risk
Reentrancy Attacks: The beginReentrancyLock
and endReentrancyLoc
k functions are used to prevent reentrancy attacks. However, they are only used in the mintTokenizedPosition
and burnTokenizedPosition
functions. If these functions are called from another function within the contract, or from an external contract, reentrancy attacks could still occur. To mitigate this risk, the Checks-Effects-Interactions (CEI) pattern should be followed in all functions that make external calls. This pattern suggests that you should make all external calls at the end of the function, after you've made all changes to the state.
Incorrect Validation of Pool Initialization: In the initializeAMMPool
function, there's a check to see if the Uniswap v3 pool has been initialized. However, this check is done by comparing the address of the pool to the zero address. If the pool has not been initialized, this will always return false, and the function will always revert. This is not the correct way to check if a pool has been initialized. Instead, the function should return the pool's fee tier and tick spacing, and check if these values are valid.
Potential for Front-Running: The mintTokenizedPosition
and burnTokenizedPosition
functions allow users to specify a slippage limit for their trades. This could potentially be exploited by front-running, where a malicious actor observes a user's transaction and then submits their own transaction with a higher gas price to execute first. To mitigate this risk, the contract could implement a mechanism to detect and prevent front-running, such as a commit-reveal scheme.
Potential for Incorrect Token Transfers: The registerTokenTransfer
function updates user position data following a token transfer. However, it assumes that all tokens are transferred correctly, which may not always be the case. To mitigate this risk, the function should include additional checks to ensure that the transfer was successful and that the correct amount of tokens was transferred.
Potential for Incorrect Slippage Limits: In the _validateAndForwardToAMM function, the slippage limits are swapped if tickLimitLow > tickLimitHigh. This could potentially lead to incorrect behavior if the user provides a valid range of slippage limits. To mitigate this risk, the function should validate the slippage limits before swapping them.
Potential for Incorrect Price Bounds: The _validateAndForwardToAMM
function checks if the new tick of the Uniswap pool is within the user-specified slippage limits. However, this check is done after the position has been created or burned in the AMM, which could potentially lead to incorrect behavior if the price of the pool changes significantly during the execution of the function. To mitigate this risk, the function should check the price bounds before creating or burning the position in the AMM.
Remember, these are potential risks and not actual vulnerabilities bt should still be checked and cross checked also undergo numerous audits to actually expose these vulnerabilities if they actually check out.
Remember, security is a continuous process and these are just some initial observations. A thorough security review and testing are recommended to identify and address any potential vulnerabilities.
Time spent 3 days.
16 hours
#0 - c4-judge
2023-12-14T17:15:06Z
Picodes marked the issue as grade-b