Open c4-bot-3 opened 8 months ago
0xRobocop marked the issue as sufficient quality report
0xRobocop marked the issue as primary issue
enthusiastmartin (sponsor) disputed
Suggested mitigation is already implemented. We gradually change the amplification.
Centralization issue, enforcing a maximum delta could be done to alleviate it a bit, but that's a design decision.
OpenCoreCH changed the severity to QA (Quality Assurance)
OpenCoreCH marked the issue as grade-a
Lines of code
https://github.com/code-423n4/2024-02-hydradx/blob/603187123a20e0cb8a7ea85c6a6d718429caad8d/HydraDX-node/pallets/stableswap/src/lib.rs#L408-L453
Vulnerability details
Impact
The amplification factor can be changed by calling the
update_amplification
function in the pool. The admin can choose a final amplification coefficient, and the pool amplification factor will change over time to reach that value by a certain block number. There are no restrictions as to how sudden or how fast this amplification change can be.Similar to curve protocol, the stableswap protocol math is essentially a linear combination of a constant product and a constant sum AMM. The amplification factor A basically determines the weights of each of the two components, and thus determines the current level of slippage in the pool. Drastic changes in the amplification factor results in sharp changes in the slippage of the pool, which can be used to extract value from the pool.
A POC is coded up in the section below. Here we observe the following scenario:
Lets assume a pool has been deployed with a certain value of A. Then, the admin decides to change the amplification factor A of the pool and decrease it. In this scenario, the amplification factor is changed from 10_000 to 2_000. It is shown that this amplification factor change can be sandwiched by a malicious user to extract tokens from the pool.
Proof of Concept
The POC has the following steps:
Below is the test case. Scroll further down for the results.
Results:
This shows that while BOB started with 200 asset_a and asset_b, he ends up with 2000.48 asset_b and 2000 asset_a, extracting some value from the pool. The Pool assets are also shown to decrease, indicating that the pool has lost some tokens.
The effect of this can be amplified by using a larger trade amount for sandwiching the pool. This is a simple example to show the potential of the attack, multiple factors like the current price of the pool, depth of liquidity etc also factor into the amount profited by the sandwich attacker.
Tools Used
Substrate
Recommended Mitigation Steps
One of the reason for this attack vector is the sudden change in amplification factor. It is recommended to have a gradual change in the amplification factor, thus consider adding a minimum block number difference between the initial and final blocks for amplification change. However this doesnt complete fix the problem, just makes the change more gradual, and thus allows multiple MEV participants to gradually take profit.
Another approach that can be taken is to limit the amount the amplification factor can be changed in one go.
Assessed type
MEV