Interest rates can be raised above the market as a griefing, disabling the pool
The interest rate algorithm uses an exponential moving average of total debt divided by loan-to-value times collateral, which a large collateral holder can depress by borrowing a tiny amount against an enormous collateral pledge. This pushes rates above market, driving borrowers and lenders away and leaving the pool unusable.
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/100
Found by
hyh
Summary
Interest rates algorithm is based on MAU to TAU dynamics, where TAU is 3.5 day EMA of total debt to LUP * total collateral. The latter value can be manipulated by a big collateral holder by becoming a borrower with insignificant debt and lots of collateral, so 3.5d EMA of Debt / (LUP * Collateral) will become depressed and rates will go up irrespective to the real debt supply/demand situation.
Vulnerability Detail
Let's suppose Bob is a big WBTC holder and current market rate for its lending is insignificant, say base WBTC deposit APY on major platforms is below 5 basis points. Say Bob is a big lender of USDC-WBTC (quote-collateral) pool or Bob has interests in disturbing that pool operations for any reasons, for example Bob is a beneficiary of a rival lending protocol.
Bob can borrow a minimal loan, say 1000 USDC with big, magnitudes excessive, WBTC collateral, say 1000 WBTC. As the pool is permissionless it is safe, Bob can withdraw any time, market risk is close to zero as the loan is too small, interest rates risk is small too as Bob left near zero market interest rate for the strict zero income while the WBTC is used as collateral in the pool. I.e. it's low risk, low cost strategy for Bob to do so.
Pool, on the other hand, will experience gradual rise of the interest rate as while MAU will stay relatively constant, TAU will become low due to total collateral amount being big (and stable, so EMA will move to the corresponding value), while other parts of Debt / (LUP * Collateral) be relatively constant.
Observing the rise of interest rates above market the borrowers will gradually leave. But not all, and Bob has achieved above market interest income from dormant part of the borrowers, who are slow to react to this dynamics. But, given borrowers being mostly rational and informed, this to be relatively short-term situation. More importantly, as the rate went up and borrowers has left, lenders will observe significantly decreased utilization and will leave pool as well, not receiving enough interest income for their deposits.
This way Bob essentially disturbed the USDC-WBTC pool, so he can leave some small part of WBTC collateral there so that the rate will stay elevated and pool remain to be unusable due to significantly elevated interest rate, as no borrower will enter there on such conditions.
Impact
Pool utility for market participants can be destroyed by manipulating the interest rate algorithm, so such pool becomes unusable and end up being abandoned. Since for a pair of quote-collateral there can be only one pool this effectively disturb the whole line of business, i.e. profit from say USDC quote, WBTC collateral operations will cease to exist for Ajna token holders.
Current borrowers can experience losses from the manipulated above market interest rate. Dormant borrowers, i.e. ones who be slow to react, will be hit the hardest.
Attack cost is proportional to the current risk-free market interest rate of the collateral as attacker gives it up for a while. This can be low enough for the majority of widely utilized collateral assets.
Code Snippet
Target utilization TAU is computed from average collateralization ratio, Debt / Collateral:
function updateInterestRate(
InterestState storage interestParams_,
DepositsState storage deposits_,
PoolState memory poolState_,
uint256 lup_
) external {
// current values of EMA samples
uint256 curDebtEma = interestParams_.debtEma;
uint256 curLupColEma = interestParams_.lupColEma;
// meaningful actual utilization
int256 mau;
// meaningful actual utilization * 1.02
int256 mau102;
if (poolState_.debt != 0) {
// update pool EMAs for target utilization calculation
curDebtEma =
Maths.wmul(poolState_.debt, EMA_7D_RATE_FACTOR) +
Maths.wmul(curDebtEma, LAMBDA_EMA_7D
);
// lup * collateral EMA sample max value is 10 times current debt
uint256 maxLupColEma = Maths.wmul(poolState_.debt, Maths.wad(10));
// current lup * collateral value
uint256 lupCol = Maths.wmul(poolState_.collateral, lup_);
curLupColEma =
Maths.wmul(Maths.min(lupCol, maxLupColEma), EMA_7D_RATE_FACTOR) +
Maths.wmul(curLupColEma, LAMBDA_EMA_7D);
// save EMA samples in storage
interestParams_.debtEma = curDebtEma;
interestParams_.lupColEma = curLupColEma;
// calculate meaningful actual utilization for interest rate update
mau = int256(_utilization(deposits_, poolState_.debt, poolState_.collateral));
mau102 = mau * PERCENT_102 / 1e18;
}
// calculate target utilization
int256 tu = (curDebtEma != 0 && curLupColEma != 0) ? int256(Maths.wdiv(curDebtEma, curLupColEma)) : int(Maths.WAD);
if (!poolState_.isNewInterestAccrued) poolState_.rate = interestParams_.interestRate;
uint256 newInterestRate = poolState_.rate;
// raise rates if 4*(tu-1.02*mau) < (tu+1.02*mau-1)^2-1
if (4 * (tu - mau102) < ((tu + mau102 - 1e18) ** 2) / 1e18 - 1e18) {
newInterestRate = Maths.wmul(poolState_.rate, INCREASE_COEFFICIENT);
}
// decrease rates if 4*(tu-mau) > 1-(tu+mau-1)^2
else if (4 * (tu - mau) > 1e18 - ((tu + mau - 1e18) ** 2) / 1e18) {
newInterestRate = Maths.wmul(poolState_.rate, DECREASE_COEFFICIENT);
}
newInterestRate = Maths.min(500 * 1e18, Maths.max(0.001 * 1e18, newInterestRate));
if (poolState_.rate != newInterestRate) {
interestParams_.interestRate = uint208(newInterestRate);
interestParams_.interestRateUpdate = uint48(block.timestamp);
emit UpdateInterestRate(poolState_.rate, newInterestRate);
}
}
_updateInterestState() and _accruePoolInterest() are called within all state changing operations of the pool:
https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/base/Pool.sol#L481-L488
function _updateInterestState(
PoolState memory poolState_,
uint256 lup_
) internal {
// if it has been more than 12 hours since the last interest rate update, call updateInterestRate function
if (block.timestamp - interestState.interestRateUpdate > 12 hours) {
PoolCommons.updateInterestRate(interestState, deposits, poolState_, lup_);
}
https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/base/Pool.sol#L426-L431
function _accruePoolInterest() internal returns (PoolState memory poolState_) {
// retrieve t0Debt amount from poolBalances struct
uint256 t0Debt = poolBalances.t0Debt;
// initialize fields of poolState_ struct with initial values
poolState_.collateral = poolBalances.pledgedCollateral;
Pool collateral is updated on any borrowing:
function drawDebt(
AuctionsState storage auctions_,
mapping(uint256 => Bucket) storage buckets_,
DepositsState storage deposits_,
LoansState storage loans_,
PoolState calldata poolState_,
address borrowerAddress_,
uint256 amountToBorrow_,
uint256 limitIndex_,
uint256 collateralToPledge_
) external returns (
DrawDebtResult memory result_
) {
Borrower memory borrower = loans_.borrowers[borrowerAddress_];
result_.poolDebt = poolState_.debt;
result_.newLup = _lup(deposits_, result_.poolDebt);
result_.poolCollateral = poolState_.collateral;
...
// pledge collateral to pool
if (collateralToPledge_ != 0) {
// add new amount of collateral to pledge to borrower balance
borrower.collateral += collateralToPledge_;
...
// add new amount of collateral to pledge to pool balance
result_.poolCollateral += collateralToPledge_;
}
Tool used
Manual Review
Recommendation
Per discussions so far the most effective approach, proposed by Matt, looks to be the weighting the collateral with the corresponding debt, i.e. instead of computing sum(D_i) / sum(C_i) (we omit 1 / LUP term as it's constant here), which is the average collateralization ratio, the debt weighted version of it can be used, sum(D_i ^ 2) / sum(C_i * D_i), which is the average collateralization ratio weighted by current debt.
As obtaining any significant debt brings in both market and interest rate risk, i.e. will raise the probability of attacker's borrow position liquidation and also ends up paying the elevated interest rate proportionally to the debt acquired, it will substantially raise the cost and diminish practical probability of the attack.
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
Likely the target utilisation is computed as an unweighted average collateralisation ratio, so a single borrower with negligible debt and excessive collateral skews the EMA and the rate.
Impact
The pool could become unusable and abandoned, with dormant borrowers hit hardest by above-market rates; the attack cost is proportional to the risk-free market interest rate of the collateral, which may be low for widely used assets.
What the attacker needed
A large holding of the collateral asset; permissionless pool access; willingness to lock collateral at low return
The fix
The collateral should be weighted by the corresponding debt, using a debt-weighted average collateralisation ratio, so acquiring significant debt raises the attacker's cost and risk.
How it was found
- Borrow a minimal loan with a very large amount of collateral.
- Wait for the collateral EMA to settle at the inflated value.
- Observe interest rates rise above market as borrowers leave.
- Leave a small part of the collateral in place so rates stay elevated and the pool remains unusable.
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 lending pool sets its interest rate partly from how much collateral backs the loans. If someone pledges a huge amount of collateral for a very small loan, the pool thinks it is over-collateralised and raises rates. The attacker can then keep rates high so nobody wants to use the pool.
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.