Skip to content
Business logic Audit finding Intermediate

PaprController pays swap fee in buyAndReduceDebt , not user

The buyAndReduceDebt function in PaprController collects the swap fee from the contract itself rather than from the caller, because it uses transfer instead of transferFrom. This causes the transaction to revert when a fee is set, and could allow any mistakenly sent underlying tokens to be transferred out.

Jeiwan Follow with HollaDieWaldfee, unforgiven and 12 others

Originally disclosed on Code4rena

Submitted by Jeiwan, also found by HollaDieWaldfee, unforgiven, evan, Franfran, teawaterwire, bin2chen, poirots, fs0c, KingNFT, noot, 0x52, rvierdiiev, Saintcode_, and stealthyz

Since PaprController is not designed to hold any underlying tokens, calling buyAndReduceDebt with a swap fee set will result in a revert. The function can also be used to transfer out any underlying tokens sent to the contract mistakenly.

Proof of Concept

PaprController implements the buyAndReduceDebt function, which allows users to buy Papr tokens for underlying tokens and burn them to reduce their debt (PaprController.sol#L208). Optionally, the function allows the caller to specify a swap fee: a fee that’s collected from the caller. However, in reality, the fee is collected from PaprController itself: transfer instead of transferFrom is called on the underlying token (PaprController.sol#L225-L227):

if (hasFee) {
    underlying.transfer(params.swapFeeTo, amountIn * params.swapFeeBips / BIPS_ONE);
}

This scenario is covered by the testBuyAndReduceDebtReducesDebt test (BuyAndReduceDebt.t.sol#L12), however the fee is not actually set in the test:

// Fee is initialized but not set.
uint256 fee;
underlying.approve(address(controller), underlyingOut + underlyingOut * fee / 1e4);
swapParams = IPaprController.SwapParams({
    amount: underlyingOut,
    minOut: 1,
    sqrtPriceLimitX96: _maxSqrtPriceLimit({sellingPAPR: false}),
    swapFeeTo: address(5),
    swapFeeBips: fee
});

If fee is set in the test, the test wil revert with an “Arithmetic over/underflow” error:

--- a/test/paprController/BuyAndReduceDebt.t.sol
+++ b/test/paprController/BuyAndReduceDebt.t.sol
@@ -26,7 +26,7 @@ contract BuyAndReduceDebt is BasePaprControllerTest {
         IPaprController.VaultInfo memory vaultInfo = controller.vaultInfo(borrower, collateral.addr);
         assertEq(vaultInfo.debt, debt);
         assertEq(underlyingOut, underlying.balanceOf(borrower));
-        uint256 fee;
+        uint256 fee = 1e3;
         underlying.approve(address(controller), underlyingOut + underlyingOut * fee / 1e4);
         swapParams = IPaprController.SwapParams({
             amount: underlyingOut,

Recommended Mitigation Steps

Consider this change:

--- a/src/PaprController.sol
+++ b/src/PaprController.sol
@@ -223,7 +223,7 @@ contract PaprController is
         );

         if (hasFee) {
-            underlying.transfer(params.swapFeeTo, amountIn * params.swapFeeBips / BIPS_ONE);
+            underlying.safeTransferFrom(msg.sender, params.swapFeeTo, amountIn * params.swapFeeBips / BIPS_ONE);
         }

         _reduceDebt({account: account, asset: collateralAsset, burnFrom: msg.sender, amount: amountOut});

wilsoncusack (Backed) confirmed

trust1995 (judge) commented:

Chosen as best because it shows how to improve an existing test, well done.


The original report as published on Code4rena, by Jeiwan. 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 transfer instead of transferFrom to collect the swap fee, so it attempts to pay from the contract's balance rather than the caller's.

Impact

The function reverts when a fee is set, and any underlying tokens mistakenly sent to the contract could be transferred out.

What the attacker needed

Swap fee is set to a non-zero value; contract holds underlying tokens (for the transfer-out case)

The fix

The code should use safeTransferFrom to collect the fee from the caller instead of transfer from the contract.

How it was found

  1. Set a non-zero swap fee in the test.
  2. Call buyAndReduceDebt with that fee.
  3. Observe the transaction reverts with an arithmetic over/underflow error.

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 call a function to buy tokens and reduce debt, you can optionally pay a fee. The code mistakenly takes that fee from the contract's own balance instead of from your wallet. Since the contract usually holds no tokens, the transaction fails, and if tokens were sent there by mistake, they could be taken.

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 Jeiwan

View all

69 more disclosed reports, best first.

More from Papr contest

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.