Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
solana_program::sysvar::instructions::get_instruction_relative
to enforce that they are called from CPI and the identify of the caller, This may contains a bug, since a transaction could contain a valid instruction atindex [0]
, but be malicious.Full description is available: #29 (comment)
Solution
To correctly determine if your program was called via CPI, you should:
Use
get_instruction_relative(-1, ...)
: This will attempt to retrieve the instruction that called the currently executing instruction. If the program was called directly, there will be no instruction at index-1
, and the function will return an error.Check for Errors: If
get_instruction_relative(-1, ...) returns an Err
, it means the program was called directly. If it returnsOk
, you can proceed, knowing it was called via CPI.Closes: [Bug/Investigation] Components CPI caller check #29
cc - @GabrielePicco