Solana development is error-prone. Often, people cite Anchor as a remedy to this problem: “write it in Anchor, and your Solana programs will be secure.” Not so fast! There are still many gotchas even when using Anchor. Let’s explore some of them.
Writing secure Solana programs is no easy task. There are many foot guns one may encounter while creating Solana programs. To safely operate in this ecosystem, a developer needs to be intimately familiar with invariants upheld by the runtime, under what conditions are those invariants no longer guaranteed, and a myriad of account confusion, confused deputy, integer over and underflow, and a dozen more classic security vulnerability classes.
Anchor was created because barebones Solana is extremely error-prone. Anchor is a Solana framework designed to make the process of writing programs significantly easier and safer than writing them without. Anchor addresses many of the most common and impactful issues that you’ll face when writing Solana smart contracts. Here are just a few:
- Account confusion
- Account types
- Account addresses
- Account liveness
- Missing account constraints
As a smart-contract auditing firm we’ve reviewed a lot of Solana programs, especially those written with Anchor. Even though Anchor addresses many of the major issues found in Solana programs, there are plenty of issues that crop up regardless.
Program Derived Addresses (PDA) are a way to programmatically derive an off-curve account address. They’re commonly used for Cross Program Invocations (CPI), as well as creating accounts that hold stateful information. For example, a liquidity pool program may create a PDA to track funds allocated for a particular purpose or use a PDA to hold the protocol’s global configuration.
Using PDAs in Anchor is simple - we define the elements that make up the seed used in deriving the account address and we’re off to the races. Here’s an example:
Here we’re using a static string
user-stats and the fixed length (32 bytes) public key address from the user account to validate the address for the provided UserStats account. This is a typical example of how seeds are often defined and used on PDAs.
Because no two distinct users share a public key, their UserStats accounts will also have unique addresses. This isn’t always the case for PDAs, though! Collisions can result in a Denial of Service vulnerability in the best cases and complete compromise on the other end of the spectrum.
Take this example of a
CreateNewProduct instruction account guard, which creates a product PDA based on the product name and a leading static string.
It might not appear to have any issues, but we begin to see problems when considering the other PDAs in our program.
Here we add a
Bid PDA account that is meant to hold a user’s bid on a product. The bid is scoped to a product and a user. While this might appear safe at first, because this
Bid and this
Product cannot collide with each other, an attacker can leverage this to prevent users from submitting bids or products from being created.
In certain circumstances, an attacker can create a product that generates the same program address as a
Bid account. This would prevent a specific user from creating a bid for that product.
This is because elements from a seed array are effectively treated as one large collection of bytes and processed in 32 byte chunks.
Therefore this seed (used by the attacker’s Product account)
and this seed (used by the victim’s Bid account)
generate the same PDA public key. There is an underwhelming warning about this specific behavior in the
solana-program crate documentation for Pubkey.
As we’ve seen in the code samples in the seed collision examples above, Anchor’s instruction account guards define a fixed number of accounts and the constraints between them. These constraints are important to upholding invariants in your protocol, but the limitation that we use a fixed number of accounts can be limiting.
Oftentimes a variable number of accounts need to be passed to instructions for processing. For instance, when attempting to cash out participants in a pool or dynamically CPI to another program. To support this, Anchor exposes
ctx.remaining_accounts to access any additional accounts not included in the original instruction account guard.
It is important to note that absolutely none of the protections that Anchor typically provides are present on the accounts in
ctx.remaining_accounts. That means that if you intend to use these accounts for anything sensitive you’ll need to perform some of the following checks:
- Account Ownership
- Account Type (anchor uses an 8 byte discriminator for this)
- Account Liveness (anchor’s discriminator solves this - basically ‘is the data field set to 0x00…00’)
- Account Address
- For PDA’s ensure the bump is correct as well!
There are plenty of ways that using
ctx.remaining_accounts can go wrong. It’s important to treat all interactions with
AccountInfo types from
ctx.remaining_accounts as dangerous.
Solana is a composable ecosystem of programs. From calling out to the System program, creating new accounts, to forwarding liquidity to Solend generating yield, Cross Program Invocations are a normal part of writing Solana programs.
For all the good CPIs bring, they also hide a danger. When a CPI is performed, accounts forwarded along that also signed the transaction carries with them their
is_signer status. This means that if Program A calls Program B with some account having signed the transaction, Program B can use that account as a signer. This is very important to understand. This means that Program B can create new accounts, send lamports and any associated tokens (provided the associated token account is passed as well), and much more.
This issue compounds with invoke_signed which is commonly used when interfacing with PDAs. PDA accounts are frequently used as an authority account in protocols. If calls to
invoke_signed pass this authority in, alongside the required signer seeds, then a substantial third-party risk is introduced. The target program could be dynamically passed in by the caller allowing an attacker to call their malicious program leveraging the authority PDA. Even statically defined programs introduce risk if they can be upgraded.
The problems CPIs can introduce do not end there. While Anchor does a lot for you automatically, one thing it doesn’t do is update deserialized accounts after a CPI.
For example, imagine you have a Mint account and are just about to mint some token for the caller to track their contribution to a liquidity pool. You perform a CPI to the token program to mint these tokens and then read the current supply from the Mint account for a calculation later on. While, intuitively, you might expect the supply to be accurate, accounts in Anchor don’t update their data after a CPI!
let authority_seeds = /* seeds */;
To get the expected behavior, make sure to call Anchor’s reload method on the account. This will refresh the struct’s fields with the current underlying data.
Anchor is a fantastic way to uplevel your Solana program’s security, but it isn’t a silver bullet. There are still several ways that vulnerabilities can manifest in Anchor-based Solana programs. These bugs’ impacts can range from denial-of-service to an attacker making off with your funds.
Thanks to Hana for helping fact-check this article.
Zellic is a smart contract auditing firm founded by hackers, for hackers. Our security researchers have uncovered vulnerabilities in the most valuable targets, from Fortune 500s to DeFi giants. Whether you’re developing or deploying smart contracts, Zellic’s experienced team can prevent you from being hacked.
Contact us for an audit that’s better than the rest. We specialize in a number of ecosystems, including Solana. Our customers include LayerZero and Pyth.