PROBUS
PROBUS

Missing owner checks on Solana: when your program trusts a fake account

A Solana account is just bytes, plus a note of which program owns it. Only the owner program can write to those bytes. That note is the whole reason you can trust the data inside.

So when your program reads a token balance, a config or a user’s position from an account, the first question is: who owns this account? If your program never asks, an attacker can create an account under their own program, lay out the bytes to look like the real thing, and pass it in.

What it looks like

This is the insecure example from sealevel-attacks, the public repo Coral (the Anchor team) wrote to teach common Solana bugs. It reads a token account and checks the right person signed:

let token = SplTokenAccount::unpack(&ctx.accounts.token.data.borrow())?;
if ctx.accounts.authority.key != &token.owner {
    return Err(ProgramError::InvalidAccountData);
}
msg!("Your account balance is: {}", token.amount);
#[derive(Accounts)]
pub struct LogMessage<'info> {
    token: AccountInfo<'info>,
    authority: Signer<'info>,
}

It looks careful. The authority signs, and the code checks the authority matches the token account’s owner field. But nothing checks that the SPL Token program owns the account. An attacker can create their own account with the same layout, put their own key in the owner field and any balance they like in the amount, and every check passes.

The example only logs the balance. In a program that lends against that balance, or pays out from it, the attacker has just invented money.

Two kinds of “owner”

This is where people slip. The owner field inside a token account is the wallet that controls the tokens. The account’s owner, on the account itself, is the program allowed to write to it. The insecure example checks the first and never the second.

The fix

The repo’s recommended version lets Anchor do it:

#[derive(Accounts)]
pub struct LogMessage<'info> {
    #[account(constraint = authority.key == &token.owner)]
    token: Account<'info, TokenAccount>,
    authority: Signer<'info>,
}

Account<'info, TokenAccount> makes Anchor check that the SPL Token program owns the account before your code runs. The same goes for your own account types: Account<'info, Vault> checks your program owns it.

If you work with raw AccountInfo, or write native programs, check it by hand. This is the repo’s other fixed version:

if ctx.accounts.token.owner != &spl_token::ID {
    return Err(ProgramError::InvalidAccountData);
}

Check your own program in five minutes

  1. Search for AccountInfo, UncheckedAccount and any manual unpack ortry_from_slice.
  2. For each account whose data you read, ask: which program should own this?
  3. If it’s a typed Account<'info, T>, Anchor checks for you. If not, check .owneragainst that program’s ID before you read a byte.

Or let Probus check it

Probus reads your Solana repo, the Anchor or native program and the backend code around it, and flags missing owner checks along with the other common Solana bugs. On sealevel-attacks it flagged the insecure version of this example and nothing in the two fixed ones. 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 signer checks.

Next in the series: arbitrary CPI.