Fei Protocol Incident Analysis

Incident Summary

From Apr-30–2022 09:01:35 AM +UTC to Apr-30–2022 09:35:24 AM +UTC, Fei Protocol announced that they were aware of and looking into an exploit on various Rari Fuse pools. The total loss currently reported is ~$80 Million. They have paused all borrowing to minimize further loss.

The root cause of this attack stems from a Reentrancy security loophole where an attacker is able to borrow assets yet withdraw all the deposited collateral. Specifically, the attacker flashloaned multiple tokens/WETH as collateral and borrowed assets from the pool. However, as the code implementation does not apply the practice of the check-effect-interaction pattern, the result means that the attacker can invoke “exitMarket()” to withdraw all his collateral as the borrow records are not properly updated.

Context

  • FEI protocol is an algorithmic stable coin project for decentralized finance. Fei Protocol aims to help other DAOs achieve deep liquidity in lending and exchange markets by deploying PCV and offering Liquidity-as-a-Service (LaaS)
  • Rari Fuse Pool is a DeFi protocol that facilitates lending, borrowing and creating isolated lending markets with unlimited flexibility.

Exploit Transactions

Attacker address: 0x6162759edad730152f0df8115c698a42e666157f

Attacker contracts :

Exploit transactions:

All relevant transactions can be found here: https://etherscan.io/address/0x6162759edad730152f0df8115c698a42e666157f

Attack Flow

The section contains step-by-step information about how this exploit is perpetrated. Note that the attack flow is simplified here for ease of explanation.

The attack flow analysis used the following transaction on Ethereum network as example: 0xab486012f21be741c9e674ffda227e30518e8a1e37a5f1d58d0b0d41f6e76530

The attacker takes the advantage of the Reentrancy loophole of the protocol and performed the following actions:

  1. Attacker flashloaned 150,000,000 USDC and 50,000 WETH
  2. Deposited 150,000,000 USDC as collateral into the fUSDC-127 contract, which is a vulnerable fork version of the compound protocol.
  3. With deposited collateral, the attacker borrowed 1,977 ETH via the “borrow()” function.
  4. However, the “borrow()” function does not follow the check-effect-interaction pattern. Specifically, it transfers ETH to the attacker’s contract before updating the attacker’s actual borrow records.
  5. Therefore, with the attacker’s borrow record not updated, the attacker made a reentrant call to “exitmarket()” in the fallback function, which allows the attacker to withdraw all his collateral (150M USDC)
  6. The attacker repeated steps 1~5 on multiple other tokens.
  7. Finally, the attacker repaid the flashloan and transferred the rest to their address as profit, and routed some of the funds onward to Tornado Cash.

Vulnerability

This attack was due to a design flaw in the FeiProtocl that failed to follow the check-effect-interaction pattern and thus allow the attacker to make a reentrant call before the borrow records are updated. In the “borrow()” function, the following code is implemented:

As the code illustrates, the “doTransferOut()” is invoked before the borrow records (i.e., “accountBorrows[]” and “totalBorrows” ) are updated.

The “doTransferOut()” function transfers ETH to the receiver via a low-level call:

Therefore, the attacker is able to make a reentrant call in the “fallback()” function to “exitMarket()”.

Profit and assets tracing

The illustrated diagram below is from the Skytrace to illustrate the attacker address movement of funds: https://www.certik.com/skytrace/eth:0x6162759edad730152f0df8115c698a42e666157f

Can the issue be found in the audit?

This issue can be found during the audit process. Security Professionals should be able to notice the following concerns during the audit process:

  1. A low-level call is invoked to transfer ETH.
  2. The code in the “borrow” logic violates the check-effect-interaction pattern.

Conclusion

The root cause of the Fei protocol exploit is the lack of a reentrancy protection mechanism implemented in the protocol. This is one of the well-known issues for this type of protocol, and this issue has been exploited or revealed prior to this incident but is still ignored by other similar protocols.

To learn more about the re-entrancy attack, CertiK recently shared a re-entrancy attack explainer article, which can be found here: https://certik.medium.com/what-is-a-reentrancy-attack-6516fefc001.
Below are a few of the recommendations for such a vulnerability:

  1. Strictly follow the check-effect-interaction pattern.
  2. Re-entrancy protection modifier should cover all corresponding functions.
  3. Introduce a third-party audit service to review the codebase.

--

--

--

Official Website: https://certik.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Shein.Com: How Do I Cancel My Order?

Cook Protocol Audits Native Token Smart Contract With CertiK

How Much Do Security Cameras Cost?

{UPDATE} Super Galaxy Baby Hack Free Resources Generator

Deploying a Secured Apache Server on Amazon AWS EC2

What is a DDoS attack?

OPEN REDIRECT EVERYWHERE

Local File Inclusion (LFI) Attack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CertiK

CertiK

Official Website: https://certik.com

More from Medium

Introducing Perp Covert!

QiDao Fundamentals Review: Week 51

Aave V3’s Price Oracle Manipulation Vulnerability

History of Reward Programs in DeFI