settleFunding will exceed block gas with more markets and activity
The settleFunding function iterates over all markets and calls _calcTwap, which uses an unbounded while loop, so as markets and activity grow, gas usage could exceed the block gas limit, preventing users from calling the function and causing a denial of service.
Submitted by bw, also found by cmichel, Dravee, gzeon, Omik, and Ruhum
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L129 https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L678
As the number of supported markets grow, settleFunding will reach a point were it exceeds the block gas limit on Avalanche C-Chain. This will prevent users from calling the function and cause a wide spread Denial of Service.
Looking at transactions for the current testnet deployment, settleFunding already reaches almost 10% of the block gas limit. This is due settle funding iteratively looping through each market, with each iteration entering an unbounded while loop in _calcTwap. The more active the markets are, the more gas intensive _calcTwap becomes, as more snapshots need to be traversed.
The combination of more active markets and an increase in available markets make it very likely that some users will be unable to call settleFunding in the long run.
Proof of Concept
Example of transactions on testnet:
Gas | Limit% | Link |
658428 | 8.2% | https://testnet.snowtrace.io/tx/0x8123a5658a98e694e7428e66c9e5f9d5cbff8af93d543ed51a80cb367bcccd2c |
653810 | 8.1% | https://testnet.snowtrace.io/tx/0xf126eb05245580a73981228d6f0f8d607ad038ca0b68593f0c903e210c1c2c57 |
Recommended Mitigation Steps
Users should be allowed to settle funding per market or using an array of markets opposed to all markets at once.
function settleFundingForMarkets(IAMM[] markets) override external whenNotPaused {
for (uint i = 0; i < markets.length; i++) {
markets[i].settleFunding();
}
}
In this way the gas cost will not increase with the number of markets created over time.
atvanguard (Hubble) acknowledged, but disagreed with High severity and commented:
It’s a known issue that adding many more markets will eventually exceed block gas limit on many operations (not just
settleFunding). For that reason, DAO governance has to be careful with not adding too many markets. Would classify this as0 (Informational).
This is definitely more than informational. You can see in the gas profiler on only a limited amount of markets with not much action, lots of gas was used. This has potential to be a much bigger issue in the future.
Very rightly so the warden points out
_calcTwapas intensive which is shown on the profiler below for the one market. Calculating a TWAP across many different markets could blow this function up very quickly.Further more, if you take ALL the gas in a single block, this becomes really expensive and difficult as you need to push out a lot of other high priority transactions that are pending. If this was needing to be executed during an NFT minting spree it would be tough.
@JasoonS, let’s discuss on medium vs high for this one.
moose-code (judge) decreased severity to Medium and commented:
Going to have this as medium.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the settleFunding function loops over all markets and each iteration calls _calcTwap, which has an unbounded while loop that becomes more gas-intensive with more market activity.
Impact
Users could be unable to call settleFunding, causing a denial of service, though the report notes this is a future risk as markets and activity grow.
What the attacker needed
Many markets added; high market activity
The fix
The function should be changed to allow settling funding per market or for a subset of markets, so gas cost does not grow with the total number of markets.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A blockchain has a limit on how much work can be done in one block. If a function tries to do too much, it can't be run at all. Here, a function that updates funding for all markets at once could become too expensive to run as more markets are added, blocking users from calling it.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.