Compound exchange rate can be manipulated to withdraw more underlying tokens from NotionalV3
The exchange rate of Compound V2 cTokens can be manipulated by donating underlying tokens, inflating the value of Notional's prime cash holdings. An attacker could flash loan funds, deposit into Notional, donate to the cToken contract to inflate the exchange rate, and then withdraw more than deposited, profiting at the expense of idle funds.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/122
Found by
mstpr-brainbot
Summary
Prime cash underlying values can be manipulated via donating the underlying to cToken contract to inflate the cToken price and then withdrawing from Notional for more.
Vulnerability Detail
Prime supply underlying value is calculated via few formulas and math operations let's check them:
To calculate the underlying interest rate: (currentUnderlyingValue - lastUnderlyingValue) / lastUnderlyingValue which we can say that underlying value can be in forms of DAI or cDAI (since the other protocol handlers are not in the audit scope I'll treat it as there is only CompoundV2 available for prime cash external lenders) We can derive this formula for more context: (((exchangeRate * totalCDAIHoldings) + totalDAIHolds) - ((previousExchangeRate * totalCDAIHoldings) + totalDAIHolds)) / ((previousExchangeRate * totalCDAIHoldings) + totalDAIHolds) = underlyingInterestRate
To calculate the underlying scalar: lastScalar * (1 + underlyingInterestRate) = underlyingScalar
To calculate the supply factor: supplyScalar * underlyingScalar = supplyFactor
To calculate the underlying final: primeCashValue * supplyFactor = underlyingFinal
now considering these, if someone can manipulate the currentUnderlying (total underlying tokens that Notional holds) up to some level that they can make the prime cash value higher, than there will be a profit. Currently, NotionalV3 deposits the funds to CompoundV2 which the exchange rate is calculated as follows:
(totalCash + totalBorrows - totalReserves) / totalCDAISupply
where as totalCash is the balanceOf(token) in the cToken contract. This means that the donations can manipulate the exchange rate and make Notional V3 underlying oracle trick that the tokens worths more. That means the exchange rate can be calculated as this:
(airDropAmount + totalCash + totalBorrows - totalReserves) / totalCDAISupply
if we can find a scenario where underlyingFinal > airDropAmount + initialPrimeCash, someone can do a flash loan attack
Let's draw a scenario where this can happen , Assume cDAI has very low liquidity because Compound incentives the v3 usage, Notional is withdrawing its CompoundV2 positions slowly to DAI and getting ready to deploy them to CompoundV3. For this scenario case assume there are total of 100K cDAI which all of them hold by Notional (this can change, just for simplicity I am keeping it), now if attacker donates 50K DAI to cDAI attacker will make the new exchange rate 1.5. Here we go:
1- There are 220K DAI idle and 100K cDAI in Notional (for easiness let's assume 1cDAI == 1DAI) 2- Alice flashloans 4.83M DAI and deposits to Notional receiving 4.83M pDAI (we assume supplyFactor is 1, which is possible if we are early stages after migration, no interest accrued). Alice will make the total idle DAI balance in Notional as 5M after her deposit 3- Alice deposits the 50K DAI to cDAI contract to make the exchange rate 1.5 (100KDAI/100KCDAI was the initial state of cDAI, now 150K DAI / 100K CDAI = 1.5) 4- Alice withdraws the 4.83M pDAI for 5M DAI
Let's prove the math, lastUnderlying = (100K cDAI * 1 + 5M) = 5.1M
currentUnderlying = (100K cDAI * 1.5 + 5M) = 5.150M
underlyingInterestRate = (5.15M - 5.1M) / 5.1M = 0.045
underlyingScalar = 1 * (0.045+ 1) = 1.045
supplyScalar = 1 * 1.045 = 1.045
Result: Alice deposited 4.78M DAI which she got 4.78M pDAI for it, now 4.78M pDAI worths 4.78M * 1.045 = 5M DAI (approx)
Alice flashloaned 4.78M + 50K = 4.83M total flash loaned amount
5M - 4.83M = 170K DAI profit after her attack
In the end, Alice stole the 220K idle balance.
Impact
Although the above example considers the compound cDAI has very low liquidity, the same scenario could happen with big numbers but that would require Notional to hold lots of liquidity. Since compound is literally incentivizing the compoundv3 over compoundv2 I assumed that scenario is not far away from reality. We also assumed supplyScalar was 1, which is possible since the prime market will start as 1 but also it doesn't really matter since we multiply with lastScalar and supplyScalar on both sides of equation, again it was 1 for simplicity.
Code Snippet
Compound stuff
Tool used
Manual Review
Recommendation
Acknowledge that CompoundV2 exchange rate is exploitable via donating, if the CompoundV2 cTokens has low liquidity, donate attacks will be quite cheap which can cause pDAI manipulations aswell. If the CompoundV2 cToken liquidity goes below some level abort the CompoundV2 strategy and migrate to other money markets where liquidity is decent and exchange rate is not manipulatable.
Discussion
jeffywu
This attack vector is known and mitigations have been put in place against it (we've already withdrawn from Compound V2). Furthermore, we've only listed large cap Compound V2 markets so this attack would be very expensive against prime cash.
mstpr
Escalate for 10 USDC
At the moment, the contest only includes the CompoundV2 adapter. From this, we could infer that the primary cash will be deposited into CompoundV2. In the future, CompoundV3 and Aave may also be integrated. However, within the current scope of the code, it only operates with CompoundV2. Consequently, we can safely assert that prime cash will be lent to CompoundV2. This is evident from the scope of the code, which includes all the necessary implementations such as rebalancing, redeeming, and depositing.
While I concur that the attack necessitates low liquidity in the CompoundV2 markets and that it's challenging to implement on-chain countermeasures, I believe this risk should be outlined by the protocol team in their documentation. Looking at the Sherlock docs, it's clear that the protocol team should account for possible attack vectors.
Which is exactly what this issue fits, the attack exists and protocol team did not include the attack in the docs. According to this, this finding should be a medium
sherlock-admin
Escalate for 10 USDC
At the moment, the contest only includes the CompoundV2 adapter. From this, we could infer that the primary cash will be deposited into CompoundV2. In the future, CompoundV3 and Aave may also be integrated. However, within the current scope of the code, it only operates with CompoundV2. Consequently, we can safely assert that prime cash will be lent to CompoundV2. This is evident from the scope of the code, which includes all the necessary implementations such as rebalancing, redeeming, and depositing.
While I concur that the attack necessitates low liquidity in the CompoundV2 markets and that it's challenging to implement on-chain countermeasures, I believe this risk should be outlined by the protocol team in their documentation. Looking at the Sherlock docs, it's clear that the protocol team should account for possible attack vectors.
Which is exactly what this issue fits, the attack exists and protocol team did not include the attack in the docs. According to this, this finding should be a medium
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
hrishibhat
Result: Medium Unique Considering this issue as a valid medium, based on the conditions required and given that the Compound v2 integration was still in scope for the contest.
sherlock-admin
Escalations have been resolved successfully!
Escalation status:
- mstpr: accepted
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 prime cash underlying value calculation relies on the Compound V2 cToken exchange rate, which can be inflated by direct token donations to the cToken contract.
Impact
An attacker could steal idle funds from Notional, but the attack requires low liquidity in the Compound V2 market and the protocol team stated that mitigations are in place and large-cap markets make it expensive.
What the attacker needed
Low liquidity in Compound V2 cToken market; Attacker can flash loan large amount; Notional holds significant idle funds
The fix
The protocol team should acknowledge the risk and abort the Compound V2 strategy if cToken liquidity falls below a safe level, migrating to other money markets with decent liquidity and non-manipulatable exchange rates.
How it was found
- Flash loan a large amount of DAI.
- Deposit the borrowed DAI into Notional to receive prime cash tokens (pDAI).
- Donate DAI directly to the Compound V2 cDAI contract to inflate the cDAI exchange rate.
- Withdraw the pDAI from Notional, receiving more DAI than originally deposited due to the inflated exchange rate.
- Repay the flash loan and keep the profit.
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.
Notional is a DeFi platform that holds user deposits in Compound, a lending protocol. Compound tracks the value of deposits using an exchange rate that depends on the total balance of tokens in its contract. An attacker can artificially increase that balance by donating tokens directly to the contract, which makes the exchange rate go up. Then, when the attacker withdraws from Notional, they get more than they put in, stealing other users' funds.