If oracle is set for ERC777 token, re-entrancy is possible to steal all LToken funds
If an oracle is set for an ERC777 token, a re-entrancy in Account.sweepTo allows an attacker to borrow funds and have them withdrawn along with all account assets without health checks, leaving the account with debt and draining LToken funds.
panprog Follow with xiaoming90, Tutturu and 3 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-08-sentiment-judging/tree/main/076-M
Found by
panprog, xiaoming90, Tutturu, devtooligan, Bahurum, Czar102
Summary
If oracle is set for any ERC777 or similar token (tokens which call receiver's hook after receiving it), re-entrancy in Account.sweepTo allows to borrow funds, which are immediately withdrawn along with all account assets without any health checks, leaving account with 0 assets and big debt, making it possible to drain all LToken funds.
Vulnerability Detail
If oracle is set for ERC777 token, it's possible to add these tokens to assets (even if collateral or controller allowed token for them is not set, using the bug in uniswap v2 controller for removeLiquidity, which doesn't check if tokenIn is allowed).
Once ERC777 token is added to assets, malicious user can close his account, which calls Account.sweepTo, which has multiple re-entrancy scenarios. As ERC777 token will call user's hook after receiving these tokens, the following actions are possible in the hook to steal funds:
- User can borrow funds, exit reentrancy, and all borrowed tokens along with all account assets will be immediately transfered to user without any health checks.
- For tokens which were in
assetslist before theERC777token,hasAsset[]is set to false even though the token is still in the assets list. Depositing these tokens again will add them to assets again, counting them twice in account balance calculations, which allows to borrow even more funds. - Ether is transferred back to user after all the
ERC20tokens, so ether can also be used to borrow funds against, and it will also be transerred back to user along with all theERC20tokens.
List of bugs used in the attack:
- Uniswap v2 controller in
removeLiquiditydoesn't check if tokens received are allowed. It seems to assume that account can only have allowed uni v2 lp tokens (as it's checked inaddLiquidity), however any lp tokens can easily be transferred to account directly (not viaexec). This makes it possible to callexectoremoveLiquidityand add ANY tokens to account's assets list (and if oracle is set for the token,accountManager.execwill succeed as it doesn't check for asset tokens to be allowed as collateral)
Account.sweepTois very vulnerable to re-entrancy. While it assumes that asset tokens are ERC20, if ERC20-compatible tokens with callback hooks are allowed (such asERC777tokens), these tokens can re-enter to steal funds.
Steps to steal funds from LToken:
- OpenAccount
- If
ERC777token is allowed as collateral, depositERC777token (such asimBTC, which is pegged 1:1 to BTC and is ERC777). IfERC777is not allowed as collateral, but the oracle for this token is set up, then use bug 1: 2.1. Deploy uniswap v2 poolUSDC-imBTC, add liquidity with minimumUSDCandimBTC2.2. Send uniswap v2 LP token to account's address 2.3. CallAccountManager.exectoremoveLiquidityfrom uniswap v2 LP token, abusing bug 1 and receiving small amounts ofUSDC+imBTC(tokensInare set and bothUSDCandimBTCare added to account'sassets) - Deposit
0 DAIto addDAIto accountassets(this is to includeDAIinsweepToat the last index, which will make it possible to borrowDAIin re-entrancy and immediately receive all of it without health checks) - Close account
- Close account will performs
account.sweepToas the last step, whereUSDCis transferred to user, thenimBTCis transferred to user, which callstokensReceivedon user's contract allowing re-entrancy. IntokensReceived: 5.1. CallAccountManager.openAccountto re-gain access to account (since it's closed) - this restores account access 5.2. Deposit1 etherto account 5.3. Borrow4000 DAI5.4. Exit re-entrancy sweepTocontinues and sends4000 DAIto user, then deletesassetsand sends1 etherto user.- At this point user has received back its
1 etherand4000 DAI, leaving account with0assets and4000 DAIdebt.
This scenario can be made even more capital efficient to steal more money if user deposits 1000 USDC instead of 1 ether, which will add USDC to assets, making his balance 2000 instead of 1000, allowing to borrow 8000 DAI for the same deposited amount. If this is repeated a few times, it's possible to steal millions with only a small initial capital.
Impact
If any ERC777 (or similar) token oracle is added, it's very easy to steal all funds from all LTokens which you can borrow from.
Code Snippet
Create folder ./protocol/src/test/integrations/attacks and put these 2 test files there:
https://gist.github.com/panprog/2f16348325303869f7d84653cf99fba1
Tool used
Manual Review
Recommendation
- Add ReEntrancy guard from openzeppelin to main entry functions, mostly all functions in
AccountManager. - Add correct token checks to uniswap v2 controller.
Sentiment Team
We found an additional issue reported by a community member which was not a real exploit but exploited a vulnerability in the logic. Would request the auditors to take a look. PR here.
Lead Senior Watson
Fixed.
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
Likely the combination of missing re-entrancy protection in Account.sweepTo and a missing token check in the Uniswap v2 controller's removeLiquidity function.
Impact
If any ERC777 (or similar) token oracle is added, it is very easy to steal all funds from all LTokens which you can borrow from.
What the attacker needed
Oracle set for an ERC777 token; attacker can deploy a contract; attacker can create a Uniswap v2 pool
How it was found
- Open an account.
- If ERC777 is allowed as collateral, deposit it; otherwise, use the Uniswap v2 removeLiquidity bug to add ERC777 tokens to assets.
- Deposit 0 DAI to add DAI to the account's assets list.
- Close the account, which triggers sweepTo and transfers tokens.
- During the ERC777 token transfer callback, re-enter to open a new account, deposit 1 ether, and borrow 4000 DAI.
- Exit re-entrancy; sweepTo continues, sending the borrowed DAI and the deposited ether to the user, leaving the account with debt.
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.
This report describes a flaw in a lending protocol where a special type of token (ERC777) can trigger a callback during a transfer. That callback lets the attacker re-enter the protocol and borrow money that is then sent out without the usual safety checks, leaving the account with debt and stealing from the pool.
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.