Skip to content

Commit

Permalink
Dont call precompiled programs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay committed Sep 20, 2021
1 parent d6d2840 commit a8c405b
Show file tree
Hide file tree
Showing 21 changed files with 322 additions and 259 deletions.
24 changes: 1 addition & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ members = [
"programs/bpf_loader",
"programs/compute-budget",
"programs/config",
"programs/ed25519",
"programs/failure",
"programs/noop",
"programs/ownable",
"programs/secp256k1",
"programs/stake",
"programs/vote",
"rbpf-cli",
Expand Down
7 changes: 7 additions & 0 deletions program-runtime/src/instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ impl InstructionProcessor {
}
}

/// Remove a program
pub fn remove_program(&mut self, program_id: Pubkey) {
if let Some(position) = self.programs.iter().position(|(key, _)| program_id == *key) {
self.programs.remove(position);
}
}

/// Process an instruction
/// This method calls the instruction's program entrypoint method
pub fn process_instruction(
Expand Down
16 changes: 0 additions & 16 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions programs/bpf/c/src/invoke/invoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ static const uint8_t TEST_PRIVILEGE_DEESCALATION_ESCALATION_WRITABLE = 13;
static const uint8_t TEST_WRITABLE_DEESCALATION_WRITABLE = 14;
static const uint8_t TEST_NESTED_INVOKE_TOO_DEEP = 15;
static const uint8_t TEST_EXECUTABLE_LAMPORTS = 16;
static const uint8_t ADD_LAMPORTS = 17;
static const uint8_t TEST_RETURN_DATA_TOO_LARGE = 18;
static const uint8_t TEST_CALL_PRECOMPILE = 17;
static const uint8_t ADD_LAMPORTS = 18;
static const uint8_t TEST_RETURN_DATA_TOO_LARGE = 19;

static const int MINT_INDEX = 0;
static const int ARGUMENT_INDEX = 1;
Expand All @@ -40,6 +41,8 @@ static const int DERIVED_KEY2_INDEX = 7;
static const int DERIVED_KEY3_INDEX = 8;
static const int SYSTEM_PROGRAM_INDEX = 9;
static const int FROM_INDEX = 10;
static const int ED25519_PROGRAM_INDEX = 11;
static const int INVOKE_PROGRAM_INDEX = 12;

uint64_t do_nested_invokes(uint64_t num_nested_invokes,
SolAccountInfo *accounts, uint64_t num_accounts) {
Expand Down Expand Up @@ -73,7 +76,7 @@ uint64_t do_nested_invokes(uint64_t num_nested_invokes,
extern uint64_t entrypoint(const uint8_t *input) {
sol_log("Invoke C program");

SolAccountInfo accounts[12];
SolAccountInfo accounts[13];
SolParameters params = (SolParameters){.ka = accounts};

if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(accounts))) {
Expand Down Expand Up @@ -588,6 +591,16 @@ extern uint64_t entrypoint(const uint8_t *input) {
*accounts[ARGUMENT_INDEX].lamports += 1;
break;
}
case TEST_CALL_PRECOMPILE: {
sol_log("Test calling builtin from cpi");
SolAccountMeta arguments[] = {};
uint8_t data[] = {};
const SolInstruction instruction = {accounts[ED25519_PROGRAM_INDEX].key,
arguments, SOL_ARRAY_SIZE(arguments),
data, SOL_ARRAY_SIZE(data)};
sol_invoke(&instruction, accounts, SOL_ARRAY_SIZE(accounts));
break;
}
case ADD_LAMPORTS: {
*accounts[0].lamports += 1;
break;
Expand Down
12 changes: 11 additions & 1 deletion programs/bpf/rust/invoke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::{ProgramResult, MAX_PERMITTED_DATA_INCREASE},
instruction::Instruction,
msg,
program::{get_return_data, invoke, invoke_signed, set_return_data},
program_error::ProgramError,
Expand All @@ -32,7 +33,8 @@ const TEST_PRIVILEGE_DEESCALATION_ESCALATION_WRITABLE: u8 = 13;
const TEST_WRITABLE_DEESCALATION_WRITABLE: u8 = 14;
const TEST_NESTED_INVOKE_TOO_DEEP: u8 = 15;
const TEST_EXECUTABLE_LAMPORTS: u8 = 16;
const ADD_LAMPORTS: u8 = 17;
const TEST_CALL_PRECOMPILE: u8 = 17;
const ADD_LAMPORTS: u8 = 18;

// const MINT_INDEX: usize = 0; // unused placeholder
const ARGUMENT_INDEX: usize = 1;
Expand All @@ -45,6 +47,8 @@ const DERIVED_KEY2_INDEX: usize = 7;
const DERIVED_KEY3_INDEX: usize = 8;
const SYSTEM_PROGRAM_INDEX: usize = 9;
const FROM_INDEX: usize = 10;
const ED25519_PROGRAM_INDEX: usize = 11;
const INVOKE_PROGRAM_INDEX: usize = 12;

fn do_nested_invokes(num_nested_invokes: u64, accounts: &[AccountInfo]) -> ProgramResult {
assert!(accounts[ARGUMENT_INDEX].is_signer);
Expand Down Expand Up @@ -663,6 +667,12 @@ fn process_instruction(
// reset executable account
**(*accounts[ARGUMENT_INDEX].lamports).borrow_mut() += 1;
}
TEST_CALL_PRECOMPILE => {
msg!("Test calling builtin from cpi");
let instruction =
Instruction::new_with_bytes(*accounts[ED25519_PROGRAM_INDEX].key, &[], vec![]);
invoke(&instruction, accounts)?;
}
ADD_LAMPORTS => {
// make sure the total balance is fine
**accounts[0].lamports.borrow_mut() += 1;
Expand Down
12 changes: 10 additions & 2 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ fn test_program_bpf_invoke_sanity() {
const TEST_WRITABLE_DEESCALATION_WRITABLE: u8 = 14;
const TEST_NESTED_INVOKE_TOO_DEEP: u8 = 15;
const TEST_EXECUTABLE_LAMPORTS: u8 = 16;
const TEST_RETURN_DATA_TOO_LARGE: u8 = 18;
const TEST_CALL_PRECOMPILE: u8 = 17;
const TEST_RETURN_DATA_TOO_LARGE: u8 = 19;

#[allow(dead_code)]
#[derive(Debug)]
Expand Down Expand Up @@ -835,6 +836,7 @@ fn test_program_bpf_invoke_sanity() {
AccountMeta::new_readonly(derived_key3, false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new(from_keypair.pubkey(), true),
AccountMeta::new_readonly(solana_sdk::ed25519_program::id(), false),
AccountMeta::new_readonly(invoke_program_id, false),
];

Expand Down Expand Up @@ -1037,6 +1039,12 @@ fn test_program_bpf_invoke_sanity() {
&[invoke_program_id.clone()],
);

do_invoke_failure_test_local(
TEST_CALL_PRECOMPILE,
TransactionError::InstructionError(0, InstructionError::ProgramFailedToComplete),
&[],
);

do_invoke_failure_test_local(
TEST_RETURN_DATA_TOO_LARGE,
TransactionError::InstructionError(0, InstructionError::ProgramFailedToComplete),
Expand Down Expand Up @@ -1091,7 +1099,7 @@ fn test_program_bpf_invoke_sanity() {
result.unwrap_err(),
TransactionError::InstructionError(0, InstructionError::ProgramFailedToComplete)
);
}
}
}

#[cfg(feature = "bpf_rust")]
Expand Down
20 changes: 11 additions & 9 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ use solana_sdk::{
feature_set::{
allow_native_ids, blake3_syscall_enabled, check_seed_length,
close_upgradeable_program_accounts, demote_program_write_locks, disable_fees_sysvar,
libsecp256k1_0_5_upgrade_enabled, mem_overlap_fix, return_data_syscall_enabled,
secp256k1_recover_syscall_enabled,
libsecp256k1_0_5_upgrade_enabled, mem_overlap_fix, prevent_calling_precompiles_as_programs,
return_data_syscall_enabled, secp256k1_recover_syscall_enabled,
},
hash::{Hasher, HASH_BYTES},
ic_msg,
instruction::{AccountMeta, Instruction, InstructionError},
keccak,
message::Message,
native_loader,
precompiles::is_precompile,
process_instruction::{self, stable_log, ComputeMeter, InvokeContext, Logger},
program::MAX_RETURN_DATA,
pubkey::{Pubkey, PubkeyError, MAX_SEEDS, MAX_SEED_LEN},
Expand Down Expand Up @@ -2125,16 +2126,21 @@ fn check_account_infos(
fn check_authorized_program(
program_id: &Pubkey,
instruction_data: &[u8],
close_upgradeable_program_accounts: bool,
invoke_context: &dyn InvokeContext,
) -> Result<(), EbpfError<BpfError>> {
#[allow(clippy::blocks_in_if_conditions)]
if native_loader::check_id(program_id)
|| bpf_loader::check_id(program_id)
|| bpf_loader_deprecated::check_id(program_id)
|| (bpf_loader_upgradeable::check_id(program_id)
&& !(bpf_loader_upgradeable::is_upgrade_instruction(instruction_data)
|| bpf_loader_upgradeable::is_set_authority_instruction(instruction_data)
|| (close_upgradeable_program_accounts
|| (invoke_context.is_feature_active(&close_upgradeable_program_accounts::id())
&& bpf_loader_upgradeable::is_close_instruction(instruction_data))))
|| (invoke_context.is_feature_active(&prevent_calling_precompiles_as_programs::id())
&& is_precompile(program_id, |feature_id: &Pubkey| {
invoke_context.is_feature_active(feature_id)
}))
{
return Err(SyscallError::ProgramNotSupported(*program_id).into());
}
Expand Down Expand Up @@ -2171,11 +2177,7 @@ fn call<'a>(
let (message, caller_write_privileges, program_indices) =
InstructionProcessor::create_message(&instruction, &signers, &invoke_context)
.map_err(SyscallError::InstructionError)?;
check_authorized_program(
&instruction.program_id,
&instruction.data,
invoke_context.is_feature_active(&close_upgradeable_program_accounts::id()),
)?;
check_authorized_program(&instruction.program_id, &instruction.data, *invoke_context)?;
let (accounts, account_refs) = syscall.translate_accounts(
&message,
account_infos_addr,
Expand Down
25 changes: 0 additions & 25 deletions programs/ed25519/Cargo.toml

This file was deleted.

55 changes: 0 additions & 55 deletions programs/ed25519/src/lib.rs

This file was deleted.

Loading

0 comments on commit a8c405b

Please sign in to comment.