Skip to content
Business logic Audit finding Intermediate

Wrong calculation of `balanceBefore` and `balanceAfter` in deposit method

The deposit function subtracted reserved funds from the vault balance both before and after the transfer, so the net amount calculation was wrong. When reserved funds exceeded the vault balance, the subtraction underflowed and the deposit could fail.

Ruhum Follow with chaduke and spyrosonic10

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/262

Found by

Ruhum, chaduke, spyrosonic10

Summary

Deposit method calculate net amount transferred from user. It use reservedFunds also in consideration when calculating balanceBefore and balanceAfter but it is not actually require.

Solidity
    uint256 balanceBefore = getVaultBalance() - reservedFunds;
    vaultCurrency.safeTransferFrom(msg.sender, address(this), _amount);
    uint256 balanceAfter = getVaultBalance() - reservedFunds;
    uint256 amount = balanceAfter - balanceBefore;

Vulnerability Detail

Deposit may fail when reservedFunds is greater than getVaultBalance()

Impact

Deposit may fail when reservedFunds is greater than getVaultBalance()

Code Snippet

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/MainVault.sol#L116-L118

Solidity
    uint256 balanceBefore = getVaultBalance() - reservedFunds;
    vaultCurrency.safeTransferFrom(msg.sender, address(this), _amount);
    uint256 balanceAfter = getVaultBalance() - reservedFunds;
    uint256 amount = balanceAfter - balanceBefore;

Tool used

Manual Review

Recommendation

Use below code. This is correct way of finding net amount transfer by depositor

Solidity
    uint256 balanceBefore = getVaultBalance();
    vaultCurrency.safeTransferFrom(msg.sender, address(this), _amount);
    uint256 balanceAfter = getVaultBalance();
    uint256 amount = balanceAfter - balanceBefore;

Discussion

sjoerdsommen

duplicate with #262 #66 #45

The original report as published on Sherlock, by Ruhum. 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 subtracted reservedFunds from the vault balance when measuring the before and after balances, which is unnecessary for computing the transferred amount and can underflow when reservedFunds exceeds the balance.

What the attacker needed

ReservedFunds exceeds vault balance

The fix

The balance before and after should be read without subtracting reservedFunds, so the net transferred amount is computed correctly.

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 smart contract keeps track of how much money it holds. This deposit code measured the vault balance twice, once before and once after the user's money arrived, and subtracted a reserved amount from both readings. If the reserved amount was larger than the actual balance, that subtraction went below zero, which the programming language treats as an error, so the deposit stopped working.

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 Ruhum

View all

55 more disclosed reports, best first.

More from Derby

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.