CDP.sol update overwrites user’s credit on every positive increment
The update function in CDP.sol overwrites the totalCredit with the incremental credit instead of adding to it, so any previously accumulated credit is lost. An attacker could not directly exploit this, but users' credit is permanently lost each time update is called.
Originally disclosed on Code4rena
Submitted by harleythedog
Impact
Within CDP.sol (https://github.com/code-423n4/2021-11-yaxis/blob/main/contracts/v3/alchemix/libraries/alchemist/CDP.sol) there is a function called update. This function slowly decreases the debt of a position as yield is earned, until the debt is fully paid off, and the idea is then that the credit should begin incrementing as more yield is accumulated. However, the current logic to increment the totalCredit is this line of code (line 39 of CDP.sol):
\_self.totalCredit = \_earnedYield.sub(\_currentTotalDebt);
Notice that that each time update is called, this overwrites the previous totalCredit with the incremental credit accumulated. The line should instead be:
\_self.totalCredit = \_self.totalCredit.add(\_earnedYield.sub(\_currentTotalDebt));
Indeed, look at the function getUpdatedTotalCredit, it returns the value:
\_self.totalCredit + (\_unclaimedYield - \_currentTotalDebt);
So it is obviously intended that the totalCredit should keep increasing over time instead of being overwritten on each update with a small value. The impact of this issue is large - the credit of every position will always be overwritten and the correct information will be lost forever. User’s credit should grow over time, but instead it is overwritten with a small value every time update is called.
Proof of Concept
See line 39 in CDP.sol here: [https://github.com/code-423n4/2021-11-yaxis/blob/main/contracts/v3/alchemix/libraries/alchemist/CDP.sol#:~:text=_self.totalCredit%20%3D%20_earnedYield.sub(_currentTotalDebt)%3B](https://github.com/code-423n4/2021-11-yaxis/blob/main/contracts/v3/alchemix/libraries/alchemist/CDP.sol#:~:text=_self.totalCredit%20%3D%20_earnedYield.sub(_currentTotalDebt%29%3B)
Tools Used
Manual inspection.
Recommended Mitigation Steps
Change code as described above to increment totalCredit instead of overwrite it.
If there is debt, the credit should be zero
0xleastwood (judge) commented:
It seems like if
_self.totalDebtis already zero and yield has been earned by the protocol,_self.totalCreditwill be overwritten. This doesn’t seem ideal, could you clarify why the issue is incorrect?
0xleastwood (judge) commented:
If I’m not mistaken, yield can be earned from a positive credit (net 0 debt) position.
Xuefeng-Zhu (yAxis) commented:
@0xleastwood
totalCreditis 0 if there is debt
0xleastwood (judge) commented:
After chatting to @Xuefeng-Zhu in Discord, he was able to confirm the issue as valid. So keeping it as is.
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 code assigns the new credit value directly to totalCredit instead of adding it to the existing totalCredit.
Impact
The credit of every position is overwritten with a small value each time update is called, so users permanently lose their accumulated credit.
What the attacker needed
None
The fix
The code should be changed to add the incremental credit to the existing totalCredit instead of overwriting it.
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 a function that is supposed to add to a user's credit balance instead replaces it with a small new value. So each time the function runs, the user loses any credit they had built up before. It is like a bank teller who, instead of adding your new deposit to your balance, just writes the deposit amount on a fresh line, erasing your old balance.
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.