The latest malt price can be less than the actual price target and StabilizerNode.stabilize will revert
In the StabilizerNode contract, the minimum price threshold is computed by subtracting a slippage amount from the latest price sample, which assumes the latest sample is at least the price target. When the latest sample is below the target, the subtraction underflows and the stabilize call reverts with an arithmetic error rather than a clear message.
hansfriese Follow with minhquanym
Originally disclosed on Code4rena
Submitted by hansfriese, also found by minhquanym
https://github.com/code-423n4/2023-02-malt/blob/main/contracts/StabilityPod/StabilizerNode.sol#L188
Impact
StabilizerNode.stabilize will revert when latestSample < priceTarget.
Proof of Concept
In StabilizerNode.stabilize, when exchangeRate > priceTarget and _msgSender is not an admin and not whitelisted, it asserts livePrice > minThreshold.
And minThreshold is calculated as follows:
uint256 priceTarget = maltDataLab.getActualPriceTarget();
uint256 latestSample = maltDataLab.maltPriceAverage(0);
uint256 minThreshold = latestSample -
(((latestSample - priceTarget) * sampleSlippageBps) / 10000);
This code snippet assumes that latestSample >= priceTarget. Although exchangeRate > priceTarget, exchangeRate is the malt average price during priceAveragePeriod. But latestSample is one of those malt prices. So latestSample can be less than exchangeRate and priceTarget, so stabilize will revert in this case.
Recommended Mitigation Steps
Use minThreshold = latestSample + (((priceTarget - latestSample) * sampleSlippageBps) / 10000) when priceTarget > latestSample.
0xScotch (Malt) confirmed and commented:
We actually do want the tx to revert when
latestSample < priceTargetas that means the most recent sample in the price average feed is below peg but we are in the above peg stabilization flow in the code. However, we do not want the revert to be subtraction overflow as that looks like something went wrong. So we should handle with an explicit error.
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 threshold calculation subtracts a slippage amount from the latest sample without checking that the sample is at least the price target, so the unsigned subtraction underflows.
Impact
When the latest sample is below the price target, stabilize reverts with a subtraction overflow instead of an explicit error, which looks like something went wrong.
What the attacker needed
Latest sample below price target; caller is not admin or whitelisted
The fix
The team should handle the case where the latest sample is below the price target with an explicit error rather than letting the subtraction underflow.
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 calculates a lower limit for a price by subtracting a small amount from the most recent price reading. If that reading is already lower than the target price, the subtraction goes below zero, which the programming language treats as an error, so the whole transaction fails. The team says the failure is intended in that situation, but it should fail with a clear message instead of a confusing arithmetic error.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.