Skip to content

Commit

Permalink
Don't update if already an executable
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay committed Jun 25, 2021
1 parent 9429d04 commit 2be7534
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
29 changes: 29 additions & 0 deletions programs/bpf/rust/invoke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const TEST_PRIVILEGE_DEESCALATION_ESCALATION_SIGNER: u8 = 12;
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 MINT_INDEX: usize = 0; // unused placeholder
const ARGUMENT_INDEX: usize = 1;
Expand Down Expand Up @@ -615,6 +617,33 @@ fn process_instruction(
TEST_NESTED_INVOKE_TOO_DEEP => {
let _ = do_nested_invokes(5, accounts);
}
TEST_EXECUTABLE_LAMPORTS => {
msg!("Test executable lamports");
let mut accounts = accounts.to_vec();

// set victim account to executable and subtract lamports
accounts[ARGUMENT_INDEX].executable = true;
**(*accounts[ARGUMENT_INDEX].lamports).borrow_mut() -= 1;
// add lamports to dest account
**(*accounts[DERIVED_KEY1_INDEX].lamports).borrow_mut() += 1;

let instruction = create_instruction(
*program_id,
&[
(accounts[ARGUMENT_INDEX].key, true, false),
(accounts[DERIVED_KEY1_INDEX].key, true, false),
],
vec![ADD_LAMPORTS, 0, 0, 0],
);
let _ = invoke(&instruction, &accounts);

// reset executable account
**(*accounts[ARGUMENT_INDEX].lamports).borrow_mut() += 1;
}
ADD_LAMPORTS => {
// make sure the total balance is fine
**accounts[0].lamports.borrow_mut() += 1;
}
_ => panic!(),
}

Expand Down
8 changes: 8 additions & 0 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ fn test_program_bpf_invoke_sanity() {
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;

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

// success cases
Expand Down Expand Up @@ -1025,6 +1027,12 @@ fn test_program_bpf_invoke_sanity() {
],
);

do_invoke_failure_test_local(
TEST_EXECUTABLE_LAMPORTS,
TransactionError::InstructionError(0, InstructionError::ExecutableLamportChange),
&[invoke_program_id.clone()],
);

// Check resulting state

assert_eq!(43, bank.get_balance(&derived_key1));
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl PreAccount {
self.account.borrow().lamports()
}

pub fn executable(&self) -> bool {
self.account.borrow().executable()
}

pub fn is_zeroed(buf: &[u8]) -> bool {
const ZEROS_LEN: usize = 1024;
static ZEROS: [u8; ZEROS_LEN] = [0; ZEROS_LEN];
Expand Down Expand Up @@ -1109,7 +1113,7 @@ impl MessageProcessor {
})?;
pre_sum += u128::from(pre_account.lamports());
post_sum += u128::from(account.lamports());
if is_writable && !account.executable() {
if is_writable && !pre_account.executable() {
pre_account.update(&account);
}
return Ok(());
Expand Down

0 comments on commit 2be7534

Please sign in to comment.