ClearingHouse May Whitelist Duplicate AMMs
The whitelistAmm function allows the same AMM contract to be added multiple times, causing unrealized profits and notional positions to be counted more than once. This can lead to incorrect margin calculations, potentially allowing insolvent trading or wrongful liquidations.
Originally disclosed on Code4rena
Submitted by kirk-baird
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L339-L342 https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L269-L282
ClearingHouse.sol allows the Governance protocol to whitelist AMM.sol contracts. These contracts allow users to earn profits based on the price of a base asset against a quote asset.
It is possible to add the same AMM twice in the function whitelistAmm(). The impact is that unrealized profits will be counted multiple times. As a result the liquidation calculations will be incorrect, potentially allowing users to trade while insolvent or incorrectly liquidating solvent users.
Note whitelistAmm() may only be called by Governance.
Proof of Concept
The function getTotalNotionalPositionAndUnrealizedPnl() will iterate over all amms summing the unrealizedPnl and notinoalPosition, thus if an amm is repeated the unrealizedPnl and notionalPosition of that asset will be counted multiple times.
This is used in _calcMarginFraction() which calculates a users margin as a fraction of the total position. The margin fraction is used to determine if a user is liquitable or is allowed to open new positions.
Recommended Mitigation Steps
Consider ensuring the AMM does not already exist in the list when adding a new AMM.
function whitelistAmm(address _amm) external onlyGovernance {
for (uint256 i; i < amm.length; i++) {
require(amm[i] != IAMM(_amm), "AMM already whitelisted");
}
emit MarketAdded(amms.length, _amm);
amms.push(IAMM(_amm));
}
atvanguard (Hubble) confirmed, but disagreed with Medium severity and commented:
As mentioned in #40, the system relies on the admin to do the right thing; hence disagreeing with the severity. Still, it’s a good idea to have this check.
Practical advice that prevents a catastrophic issue that could very possibly occur (having run deployment / whitelist and many other scripts, it’s way too easy to run something again etc and end up in this situation - even though it feels like it would never be possible).
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
The whitelistAmm function does not check whether the AMM address is already in the list before adding it.
Impact
Unrealized profits and notional positions are counted multiple times, leading to incorrect margin calculations that could allow insolvent trading or incorrect liquidations; however, only Governance can call whitelistAmm, so the issue requires an admin mistake.
What the attacker needed
Governance calls whitelistAmm with a duplicate AMM
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 smart contract lets administrators approve certain trading pools (AMMs). If the same pool is approved twice, the system counts its profits twice when checking if a user has enough collateral. This could let someone trade with too little money or get wrongly liquidated.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.