Division before multiplication truncate minOut and incurs heavy precision loss and result in insufficient slippage protection
The minOut calculation in two derivative contracts performs division before multiplication, which truncates the result and can set minOut to zero when maxSlippage is 10%. An attacker could then execute a trade with no effective slippage protection, causing the user to receive a sub-optimal amount.
Submitted by ladboy233, also found by juancito, juancito, neumo, Bauer, 0xkazim, UniversalCrypto, Matin, jasonxiale, J4de, cryptothemex, latt1ce, 0xnev, and koxuan
https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/derivatives/Reth.sol#L173 https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/derivatives/SfrxEth.sol#L74
When Calcuting the minOut before doing trade, division before multiplication truncate minOut and incurs heavy precision loss, then very sub-optimal amount of the trade output can result in loss of fund from user because of the insufficient slippage protection.
Proof of Concept
In the current implementation, slippage can be set by calling
/**
@notice - Sets the max slippage for a certain derivative index
@param _derivativeIndex - index of the derivative you want to update the slippage
@param _slippage - new slippage amount in wei
*/
function setMaxSlippage(
uint _derivativeIndex,
uint _slippage
) external onlyOwner {
derivatives[_derivativeIndex].setMaxSlippage(_slippage);
emit SetMaxSlippage(_derivativeIndex, _slippage);
}
Which calls the corresponding derivative contract.
Case 1
/**
@notice - Owner only function to set max slippage for derivative
@param _slippage - new slippage amount in wei
*/
function setMaxSlippage(uint256 _slippage) external onlyOwner {
maxSlippage = _slippage;
}
calling
uint256 minOut = ((((rethPerEth * msg.value) / 10 ** 18) *
((10 ** 18 - maxSlippage))) / 10 ** 18);
IWETH(W_ETH_ADDRESS).deposit{value: msg.value}();
uint256 amountSwapped = swapExactInputSingleHop(
W_ETH_ADDRESS,
rethAddress(),
500,
msg.value,
minOut
);
As we can see, the division before multiplication happens in the line of code.
uint256 minOut = ((((rethPerEth * msg.value) / 10 ** 18) *
((10 ** 18 - maxSlippage))) / 10 ** 18);
For example, if maxSlippage is 10 ** 17
(10 ** 18 - 10 ** 17) / (10 ** 18) = 0
Then minOut is 0, slippage control is disabled because of the division before multipcation.
Case 2
/**
@notice - Owner only function to set max slippage for derivative
*/
function setMaxSlippage(uint256 _slippage) external onlyOwner {
maxSlippage = _slippage;
}
The divison before multiplication happens below.
uint256 minOut = (((ethPerDerivative(_amount) * _amount) / 10 ** 18) *
(10 ** 18 - maxSlippage)) / 10 ** 18;
IFrxEthEthPool(FRX_ETH_CRV_POOL_ADDRESS).exchange(
1,
0,
frxEthBalance,
minOut
);
For example, if maxSlippage is 10 ** 17
(10 ** 18 - 10 ** 17) / (10 ** 18) = 0
Then minOut is 0, slippage control is disabled because of the division before multiplication.
Recommended Mitigation Steps
We recommend the protocol avoid division before multiplcation when calcaluting the minOut to enable slippage protection and avoid front-running.
QA, I’m not seeing the precision errors.
Note that there is a multiplication before the division, so the loss of precision is significant only if
msg.valueis small.
Don’t divide before multiply.
Status: Not fully mitigated. Full details in reports from d3e4 and adriro - and also shared below in the Mitigation Review section.
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 minOut formula divides by 10^18 before multiplying by the slippage factor, causing integer truncation that can zero out the minimum output.
Impact
Slippage protection can be disabled, allowing a user to receive a sub-optimal trade output and lose funds; the judge noted the loss is significant only when msg.value is small.
What the attacker needed
MaxSlippage set to 10%; small msg.value
The fix
The protocol should avoid division before multiplication when calculating minOut; a mitigation was attempted but not fully effective.
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.
When a contract swaps tokens, it calculates a minimum amount it will accept to protect against bad prices. This code divides before multiplying, which can round the minimum down to zero, so the protection disappears. An attacker could then manipulate the trade so the user gets much less than expected.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.