Skip to content
Business logic Audit finding Intermediate

LybraPeUSDVaultBase.rigidRedemption should use getBorrowedOf instead of borrowed

The rigidRedemption function uses the borrowed mapping instead of getBorrowedOf, which excludes fees, so the debt check and collateral ratio are understated. An attacker could redeem more than allowed and perform redemptions even when the provider's actual collateral ratio is below 100%.

cccz Follow

Originally disclosed on Code4rena

Submitted by cccz

In LybraPeUSDVaultBase, the return value of getBorrowedOf represents the user’s debt, while borrowed only represents the user’s borrowed funds and does not include fees. Using borrowed instead of getBorrowedOf in rigidRedemption results in:

  1. The requirement for the peusdAmount parameter is smaller than it actually is.
  2. The calculated providerCollateralRatio is larger, so that rigidRedemption can be performed, even if the actual providerCollateralRatio is less than 100e18.
    function rigidRedemption(address provider, uint256 peusdAmount) external virtual {
        require(configurator.isRedemptionProvider(provider), "provider is not a RedemptionProvider");
        require(borrowed[provider] >= peusdAmount, "peusdAmount cannot surpass providers debt");
        uint256 assetPrice = getAssetPrice();
        uint256 providerCollateralRatio = (depositedAsset[provider] * assetPrice * 100) / borrowed[provider];
        require(providerCollateralRatio >= 100 * 1e18, "provider's collateral ratio should more than 100%");
        _repay(msg.sender, provider, peusdAmount);
        uint256 collateralAmount = (((peusdAmount * 1e18) / assetPrice) * (10000 - configurator.redemptionFee())) / 10000;
        depositedAsset[provider] -= collateralAmount;
        collateralAsset.transfer(msg.sender, collateralAmount);
        emit RigidRedemption(msg.sender, provider, peusdAmount, collateralAmount, block.timestamp);
    }

Proof of Concept

https://github.com/code-423n4/2023-06-lybra/blob/5d70170f2c68dbd3f7b8c0c8fd6b0b2218784ea6/contracts/lybra/pools/base/LybraPeUSDVaultBase.sol#L157-L168

Recommended Mitigation Steps

Change to:

    function rigidRedemption(address provider, uint256 peusdAmount) external virtual {
        require(configurator.isRedemptionProvider(provider), "provider is not a RedemptionProvider");
-       require(borrowed[provider] >= peusdAmount, "peusdAmount cannot surpass providers debt");
+       require(getBorrowedOf(provider) >= peusdAmount, "peusdAmount cannot surpass providers debt");
        uint256 assetPrice = getAssetPrice();
-       uint256 providerCollateralRatio = (depositedAsset[provider] * assetPrice * 100) / borrowed[provider];
+       uint256 providerCollateralRatio = (depositedAsset[provider] * assetPrice * 100) / getBorrowedOf(provider);
        require(providerCollateralRatio >= 100 * 1e18, "provider's collateral ratio should more than 100%");
        _repay(msg.sender, provider, peusdAmount);
        uint256 collateralAmount = (((peusdAmount * 1e18) / assetPrice) * (10000 - configurator.redemptionFee())) / 10000;
        depositedAsset[provider] -= collateralAmount;
        collateralAsset.transfer(msg.sender, collateralAmount);
        emit RigidRedemption(msg.sender, provider, peusdAmount, collateralAmount, block.timestamp);
    }

Assessed type

Error

LybraFinance confirmed


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

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 code uses the borrowed mapping, which excludes fees, instead of getBorrowedOf, which includes fees, when checking debt and calculating the collateral ratio.

Impact

An attacker could redeem more PeUSD than the provider's actual debt allows and perform rigid redemptions even when the provider's collateral ratio is below 100%, potentially taking more collateral than entitled.

What the attacker needed

Provider is a redemption provider; caller has PeUSD to redeem

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.

This is a smart contract bug where a function checks the wrong number for a user's debt. The debt includes fees, but the code only looks at the base borrowed amount, so it thinks the user owes less than they do. That lets someone redeem more collateral than they should, even when the account is undercollateralised.

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 cccz

View all

104 more disclosed reports, best first.

More from Lybra Finance

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.