Skip to content
DoS Audit finding Advanced

BalancedVault.sol: claim can be impossible due to unsigned integer underflow

The claim function in BalancedVault can revert due to an unsigned integer underflow when the vault's total assets at epoch are zero, preventing users from claiming their assets. This can occur after a liquidation, and the user must send additional funds to resolve it, resulting in a loss of funds.

roguereddwarf Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-05-perennial-judging/issues/57

Found by

roguereddwarf

Summary

When a user redeems his shares, he then needs to call the claim function to actually claim his assets.

This will downstream call the _rebalancePosition function with the claimAmount as an argument.

Under some circumstances the _rebalancePosition function can revert due to unsigned integer underflow, making it impossible to claim assets.

This condition is not permanent but can only be solved by sending additional assets to the Vault causing a loss of funds.

Vulnerability Detail

Assume the following situation:

Text
_assets() = 5e18
_totalUnclaimed = 10e18

unclaimed of user1 = 5e18

When user1 calls claim, pro-rata claiming applies and the actual claimAmount is equal to 5e18*10e18/10e18=5e18.

Now let's look at the vulnerable line: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L474

_totalAssetsAtEpoch would be equal to 0: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L684-L690

Thereby the line in _rebalancePosition reverts due to unsigned integer underflow (0 - claimAmount),

You can add the following test to BalancedVaultMulti.test.ts which is a modified version of the existing gracefully unwinds upon insolvency test:

JavaScript
it('reverts upon pro rata claim', async () => {
        // 1. Deposit initial amount into the vault
        await vault.connect(user).deposit(utils.parseEther('50000'), user.address)
        await vault.connect(user2).deposit(utils.parseEther('50000'), user2.address)
        await updateOracle()
        await vault.sync()

        // 2. Redeem most of the amount, but leave it unclaimed
        
        await vault.connect(user).redeem(utils.parseEther('40000'), user.address)
        await vault.connect(user2).redeem(utils.parseEther('20000'), user2.address)
        await updateOracle()
        await vault.sync()

        // 3. An oracle update makes the long position liquidatable, initiate take close
        await updateOracle(utils.parseEther('20000'))
        await long.connect(user).settleAccount(vault.address)
        await short.connect(user).settleAccount(vault.address)
        await long.connect(perennialUser).closeTake(utils.parseEther('700'))
        await collateral.connect(liquidator).liquidate(vault.address, long.address)

        // // 4. Settle the vault to recover and rebalance
        await updateOracle() // let take settle at high price
        await updateOracle(utils.parseEther('1500')) // return to normal price to let vault rebalance
        await vault.sync()
        await updateOracle()
        await vault.sync()

        // 6. Claim should be pro-rated
        await vault.claim(user2.address)
})

Impact

The user is at least temporarily unable to claim his assets and the situation needs to be solved by sending additional assets to the Vault (of which the user most likely does not get the majority back with the claim) -> loss of funds

Code Snippet

https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L211-L228

https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L472-L492

https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L684-L690

Tool used

Manual Review

Recommendation

In the _rebalancePosition function it must be ensured that the following line does not execute when claimAmount > _totalAssetsAtEpoch(context): https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L474

Discussion

sherlock-admin

Escalate for 10 USDC I also noticed this issue, thanks to roguereddwarf for good explanation. The reasons for not submitting are as follows:

  1. This situation is not permanent. _rebalance is called by multiple functions, but only in the claim function that claimAmount is not 0. So it will not affect syncAccount/deposit/redeem.
  2. This happens only after the vault is liquidated. This does make the user lose funds (unable to redeem) if no funds are subsequently deposited(from other users). But shouldn't this user take the risk of liquidation?

So this is not a valid M. I would like to hear everyone's opinion.

You've deleted an escalation for this issue.

roguereddwarf

Escalate for 10 USDC

I disagree with the first escalation and argue that this should remain a Medium severity issue. The situation is not permanent but can only be resolved by the user by sending additional funds (of which the user most likely does not get the majority back because they will also be allocated to other users).

And yes this only happens when the Vault got liquidated as can be seen from my test case as well. But this does not take away from the fact that this is a valid scenario. Liquidations are part of the normal operation of the Vault, I'm not making any unrealistic assumptions in this report.

Also the user DOES take the risk of liquidation which is reflected by only being able to claim pro-rata. But as I have showed the claiming fails altogether so he cannot claim the funds that he is entitled to after the liquidation.

sherlock-admin

Escalate for 10 USDC

I disagree with the first escalation and argue that this should remain a Medium severity issue. The situation is not permanent but can only be resolved by the user by sending additional funds (of which the user most likely does not get the majority back because they will also be allocated to other users).

And yes this only happens when the Vault got liquidated as can be seen from my test case as well. But this does not take away from the fact that this is a valid scenario. Liquidations are part of the normal operation of the Vault, I'm not making any unrealistic assumptions in this report.

Also the user DOES take the risk of liquidation which is reflected by only being able to claim pro-rata. But as I have showed the claiming fails altogether so he cannot claim the funds that he is entitled to after the liquidation.

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.

securitygrid

You convinced me, I agree. Thanks. @roguereddwarf

KenzoAgada

The original escalation has been deleted. All that remains is roguerddwarf's escalation which is valid (as it explained why original escalation was invalid).

No change is needed in issue's status.

jacksanford1

Ok, should remain a Medium.

jacksanford1

Result: Medium Unique Escalation was deleted.

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by roguereddwarf. 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 code subtracts the claim amount from the total assets at epoch without checking if the total is sufficient, causing an underflow when the total is zero.

Impact

The user is at least temporarily unable to claim his assets and the situation needs to be solved by sending additional assets to the Vault, causing a loss of funds.

What the attacker needed

Vault has been liquidated; user has unclaimed assets; total assets at epoch is zero

The fix

The code should be fixed to ensure that the subtraction does not execute when the claim amount exceeds the total assets at epoch.

How it was found

  1. Deposit funds into the vault from two users.
  2. Redeem most of the amount but leave it unclaimed.
  3. An oracle update makes the long position liquidatable, initiate take close.
  4. Settle the vault to recover and rebalance.
  5. Call claim, which should be pro-rated but reverts.

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 take your money out of a shared investment pool, you sometimes have to make a second request to actually receive it. In this case, a calculation error (a number going below zero) can stop that second request from working, so you cannot get your money until someone adds more money to the pool, which may cost you.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by roguereddwarf

View all

12 more disclosed reports, best first.

More from Perennial V2 Update #1

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.