Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

seeques - Incorrect calculations of borrowingCollateral leads to DoS for positions in the current tick range due to underflow #86

Open
sherlock-admin2 opened this issue Oct 23, 2023 · 14 comments
Assignees
Labels
Escalation Resolved This issue's escalations have been approved/rejected Has Duplicates A valid issue with 1+ other issues describing the same vulnerability Medium A valid Medium severity issue Reward A payout will be made for this issue Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed

Comments

@sherlock-admin2
Copy link
Contributor

sherlock-admin2 commented Oct 23, 2023

seeques

high

Incorrect calculations of borrowingCollateral leads to DoS for positions in the current tick range due to underflow

Summary

The borrowingCollateral is the amount of collateral a borrower needs to pay for his leverage. It should be calculated as the difference of holdTokenBalance (the amount borrowed + holdTokens received after saleTokens are swapped) and the amount borrowed and checked against user-specified maxCollateral amount which is the maximum the borrower wishes to pay. However, in the current implementation the borrowingCollateral calculation is most likely to underflow.

Vulnerability Detail

This calculation is most likely to underflow

uint256 borrowingCollateral = cache.borrowedAmount - cache.holdTokenBalance;

The cache.borrowedAmount is the calculated amount of holdTokens based on the liquidity of a position. cache.holdTokenBalance is the balance of holdTokens queried after liquidity extraction and tokens transferred to the LiquidityBorrowingManager. If any amounts of the saleToken are transferred as well, these are swapped to holdTokens and added to cache.holdTokenBalance.

So in case when liquidity of a position is in the current tick range, both tokens would be transferred to the contract and saleToken would be swapped for holdToken and then added to cache.holdTokenBalance. This would make cache.holdTokenBalance > cache.borrowedAmount since cache.holdTokenBalance == cache.borrowedAmount + amount of sale token swapped and would make the tx revert due to underflow.

Impact

Many positions would be unavailable to borrowers. For non-volatile positions like that which provide liquidity to stablecoin pools the DoS could last for very long period. For volatile positions that provide liquidity in a wide range this could also be for more than 1 year.

Code Snippet

https://github.com/sherlock-audit/2023-10-real-wagmi/blob/main/wagmi-leverage/contracts/LiquidityBorrowingManager.sol#L492-L503
https://github.com/sherlock-audit/2023-10-real-wagmi/blob/main/wagmi-leverage/contracts/LiquidityBorrowingManager.sol#L470
https://github.com/sherlock-audit/2023-10-real-wagmi/blob/main/wagmi-leverage/contracts/LiquidityBorrowingManager.sol#L848-L896

Tool used

Manual Review

Recommendation

The borrowedAmount should be subtracted from holdTokenBalance

uint256 borrowingCollateral = cache.holdTokenBalance - cache.borrowedAmount;
@github-actions github-actions bot added High A valid High severity issue Has Duplicates A valid issue with 1+ other issues describing the same vulnerability labels Oct 26, 2023
@cvetanovv cvetanovv added the Sponsor Confirmed The sponsor acknowledged this issue is valid label Oct 27, 2023
@fann95 fann95 self-assigned this Oct 29, 2023
@fann95 fann95 added the Will Fix The sponsor confirmed this issue will be fixed label Oct 30, 2023
@sherlock-admin2 sherlock-admin2 changed the title Dandy Taupe Barracuda - Incorrect calculations of borrowingCollateral leads to DoS for positions in the current tick range due to underflow seeques - Incorrect calculations of borrowingCollateral leads to DoS for positions in the current tick range due to underflow Oct 30, 2023
@sherlock-admin2 sherlock-admin2 added the Reward A payout will be made for this issue label Oct 30, 2023
@Ali-Shehab
Copy link

Escalate

First issue has nothing to do with the other ones. It must not be duplicate

[peanuts - Max collateral check is not done when increasing collateral balance] #37

@sherlock-admin2
Copy link
Contributor Author

Escalate

First issue has nothing to do with the other ones. It must not be duplicate

[peanuts - Max collateral check is not done when increasing collateral balance] #37

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

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

@fann95
Copy link

fann95 commented Oct 31, 2023

Fixed: RealWagmi/wagmi-leverage@7937e25

@IAm0x52
Copy link

IAm0x52 commented Nov 1, 2023

Escalate

This is a valid issue but it should be medium rather than high. Impact is broken functionality and DOS which doesn't qualify as high.

@sherlock-admin2
Copy link
Contributor Author

Escalate

This is a valid issue but it should be medium rather than high. Impact is broken functionality and DOS which doesn't qualify as high.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

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

@Ali-Shehab
Copy link

Ali-Shehab commented Nov 1, 2023

Some reports were accepted as high with similar impact: https://solodit.xyz/issues/h-2-possible-dos-in-rollerperiphery-approve-function-sherlock-sense-sense-git I don't know if am missing something.

@IAm0x52
Copy link

IAm0x52 commented Nov 2, 2023

While I acknowledge the similarity of the issues, I have made my escalation based on current Sherlock rules which overrides previous judgments. Loss of functionality is a medium issue. The DOS could be over a year but only for some ranges/LP tokens which is why it is also medium.

@Ali-Shehab
Copy link

But can't we send any token and make it DOS?

@Czar102
Copy link

Czar102 commented Nov 2, 2023

As I understand, users wouldn't be able to create a borrow position for borrows with the active tick within the borrow tick range. This is a partial loss of the core functionality of the protocol, but doesn't lock any funds for >1 year. There also doesn't seem to be any loss of funds.

Current interpretation of the Sherlock rules imply that when there is no loss of funds or funds locked, it is not a high issue. I think it should be downgraded to medium, so will be accepting the escalation if there are not counterarguments.

@Ali-Shehab
Copy link

Also, I want to remind you that there is an issue that doesn't relate to this bug: #37

@Czar102
Copy link

Czar102 commented Nov 4, 2023

The first escalation here concerns another issue, #37.

Will be accepting both escalations. This will be downgraded to medium and #37 is not a duplicate. It is invalid.

@Evert0x Evert0x added Medium A valid Medium severity issue and removed High A valid High severity issue labels Nov 8, 2023
@Evert0x
Copy link

Evert0x commented Nov 8, 2023

Result:
Medium
Has Duplicates

@sherlock-admin2 sherlock-admin2 removed the Escalated This issue contains a pending escalation label Nov 8, 2023
@sherlock-admin sherlock-admin added the Escalation Resolved This issue's escalations have been approved/rejected label Nov 8, 2023
@sherlock-admin2
Copy link
Contributor Author

Escalations have been resolved successfully!

Escalation status:

@IAm0x52
Copy link

IAm0x52 commented Nov 16, 2023

Fix looks good. Underflow of this calculation is prevented via a ternary operator

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Escalation Resolved This issue's escalations have been approved/rejected Has Duplicates A valid issue with 1+ other issues describing the same vulnerability Medium A valid Medium severity issue Reward A payout will be made for this issue Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed
Projects
None yet
Development

No branches or pull requests

8 participants