Skip to content
Business logic Audit finding Intermediate

`moveQuoteToken()` can cause bucket to go bankrupt but it is not reflected in the accounting

The moveQuoteToken() function lacks the bankruptcy check present in removeQuoteToken(), so a bucket can be left with zero collateral and zero quote tokens but non-zero LP tokens without being marked bankrupt. A future depositor would receive fewer LP tokens than expected because their deposit is diluted by the existing LP tokens.

yixxas Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/83

Found by

yixxas

Summary

Both removeQuoteToken() and moveQuoteToken() can be used to completely remove all quote tokens from a bucket. When this happens, if at the same time bucketCollateral == 0 && lpsRemaining != 0, then the bucket should be declared bankrupt. This update is done in removeQuoteToken() but not in moveQuoteToken().

Vulnerability Detail

removeQuoteToken() has the following check to update bankruptcy time when collateral and quote token remaining is 0, but lps is more than 0. moveQuoteToken() is however missing this check. Both this functions has the same effects on the fromBucket and the only difference is that removeQuoteToken() returns the token to msg.sender but moveQuoteToken() moves the token to another bucket.

Solidity
if (removeParams.bucketCollateral == 0 && unscaledRemaining == 0 && lpsRemaining != 0) {
	emit BucketBankruptcy(params_.index, lpsRemaining);
	bucket.lps            = 0;
	bucket.bankruptcyTime = block.timestamp;
} else {
	bucket.lps = lpsRemaining;
}

Impact

A future depositor to the bucket will get less lps than expected due to depositing in a bucket that is supposedly bankrupt, hence the lps they get will be diluted with the existing ones in the bucket.

Code Snippet

https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/libraries/external/LenderActions.sol#L359-L365

Tool used

Manual Review

Recommendation

We should check if a bucket is bankrupt after moving quote tokens.

Discussion

yixxas

Escalate for 5 USDC

This issue is different from #133 and should not be duped together. While both highlights the missing bankruptcy logic, #133 describes the missing check in removeCollateral(). This reported issue describes the missing check in moveQuoteToken(). Note that fixing #133 would not fix this issue.

sherlock-admin

Escalate for 5 USDC

This issue is different from #133 and should not be duped together. While both highlights the missing bankruptcy logic, #133 describes the missing check in removeCollateral(). This reported issue describes the missing check in moveQuoteToken(). Note that fixing #133 would not fix this issue.

You've created a valid escalation for 5 USDC!

To remove the escalation from consideration: Delete your comment. To change the amount you've staked on this escalation: Edit your comment (do not create a new comment).

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

grandizzy

Escalate for 5 USDC

This issue is different from #133 and should not be duped together. While both highlights the missing bankruptcy logic, #133 describes the missing check in removeCollateral(). This reported issue describes the missing check in moveQuoteToken(). Note that fixing #133 would not fix this issue.

I agree with this comment.

hrishibhat

Escalation accepted

Although the underlying root cause is similar to #133, the occurrence is different as pointed out in the escalation. Hence will be considered separately and not a duplicate of #133

sherlock-admin

Escalation accepted

Although the underlying root cause is similar to #133, the occurrence is different as pointed out in the escalation. Hence will be considered separately and not a duplicate of #133

This issue's escalations have been accepted!

Contestants' payouts and scores will be updated according to the changes made on this issue.

The original report as published on Sherlock, by yixxas. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 moveQuoteToken() function does not include the same bankruptcy condition check that removeQuoteToken() has, so it fails to update the bucket's bankruptcy state when collateral and quote tokens reach zero while LP tokens remain.

Impact

A future depositor to the bucket could receive fewer LP tokens than expected because their deposit is diluted by the existing LP tokens in a bucket that should have been declared bankrupt.

What the attacker needed

Bucket has zero collateral and zero quote tokens; LP tokens remain in the bucket; attacker moves quote tokens out

The fix

The moveQuoteToken() function should check whether the bucket is bankrupt after moving quote tokens, mirroring the logic in removeQuoteToken().

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.

In a lending pool, each bucket tracks shares (LP tokens) that represent a depositor's portion. If all the money and collateral are removed but shares remain, the bucket should be marked bankrupt so new deposits are handled correctly. The function that moves tokens between buckets forgets to do this check, so new depositors get fewer shares than they should.

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.

More by yixxas

View all

13 more disclosed reports, best first.

More from Ajna #2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.