Skip to content

Commit

Permalink
Enforces the program_id being passed explicitly by removing it from g…
Browse files Browse the repository at this point in the history
…et_instruction_keyed_accounts().
  • Loading branch information
Lichtso committed Dec 20, 2021
1 parent 3586ffa commit 559ebc8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
21 changes: 11 additions & 10 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,15 @@ impl<'a> InvokeContext<'a> {

/// Get the owner of the currently executing program
pub fn get_loader(&self) -> Result<Pubkey, InstructionError> {
self.get_instruction_keyed_accounts()
.and_then(|keyed_accounts| keyed_accounts.first().ok_or(InstructionError::CallDepth))
.and_then(|keyed_account| keyed_account.owner())
let frame = self
.invoke_stack
.last()
.ok_or(InstructionError::CallDepth)?;
let first_instruction_account = frame
.number_of_program_accounts
.checked_sub(1)
.ok_or(InstructionError::CallDepth)?;
frame.keyed_accounts[first_instruction_account].owner()
}

/// Removes the first keyed account
Expand Down Expand Up @@ -759,18 +765,13 @@ impl<'a> InvokeContext<'a> {

/// Get the list of keyed accounts without the chain of program accounts
///
/// Note: The `KeyedAccount` at index `0` has the key `program_id` and
/// is followed by the `KeyedAccount`s passed by the caller.
/// Note: This only contains the `KeyedAccount`s passed by the caller.
pub fn get_instruction_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError> {
let frame = self
.invoke_stack
.last()
.ok_or(InstructionError::CallDepth)?;
let first_instruction_account = frame
.number_of_program_accounts
.checked_sub(1)
.ok_or(InstructionError::CallDepth)?;
Ok(&frame.keyed_accounts[first_instruction_account..])
Ok(&frame.keyed_accounts[frame.number_of_program_accounts..])
}

/// Get this invocation's LogCollector
Expand Down
5 changes: 2 additions & 3 deletions programs/bpf/rust/instruction_introspection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ fn process_instruction(
}

let secp_instruction_index = instruction_data[0];
let account_info_iter = &mut accounts.iter();
let instruction_accounts = next_account_info(account_info_iter)?;
let instruction_accounts = accounts.last().ok_or(ProgramError::NotEnoughAccountKeys)?;
assert_eq!(*instruction_accounts.key, instructions::id());
let data_len = instruction_accounts.try_borrow_data()?.len();
if data_len < 2 {
Expand Down Expand Up @@ -56,7 +55,7 @@ fn process_instruction(
&[instruction_data[0], instruction_data[1], 1],
vec![AccountMeta::new_readonly(instructions::id(), false)],
),
&[instruction_accounts.clone()],
accounts,
)?;
}

Expand Down
5 changes: 4 additions & 1 deletion programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,10 @@ fn test_program_bpf_instruction_introspection() {
);

// Passing transaction
let account_metas = vec![AccountMeta::new_readonly(sysvar::instructions::id(), false)];
let account_metas = vec![
AccountMeta::new(program_id, false),
AccountMeta::new(sysvar::instructions::id(), false),
];
let instruction0 = Instruction::new_with_bytes(program_id, &[0u8, 0u8], account_metas.clone());
let instruction1 = Instruction::new_with_bytes(program_id, &[0u8, 1u8], account_metas.clone());
let instruction2 = Instruction::new_with_bytes(program_id, &[0u8, 2u8], account_metas);
Expand Down

0 comments on commit 559ebc8

Please sign in to comment.