Incoming Redemption Assets not being tracked when repurchase is called
The repurchase function transfers redemption assets into the contract without updating the internal locked balance, so the accounting understates the actual redemption assets held. After expiry, when liquidity is separated, the untracked assets remain stuck in the contract, causing a loss of funds.
0x73696d616f Follow with 0xNirix, Ace-30 and 6 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-cork-protocol-judging/issues/126
Found by
0x73696d616f, 0xNirix, Ace-30, KupiaSec, Pheonix, dimulski, sakshamguruji, steadyman, vinica_boy
Summary
repurchase() function take redemption asset and gives back depeg swap along with pegged. However, the incoming redemption asset is not being tracked via lockFrom but there's a direct transfer of ra via lockUnchecked() causing mismatch in ra accounting.
Vulnerability Detail
Users deposit redemption asset via deposit to get back Cover Token + DepegSwap which can be redeemed back using various combinations.
- Redeem with CoverToken + DepegSwap ( only before expiry of DS )
- Redeem with DepegSwap + Pegged Asset ( only before expiry of DS )
- Redeem with CoverToken ( only after expiry )
Is users use 2. as a means to redeem deposited RA, they can repurchase DepegSwap + Pegged Asset back using repurchase()
The repurchase() function is designed for getting back a combination of DepegSwap + Pegged Asset by giving Redemption Asset.
Incoming and outgoing RA is tracked via PsmRedemptionAssetManager struct attached with State struct. Consider the system before expiry. Let's look at all the places where RA can come and go and how internal tracking is changing respectively.
- deposit() : ra increased
- redeem with DS + PA : ra decreased
- redeem with CT + DS : ra decreased
Everytime there is incoming of ra, the contracts increases internal state variable ( locked) with that amount.
struct PsmRedemptionAssetManager {
address _address;
uint256 locked;
uint256 free;
}
But this is not increased when ra comes with repurchase() .
This can be problematic after time of expiry.
After expiry, when users redeems with ct by calling redeemWithCt() it internally invokes _separateLiquidity().
function _separateLiquidity(State storage self, uint256 prevIdx) internal {
if (self.psm.liquiditySeparated.get(prevIdx)) {
return;
}
DepegSwap storage ds = self.ds[prevIdx];
Guard.safeAfterExpired(ds);
uint256 availableRa = self.psm.balances.ra.convertAllToFree();
uint256 availablePa = self.psm.balances.paBalance;
self.psm.poolArchive[prevIdx] = PsmPoolArchive(availableRa, availablePa, IERC20(ds.ct).totalSupply());
// reset current balances
self.psm.balances.ra.reset();
self.psm.balances.paBalance = 0;
self.psm.liquiditySeparated.set(prevIdx);
}
In this function all the accumulated ra goes to pool archive. This is achieved by zeroing out the variable that was tracking incoming and outgoing ra ( by reset()) and storing it in PoolArchive.
But since repurchase does not change this internal variable, at the time of _separateLiquidity() , actual ra in the system will be much more than what is expected.
Impact
Since protocol's internal tracking assumes less ra in the system than the actual amount, the remaining amount will be stuck in the contract causing direct loss of funds.
Code Snippet
Tool used
Manual Review
Recommendation
Increase locked ra amount whenever repurchase is called by calling lockFrom instead of lockUnchecked()
Discussion
ziankork
this is valid, although we already fixed this for the trading competition. will provide links later
ziankork
the fix recommended will cause an issue if there's a fee as #155 described. the fix can also be seen there
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Cork-Technology/Depeg-swap/pull/77
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
Impact
The protocol's internal tracking assumes less redemption asset than actually present, so the excess remains stuck in the contract, causing a direct loss of funds.
What the attacker needed
User has redemption asset to repurchase; Repurchase occurs before expiry; Expiry occurs and redeemWithCt is called
The fix
The protocol team fixed this issue in a pull request, but the recommended fix of using lockFrom instead of lockUnchecked may cause issues if a fee is involved.
How it was found
- User deposits redemption asset to receive Cover Token and DepegSwap.
- User redeems using DepegSwap and Pegged Asset before expiry.
- User calls repurchase() to get back DepegSwap and Pegged Asset by providing Redemption Asset.
- After expiry, user calls redeemWithCt(), which triggers _separateLiquidity().
- _separateLiquidity() moves only the tracked redemption assets to the pool archive, leaving the untracked assets stuck.
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.
The protocol keeps a running tally of how many redemption assets it holds. When users repurchase, the assets arrive but the tally is not updated, so the contract thinks it has less than it really does. Later, when the contract moves all assets to a pool archive based on the tally, the extra assets are left behind and cannot be recovered.
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.