diff --git a/programs/bpf/rust/invoke/src/lib.rs b/programs/bpf/rust/invoke/src/lib.rs index b84ead95dbdd93..d17d2c97196aa6 100644 --- a/programs/bpf/rust/invoke/src/lib.rs +++ b/programs/bpf/rust/invoke/src/lib.rs @@ -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; @@ -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!(), } diff --git a/programs/bpf/tests/programs.rs b/programs/bpf/tests/programs.rs index 5b8acc15655bd7..a64049fcc5a5a2 100644 --- a/programs/bpf/tests/programs.rs +++ b/programs/bpf/tests/programs.rs @@ -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)] @@ -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 @@ -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)); diff --git a/runtime/src/message_processor.rs b/runtime/src/message_processor.rs index fa86f5c9829ba7..1a76e9a763bf2d 100644 --- a/runtime/src/message_processor.rs +++ b/runtime/src/message_processor.rs @@ -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]; @@ -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(());