Skip to content
Business logic Audit finding Intermediate

PositionAction20._onWithdraw and PositionPendle20._onWithdraw also returns token amount in wrong scale

The withdraw function in CDPVault returns the token amount in wad (18-decimal) scale, but the PositionAction20 and PositionActionPendle wrappers expect the amount in the collateral token's native decimal scale. When the collateral token does not have 18 decimals, the wrapper attempts to transfer more tokens than were withdrawn, causing the transaction to revert due to insufficient funds.

nnez Follow with pkqs90

Originally disclosed on Code4rena

Submitted by nnez, also found by pkqs90

Withdraw operations will invariably revert due to insufficient funds if the collateral’s decimal deviates from 18.

Proof-of-Concept

CDPVault.withdraw accepts amount in tokenScale but returns token amount in wad scale.

See CDPVault.withdraw

function withdraw(address to, uint256 amount) external whenNotPaused returns (uint256 tokenAmount) {
    tokenAmount = wdiv(amount, tokenScale);
    int256 deltaCollateral = -toInt256(tokenAmount);
    modifyCollateralAndDebt({
        owner: to,
        collateralizer: msg.sender,
        creditor: msg.sender,
        deltaCollateral: deltaCollateral,
        deltaDebt: 0
    });
}

However, the PositionAction20._onWithdraw and PositionActionPendle._onWithdraw functions expect a return value scaled to the collateral’s decimal, as they use this value to transfer the token back to the collateralizer.

See PositionAction20._onWithdraw and PositionActionPendle._onWithdraw

File: /src/proxy/PositionAction20.sol
function _onWithdraw(address vault, address position, address /*dst*/, uint256 amount) internal override returns (uint256) {
    return ICDPVault(vault).withdraw(position, amount); <-- return in wad scale
}

File: /src/proxy/PositionActionPendle.sol
function _onWithdraw(address vault, address position, address /*dst*/, uint256 amount) internal override returns (uint256) {
    return ICDPVault(vault).withdraw(address(position), amount); <-- return in wad scale
}

File: /src/proxy/PositionAction.sol
function _withdraw(address vault, address position, CollateralParams calldata collateralParams) internal returns (uint256) {
    uint256 collateral = _onWithdraw(vault, position, collateralParams.targetToken, collateralParams.amount);
    |-- the below operation will fail because it uses collateral amount in wrong scale
    v
    // perform swap from collateral to arbitrary token
    if (collateralParams.auxSwap.assetIn != address(0)) {
        _delegateCall(
            address(swapAction),
            abi.encodeWithSelector(swapAction.swap.selector, collateralParams.auxSwap)
        );
    } else {
        // otherwise just send the collateral to `collateralizer`
        IERC20(collateralParams.targetToken).safeTransfer(collateralParams.collateralizer, collateral);
    }
    return collateral;
}

The problem arises when collateral’s decimal is not 18 (wad). For instance, let’s say the collateral’s decimal is 8:

  • Users want to withdraw 1e8 tokens, users must call withdraw function with amount=1e8.
  • CDPVault.withdraw transfer 1e8 tokens back but it returns token amount in wad, 1e18.
  • _withdraw will attempt to transfer 1e18 tokens which is much greater than 1e8; therefore, the execution revert due to insufficient funds.

Additional Notes

I want to emphasize that although this issue is similar to WP-M2, it is not the same finding as it happens in a different code path.

Recommended Mitigations

Convert return amount to token scale first before returning value.

    function _onWithdraw(address vault, address position, address /*dst*/, uint256 amount) internal override returns (uint256) {
        uint wadAmount = ICDPVault(vault).withdraw(address(position), amount);

        return wmul(wadAmount, ICDPVault(vault).tokenScale());
    }

amarcu (LoopFi) confirmed

Koolex (judge) commented:

Looks like the same issue as WP-M2. If it is not the same, please point out the diff - will re-evaluate in PJQA.

nnez (warden) commented:

@Koolex - To answer your question, WP-M2 specifically mentions PositionAction4626._onWithdraw() and does not include PositionAction20 and PositionActionPendle.

Therefore, we cannot assume that it is a known issue, as it exists in different contracts.

Koolex (judge) commented:

Stays as-is.


The original report as published on Code4rena, by nnez. 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

Likely a mismatch in decimal scaling: CDPVault.withdraw returns the amount in wad (18-decimal) scale, while the calling functions expect the amount in the collateral token's native scale.

What the attacker needed

Collateral token has decimals other than 18; user attempts to withdraw collateral

The fix

The return value from CDPVault.withdraw should be converted from wad scale to the token's native scale before being used in the wrapper functions.

How it was found

  1. Call withdraw on a CDPVault with a collateral token that has decimals other than 18, specifying the amount in the token's native scale.
  2. CDPVault.withdraw converts the amount to wad scale and returns that value.
  3. The wrapper function _onWithdraw receives the wad-scaled amount and passes it to _withdraw.
  4. _withdraw attempts to transfer that wad-scaled amount of the collateral token to the collateralizer.
  5. The transfer fails because the amount exceeds the actual token balance, causing a revert.

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.

Smart contracts often represent token amounts as integers, and the number of decimal places matters. This code mixed up two different decimal scales: one for internal accounting (18 decimals) and one for the actual token (e.g., 8 decimals). As a result, when a token with fewer decimals was used, the contract tried to send more tokens than it had, so the withdrawal failed.

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 nnez

View all

32 more disclosed reports, best first.

More from LoopFi

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.