Skip to content
Smart contracts Audit finding Intermediate

VaultTracker miscalculates compounding interest

VaultTracker calculated new interest only on the notional amount, ignoring previously accrued redeemable interest, so nToken holders received less yield than they should. An attacker could not directly steal funds, but the protocol leaked value to other users over time.

GimelSec Follow

Originally disclosed on Code4rena

Submitted by GimelSec

https://github.com/code-423n4/2022-07-swivel/blob/main/VaultTracker/VaultTracker.sol#L65

https://github.com/code-423n4/2022-07-swivel/blob/main/VaultTracker/VaultTracker.sol#L100

https://github.com/code-423n4/2022-07-swivel/blob/main/VaultTracker/VaultTracker.sol#L130

https://github.com/code-423n4/2022-07-swivel/blob/main/VaultTracker/VaultTracker.sol#L172

https://github.com/code-423n4/2022-07-swivel/blob/main/VaultTracker/VaultTracker.sol#L191

https://github.com/code-423n4/2022-07-swivel/blob/main/VaultTracker/VaultTracker.sol#L228

Impact

VaultTracker neglect previously accrued interest while attempting to calculate new interest. This causes nToken holders to receive less yield than they should.

All functions within VaultTracker that calculate interest are affected, including addNotional, removeNotional, redeemInterest, transferNotionalFrom and transferNotionalFee.

Proof of Concept

Consider the case where some user N tries to initiate a vault at 3 specific moments where cToken exchange rate is 5/10/20 respectively. The corresponding market stays active and has not reached maturity. Additionally, N selects his premium volume to make principalFilled match the cToken exchange rate during each call to initiateVaultFillingZcTokenInitiate. We recognize those exchange rates are most likely unrealistic, but we chose those for ease of demonstrating the bug. We also assume fees to be 0 for simplicity.

For the first call to Swivel.deposit

  • a = 5
  • exchangeRate = 5

Assuming no additional fees while minting cToken, N will receive cToken\ for his 5 underlying tokens.

For the matching call to VaultTracker.addNotional

  • a = 5
  • vlt.notional = 0
  • exchangeRate = 5

Since this is the first time adding nToken to the vault, there is no need to consider any accumulated interests, and we can assign a directly to vlt.notional.

The result will be

  • vlt.notional = 5
  • vlt.redeemable = 0
  • cToken held by Swivel = 1

For the second call to `Swivel.deposit, we have

  • a = 10
  • exchangeRate = 10

The matching call to VaultTracker.addNotional has

  • a = 10
  • vlt.notional = 5
  • vlt.redeemable = 0
  • exchangeRate = 10
  • vlt.exchangeRate = 5

The yield is derived from ((exchangeRate * 1e26) / vlt.exchangeRate) - 1e26 = ((10 * 1e26) / 5) - 1e26 = 1e26 Applying this to vlt.notional, we get interest = 1e26 * 5 / 1e26 = 5

This results in

  • vlt.notional = 5+10 = 15
  • vlt.redeemable = 0+5 = 5
  • cToken held by Swivel = 1+1 = 2

Now comes the last call to Swivel.deposit, where

  • a = 20
  • exchangeRate = 20

VaultTracker.addNotional has

  • a = 20
  • vlt.notional = 15
  • vlt.redeemable = 5
  • exchangeRate = 20
  • vlt.exchangeRate = 10

yield = ((20 * 1e26) / 10) - 1e26 = 1e26 interest = 1e26 * 15 / 1e26 = 15

So we finally end up with

  • vlt.notional = 15+20 = 35
  • vlt.redeemable = 5+15 = 20
  • cToken held by Swivel = 2+1 = 3
Swivel{
  ...
  function deposit(uint8 p, address u, address c, uint256 a) internal returns (bool) {
    ...
    if (p == uint8(Protocols.Compound)) { // TODO is Rari a drop in here?
      return ICompound(c).mint(a) == 0;
    }
    ...
  }
  ...
}

VaultTracker{
  ...
  function addNotional(address o, uint256 a) external authorized(admin) returns (bool) {
    uint256 exchangeRate = Compounding.exchangeRate(protocol, cTokenAddr);

    Vault memory vlt = vaults[o];

    if (vlt.notional > 0) {
      uint256 yield;

      // if market has matured, calculate marginal interest between the maturity rate and previous position exchange rate
      // otherwise, calculate marginal exchange rate between current and previous exchange rate.
      if (maturityRate > 0) { // Calculate marginal interest
        yield = ((maturityRate * 1e26) / vlt.exchangeRate) - 1e26;
      } else {
        yield = ((exchangeRate * 1e26) / vlt.exchangeRate) - 1e26;
      }

      uint256 interest = (yield * vlt.notional) / 1e26;
      // add interest and amount to position, reset cToken exchange rate
      vlt.redeemable += interest;
      vlt.notional += a;
    } else {
      vlt.notional = a;
    }
    vlt.exchangeRate = exchangeRate;
    vaults[o] = vlt;

    return true;
  }
    ...
}

Now take a step back and think about the actual value of 3 cToken when exchangeRate = 20, it should be pretty obvious that the value tracked by VaultTracker = 35 + 20 = 55 is lesser than the actual value of cToken held by Swivel = 20*3 = 60.

This is due to VaultTracker neglecting that previously accrued interest should also be considered while calculating new interest.

Recommended Mitigation Steps

For all interest calculations, use vlt.notional + vlt.redeemable instead of just vlt.notional as yield base.

JTraversa (Swivel) disputed and commented:

I believe that this would be valid if the redeemable was not redeemable by the user at any point in time.

While interest accrues, it accrues to the redeemable balance which is withdrawn at any time.

That said, in most cases, the math required to store and do the additional calculation marginal interest on the redeemable balance is largely a UX consideration? Should users be required to redeem their redeemable to earn interest or should it be compounded naturally though bloat tx costs?

Should we force additional costs on a per transaction basis (that likely costs more than the interest itself for the vast majority of users), or should we assume that once significant enough redeemable is accrued to earn reasonable further interest, it will be redeemed by the user.

(e.g. with example being 400% interest, and assuming an (optimistic) 5% APY is possible, the example would take ~28 years to replicate, and gas costs for transactions in that 28 years would be significant).

JTraversa (Swivel) disagreed with severity and commented:

Thought about this one a bit more and it might slip in as acknowledged and disagreed with severity as it’s more value leakage than anything else.

It’s a design decision, but could still be considered a detriment so perhaps not worth disputing all together?

robrobbins (Swivel) commented:

This is a design feature. We could just as easily dismiss this all together by stating it is now documented that .notional is non compounding. I don’t think we should do that however as I agree with @JTraversa here that some acknowledgement should happen for bringing this up. But it is not a risk in any situation.

robrobbins (Swivel) resolved:

Addressed: https://github.com/Swivel-Finance/gost/pull/427

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

Going to reduce the severity on this to Medium as it has some pretty large external factors to end up in a scenario where it leaks any real value.


The original report as published on Code4rena, by GimelSec. 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

Likely the interest calculation uses only the notional balance as the base, omitting the already accrued redeemable interest, so compounding is not applied.

Impact

nToken holders could receive less yield than they should because interest is not compounded on previously accrued redeemable amounts; the judge noted large external factors are needed for any real value leakage.

What the attacker needed

Multiple deposits over time; cToken exchange rate increases; fees assumed zero for simplicity

The fix

The interest calculation should use the sum of notional and redeemable as the base, and the project addressed it in a pull request.

How it was found

  1. User deposits 5 underlying tokens when cToken exchange rate is 5, receiving 1 cToken; VaultTracker sets notional to 5.
  2. User deposits 10 underlying tokens when exchange rate is 10, receiving 1 more cToken; VaultTracker calculates interest on notional only, adding 5 to redeemable and 10 to notional.
  3. User deposits 20 underlying tokens when exchange rate is 20, receiving 1 more cToken; VaultTracker again calculates interest on notional only, adding 15 to redeemable and 20 to notional.
  4. After three deposits, VaultTracker records total value 55 (notional 35 + redeemable 20), but the 3 cTokens are actually worth 60 at exchange rate 20, showing a shortfall.

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.

When you deposit money, the protocol tracks how much you put in (notional) and how much interest you have earned (redeemable). The bug is that when new interest is calculated, it only looks at the original deposit and forgets the interest already earned, so you earn less than you should. This is a design flaw that slowly loses value for users.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by GimelSec

View all

18 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.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.