# Wrong-account instruction validation

Companion or repay instruction checked for presence/owner but not that its position account matches the one acted on

_Area: Smart contract access control and control flow. Seen in 2 disclosed reports; latest 2024-05-29. Updated 2026-11-22._

## How it works
A program enforces a rule by requiring a companion instruction in the same transaction (a repay before closing collateral, an addCollateral before borrowing). The check confirms the companion exists and that some of its accounts (trader, pool, owner) match, but never confirms the companion's *position/state account* is the same one the current instruction acts on. The binding between the two instructions is therefore forgeable: the companion can be pointed at a different position, and the rule is satisfied without applying to the position being changed [E1](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-01.md)[E3](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-02.md).

## Where it shows up
- Instruction introspection / sysvar-instruction checks that assert presence of a companion instruction rather than matching its account keys [E3](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-02.md)
- Validation loops that compare `trader` and `trading_pool` but skip `position_account` [E1](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-01.md)
- Any "must repay before withdraw", "must add collateral before borrow", "must close before claim" pairing
- Validation comparing a caller-supplied amount against a bound recomputed from live state (liquidation `maxClose` vs `repayAmount`) [E2](https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-08.md)

## How to review
For every cross-instruction dependency, list each account that binds the companion to the current context and confirm all are compared, not a subset. The invariant: *the companion instruction's position/state account must equal the account the current instruction mutates*. For amount checks, the invariant is that the bound and the amount are derived from the same state snapshot; if the bound is recomputed live, ask what cheap mutation shifts it [E2](https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-08.md).

## How finders spotted it
- Read the validation block and listed which keys were compared; noticed `position_account` absent from the repay check, then built a stateful PoC reusing a prior repay [E1](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-01.md)
- Noticed borrow only asserted an addCollateral instruction *existed*, not that it matched the position; confirmed with a two-position single-transaction PoC [E3](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-02.md)
- Noticed `maxClose` came from live `borrowBalance` while `repayAmount` was fixed at submission, making the comparison sensitive to any intervening debt change [E2](https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-08.md)

## Variants seen
- Repay-for-dust-position unlocks another position's collateral [E1](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-01.md)
- Collateral added to position A backs a borrow on position B [E3](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-02.md)
- Live-recomputed liquidation bound reverted by a 1-wei repay [E2](https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-08.md)

## Impact and severity
E1 and E3 let a borrower withdraw collateral or borrow SOL with no backing, repeatable across positions to drain a pool's node-wallet liquidity — critical [E1](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-01.md)[E3](https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-02.md). E2 is a griefing DoS: liquidations revert, bad debt accrues — medium [E2](https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-08.md). Impact rises with repeatability and whether the mis-bound account controls funds versus only blocking a keeper.

## Evidence
- E1: https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-01.md
- E2: https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-08.md
- E3: https://bugbountylibrary.com/r/c4%3A2024-04-lavarage%3AH-02.md
