Skip to content
Race condition Audit finding Intermediate

ERC777 reentrancy when withdrawing can be used to withdraw all collateral

The withdraw function in a lending market lacked reentrancy protection, allowing an attacker to reenter during an ERC777 token transfer hook and withdraw more collateral than permitted while keeping the loan. This could let the attacker drain all collateral from the escrow.

Lambda Follow

Originally disclosed on Code4rena

Submitted by Lambda

Market.sol#L464

Markets can be deployed with arbitrary tokens for the collateral, including ERC777 tokens (that are downwards-compatible with ERC20). However, when the system is used with those tokens, an attacker can drain his escrow contract completely while still having a loan. This happens because with ERC777 tokens, there is a tokensToSend hook that is executed before the actual transfer (and the balance updates) happen. Therefore, escrow.balance() (which retrieves the token balance) will still report the old balance when an attacker reenters from this hook.

Proof Of Concept

We assume that collateral is an ERC777 token and that the collateralFactorBps is 5,000 (50%). The user has deposited 10,000 USD (worth of collateral) and taken out a loan worth 2,500 USD. He is therefore allowed to withdraw 5,000 USD (worth of collateral). However, he can usse the ERC777 reentrancy to take out all 10,000 USD (worth of collateral) and still keep the loaned 2,500 USD:

  1. The user calls withdraw(amount) to withdraw his 5,000 USD (worth of collateral).
  2. In withdrawInternal, the limit check succeeds (the user is allowed to withdraw 5,000 USD) and escrow.pay(to, amount) is called. This will initiate a transfer to the provided address (no matter which escrow is used, but we assume SimpleERC20Escrow for this example).
  3. Because the collateral is an ERC777 token, the tokensToSend hook is executed before the actual transfer (and before any balance updates are made). The user can exploit this by calling withdraw(amount) again within the hook.
  4. withdrawInternal will call getWithdrawalLimitInternal, which calls escrow.balance(). This receives the collateral balance of the escrow, which is not yet updated. Because of that, the balance is still 10,000 USD (worth of collateral) and the calculated withdraw limit is therefore still 5,000 USD.
  5. Both transfers (the reentered one and the original one) succeed and the user has received all of his collateral (10,000 USD), while still having the 2,500 USD loan.

Recommended Mitigation Steps

Mark these functions as nonReentrant.

0xean (judge) commented:

Sponsor should review as the attack does seem valid with some pre-conditions (ERC777 tokens being used for collateral). Probably more of a Medium severity.

08xmt (Inverse) acknowledged, but disagreed with severity and commented:

We make the security assumption that future collateral added by Inverse Finance DAO is compliant with standard ERC-20 behavior. Inverse Finance is full control of collateral that will be added to the platform and only intend to add collateral that properly reverts on failed transfers. Each ERC20 token added as collateral will be audited for non-standard behaviour. I would consider this a Low Risk finding, depending on how you value errors made in launch parameters.

0xean (judge) decreased severity to Medium and commented:

@08xmt - The revert on a failed transfer here isn’t the issue, it is the re-entrancy that isn’t guarded against properly. While I understand your comment, if it were my codebase, I would simply add the modifier and incur the small gas costs as an additional layer of security to avoid mistakes in the future. I don’t think this qualifies as High, but does show an attack path that could be achieved with an ERC777 token being used as collateral. Going to downgrade to Medium and will be happy to hear more discussion on the topic before final review.

08xmt (Inverse) commented:

@0xean - The risk is still only present with unvetted contracts, and if the desire should exist in the future to implement a market with a token with re-entrancy, the code can be modified as necessary.

Will respect the judge’s decision on severity in the end, but ultimately seem like a deployment parameter risk more than anything.

0xean (judge) commented:

Thanks @08xmt for the response.

While I agree that proper vetting could avoid this issue, the wardens are analyzing the code and documentation that is presented before them and I think in light of this, the issue is valid. Had the warden simply stated that there was a reentrancy modifier missing without showing a valid path to it being exploited, I would downgrade to QA. But given they showed a valid attack path due to the lack of reentrancy controls I think this should be awarded.


The original report as published on Code4rena, by Lambda. 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 lacked a reentrancy guard, allowing an attacker to reenter during an ERC777 tokensToSend hook before the escrow balance was updated.

Impact

An attacker could withdraw all collateral while keeping the loan, but only if the market uses an ERC777 token as collateral; the judge noted this requires pre-conditions and rated it Medium.

What the attacker needed

Market uses an ERC777 token as collateral; Attacker has a loan with that collateral

The fix

The withdraw functions should be marked as nonReentrant to prevent reentrancy.

How it was found

  1. The user calls withdraw to withdraw the allowed amount of collateral.
  2. The withdraw function checks the withdrawal limit and calls escrow.pay to transfer the tokens.
  3. Because the collateral is an ERC777 token, the tokensToSend hook runs before the balance updates.
  4. The user reenters withdraw from the hook, and the limit check still sees the old balance.
  5. Both transfers succeed, and the user receives all collateral while keeping the loan.

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 you withdraw collateral from a lending platform, the platform checks how much you are allowed to take based on your current balance. If the token used for collateral has a callback that runs before the balance updates, an attacker can call withdraw again during that callback. The second check still sees the old balance, so the attacker can take out more than allowed.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by Lambda

View all

64 more disclosed reports, best first.

More from Inverse Finance contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.