Resources

People often ask me "How did you learn how to hack?" The answer: by reading. This page is a collection of the blog posts and other articles that I have accumulated over the years of my journey. Enjoy!

The Fei Protocol Hack (April 2022)- 854

Rob BehnkePosted 3 Years Ago
  • Fei Protocol is a direct incentive stable coin which is undercollateralized and fully decentralized.
  • A recent update to their code fixed a reentrancy. This occurs when the check-effect-interaction pattern is not used. In other words, doing some action then updating the state of the contract later.
  • Why is this a bad flow? When sending money, or performing actions against a smart contract, a hook can be set on that interaction. Then, within the hook, the code can be called once again, putting the contract into a very weird state.
  • In this case, not all of the reentrancy bugs were fixed. The attacker abused two functions in this case: exitMarket and borrow. exitMarket verifies that a deposit is no longer being used as collateral, then withdraws it. borrow lets a user take out a loan.
  • The reentrancy attack was performed by calling borrow using a smart contact. When the function sends the loaned amount of money, it has NOT updated the internal state of that the asset is being used as collateral. As a result, a nested call can to exitMarket extracts the collateral for the loan.
  • This leads to the ability to extract the deposit used as collateral for the loan. This effectively allows infinite money to be borrowed, since nothing is used as collateral. $80 million was stolen using this technique.