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.
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
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
- Deploy a malicious contract that calls the withdraw function.
- When the transferred tokens arrive, re-enter withdraw from the fallback function.
- 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.