Skip to content
Smart contracts Audit finding Intermediate

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.

ladboy233 Follow with juancito, juancito and 11 others

Originally disclosed on Code4rena

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

https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L206

/**
	@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

https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/derivatives/Reth.sol#L58

/**
	@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;
}

https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/derivatives/Reth.sol#L173

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

https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/derivatives/SfrxEth.sol#L51

/**
	@notice - Owner only function to set max slippage for derivative
*/
function setMaxSlippage(uint256 _slippage) external onlyOwner {
	maxSlippage = _slippage;
}

https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/derivatives/SfrxEth.sol#L74

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.

toshiSat (Asymmetry) acknowledged, but disagreed with severity and commented via duplicate issue #1044:

QA, I’m not seeing the precision errors.

Picodes (judge) commented:

Note that there is a multiplication before the division, so the loss of precision is significant only if msg.value is small.

Asymmetry mitigated:

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.


The original report as published on Code4rena, by ladboy233. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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.

More by ladboy233

View all

47 more disclosed reports, best first.

More from Asymmetry Finance afETH Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.