The repayment process in the NFTPositionManager can sometimes be reverted
The NFTPositionManager's repayment function can revert due to a rounding mismatch between the debt balance change and the repaid assets. An attacker could exploit this to cause a denial of service, preventing legitimate repayments.
Obsidian Follow with dany.armstrong90 and ether_sky
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/488
Found by
Obsidian, dany.armstrong90, ether_sky
Summary
Users can supply assets to the pools through the NFTPositionManager to earn rewards in zero tokens.
Functions like deposit, withdraw, repay, and borrow should operate normally.
However, due to an additional check, repayments might be reverted.
Vulnerability Detail
Here's the relationship between shares (s) and assets (a) in the Pool:
- Share to Asset Conversion:
a = [(s * I + 10^27 / 2) / 10^27] (rayMul)
function rayMul(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly {
if iszero(or(iszero(b), iszero(gt(a, div(sub(not(0), HALF_RAY), b))))) { revert(0, 0) }
c := div(add(mul(a, b), HALF_RAY), RAY)
}
}
- Asset to Share Conversion:
s = [(a * 10^27 + I / 2) / I] (rayDiv)
function rayDiv(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly {
if or(iszero(b), iszero(iszero(gt(a, div(sub(not(0), div(b, 2)), RAY))))) { revert(0, 0) }
c := div(add(mul(a, RAY), div(b, 2)), b)
}
}
Numerical Example:
Suppose there is a pool P where users borrow assets A using the NFTPositionManager.
- The current
borrow indexofPis2 * 10^27, and theshareis5. - The
previous debt balanceis as below (Line 119):previousDebtBalance = [(s * I + 10^27 / 2) / 10 ^27] = [(5 * 2 * 10^27 + 10^27 / 2) / 10^27] = 10 - If we are going to
repay3assets:- The removed
sharesis:[(a * 10^27 + I / 2) / I] = [(3 * 10^27 + 2 * 10^27 / 2) / (2 * 10^27)] = 2 - Therefore, the remaining
shareis:5 - 2 = 3
- The removed
- The
current debt balanceis as below (Line 121):currentDebtBalance = [(s * I + 10^27 / 2) / 10 ^27] = [(3 * 2 * 10^27 + 10^27 / 2) / 10^27 = 6Then inline 123,previousDebtBalance - currentDebtBalancewould be4andrepaid.assetsis3. As a result, therepaymentwould be reverted.
function _repay(AssetOperationParams memory params) internal nonReentrant {
119: uint256 previousDebtBalance = pool.getDebt(params.asset, address(this), params.tokenId);
DataTypes.SharesType memory repaid = pool.repay(params.asset, params.amount, params.tokenId, params.data);
121: uint256 currentDebtBalance = pool.getDebt(params.asset, address(this), params.tokenId);
123: if (previousDebtBalance - currentDebtBalance != repaid.assets) {
revert NFTErrorsLib.BalanceMisMatch();
}
}
This example demonstrates a potential 1 wei mismatch between previousDebtBalance and currentDebtBalance due to rounding in the calculations.
Impact
This check seems to cause a denial-of-service (DoS) situation where repayments can fail due to small rounding errors.
This issue can occur with various combinations of borrow index, share amounts, and repaid assets.
Code Snippet
https://github.com/sherlock-audit/2024-06-new-scope/blob/c8300e73f4d751796daad3dadbae4d11072b3d79/zerolend-one/contracts/core/pool/utils/WadRayMath.sol#L77 https://github.com/sherlock-audit/2024-06-new-scope/blob/c8300e73f4d751796daad3dadbae4d11072b3d79/zerolend-one/contracts/core/pool/utils/WadRayMath.sol#L93 https://github.com/sherlock-audit/2024-06-new-scope/blob/c8300e73f4d751796daad3dadbae4d11072b3d79/zerolend-one/contracts/core/positions/NFTPositionManagerSetters.sol#L119-L125
Tool used
Manual Review
Recommendation
Either remove this check or adjust it to allow a 1 wei mismatch to prevent unnecessary reversion of repayments.
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 strict equality check between the change in debt balance and the repaid assets does not account for rounding errors in the share-to-asset and asset-to-share conversions.
What the attacker needed
A pool with borrowing via NFTPositionManager; a debt position with specific index and shares
The fix
The check should be removed or adjusted to allow a 1 wei mismatch to prevent unnecessary reversion of repayments.
How it was found
- Have a pool where users borrow assets using the NFTPositionManager.
- Set the borrow index to 2 * 10^27 and the share to 5.
- Attempt to repay 3 assets.
- The removed shares are calculated as 2, leaving 3 shares.
- The previous debt balance is 10 and the current debt balance is 6, so the difference is 4, but repaid.assets is 3.
- The repayment reverts due to the mismatch.
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.
When you repay a loan, the system checks that the amount you repay matches the reduction in your debt. Due to how numbers are rounded in the code, these two values can differ by a tiny amount (1 wei), causing the repayment to fail. This means you might be unable to repay your loan, which is a denial of service.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.