sync function in RdpxV2Core.sol should be called in multiple scenarios to account for the balance changes that occurs
The sync function in RdpxV2Core.sol is not called after certain token transfers from the AMO contracts, so the contract's internal balance records become stale. This can cause underflow errors or incorrect reserve calculations in functions that rely on those balances.
Submitted by Vagner, also found by oakcobalt, T1MOH, Aymen0909, savi0ur, codegpt, KmanOfficial, alexzoid, wintermute, nemveer, bin2chen, Evo, ak1 (1, 2), 0Kage, pep7siup, MohammedRizwan, qbs, hals, said, ladboy233 (1, 2), Viktor_Cortess, peakbolt, 0xnev (1, 2), zaevlad (1, 2), ABAIKUNANBAEV (1, 2), mrudenko, zzebra83, 0xCiphky, Yanchuan, and tapir
The function sync is used in RdpxV2Core.sol to synchronize the balances of the reserve assets stored in the contract, but it is not called in all of the situations where swaps/providing/removing liquidity is made.
Proof of Concept
As you can see sync is called in UniV3LiquidityAmo.sol after _sendTokensToRdpxV2Core is called to occur for all of the balances change after modifying the liquidity in UniswapV3
https://github.com/code-423n4/2023-08-dopex/blob/eb4d4a201b3a75dd4bddc74a34e9c42c71d0d12f/contracts/amo/UniV3LiquidityAmo.sol#L361
But it is not called functions like collectFees where funds are directly transferred to RdpxV2Core.sol
https://github.com/code-423n4/2023-08-dopex/blob/eb4d4a201b3a75dd4bddc74a34e9c42c71d0d12f/contracts/amo/UniV3LiquidityAmo.sol#L118-L133
Or in _sendTokensToRdpxV2Core in the UniV2LiquidityAmo.sol which transfers tokens to RdpxV2Core
https://github.com/code-423n4/2023-08-dopex/blob/eb4d4a201b3a75dd4bddc74a34e9c42c71d0d12f/contracts/amo/UniV2LiquidityAmo.sol#L157-L178
Because of that wrong assumptions can be taken in cases of lowerDepeg or upperDepeg, or any other functions that uses the balances of assets stored, if sync was not called before.
Recommended Mitigation Steps
Since you already call sync in _sendTokensToRdpxV2Core from UniV3LiquidityAmo.sol, call it in collectFees and _sendTokensToRdpxV2Core from UniV2LiquidityAmo.sol also to accounts for any balance changes, which will protect the protocol against errors.
- This is related to Uni V2, in the implementation for V3 it is called.
- This will cause less than expected backing reserves, which can lead to underflow when performing various other actions such as when providing funding for APP options (provideFunding()), settling options (settle()).
- The function getReserveTokenInfo is no longer a reliable source for external integration to retrieve the token balance state.
Potentially impacts the following functions:
Edit: It’s actually not called in collectFee’s as well. I’m duplicating all of these under a single primary issue, because #269 mentions both.
Alex the Entreprenerd (Judge) commented:
Seems like an opportunity for a higher exploit was missed by stopping at the broken sync.
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 sync function is not invoked after token transfers in collectFees and _sendTokensToRdpxV2Core, leaving the contract's stored reserve balances out of date.
Impact
The stale balances could cause underflow when providing funding or settling options, and make getReserveTokenInfo unreliable for external integrations.
What the attacker needed
None
The fix
The sync function should be called in collectFees and _sendTokensToRdpxV2Core from UniV2LiquidityAmo.sol to account for balance changes.
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.
A smart contract keeps its own record of how many tokens it holds. If tokens are sent to it without updating that record, the contract thinks it has less than it really does. Later actions that check the recorded balance can fail or behave incorrectly.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.