Bump Seed Canonicalization
Understand the need for consistent PDA calculation by storing and reusuing the canonical bump.
Summary
- The
create_program_address
function derives a PDA without searching for the canonical bump. This means there are multiple valid bumps, all of which will produce different addresses. - Using
find_program_address
ensures that the highest valid bump, or canonical bump, is used for the derivation, thus creating a deterministic way to find an address given specific seeds. - Upon initialization, you can use Anchor’s
seeds
andbump
constraint to ensure that PDA derivations in the account validation struct always use the canonical bump - Anchor allows you to specify a bump with the
bump = <some_bump>
constraint when verifying the address of a PDA - Because
find_program_address
can be expensive, best practice is to store the derived bump in an account’s data field to be referenced later on when re-deriving the address for verification
Lesson
Bump seeds are a number between 0 and 255, inclusive, used to ensure that an
address derived using
create_program_address
is a valid PDA. The canonical bump is the highest bump value that produces a
valid PDA. The standard in Nexis Native Chain is to always use the canonical bump when
deriving PDAs, both for security and convenience.
Insecure PDA derivation using create_program_address
Given a set of seeds, the create_program_address
function will produce a valid
PDA about 50% of the time. The bump seed is an additional byte added as a seed
to “bump” the derived address into valid territory. Since there are 256 possible
bump seeds and the function produces valid PDAs approximately 50% of the time,
there are many valid bumps for a given set of input seeds.
You can imagine that this could cause confusion for locating accounts when using seeds as a way of mapping between known pieces of information to accounts. Using the canonical bump as the standard ensures that you can always find the right account. More importantly, it avoids security exploits caused by the open-ended nature of allowing multiple bumps.
In the example below, the set_value
instruction uses a bump
that was passed
in as instruction data to derive a PDA. The instruction then derives the PDA
using create_program_address
function and checks that the address
matches
the public key of the data
account.
While the instruction derives the PDA and checks the passed-in account, which is good, it allows the caller to pass in an arbitrary bump. Depending on the context of your program, this could result in undesired behavior or potential exploit.
If the seed mapping was meant to enforce a one-to-one relationship between PDA and user, for example, this program would not properly enforce that. A user could call the program multiple times with many valid bumps, each producing a different PDA.
Recommended derivation using find_program_address
A simple way around this problem is to have the program expect only the
canonical bump and use find_program_address
to derive the PDA.
The
find_program_address
always uses the canonical bump. This function iterates through calling
create_program_address
, starting with a bump of 255 and decrementing the bump
by one with each iteration. As soon as a valid address is found, the function
returns both the derived PDA and the canonical bump used to derive it.
This ensures a one-to-one mapping between your input seeds and the address they produce.
Use Anchor’s seeds
and bump
constraints
Anchor provides a convenient way to derive PDAs in the account validation struct
using the seeds
and bump
constraints. These can even be combined with the
init
constraint to initialize the account at the intended address. To protect
the program from the vulnerability we’ve been discussing throughout this lesson,
Anchor does not even allow you to initialize an account at a PDA using anything
but the canonical bump. Instead, it uses find_program_address
to derive the
PDA and subsequently performs the initialization.
If you aren’t initializing an account, you can still validate PDAs with the
seeds
and bump
constraints. This simply rederives the PDA and compares the
derived address with the address of the account passed in.
In this scenario, Anchor does allow you to specify the bump to use to derive
the PDA with bump = <some_bump>
. The intent here is not for you to use
arbitrary bumps, but rather to let you optimize your program. The iterative
nature of find_program_address
makes it expensive, so best practice is to
store the canonical bump in the PDA account’s data upon initializing a PDA,
allowing you to reference the bump stored when validating the PDA in subsequent
instructions.
When you specify the bump to use, Anchor uses create_program_address
with the
provided bump instead of find_program_address
. This pattern of storing the
bump in the account data ensures that your program always uses the canonical
bump without degrading performance.
If you don’t specify the bump on the bump
constraint, Anchor will still use
find_program_address
to derive the PDA using the canonical bump. As a
consequence, your instruction will incur a variable amount of compute budget.
Programs that are already at risk of exceeding their compute budget should use
this with care since there is a chance that the program’s budget may be
occasionally and unpredictably exceeded.
On the other hand, if you only need to verify the address of a PDA passed in without initializing an account, you’ll be forced to either let Anchor derive the canonical bump or expose your program to unecessary risks. In that case, please use the canonical bump despite the slight mark against performance.
Lab
To demonstrate the security exploits possible when you don’t check for the canonical bump, let’s work with a program that lets each program user “claim” rewards on time.
1. Setup
Start by getting the code on the starter
branch of
this repository.
Notice that there are two instructions on the program and a single test in the
tests
directory.
The instructions on the program are:
create_user_insecure
claim_insecure
The create_user_insecure
instruction simply creates a new account at a PDA
derived using the signer’s public key and a passed-in bump.
The claim_insecure
instruction mints 10 tokens to the user and then marks the
account’s rewards as claimed so that they can’t claim again.
However, the program doesn’t explicitly check that the PDAs in question are using the canonical bump.
Have a look at the program to understand what it does before proceeding.
2. Test insecure instructions
Since the instructions don’t explicitly require the user
PDA to use the
canonical bump, an attacker can create multiple accounts per wallet and claim
more rewards than should be allowed.
The test in the tests
directory creates a new keypair called attacker
to
represent an attacker. It then loops through all possible bumps and calls
create_user_insecure
and claim_insecure
. By the end, the test expects that
the attacker has been able to claim rewards multiple times and has earned more
than the 10 tokens allotted per user.
Run anchor test
to see that this test passes, showing that the attacker is
successful. Since the test calles the instructions for every valid bump, it
takes a bit to run, so be patient.
3. Create secure instructions
Let’s demonstrate patching the vulnerability by creating two new instructions:
create_user_secure
claim_secure
Before we write the account validation or instruction logic, let’s create a new
user type, UserSecure
. This new type will add the canonical bump as a field on
the struct.
Next, let’s create account validation structs for each of the new instructions. They’ll be very similar to the insecure versions but will let Anchor handle the derivation and deserialization of the PDAs.
Finally, let’s implement the instruction logic for the two new instructions. The
create_user_secure
instruction simply needs to set the auth
, bump
and
rewards_claimed
fields on the user
account data.
The claim_secure
instruction needs to mint 10 tokens to the user and set the
user
account’s rewards_claimed
field to true
.
4. Test secure instructions
Let’s go ahead and write a test to show that the attacker can no longer claim more than once using the new instructions.
Notice that if you start to loop through using multiple PDAs like the old test, you can’t even pass the non-canonical bump to the instructions. However, you can still loop through using the various PDAs and at the end check that only 1 claim happened for a total of 10 tokens. Your final test will look something like this:
If you use Anchor for all of the PDA derivations, this particular exploit is pretty simple to avoid. However, if you end up doing anything “non-standard,” be careful to design your program to explicitly use the canonical bump!
If you want to take a look at the final solution code you can find it on the
solution
branch of
the same repository.
Challenge
Just as with other lessons in this unit, your opportunity to practice avoiding this security exploit lies in auditing your own or other programs.
Take some time to review at least one program and ensure that all PDA derivations and checks are using the canonical bump.
Remember, if you find a bug or exploit in somebody else’s program, please alert them! If you find one in your own program, be sure to patch it right away.