Skip to content
Smart contracts Audit finding Advanced

Cross-Contract Reentrancy Vulnerability in CDS Withdraw Function

The CDS withdraw function reads global state into memory, transfers ETH to the user via a low-level call, and only later writes the in-memory state back, overwriting any changes made during the call. A malicious user can reenter the borrowing contract to modify the global state, which is then overwritten, leading to incorrect calculations and potentially blocking withdrawals.

t.aksoy Follow with valuevalk

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-autonomint-judging/issues/832

Found by

t.aksoy, valuevalk

Summary

The withdraw function in the CDS contract is vulnerable to cross-contract reentrancy attacks due to delayed updates to the omniChainData global state. During execution, funds are transferred to the user via a low-level .call function, granting control to the user before omniChainData is updated. A malicious user could exploit this by calling borrowing contract and altering the global omniChainData state. This results in the outdated in-memory omniChainData being used to overwrite the updated global state at the end of the transaction, breaking calculations and potentially preventing user withdrawals from borrowing contract.

Root Cause

In the CDS withdraw function, the omniChainData is fetched and stored in memory at the beginning of the transaction. This data is subsequently updated in the global state only after all operations are complete. During the execution of the withdraw function, if the user opts for liquidation gains, the function transfers ETH to the user using the .call function. This grants control to the user mid-execution.

A malicious user can exploit this control to interact with borrowing contract, modifying the global omniChainData state. However, at the end of the withdraw function, the outdated in-memory omniChainData is written back to the global state, overwriting any legitimate changes made during the reentrant call.

Solidity
function withdraw(
    uint64 index,
    uint256 excessProfitCumulativeValue,
    uint256 nonce,
    bytes memory signature
) external payable nonReentrant whenNotPaused(IMultiSign.Functions(5)) {
    // Step 1: Fetch omniChainData in memory
    IGlobalVariables.OmniChainData memory omniChainData = globalVariables.getOmniChainData();
    // Perform calculations and updates using in-memory omniChainData
   withdrawResult = CDSLib.withdrawUser(
              WithdrawUserParams(cdsDepositDetails,
                omniChainData, ...));

    // Step 4: Update global state, overwriting changes made during reentrancy
     globalVariables.setOmniChainData(withdrawResult.omniChainData); // @audit outdated data overwrites updates
}

 function withdrawUser(
    ) public returns (CDSInterface.WithdrawResult memory) {
        if (!params.cdsDepositDetails.optedLiquidation) {
        } else {
                

               //  Step 2: Transfer liquidation gains to user from treasury
                if (params.ethAmount != 0) {
                    params.omniChainData.collateralProfitsOfLiquidators -= totalWithdrawCollateralAmountInETH;
                    // Call transferEthToCdsLiquidators to tranfer eth
                    interfaces.treasury.transferEthToCdsLiquidators(
                        msg.sender,
                        params.ethAmount
                    );
                }

}
}

    function transferEthToCdsLiquidators(
        address user,
        uint128 amount
    ) external onlyCoreContracts {
  
        //  Step 3: Transfer ETH, control transferred to user
        (bool sent, ) = payable(user).call{value: amount}("");
        if (!sent) revert Treasury_EthTransferToCdsLiquidatorFailed();
    }

https://github.com/sherlock-audit/2024-11-autonomint/blob/0d324e04d4c0ca306e1ae4d4c65f0cb9d681751b/Blockchain/Blockchian/contracts/Core_logic/CDS.sol#L399

https://github.com/sherlock-audit/2024-11-autonomint/blob/0d324e04d4c0ca306e1ae4d4c65f0cb9d681751b/Blockchain/Blockchian/contracts/lib/CDSLib.sol#L828

Internal pre-conditions

  1. Malious user deposit to CDS contract with an custom contract that implements fallback function
  2. Liquidation gains should include ETH amount
  3. Malious user should be able to claim ETH when withdrawing from CDS

External pre-conditions

No response

Attack Path

  1. User Calls CDS withdraw
  • omniChainData is fetched and stored in memory.
  1. Malicious User Gains Control via .call:
  • During the execution of withdraw, if the user has opted for liquidation gains, the function attempts to transfer ETH to the user via .call.
  1. Malicious User Modifies omniChainData via Borrowing Contract
  • While holding control, the malicious user interacts with the borrowing contract and deposits a large amount (X) to manipulate the global omniChainData.
  • This action updates the global omniChainData, specifically increasing totalVolumeOfBorrowersAmountinWei.
  1. Outdated omniChainData Overwrites Updates:
  • After the control is returned to the withdraw function, it proceeds to write back the outdated in-memory omniChainData to the global state.
  • Changes made to omnichainData in the borrowing contract are overwritten, causing incorrect state updates.
  1. Malicious User Withdraws from Borrowing Contract
  • After the withdraw function completes, the malicious user interacts with the borrowing contract again and withdraws the previously deposited amount (X).
  • This action reduces totalVolumeOfBorrowersAmountinWei in the global omniChainData to a value lower than it should be, due to the overwritten state.
  1. If the malicious user repeats this attack multiple times with high X values totalVolumeOfBorrowersAmountinWei becomes artificially low preventing users withdrawal from borrowing the contract

Impact

Incorrect omniChainData values disrupt critical calculations, such as the CDS pool-to-ETH ratio, cumulative values Withdrawal operations in borrowing contract may fail due to invalid state variables.

PoC

No response

Mitigation

Adhere to the checks-effects-interactions pattern. Update the global state (omniChainData) before transferring control to the user.

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

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

The contract reads global state into memory, performs an external call that transfers control to the user, and then writes the stale in-memory state back, overwriting any legitimate updates made during the call.

Impact

Incorrect omniChainData values could disrupt calculations and cause withdrawal operations in the borrowing contract to fail.

What the attacker needed

Malicious user deposits to CDS with a custom contract; Liquidation gains include ETH amount; User can claim ETH when withdrawing from CDS

The fix

The contract should follow the checks-effects-interactions pattern, updating global state before transferring control to the user.

How it was found

  1. User calls CDS withdraw, which fetches omniChainData into memory.
  2. During withdraw, if the user opted for liquidation gains, ETH is transferred via .call, giving control to the user.
  3. The user interacts with the borrowing contract to deposit a large amount, updating global omniChainData.
  4. Control returns to withdraw, which writes the outdated in-memory omniChainData back, overwriting the updates.
  5. The user withdraws the deposited amount from the borrowing contract, reducing totalVolumeOfBorrowersAmountinWei incorrectly.
  6. Repeating the attack with high amounts makes totalVolumeOfBorrowersAmountinWei artificially low, preventing withdrawals.

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 sends Ether to a user, the user's contract can run code immediately. If the sending contract hasn't finished updating its records, the user can call back into the system and change things. Here, the contract later overwrites those changes with old data, causing errors.

What is Smart contracts?

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

More from Autonomint Colored Dollar V1

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.