Withdrawing all `lv` before expiry will lead to lost funds in the Vault
0x73696d616f Follow with dimulski
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-cork-protocol-judging/issues/211
Found by
0x73696d616f, dimulski
Summary
VaultLib:redeemEarly() redeems users' liquidity vault positions, lv for Ra, before expiry. After expiry, it is not possible to deposit into the vault or redeem early.
Whenever all users redeem early, when it gets to the expiry date, VaultLib::_liquidatedLp() is called to remove the lp position from the AMM into Ra and Ct (redeem from the PSM into more Ra and Pa) and split among all lv holders.
However, as the total supply of lv is 0 due to users having redeemed all their positions via VaultLib::redeemEarly(), when it gets to VaultPoolLib::reserve(), it reverts due to a division by 0 error, never allowing the Vault::_liquidatedLp() call to go through.
As the Ds has expired, it is also not possible to deposit into it to increase the lv supply, so all funds are forever stuck.
Root Cause
In MathHelper:134, the ratePerLv reverts due to division by 0. It should calculate the rate after the return guard that checks if the totalLvIssued == 0.
Internal pre-conditions
- All users must redeem early.
External pre-conditions
None.
Attack Path
- All users redeem early via
Vault::redeemEarlyLv(). - The
Dsexpires and there is nolvtokens, making all funds stuck.
Impact
All funds are stuck.
PoC
The MathHelper separates liquidity by calculating first the ratePerLv, which will trigger a division by 0 revert.
function separateLiquidity(uint256 totalAmount, uint256 totalLvIssued, uint256 totalLvWithdrawn)
external
pure
returns (uint256 attributedWithdrawal, uint256 attributedAmm, uint256 ratePerLv)
{
// with 1e18 precision
ratePerLv = ((totalAmount * 1e18) / totalLvIssued);
// attribute all to AMM if no lv issued or withdrawn
if (totalLvIssued == 0 || totalLvWithdrawn == 0) {
return (0, totalAmount, ratePerLv);
}
...
}
Mitigation
The MathHelper should place the return guard first:
function separateLiquidity(uint256 totalAmount, uint256 totalLvIssued, uint256 totalLvWithdrawn)
external
pure
returns (uint256 attributedWithdrawal, uint256 attributedAmm, uint256 ratePerLv)
{
// attribute all to AMM if no lv issued or withdrawn
if (totalLvIssued == 0 || totalLvWithdrawn == 0) {
return (0, totalAmount, 0);
}
...
// with 1e18 precision
ratePerLv = ((totalAmount * 1e18) / totalLvIssued);
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
tsvetanovv commented:
Borderline Low/Medium
ziankork
This is valid. will fix
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Cork-Technology/Depeg-swap/pull/80
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity doS bug in Cork Protocol. Sherlock made it public on 26 September 2024. Found in the 506 audit contest (finding M-5), also found by 1 other warden.
About DoS
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.