9 — Unchecked CPI Target
Severity: 9 | Real: arbitrary signed CPI class (Neodyme "Arbitrary Signed Program Invocation"); Wormhole is KV-101 (sysvar spoofing) and Cashio is KV-146/KV-016 (unanchored collateral validation)
CPI to wrong program — attacker passes their own program instead of SPL Token.
Verification Procedure
Step 1: Find all CPI calls and their target programs
grep -rn --include="*.rs" -B2 "CpiContext::new\|invoke(\|invoke_signed(" programs/*/src/instructions/- Record: Every CPI with its target program source
Step 2: Verify every CPI target is constrained in the Accounts struct
grep -rn --include="*.rs" -A30 "#\[derive(Accounts)\]" programs/*/src/instructions/ | grep -E "Program<|program:.*Program|token_program|system_program"- ✅ PASS: Every program account used in CPI is typed as
Program<'info, Token>orProgram<'info, System>(Anchor auto-validates the program ID) - ❌ FAIL: Program account is
UncheckedAccountorAccountInfowithout explicit ID check
Step 2b: Trace Instruction.program_id (not the AccountInfo slot)
rg -n 'Instruction\s*\{[^}]*program_id\s*:\s*\*?[a-z_]+\.(key|key\(\))' programs/
rg -n 'Instruction::new_with_(bytes|borsh|bincode)\(\s*\*?[a-z_]+\.key' programs/
rg -n 'CpiContext::new(_with_signer)?\(\s*ctx\.accounts\.\w+\.key\(\)' programs/ # Anchor 1.x
rg -n 'CpiContext::new(_with_signer)?\(\s*ctx\.accounts\.\w+\.to_account_info' programs/ # Anchor 0.x
rg -n "Program<'info *>|Program<'info, *\(\) *>" programs/
rg -n 'spl_token(_2022)?::instruction::[a-z_]+\(\s*&?[a-z_.]+\.key' programs/ # builder-validated: downgrade- ✅ PASS / downgrade: the id is a constant, or goes through an SPL builder that validates it (
spl_token::instruction::*accepts only SPL Token;spl_token_2022::instruction::*core builders accept SPL Token or Token-2022, extension builders only Token-2022), or throughanchor_spl::token::*(hardcodesspl_token::ID) oranchor_spl::token_2022::*/token_interface::*(pass the id to those validating builders) — missingProgram<Token>/Interface<TokenInterface>is then hygiene (FP-7); still check that the program accepted is the one the protocol intends (SPL Token vs Token-2022) - ❌ FAIL: hand-built instruction (
Instruction { program_id, .. },Instruction::new_with_*) with the id from an unchecked account; Anchor-generatedcpi::client of a non-SPL program with an unchecked program account (0.29+ clients invokectx.program's key, 1.xCpiContext::new(unchecked.key()), notcrate::ID); untypedProgram<'info>target;Program<'info, System>on anchor-lang 1.0.0–1.0.1 and 1.0.0-rc.3–rc.5 (the System id isPubkey::default(), which those versions treat as the untyped "unit" case: executable-only check, KV-154) when its key is used as a program id (hand-builtInstruction, acpi::client, aCpiContextpassed to a helper that invokesctx.program_id) —anchor_lang::system_program::*helpers and generatedinit/reallocbuild with the hard-coded System id, so they are not affected by the substitution; id check not dominating the invoke on every path
Sources: Trail of Bits plugins/building-secure-contracts/skills/solana-vulnerability-scanner/resources/VULNERABILITY_PATTERNS.md [paraphrased, CC-BY-SA-4.0]; Solana Foundation course content/courses/program-security/arbitrary-cpi.md, coral-xyz/sealevel-attacks (repo programs/5-arbitrary-cpi), Neodyme "Common Pitfalls" "Arbitrary signed program invocation", crytic/solana-lints lints/arbitrary_cpi [paraphrased, cited]; workersio/skills plugins/solana-audit/skills/solana-audit/references/arbitrary-cpi-target.md (PR #9 tree, MIT, Copyright (c) 2026 workers.io), Wormhole/Cashio attribution corrected.
Step 3: Check for manual program ID verification (if UncheckedAccount is used)
grep -rn --include="*.rs" -A5 "UncheckedAccount\|/// CHECK:" programs/*/src/instructions/- For each
/// CHECK:comment: verify there's an actual runtime check (require_keys_eq!(account.key(), expected_id)) - ✅ PASS: Every
/// CHECK:has corresponding runtime validation - ❌ FAIL:
/// CHECK:comments say "safe" but no actual validation code follows
Step 4: Verify no CPI to user-provided program
grep -rn --include="*.rs" "remaining_accounts" programs/*/src/instructions/ | grep -c ""- Then check if any remaining_account is used as a CPI target
- ✅ PASS: remaining_accounts are only used as data accounts, never as CPI targets
- ❌ FAIL: A remaining_account is passed as the program to invoke
Overall verdict:
- ✅: All CPI targets are Anchor Program<> types with compile-time verification
- ⚠️: Some CPIs use manual checks that are correct but not compile-time enforced
- ❌: Any CPI target from user input without verification