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.
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.
In order to overflow, you would need
pay_amtto exceedtype(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.
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.