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.
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
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
uint256 balanceBefore = getVaultBalance();
vaultCurrency.safeTransferFrom(msg.sender, address(this), _amount);
uint256 balanceAfter = getVaultBalance();
uint256 amount = balanceAfter - balanceBefore;
Discussion
sjoerdsommen
duplicate with #262 #66 #45
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.