Skip to content
Business logic Audit finding Advanced

Incorrect approach to tracking the PnL of a DV

The vault's profit and loss tracking compares the current debt value to the last recorded debt basis, so after a small rebalancing the debt basis is updated and the vault appears not to be at a loss even though it still is. This allows users to burn all shares of the destination vault, locking in the loss for the vault and its shareholders.

xiaoming90 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-tokemak-judging/issues/589

Found by

xiaoming90

A DV might be incorrectly marked as not sitting in a loss, thus allowing users to burn all the DV shares, locking in all the loss of the DV and the vault shareholders.

Vulnerability Detail

Let $DV_A$ be a certain destination vault.

Assume that at $T0$, the current debt value (currentDvDebtValue) of $DV_A$ is 95 WETH, and the last debt value (updatedDebtBasis) is 100 WETH. Since the current debt value has become smaller than the last debt value, the vault is making a loss of 5 WETH since the last rebalancing, so $DV_A$ is sitting at a loss, and users can only burn a limited amount of DestinationVault_A's shares.

Assume that at $T1$, there is some slight rebalancing performed on $DV_A$, and a few additional LP tokens are deposited to it. Thus, its current debt value increased to 98 WETH. At the same time, the destInfo.debtBasis and destInfo.ownedShares will be updated to the current value.

Immediately after the rebalancing, $DV_A$ will not be considered sitting in a loss since the currentDvDebtValue and updatedDebtBasis should be equal now. As a result, users could now burn all the $DV_A$ shares of the LMPVault during withdrawal.

$DV_A$ suddenly becomes not sitting at a loss even though the fact is that it is still sitting at a loss of 5 WETH. The loss has been written off.

https://github.com/sherlock-audit/2023-06-tokemak/blob/main/v2-core-audit-2023-07-14/src/vault/libs/LMPDebt.sol#L275

Solidity
File: LMPDebt.sol
274:         // Neither of these numbers include rewards from the DV
275:         if (currentDvDebtValue < updatedDebtBasis) {
276:             // We are currently sitting at a loss. Limit the value we can pull from
277:             // the destination vault
278:             currentDvDebtValue = currentDvDebtValue.mulDiv(userShares, totalVaultShares, Math.Rounding.Down);
279:             currentDvShares = currentDvShares.mulDiv(userShares, totalVaultShares, Math.Rounding.Down);
280:         }

Impact

A DV might be incorrectly marked as not sitting in a loss, thus allowing users to burn all the DV shares, locking in all the loss of the DV and the vault shareholders.

Code Snippet

https://github.com/sherlock-audit/2023-06-tokemak/blob/main/v2-core-audit-2023-07-14/src/vault/libs/LMPDebt.sol#L275

Tool used

Manual Review

Recommendation

Consider a more sophisticated approach to track a DV's Profit and Loss (PnL).

In our example, $DV_A$ should only be considered not making a loss if the price of the LP tokens starts to appreciate and cover the loss of 5 WETH.

Discussion

sherlock-admin2

1 comment(s) were left on this issue during the judging contest.

Trumpero commented:

invalid, it is intended that sitting at a loss is considered by comparing the current debt to the latest recorded debt. Additionally, when it considers not sitting at a loss, it doesn't limit the value can pull but it only pulls enough tokens to withdraw. These withdrawn tokens will not be locked

xiaoming9090

Escalate

The main point of this report is to highlight the issues with the current algorithm/approach of tracking the PnL of a DV and marking it as sitting on a loss or not, which is obviously incorrect, as shown in my report. I have discussed this issue with the protocol team during the audit period, and the impact is undesirable, as shown below.

It should not be confused with how it is intended to be used in other parts of the protocol, which is unrelated. In addition, the intended approach does not mean that those issues/risks are acceptable and thus considered invalid. If the approach is incorrect, those issues must be flagged during the audit.

Thus, this is a valid High issue.

sherlock-admin2

Escalate

The main point of this report is to highlight the issues with the current algorithm/approach of tracking the PnL of a DV and marking it as sitting on a loss or not, which is obviously incorrect, as shown in my report. I have discussed this issue with the protocol team during the audit period, and the impact is undesirable, as shown below.

It should not be confused with how it is intended to be used in other parts of the protocol, which is unrelated. In addition, the intended approach does not mean that those issues/risks are acceptable and thus considered invalid. If the approach is incorrect, those issues must be flagged during the audit.

Thus, this is a valid High issue.

You've created a valid escalation!

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.

Trumpero

Tks for the clarification @xiaoming9090 Please review this issue as well @codenutt

codenutt

Tks for the clarification @xiaoming9090 Please review this issue as well @codenutt

Yup agree with @xiaoming9090 here.

Evert0x

Planning to accept escalation and make issue high severity

@Trumpero

Trumpero

Agree with the high severity

Evert0x

Result: High Unique

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by xiaoming90. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 loss check compares the current debt value to the last recorded debt basis, which is updated during rebalancing, so a loss can be hidden after a small deposit.

What the attacker needed

A destination vault is at a loss; A slight rebalancing occurs; User withdraws during the window

The fix

The text recommends a more sophisticated approach to track a destination vault's profit and loss, where the vault should only be considered not making a loss if the price of the LP tokens starts to appreciate and cover the loss.

How it was found

  1. At T0, the current debt value is 95 WETH and the last debt value is 100 WETH, so the vault is at a loss and users can only burn a limited amount of shares.
  2. At T1, a slight rebalancing deposits a few additional LP tokens, increasing the current debt value to 98 WETH.
  3. The debt basis and owned shares are updated to the current value.
  4. Immediately after rebalancing, the current debt value equals the updated debt basis, so the vault is not considered at a loss.
  5. Users can now burn all shares of the destination vault during withdrawal.

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.

A destination vault is like a savings account that can gain or lose value. The system checks if it is losing money by comparing the current value to the last recorded value. After a small deposit, the recorded value is updated, so the system thinks there is no loss even though the vault is still worth less than before. This lets users take out all their shares, leaving the loss with the remaining shareholders.

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 xiaoming90

View all

122 more disclosed reports, best first.

More from Tokemak

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.