LienToken._payment function increases users debt
The partial-repayment function in the LienToken contract overwrote the stored principal with the total owed amount, which includes accrued interest, so interest was then charged on interest. A borrower making partial repayments would therefore owe more than the simple-interest terms the protocol documented.
rvierdiiev Follow with bin2chen, evan and 1 other
Originally disclosed on Code4rena
Submitted by rvierdiiev, also found by bin2chen, evan, and ladboy233
LienToken._payment function increases users debt. Every time when user pays not whole lien amount then interests are added to the loan amount, so next time user pays interests based on interests.
Proof of Concept
LienToken._payment is used by LienToken.makePayment function that allows borrower to repay part or all his debt.
https://github.com/code-423n4/2023-01-astaria/blob/main/src/LienToken.sol#L790-L853
function _payment(
LienStorage storage s,
Stack[] memory activeStack,
uint8 position,
uint256 amount,
address payer
) internal returns (Stack[] memory, uint256) {
Stack memory stack = activeStack[position];
uint256 lienId = stack.point.lienId;
if (s.lienMeta[lienId].atLiquidation) {
revert InvalidState(InvalidStates.COLLATERAL_AUCTION);
}
uint64 end = stack.point.end;
// Blocking off payments for a lien that has exceeded the lien.end to prevent repayment unless the msg.sender() is the AuctionHouse
if (block.timestamp >= end) {
revert InvalidLoanState();
}
uint256 owed = _getOwed(stack, block.timestamp);
address lienOwner = ownerOf(lienId);
bool isPublicVault = _isPublicVault(s, lienOwner);
address payee = _getPayee(s, lienId);
if (amount > owed) amount = owed;
if (isPublicVault) {
IPublicVault(lienOwner).beforePayment(
IPublicVault.BeforePaymentParams({
interestOwed: owed - stack.point.amount,
amount: stack.point.amount,
lienSlope: calculateSlope(stack)
})
);
}
//bring the point up to block.timestamp, compute the owed
stack.point.amount = owed.safeCastTo88();
stack.point.last = block.timestamp.safeCastTo40();
if (stack.point.amount > amount) {
stack.point.amount -= amount.safeCastTo88();
// // slope does not need to be updated if paying off the rest, since we neutralize slope in beforePayment()
if (isPublicVault) {
IPublicVault(lienOwner).afterPayment(calculateSlope(stack));
}
} else {
amount = stack.point.amount;
if (isPublicVault) {
// since the openLiens count is only positive when there are liens that haven't been paid off
// that should be liquidated, this lien should not be counted anymore
IPublicVault(lienOwner).decreaseEpochLienCount(
IPublicVault(lienOwner).getLienEpoch(end)
);
}
delete s.lienMeta[lienId]; //full delete of point data for the lien
_burn(lienId);
activeStack = _removeStackPosition(activeStack, position);
}
s.TRANSFER_PROXY.tokenTransferFrom(stack.lien.token, payer, payee, amount);
emit Payment(lienId, amount);
return (activeStack, amount);
}
The main problem is in line 826. stack.point.amount = owed.safeCastTo88();
https://github.com/code-423n4/2023-01-astaria/blob/main/src/LienToken.sol#L826
Here stack.point.amount becomes stack.point.amount + accrued interests, because owed is loan amount + accrued interests by this time.
stack.point.amount is the amount that user borrowed. So actually that line has just increased user’s debt. And in case if he didn’t pay all amount of lien, then next time he will pay more interests, because interests depend on loan amount.
In the docs Astaria protocol claims that:
All rates on the Astaria protocol are in simple interest and non-compounding.
https://docs.astaria.xyz/docs/protocol-mechanics/loanterms
You can check that inside “Interest rate” section.
However _payment function is compounding interests.
To avoid this, another field interestsAccrued should be introduced which will track already accrued interests.
Tools Used
VsCode
Recommended Mitigation Steps
You need to store accrued interests by the moment of repayment to another variable interestsAccrued.
And calculate _getInterest functon like this.
function _getInterest(Stack memory stack, uint256 timestamp)
internal
pure
returns (uint256)
{
uint256 delta_t = timestamp - stack.point.last;
return stack.point.interestsAccrued + (delta_t * stack.lien.details.rate).mulWadDown(stack.point.amount);
}
SantiagoGregory (Astaria) acknowledged and commented via duplicate issue #574:
This is the intended flow, early payments will compound interest.
Picodes (judge) commented via duplicate issue #574
Keeping medium severity because of the mention in the doc stating that interests are non-compounding, so considering this is a broken functionality.
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 payment routine assigns the total owed (principal plus accrued interest) back into the principal field instead of tracking accrued interest separately.
Impact
Borrowers who make partial repayments could be charged compounding interest, contradicting the documented simple-interest terms; the protocol team said this was intended, but the judge kept it as a medium-severity broken functionality.
What the attacker needed
An active lien with interest accruing; borrower makes a partial repayment
The fix
Accrued interest should be stored in a separate variable and added to the freshly calculated interest rather than folded into the principal.
How it was found
- A borrower takes a lien and interest accrues over time.
- The borrower repays only part of the amount owed.
- The contract stores the full owed amount, including interest, as the new principal.
- On the next repayment, interest is calculated on that inflated principal.
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 loan has a principal (the amount borrowed) and interest that builds up over time. This code, when someone paid only part of what they owed, replaced the stored principal with principal plus interest, so future interest was calculated on the larger figure. That is called compounding, and the protocol's own documentation said it used simple interest instead.
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.