Skip to content
Smart contracts Audit finding Advanced

Multiple Unsafe Arithmetic Operations

The smart contract performs multiplications with large constants (1e18 or 1e9) without overflow protection, which could cause incorrect order fulfilment and tracking. An attacker could potentially exploit this with tokens of high decimals to cause improper buying or selling.

0xsomeone Follow

Originally disclosed on Code4rena

Submitted by 0xsomeone

https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/RubiconMarket.sol#L844

https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/RubiconMarket.sol#L857

https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/RubiconMarket.sol#L883

https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/RubiconMarket.sol#L898

https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/RubiconMarket.sol#L927

https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/RubiconMarket.sol#L951

Vulnerability details

RMT-02M: Multiple Unsafe Arithmetic Operations

File | Lines | Type |

RubiconMarket.sol | L844, L857, L883, L898, L927, L951 | Mathematical Operations |

Description

The referenced lines all perform unsafe multiplications using the unitary denominations of either 1 ether (1e18) or 10**9 (1e9), both of which can easily lead to overflows when used as a multiplier for large amounts of assets.

Impact

Purchasing and selling amounts will be improperly fulfilled as well as improperly tracked as “sold out” / “bought out”.

Recommended Mitigation Steps

We advise the codebase to make use of the mul operation exposed by the DSMath library already incorporated into the codebase to guarantee all operations are performed safely and cannot overflow.

Proof of Concept

Issue is deducible by inspecting the relevant lines referenced in the issue and making note of the raw multiplication (*) operations performed.

bghughes (Rubicon) confirmed

HickupHH3 (judge) commented:

In order to overflow, you would need pay_amt to exceed type(uint256).max / 1e18, which would be highly unlikely with majority of the ERC20 tokens.

Possibly arguable with ERC20 tokens of much higher decimals though. Considering that the product is an open orderbook, I’ll let this issue stand.


The original report as published on Code4rena, by 0xsomeone. 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 use of raw multiplication (`*`) with large constants instead of a safe math library, allowing integer overflow when amounts are sufficiently large.

Impact

Purchasing and selling amounts could be improperly fulfilled and tracked as sold out or bought out, but only with tokens of very high decimals; the judge noted overflow requires pay_amt to exceed type(uint256).max / 1e18, which is highly unlikely for most ERC20 tokens.

What the attacker needed

ERC20 token with very high decimals; large pay_amt value

The fix

The code should use the safe `mul` operation from the DSMath library to prevent overflow.

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.

In smart contracts, numbers have a maximum size. If you multiply two large numbers, the result can wrap around to a small number, like a car odometer rolling over. This contract multiplies user amounts by very large constants without checking for overflow, so with certain tokens the recorded amounts could be wrong, leading to incorrect trades.

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 0xsomeone

View all

7 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.

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.