Skip to content
Smart contracts Audit finding Intermediate

Reentrancy issue with the withdraw method of USDC. All tokens could be drained.

The withdraw function transfers the underlying token before burning the user's balance, so a malicious contract can re-enter withdraw from its fallback and repeat until the protocol's tokens are drained. An attacker could therefore steal all USDC held by the contract.

SanketKogekar Follow with ak1 and emerald7017

Originally disclosed on Code4rena

Submitted by SanketKogekar, also found by ak1 and emerald7017

https://github.com/code-423n4/2023-07-amphora/blob/daae020331404647c661ab534d20093c875483e1/core/solidity/contracts/core/USDA.sol#L147-L157 https://github.com/code-423n4/2023-07-amphora/blob/daae020331404647c661ab534d20093c875483e1/core/solidity/contracts/core/USDA.sol#L114

Impact

High: All USDC tokens could be drained from the protocol.

Proof of Concept

There is a reentrancy issue with the ‘withdraw’ methods of USDC.

A user could call either of the external withdraw functions: withdraw or withdrawTo.

Which further calls _withdraw(); which basically burns the user’s token and transfers user the _susdAmount. The exchange is 1 to 1.

function withdraw(uint256 _susdAmount) external override {
    _withdraw(_susdAmount, _msgSender());
}
  function _withdraw(uint256 _susdAmount, address _target) internal paysInterest whenNotPaused {
    if (reserveAmount == 0) revert USDA_EmptyReserve();
    if (_susdAmount == 0) revert USDA_ZeroAmount();
    if (_susdAmount > this.balanceOf(_msgSender())) revert USDA_InsufficientFunds();
    // Account for the susd withdrawn
    reserveAmount -= _susdAmount;
    sUSD.transfer(_target, _susdAmount);
    _burn(_msgSender(), _susdAmount);

    emit Withdraw(_target, _susdAmount);
  }

The issue is that the _withdraw() function does not follow the CEI pattern and fails to update the state (burning token in this case) before the token transfer.

sUSD.transfer(_target   , _susdAmount);
_burn(_msgSender(), _susdAmount);

It is possible to re-enter the function USDC.withdraw() from the malicious attack contract’s fallback function as soon as it recieves the transfered amount.

This will again hit the attacker contract’s fallback function (with the withdrawn token amount) and repeat the flow until USDA tokens from contract are completely drained.

https://github.com/code-423n4/2023-07-amphora/blob/daae020331404647c661ab534d20093c875483e1/core/solidity/contracts/core/USDA.sol#L147-L157

Recommended Mitigation Steps

This could be prevented by first burning the user’s tokens or using a Mutex / Re-entrancy guard from OpenZeppelin lib.

Assessed type

Reentrancy


The original report as published on Code4rena, by SanketKogekar. 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

The _withdraw function violates the checks-effects-interactions pattern by transferring tokens before burning the caller's balance, allowing re-entry.

Impact

All USDC tokens could be drained from the protocol.

What the attacker needed

Attacker deploys a malicious contract; contract has a fallback that re-enters withdraw; protocol holds USDC reserves

The fix

The tokens should be burned before the transfer, or a re-entrancy guard such as OpenZeppelin's should be used.

How it was found

  1. Deploy a malicious contract that calls the withdraw function.
  2. When the transferred tokens arrive, re-enter withdraw from the fallback function.
  3. Repeat the re-entry until the protocol's tokens are drained.

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 reentrancy bug happens when a contract sends money to someone before updating its own records. The recipient can call back into the same function before the record is updated, so the contract thinks they still have their full balance and pays out again and again. Here the burn of the user's tokens happens after the transfer, so the attacker can loop the withdrawal.

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 SanketKogekar

View all

1 more disclosed report, best first.

More from Amphora Protocol

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.