Skip to content
Business logic Audit finding Intermediate

PaprController.buyAndReduceDebt: msg.sender can lose paper by paying the debt twice

The buyAndReduceDebt function sends swapped papr tokens to the account address but burns papr tokens from the caller, so the caller pays twice while the account receives the tokens and debt reduction. An attacker could cause a caller to lose funds if the caller has enough papr tokens to burn.

HollaDieWaldfee Follow with evan, bin2chen and 1 other

Originally disclosed on Code4rena

Submitted by HollaDieWaldfee, also found by evan, bin2chen, and 0x52

https://github.com/with-backed/papr/blob/9528f2711ff0c1522076b9f93fba13f88d5bd5e6/src/PaprController.sol#L208-L232

https://github.com/with-backed/papr/blob/9528f2711ff0c1522076b9f93fba13f88d5bd5e6/src/libraries/UniswapHelpers.sol#L31-L61

Impact

The PaprController.buyAndReduceDebt function (https://github.com/with-backed/papr/blob/9528f2711ff0c1522076b9f93fba13f88d5bd5e6/src/PaprController.sol#L208-L232) should work like this:

  1. msg.sender swaps some amount of the underlying token for papr token
  2. This amount of papr token is used to repay debt for the address in the account parameter

msg.sender and account can be different addresses such that one can repay anyone’s debt.

However there is a mistake in the function which leads to this behavior:

  1. msg.sender swaps some amount of the underlying token for papr token
  2. The papr token is sent to the account address
  3. The papr token is burnt from the msg.sender
  4. The amount of papr token burnt from the msg.sender is used to pay back the debt of the account address

The issue is that the swapped papr token are sent to account but the papr token are burnt from msg.sender.

In the best scenario when calling this function, the msg.sender does not have enough papr token to burn so the function call reverts.

In the scenario that is worse, the msg.sender has enough papr token to be burnt.

So the account address receives the swapped papr token and the debt of account is paid as well by the msg.sender.

Thereby the msg.sender pays double the amount he wants to.

Once by swapping his underlying tokens for papr.

The second time because his papr token are burnt.

Proof of Concept

The PaprController.buyAndReduceDebt function (https://github.com/with-backed/papr/blob/9528f2711ff0c1522076b9f93fba13f88d5bd5e6/src/PaprController.sol#L208-L232) calls UniswapHelpers.swap (https://github.com/with-backed/papr/blob/9528f2711ff0c1522076b9f93fba13f88d5bd5e6/src/libraries/UniswapHelpers.sol#L31-L61):

(uint256 amountOut, uint256 amountIn) = UniswapHelpers.swap(
    pool,
    account,
    token0IsUnderlying,
    params.amount,
    params.minOut,
    params.sqrtPriceLimitX96,
    abi.encode(msg.sender)
);

The second parameter which has the value account is the recipient of the swap.

The last parameter which is msg.sender is the address paying the input amount for the swap.

So the msg.sender pays some amount of underlying and the papr that the underlying is swapped for is sent to the account.

But then the debt of account is reduced by burning papr token from msg.sender:

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

However the papr token from the swap were received by account. So the msg.sender pays twice and account receives twice.

Tools Used

VS Code

Recommended Mitigation Steps

The swapped papr token should be sent to the msg.sender instead of account such that they can then be burnt from msg.sender.

In order to achieve this, a single line in PaprController.buyAndReduceDebt must be changed:

         (uint256 amountOut, uint256 amountIn) = UniswapHelpers.swap(
             pool,
-            account,
+            msg.sender,
             token0IsUnderlying,
             params.amount,
             params.minOut,
             params.sqrtPriceLimitX96,
            abi.encode(msg.sender)
        );

wilsoncusack (Backed) confirmed


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

Impact

The caller could lose the papr tokens they pay for the swap and the papr tokens burnt from their balance, while the account receives the swapped tokens and debt reduction; if the caller lacks enough papr tokens, the transaction reverts.

What the attacker needed

Caller has enough papr tokens to burn

The fix

The swapped papr tokens should be sent to the caller instead of the account, so they can be burnt from the caller.

How it was found

  1. Caller swaps underlying tokens for papr tokens, which are sent to the account address.
  2. The debt of the account is reduced by burning papr tokens from the caller.
  3. If the caller has enough papr tokens, the transaction succeeds and the caller loses papr tokens while the account receives them.

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 that lets you swap tokens to pay off someone else's debt accidentally sends the swapped tokens to that person but takes the tokens used for repayment from you. So you end up paying both the swap cost and the repayment cost, while the other person gets the swapped tokens and their debt reduced.

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 HollaDieWaldfee

View all

39 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.