Skip to content
Business logic Audit finding Advanced

Loss of funds in an underlying protocol would cause catostrophic loss of funds for swivel

The yield calculation for matured positions assumes the underlying protocol's exchange rate never decreases, so if it drops, a user can repeatedly redeem their matured position and drain all funds. An attacker could also mint nTokens for an already expired market and then exploit the same flaw.

0x52 Follow

Originally disclosed on Code4rena

Submitted by 0x52

Loss of all user funds.

Proof of Concept

This exploit stems from a quirk in the way that exchange rate is tracked for matured positions. We first need to breakdown how interest is calculate for a matured position.

https://github.com/code-423n4/2022-07-swivel/blob/daf72892d8a8d6eaa43b9e7d1924ccb0e612ee3c/VaultTracker/VaultTracker.sol#L123-L132

In L124 the yield for a matured position is calculated as the difference between the previous exchange ratio and the maturity rate. This counts on the fact the exchange rate of the underlying protocol never decreases, as it always set the previous exchange rate to the current exchange rate after yield is calculated regardless of whether it is mature or not. The assumption is that the current exchange rate will always be greater than or equal to the maturity exchange rate. If it is higher then L124 will revert due to underflow and if it is equal then L124 will return yield = 0. The issue comes in when this assumption is invalidated. This would happen were the underlying protocol to lose value (hacked, UST depeg, etc.). With the loss of value, the exchange rate would drop, allowing a user to repeatedly redeem their matured position until all funds from the affected protocol are drained.

Example: Imagine a yearn vault that takes USDC as the underlying token. It’s current price per share is 1.25e6 (1.25 USDC per vault share). Swivel has a recently expired VaultTracker.sol for this yearn vault for which mature() is called, setting maturityRate = 1.25e6. Now let’s imagine that one small strategy deployed by the vault is compromised and the vault loses 4% of it’s USDC. Now the vault will return 1.2e6 as the price per share. When calling redeemInterest for the first time, vlt.exchangeRate will be updated to 1.2e6 in L132. The next time redeemInterest is called it will pay the difference between 1.25e6 (maturityRate) and 1.2e6 (vlt.exchangeRate). redeemInterest can be called repeatedly like this until all USDC deposited in the yearn vault by Swivel users has been drained.

Additionally the user in question would not even need to have an expired position before the loss of funds occurred. SplitUnderlying in Swivel.sol has no checks to keep a user from minting previously expired market. After the loss of funds occurred the malicious user could use SplitUnderlying to create nTokens for the expired market, then carry out the exploit

Recommended Mitigation Steps

The impact of such an event could be decreased with changes. In splitUnderlying add:

require(block.timestamp < m)

This prevents nTokens from being created after expiration which dramatically reduces the ability to take advantage of the opportunity. As for redeemInterest, add the following line after L124:

vlt.notional = 0

This would clear the notional balance of the user when redeeming after maturity, making it impossible to call repeatedly and reduces the chances that any users have a notional balance to exploit it should an event like this happen.

JTraversa (Swivel) disputed, disagreed with severity and commented:

I’m unsure if this is a proper report / should be accepted given similar issues exist when you integrate nearly any DeFi primitive.

You have risks that your integrated protocols could break, but make assumptions that they will not while placing failsafes like pause mechanism in your protocol.

That said, the suggestions dont really solve anything, though we may end up taking them to heart?

If the issue is that funds are lost when integrated protocol funds are lost and an attacker can then abuse these methods then the solutions provided just make someone spend more on gas in whatever flash loan tx they use to drain the protocol or slightly reduce the likelihood than an attacker will be in the correct position.

bghughes (judge) commented:

Note that this builds on #79 and I believe it is a good issue. The warden lays out a clear case in which Vault funds can be drained if these conditions occur:

  • A market has matured
  • The exchange rate of the underlying vault token drops below the set maturityRate.

I agree that integrations can break @JTraversa, but given that Swivel aims to be the place people can go to deposit vault tokens and get ZCB + yield tokens (including allowing 4626 vaults) this is an attack vector IMO. Moreover, Yearn uses some discrete strategies in which the price per share realistically could reduce or even be manipulated in atomicity in a flash loan attack (which may seem counterintuitive bc generally lending vaults are supposed to be “up only”).

Confirming this as a High-Risk issue as an attack vector to drain all vault funds is present. Please let me know Sponsor if you have any questions.

robrobbins (Swivel) resolved and commented:

Much of this is addressed with: https://github.com/Swivel-Finance/gost/pull/417 (cannot mint post maturity).

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

Downgrading to Medium as it requires external factors for this situation to present itself and the attack to occur.


The original report as published on Code4rena, by 0x52. 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 yield calculation for matured positions assumes the underlying exchange rate never decreases, and it does not clear the user's notional balance after redemption, allowing repeated claims when the rate drops.

Impact

If the underlying protocol loses value, an attacker could drain all funds deposited by Swivel users; the judge downgraded severity to Medium because external factors are required.

What the attacker needed

A market has matured; Underlying exchange rate drops below maturity rate; Attacker can call redeemInterest repeatedly

How it was found

  1. A market matures and the maturity rate is set to the current exchange rate.
  2. The underlying protocol loses value, so its exchange rate drops below the maturity rate.
  3. The attacker calls redeemInterest, which updates the stored exchange rate to the lower value.
  4. The attacker calls redeemInterest again, receiving the difference between the maturity rate and the lower exchange rate.
  5. The attacker repeats this until all funds are drained.
  6. Alternatively, the attacker uses SplitUnderlying to mint nTokens for an already expired market and then performs the same repeated redemption.

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 is about a smart contract that tracks interest for positions that have reached their maturity date. The code assumes the value of the underlying investment only goes up; if it goes down, the same position can be redeemed over and over to take more money than it should. An attacker could use this to empty the contract's funds.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0x52

View all

211 more disclosed reports, best first.

More from Swivel v3 contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.