# PositionAction4626::_onDecreaseLever wrongly updates tokenOut forcing user's funds to be stuck in the position action contract

PositionAction4626._onDecreaseLever overwrites tokenOut with Balancer exit amount, so residual collateral is under-reported and stuck in the contract.

- Payout: Audit contest
- Severity: medium
- Program: LoopFi
- Found by: 0xAlix2
- Disclosed: 2025-02-17
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-07-loopfi-findings/issues/240
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-loopfi%3AM-07
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** PositionAction4626 leverage-down flow with Balancer auxAction
**Root cause:** _onDecreaseLever sets tokenOut to the Balancer exit return amount instead of the actual underlying balance held by the contract, so the residual returned to the user is understated when the exit amount differs from the redeemed 4626 amount.
**Steps:**
1. Deploy PositionAction4626 with flashlender, swapAction, poolAction, vaultRegistry
2. User deposits 250 sLPETH into CDPVault via positionAction.deposit
3. Borrow 100 ETH via positionAction.borrow
4. Call increaseLever with flashLoanAmount=50 ETH, primarySwap BALANCER EXACT_IN, auxAction join Balancer pool -> ~56 BPT to user
5. Transfer BPT to positionAction, call decreaseLever with subCollateral=70 ether, primarySwap EXACT_OUT 40 ether, auxAction exit Balancer pool
6. Observe user receives only ~59 tokens while ~20 tokens remain stuck in positionAction
**How found:** Code review of _onDecreaseLever: noticed tokenOut is overwritten by the auxAction exit return value, then reasoned through three mismatch scenarios (different token, different recipient, smaller amount) and built a PoC for the smaller-amount case.
**Exploitation:** User performs a legitimate decreaseLever with a Balancer auxAction; the contract miscomputes tokenOut from the Balancer exit return, sends a smaller residual to the user, and the remaining collateral is permanently trapped in the position action contract.
**Insight:** When a function returns an amount from a delegatecall to an external action, verify the returned value matches the actual token balance delta at the contract, especially when the action's recipient and token can differ from the primary flow.

## Proof of concept (copied exactly from the original report)

**decreaseLever call exiting Balancer pool leaving funds stuck**

```
userProxy.execute(
                address(positionAction),
                abi.encodeWithSelector(
                    positionAction.decreaseLever.selector,
                    LeverParams({
                        position: address(userProxy),
                        vault: address(vault),
                        collateralToken: address(stakingLPEth),
                        auxSwap: emptySwap,
                        primarySwap: SwapParams({
                            swapProtocol: SwapProtocol.BALANCER,
                            swapType: SwapType.EXACT_OUT,
                            assetIn: address(token),
                            amount: 40 ether,
                            limit: 50 ether,
                            recipient: address(positionAction),
                            deadline: block.timestamp,
                            args: abi.encode(weightedPoolIdArray, assets)
                        }),
                        auxAction: PoolActionParams(
                            Protocol.BALANCER,
                            0,
                            user,
                            abi.encode(poolId, wstETH_bb_a_WETH_BPTl, bptAmount, 0, tokens, minAmountsOut)
                        )
                    }),
                    70 ether,
                    address(user)
                )
            );

            // Collateral is 180 ETH, debt is 110 ETH
            (uint256 collateral, uint256 debt, , , , ) = vault.positions(address(userProxy));
            assertEq(collateral, depositAmount - 70 ether);
            assertEq(debt, borrowAmount + flashLoanAmount - 40 ether);

            // All balancer LP tokens are burnt
            assertEq(IERC20(wstETH_bb_a_WETH_BPTl).balanceOf(user), 0);
            assertEq(IERC20(wstETH_bb_a_WETH_BPTl).balanceOf(address(positionAction)), 0);

            // User has 59 collateral tokens, Balancer's exit tokens (49 from above) + residual ~(50 - 40)
            assertEq(token.balanceOf(user) / 1 ether, 59);
            // Position action now holds 20 collateral tokens that are stuck (70 - ~50)
            assertEq(token.balanceOf(address(positionAction)) / 1 ether, 20);
```

