usedFunds can be greater than totalFunds in contract KangarooVault , which leads to KangarooVault being unable to close its trades and users being unable to withdraw
The KangarooVault contract tracks usedFunds and totalFunds without ensuring totalFunds stays at least usedFunds. If someone sends funds directly to the vault, usedFunds can exceed totalFunds, causing underflow reverts that prevent closing trades and user withdrawals.
Originally disclosed on Code4rena
Submitted by KIntern_NA
KangarooVault contract can’t close the trades and users can’t withdraw from it, then users and KangarooVaults will lose a lot of funds.
Proof of concept
- In contract
KangarooVault,usedFundsis the uint256 variable which tracks the funds utilitized from the vault. AndtotalFundsis the uin256 variable which tracks the funds claimed by vault from profits and users’ depositing. - Contract
KangarooVaulthas no check iftotalFunds>=usedFundswhenusedFundsis increased (transfer from vault) ortotalFundsis decreased (transfer to vault). - If someone transfers funds directly into the vault,
usedFundscan be greater thantotalFundsbecause the vault can transfer out more thantotalFunds. - When
usedFunds>totalFunds, KangarooVault can not close its trades because it will revert on underflow in function_resetTrade:
function _resetTrade() internal {
...
totalFunds -= usedFunds;
...
}
- When
usedFunds>totalFunds, user can’t not withdraw by functionprocessWithdrawalQueuebecause it will revert on underflow.
function processWithdrawalQueue(uint256 idCount) external nonReentrant {
for (uint256 i = 0; i < idCount; i++) {
...
uint256 availableFunds = totalFunds - usedFunds;
...
Scenario:
- A deposit 1000 SUSD, then
totalFunds= 1000 SUSD,usedFunds= 0 - B transfer directly 1000 SUSD to the KangarooVault, that doesn’t change
totalFundsandusedFunds - KangarooVault opens a short position and uses 2000 SUSD to increase the margin, then
usedFunds= 2000 - After that,
usedFunds>totalFunds(2000 > 1000) then KangarooVault can’t close its position
Recommended Mitigation Steps
Should add the checks if totalFunds >= usedFunds when increasing usedfunds or decreasing totalFunds in contract KangarooVault.sol.
mubaris (Polynomial) acknowledged, but disagreed with severity
Dravee (judge) decreased severity to Medium
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 contract does not check that totalFunds is at least usedFunds when usedFunds increases or totalFunds decreases, so a direct transfer can make usedFunds exceed totalFunds.
Impact
KangarooVault cannot close its trades and users cannot withdraw, so users and the vault could lose funds.
What the attacker needed
Attacker can transfer funds directly to the vault; vault opens a position using more than totalFunds
The fix
The contract should check that totalFunds is at least usedFunds when usedFunds is increased or totalFunds is decreased.
How it was found
- A deposits 1000 SUSD, setting totalFunds to 1000 and usedFunds to 0.
- B transfers 1000 SUSD directly to the KangarooVault, which does not change totalFunds or usedFunds.
- KangarooVault opens a short position and uses 2000 SUSD to increase the margin, setting usedFunds to 2000.
- Now usedFunds (2000) is greater than totalFunds (1000), so KangarooVault cannot close its position.
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 vault contract keeps two numbers: totalFunds (money it thinks it has) and usedFunds (money it has lent out or used). If someone sends money straight to the contract, the contract does not update totalFunds, so it can later use more than it recorded. When it tries to subtract usedFunds from totalFunds, the maths goes negative and the transaction fails, locking the vault.
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.