If the accounted token balance is higher than actual token balance some transfers can send "0" tokens to destination
The vault's transfer function skips the actual token transfer when the contract's balance is insufficient, but still updates the internal accounting as if the transfer occurred. In extreme market conditions, this can lead to users receiving zero tokens while the protocol's records show a successful transfer, causing insolvency.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/100
Found by
mstpr-brainbot
Summary
In extreme cases, such as when late liquidations incur losses to liquidity providers, the actual balance will not exist in the vaults. The transfer will not revert due to how it's implemented, resulting in no token transfer while the storage accounting remains unchanged. This will lead to insolvency.
Vulnerability Detail
When tokens are insufficient in the vaults, the token amount might never be sent to the users/pools without a revert.
For example, in cases of late liquidation during extreme market conditions, closing the liquidated positions will update the corresponding storage balances in pools and vaults. However, if there are insufficient funds in the pool, the tokens are not transferred to their destination, and the amount not sent is not checked. As seen in the DecreasePositionProcess::_settleCrossAccount and _settleIsolateAccount functions, VaultProcess.transferOut is called with the boolean set to "true," which will skip the token transfer if the contract's token balance is insufficient.
Consequently, the tokens are assumed to be sent in the exact amount reflected in the storage variables. However, the actual tokens transferred(0) can be significantly different, leading to insolvency.
Impact
This is definitely a problem in extreme market conditions where the pool incurs losses from undercollateralized borrowed positions or very high funding fees. In such cases, closing positions will not be tracked, and the actual balance transferred might be "0" for some users because there are not enough tokens in the vault. Since this would require a volatile market for the asset, I will label this as medium.
Code Snippet
Tool used
Manual Review
Recommendation
Also check what's transferred and if there is a leak account it. Also, if the users requested is not enough instead of not sending any tokens send the existing balance OR socialize the losses in such cases and make sure no account can close their position without incurring the loss.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/30
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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
The transfer function does not verify that the actual token transfer succeeded when the contract's balance is insufficient, and it still updates the storage accounting.
Impact
In extreme market conditions, users closing positions may receive zero tokens while the protocol's accounting remains unchanged, leading to insolvency.
What the attacker needed
Extreme market conditions; insufficient vault balance
The fix
The protocol team fixed this issue in the referenced PR/commit.
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.
This is a smart contract bug where the system thinks it sent tokens but actually sent nothing because the vault didn't have enough tokens. The code updates the internal ledger but skips the real transfer, so users can lose funds without any error.
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.