The amount of shares needed for redemption of borrow tokens is underquoted during the removal of leverage process leading to reverting
The LeverageManager contract underquotes the number of shares needed to redeem borrow tokens when removing leverage, due to rounding down in convertToShares and redeem. This causes the flash loan repayment to fail, reverting the removal of leverage.
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/256
Found by
X77, ZoA, ck, globalace, pkqs90
Summary
When removing leverage, some additional borrow amount is acquired in the LeverageManager._acquireBorrowTokenForRepayment function. The amount needed is however underquoted due to the rounding down nature of the convertToShares function. This means not enough amount will be acquired leading to the removing of leverage reverting.
Root Cause
In LeverageManager._acquireBorrowTokenForRepayment, _borrowAmtNeededToSwap is acquired to provide enough tokens to repay the flash loan:
// sell pod token into LP for enough borrow token to get enough to repay
// if self-lending swap for lending pair then redeem for borrow token
if (_borrowAmtNeededToSwap > 0) {
if (_isPodSelfLending(_props.positionId)) {
_podAmtRemaining = _swapPodForBorrowToken(
_pod,
positionProps[_props.positionId].lendingPair,
_podAmtReceived,
IFraxlendPair(positionProps[_props.positionId].lendingPair).convertToShares(_borrowAmtNeededToSwap),
_podSwapAmtOutMin
);
IFraxlendPair(positionProps[_props.positionId].lendingPair).redeem(
IERC20(positionProps[_props.positionId].lendingPair).balanceOf(address(this)),
address(this),
address(this)
);
As can be seen convertToShares is used to determine the equivalent number of shares that should be acquired for redemption into the borrow token.
convertToShares is a rounding down function:
function convertToShares(uint256 _assets) external view returns (uint256 _shares) {
_shares = toAssetShares(_assets, false, true);
}
function toAssetShares(uint256 _amount, bool _roundUp, bool _previewInterest)
public
view
returns (uint256 _shares)
{
if (_previewInterest) {
(,,,, VaultAccount memory _totalAsset,) = previewAddInterest();
_shares = _totalAsset.toShares(_amount, _roundUp);
} else {
_shares = totalAsset.toShares(_amount, _roundUp);
}
}
This means that the shares that will be used for the redemption of the borrow tokens will be underquoted.
The redemption process will also lead to a further rounding down compounding the issue:
IFraxlendPair(positionProps[_props.positionId].lendingPair).redeem(
IERC20(positionProps[_props.positionId].lendingPair).balanceOf(address(this)),
address(this),
address(this)
);
function redeem(uint256 _shares, address _receiver, address _owner)
external
nonReentrant
returns (uint256 _amountToReturn)
{
if (_receiver == address(0)) revert InvalidReceiver();
// Check if withdraw is paused and revert if necessary
if (isWithdrawPaused) revert WithdrawPaused();
// Accrue interest if necessary
_addInterest();
// Pull from storage to save gas
VaultAccount memory _totalAsset = totalAsset;
// Calculate the number of assets to transfer based on the shares to burn
_amountToReturn = _totalAsset.toAmount(_shares, false);
function toAmount(VaultAccount memory total, uint256 shares, bool roundUp) internal pure returns (uint256 amount) {
if (total.shares == 0) {
amount = shares;
} else {
amount = (shares * total.amount) / total.shares;
if (roundUp && (amount * total.shares) / total.amount < shares) {
amount = amount + 1;
}
}
}
In the end the amount of borrow tokens acquired will be less than the required amount leading to reverting when trying to repay the flash loan.
// pay back flash loan and send remaining to borrower
uint256 _repayAmount = _d.amount + _d.fee;
if (_pairedAmtReceived < _repayAmount) {
_podAmtRemaining = _acquireBorrowTokenForRepayment(
_props,
_posProps.pod,
_d.token,
_repayAmount - _pairedAmtReceived,
_podAmtReceived,
_podSwapAmtOutMin,
_userProvidedDebtAmtMax
);
}
IERC20(_d.token).safeTransfer(IFlashLoanSource(_getFlashSource(_props.positionId)).source(), _repayAmount);
Internal Pre-conditions
None
External Pre-conditions
None
Attack Path
- User tries to remove leverage.
- The process reverts due to the error in determining the borrow tokens needed.
Impact
The remove leverage process reverts leading to a denial of service.
PoC
No response
Mitigation
Round up the shares needed to be redeemed for the borrow tokens. This is safe because any surplus will later be refunded to the user.
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 use of a rounding-down function (convertToShares) to determine the number of shares to redeem, combined with further rounding down in redeem, leads to an insufficient amount of borrow tokens being acquired.
Impact
The remove leverage process reverts, causing a denial of service for users attempting to remove leverage.
What the attacker needed
None
The fix
Round up the shares needed to be redeemed for the borrow tokens, as any surplus will be refunded to the user.
How it was found
- User tries to remove leverage.
- The process reverts due to the error in determining the borrow tokens needed.
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 remove leverage, the system needs to get back some borrowed tokens to repay a flash loan. It calculates how many shares to redeem, but because the calculation always rounds down, it asks for slightly too few shares. As a result, it doesn't get enough tokens and the whole process fails.
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.