Skip to content
Business logic Audit finding Intermediate

RubiconRouter _swap does not pass whole amount to RubiconMarket

The RubiconRouter _swap function calculated the amount to forward to RubiconMarket using a fee formula that subtracted the fee from the full amount, so the market then charged its fee on the reduced amount. As a result a small dust amount of the user's funds was never pulled by the market and was effectively lost.

kenzo Follow

Originally disclosed on Code4rena

Submitted by kenzo, also found by IllIllI, PP1004, blackscale, and hansfriese

When swapping amongst multiple pairs in RubiconRouter’s _swap, the fee is wrongly accounted for.

Impact

Not all of the user’s funds would be forwarded to RubiconMarket, therefore the user would lose funds.

Proof of Concept

The _swap function is calculating the pay amount to send to RubiconMarket.sellAllAmount to be:

currentAmount.sub(currentAmount.mul(expectedMarketFeeBPS).div(10000)

But this would lead to not all of the funds being pulled by RubiconMarket. I mathematically show this in this image. The correct parameter that needs to be sent to sellAllAmount is:

currentAmount.sub(currentAmount.mul(expectedMarketFeeBPS).div(10000+expectedMarketFeeBPS)

I mathematically prove this in this image.

Recommended Mitigation Steps

Change the parameter to the abovementioned one.

bghughes (Rubicon) confirmed

HickupHH3 (judge) commented:

For the benefit of readers who aren’t as math savvy, let’s work this out with a numerical example.

Let’s assume a 1% fee: expectedMarketFeeBPS = 100. The RubiconMarket charges and pulls this fee separately, so if I have a trade amount of 100, what would be the actual amount to pass into the function?

The current implementation is 100 - 1% * 100 = 100 - 1 = 99. However, if that’s the case, the market charges 1% of 99 instead, which is 0.99. Hence, the total amount used is 99 + 0.99 = 99.99, leaving a dust amount of 0.01.

Thus, as the warden has proven mathematically, the formula should be 100 - 100 * 100 / (10_000 + 100) ~= 99.0099. Then, the 1% fee charged is 0.990099..., making the total approximately equal to 100 (rounding errors).


The original report as published on Code4rena, by kenzo. 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 fee was subtracted from the gross amount rather than dividing by (10000 + fee) so the fee is charged on the net amount, leaving a dust remainder.

Impact

A small dust amount of the user's funds is not forwarded to the market and is lost on each multi-pair swap; the loss is limited to rounding-level dust per trade.

What the attacker needed

User performs a multi-pair swap via RubiconRouter

The fix

The parameter passed to sellAllAmount should be changed to subtract the fee using the corrected formula so the full amount is consumed.

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 you swap tokens, a small fee is taken by the market. The router tried to work out how much to send so that after the fee the full amount is used, but its maths was slightly off, so a tiny leftover (dust) stayed behind. Over many swaps this dust adds up and the user loses a little money each time.

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.

More by kenzo

View all

53 more disclosed reports, best first.

More from Rubicon v2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.