Rules › Anchor (Rust)
CPI to a program the caller chooses
invoke/invoke_signed with a program account that isn't Program<'info, T> or checked against a known id runs whatever program the caller passes.
Why it matters
A cross-program invocation to a program account the caller passed in runs whatever program they chose, with whatever accounts and signer seeds you hand it. An attacker substitutes a fake "token program" that reports success without moving anything, or steals PDA-signed authority. Use Program<'info, T> (which checks the program id) or compare the key to the expected id before invoking.
Vulnerable
let ix = Instruction { program_id: ctx.accounts.target.key(), accounts: vec![], data };
invoke(&ix, &[ctx.accounts.target.to_account_info()])?;
Fixed
// Anchor checks token_program.key() == spl_token::ID for Program<'info, Token>.
pub token_program: Program<'info, Token>,
// ...
token::transfer(CpiContext::new(ctx.accounts.token_program.to_account_info(), accounts), amount)?;
What other tools call it
| Tool | Detector or reference |
|---|---|
| Sealevel attacks | 5-arbitrary-cpi |
Check your code
Paste a contract into the in-browser checker, or let the Claude Code plugin scan before every deploy. The quick check for this rule is a pattern match: treat a hit as "look here", and a miss as no guarantee.
Related rules
- Unchecked account without a /// CHECK: note
- Authority account that isn't a Signer
- Account data deserialized without an owner check
- init_if_needed (reinitialization risk)
- Account closed by draining lamports by hand
- overflow-checks not enabled for release builds