CNote updates the accounts after sending the funds, allowing for reentrancy
The CNote contract sends borrowed funds to the borrower before updating the borrower's debt record, and the exitMarket function lacks reentrancy protection. An attacker could re-enter exitMarket during the transfer to remove their debt, potentially draining available funds.
Submitted by hyh, also found by defsec
Having no reentrancy control and updating the records after external interactions allows for funds draining by reentrancy.
Setting the severity to medium as this is conditional to transfer flow control introduction on future upgrades, but the impact is up to the full loss of the available funds by unrestricted borrowing.
Proof of Concept
CNote runs doTransferOut before borrowing accounts are updated:
/*
* We invoke doTransferOut for the borrower and the borrowAmount.
* Note: The cToken must handle variations between ERC-20 and ETH underlying.
* On success, the cToken borrowAmount less of cash.
* doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
*/
doTransferOut(borrower, borrowAmount);
require(getCashPrior() == 0,"CNote::borrowFresh: Error in doTransferOut, impossible Liquidity in LendingMarket");
//Amount minted by Accountant is always flashed from account
/* We write the previously calculated values into storage */
accountBorrows[borrower].principal = accountBorrowsNew;
accountBorrows[borrower].interestIndex = borrowIndex;
totalBorrows = totalBorrowsNew;
/* We emit a Borrow event */
emit Borrow(borrower, borrowAmount, accountBorrowsNew, totalBorrowsNew);
}
Call sequence here is borrow() -> borrowInternal() -> borrowFresh() -> doTransferOut(), which transfers the token to an external recipient:
/**
* @dev Similar to EIP20 transfer, except it handles a False success from `transfer` and returns an explanatory
* error code rather than reverting. If caller has not called checked protocol's balance, this may revert due to
* insufficient cash held in this contract. If caller has checked protocol's balance prior to this call, and verified
* it is >= amount, this should not revert in normal conditions.
*
* Note: This wrapper safely handles non-standard ERC-20 tokens that do not return a value.
* See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca
*/
function doTransferOut(address payable to, uint amount) virtual override internal {
EIP20NonStandardInterface token = EIP20NonStandardInterface(underlying);
token.transfer(to, amount);
There an attacker can call exitMarket() that have no reentrancy control to remove the account of the debt:
/**
* @notice Removes asset from sender's account liquidity calculation
* @dev Sender must not have an outstanding borrow balance in the asset,
* or be providing necessary collateral for an outstanding borrow.
* @param cTokenAddress The address of the asset to be removed
* @return Whether or not the account successfully exited the market
*/
function exitMarket(address cTokenAddress) override external returns (uint) {
This attack was carried out several times:
https://certik.medium.com/fei-protocol-incident-analysis-8527440696cc
Recommended Mitigation Steps
Consider moving accounting update before funds were sent out, for example as it is done in CToken’s borrowFresh():
https:
/*
* We write the previously calculated values into storage.
* Note: Avoid token reentrancy attacks by writing increased borrow before external transfer.
`*/
accountBorrows[borrower].principal = accountBorrowsNew;
accountBorrows[borrower].interestIndex = borrowIndex;
totalBorrows = totalBorrowsNew;
/*
* We invoke doTransferOut for the borrower and the borrowAmount.
* Note: The cToken must handle variations between ERC-20 and ETH underlying.
* On success, the cToken borrowAmount less of cash.
* doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
*/
doTransferOut(borrower, borrowAmount);
Alex the Entreprenerd (judge) commented:
The warden has shown how, the in-scope codebase has historically been attacked due to a reentrancy attack.
Because:
- The warden has provided POC and historical references
- The attack is contingent on a specific token that enables it
I agree with Medium Severity.
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 updates the borrower's debt record after transferring funds, and the exitMarket function lacks reentrancy protection, allowing an attacker to re-enter and remove their debt during the transfer.
Impact
An attacker could drain the available funds through unrestricted borrowing, but the severity is medium because the attack is conditional on a future upgrade introducing transfer flow control.
What the attacker needed
A token that enables reentrancy during transfer; future upgrade introducing transfer flow control
The fix
The accounting update should be moved before the external transfer, as done in CToken's borrowFresh().
How it was found
- Call borrow() which triggers borrowInternal() then borrowFresh() then doTransferOut(), sending funds to the borrower.
- During the token transfer, re-enter the protocol and call exitMarket() to remove the account's debt.
- The debt is removed before the original borrow updates the records, allowing unrestricted borrowing and potential draining of funds.
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 a smart contract, a reentrancy attack happens when a contract sends funds to an external address before it updates its own records. The recipient can call back into the contract before the record is updated, letting them repeat actions or escape obligations. Here, the contract sent the loan before recording the debt, and the exit function had no guard, so an attacker could remove their debt while the transfer was still happening.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.