solana_fender 0.5.4

Static analysis tool for Solana smart contracts
Documentation
# Improper Instruction Introspection

## Reason for Detection

Improper Instruction Introspection occurs when a Solana program inspects other instructions in the same transaction using the `Instructions` sysvar but fails to correctly validate the relationship between the instructions.

Specifically, using absolute indices (e.g., `load_instruction_at_checked(0, ...)`) to access instructions is risky. An attacker can construct a transaction with multiple instructions where the program checks a specific instruction (e.g., at index 0) but executes logic based on that check in multiple other instructions within the same transaction. If the program relies on absolute indexing without verifying that the instruction being inspected is the one intended (or correlated) for the current operation, it can be tricked into authorizing actions (like minting tokens) multiple times based on a single valid authorization.

Detecting the use of `load_instruction_at_checked` with constant integer arguments (like `0`) allows developers to identify potential vulnerabilities where absolute indexing is used.

## Static Analysis Suitability

Is it appropriate to have a static analyzer fix it?

**Detection**: Yes. Static analysis is well-suited for detecting this pattern. We can identify calls to `solana_program::sysvar::instructions::load_instruction_at_checked` where the index argument is a constant literal. This is a strong indicator of absolute indexing, which requires careful manual review or should be replaced with relative indexing.

**Fixing**: Partially.
- **Relative Indexing**: A static analyzer could suggest replacing absolute indexing with relative indexing (e.g., `get_instruction_relative(-1, ...)`) if the intent is to check a preceding instruction. However, automatically applying this fix is risky because the tool cannot know for certain which instruction the developer intended to reference.
- **Correlation Checks**: If absolute indexing is intentional (which is rare but possible), the fix involves adding logic to verify that the inspected instruction is relevant to the current execution context. This logic is highly specific to the program's semantics and cannot be automatically generated by a static analyzer.

**Conclusion**: While detection is reliable and highly recommended, automated fixing is not fully appropriate due to the semantic understanding required to determine the correct instruction to inspect. The tool should flag the issue and recommend using relative indexing or adding correlation checks.