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
Chosen as best because it shows how to improve an existing test, well done.
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
- Set a non-zero swap fee in the test.
- Call buyAndReduceDebt with that fee.
- 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.