Arbitrary CPI on Solana: when your program calls whatever program it’s handed
When a Solana program moves tokens, it doesn’t move them itself. It calls the SPL Token program to do it. That call from one program to another is a cross-program invocation, or CPI.
The program you call is just another account in the transaction, and the caller picks the accounts. So the question is the same one as in missing owner checks: did you check the account is the one you think it is? If your program never checks that the “token program” it was handed is the real SPL Token program, an attacker can pass in a program of their own. Your program then calls it with the source, destination, authority and amount, and their program decides what happens.
The example below comes from sealevel-attacks, the public repo Coral (the Anchor team) wrote to teach common Solana bugs.
The bug
In the insecure example, the token program is a plain AccountInfo, and its key goes straight into the transfer:
solana_program::program::invoke(
&spl_token::instruction::transfer(
ctx.accounts.token_program.key,
ctx.accounts.source.key,
ctx.accounts.destination.key,
ctx.accounts.authority.key,
&[],
amount,
)?,Nothing ties token_program to the real SPL Token program. A fake program can report success without moving anything, so your program carries on as if the transfer happened: credits a deposit, marks an order paid, mints a share. Or it can do something else with the accounts it was handed.
The fix
In native code, check the program ID before you call it:
if &spl_token::ID != ctx.accounts.token_program.key {
return Err(ProgramError::IncorrectProgramId);
}In Anchor, the repo’s recommended version types the account so Anchor does the check for you, and uses Anchor’s own transfer helper:
#[derive(Accounts)]
pub struct Cpi<'info> {
source: Account<'info, TokenAccount>,
destination: Account<'info, TokenAccount>,
authority: Signer<'info>,
token_program: Program<'info, Token>,
}Program<'info, Token> fails the instruction if the account isn’t the SPL Token program. The same version also types the token accounts and makes the authority a Signer, which closes the owner and signer holes from posts 1 and 2 at the same time.
Check your own program in five minutes
- Search for
invoke(andinvoke_signed(. - For each call, find where the program being called comes from. If it’s an account passed in by the caller, check there’s a comparison against a fixed program ID, or that it’s typed as
Program<'info, ...>in Anchor. - Look for any program account typed as a bare
AccountInfoorUncheckedAccount. Each one needs a reason.
Or let Probus check it
Probus reads your Solana repo, the Anchor or native program and the backend code around it, and flags this along with the other common Solana bugs. On sealevel-attacks it flagged the insecure version of this example and nothing in its fixed versions. See the full report.
That’s an audit of your code for the mistakes that cost Solana teams the most, done in minutes rather than weeks.
Run a free scan on your own repo. No card.
Earlier in the series: missing owner checks.