From 36c45d0c605ef4a2974999a6e50217a804d1b8d8 Mon Sep 17 00:00:00 2001 From: Fahad Date: Tue, 1 Dec 2026 01:51:41 +0530 Subject: [PATCH] program: Close the record account instead of only moving its lamports CloseAccount moved the lamports out but left the account data intact, so the record stayed initialized with its authority. The runtime only reaps a zero-lamport account at the end of the transaction, so until then the closed record could still be written to, and funding it again in the same transaction kept it alive entirely. Resize the data to zero and assign the account to the system program, as token-2022 does when it closes an account. --- Cargo.lock | 1 + program/Cargo.toml | 1 + program/src/processor.rs | 32 +++++++++++++++---------- program/tests/functional.rs | 48 +++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22b388e..40c75c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2847,6 +2847,7 @@ dependencies = [ "solana-program-error", "solana-program-pack", "solana-rent", + "solana-sdk-ids", "solana-security-txt", "solana-system-interface", "thiserror 2.0.21", diff --git a/program/Cargo.toml b/program/Cargo.toml index a5c734c..3a4d7d7 100644 --- a/program/Cargo.toml +++ b/program/Cargo.toml @@ -21,6 +21,7 @@ solana-msg = "3.1.0" solana-program-entrypoint = "3.1.1" solana-program-error = "3.0.1" solana-program-pack = "3.1.0" +solana-sdk-ids = "3.1.0" solana-security-txt = "1.1.3" thiserror = "2.0.18" diff --git a/program/src/processor.rs b/program/src/processor.rs index f435403..effac77 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -7,6 +7,7 @@ use { solana_msg::msg, solana_program_error::{ProgramError, ProgramResult}, solana_program_pack::IsInitialized, + solana_sdk_ids::system_program, }; fn check_authority(authority_info: &AccountInfo, expected_authority: &Address) -> ProgramResult { @@ -112,25 +113,32 @@ pub fn process_instruction( let data_info = next_account_info(account_info_iter)?; let authority_info = next_account_info(account_info_iter)?; let destination_info = next_account_info(account_info_iter)?; - let raw_data = &mut data_info.data.borrow_mut(); - if raw_data.len() < RecordData::WRITABLE_START_INDEX { - return Err(ProgramError::InvalidAccountData); - } - let account_data = bytemuck::try_from_bytes_mut::( - &mut raw_data[..RecordData::WRITABLE_START_INDEX], - ) - .map_err(|_| ProgramError::InvalidArgument)?; - if !account_data.is_initialized() { - msg!("Record not initialized"); - return Err(ProgramError::UninitializedAccount); + { + let raw_data = &mut data_info.data.borrow_mut(); + if raw_data.len() < RecordData::WRITABLE_START_INDEX { + return Err(ProgramError::InvalidAccountData); + } + let account_data = bytemuck::try_from_bytes_mut::( + &mut raw_data[..RecordData::WRITABLE_START_INDEX], + ) + .map_err(|_| ProgramError::InvalidArgument)?; + if !account_data.is_initialized() { + msg!("Record not initialized"); + return Err(ProgramError::UninitializedAccount); + } + check_authority(authority_info, &account_data.authority)?; } - check_authority(authority_info, &account_data.authority)?; let destination_starting_lamports = destination_info.lamports(); let data_lamports = data_info.lamports(); **data_info.lamports.borrow_mut() = 0; **destination_info.lamports.borrow_mut() = destination_starting_lamports .checked_add(data_lamports) .ok_or(RecordError::Overflow)?; + // The runtime only reaps a zero-lamport account at the end of the + // transaction, so empty the account and hand it back to the system + // program to stop a closed record being used again before then. + data_info.resize(0)?; + data_info.assign(&system_program::ID); Ok(()) } diff --git a/program/tests/functional.rs b/program/tests/functional.rs index 2df10da..80ae15c 100644 --- a/program/tests/functional.rs +++ b/program/tests/functional.rs @@ -767,3 +767,51 @@ fn reallocate_fail_data_length_overflow() { ], ); } + +#[test] +fn close_account_cannot_be_revived() { + let mollusk = Mollusk::new(&id(), "spl_record"); + let payer = Address::new_unique(); + let authority = Address::new_unique(); + let recipient = Address::new_unique(); + let account = Address::new_unique(); + let data = &[111u8; 8]; + let account_length = std::mem::size_of::() + .checked_add(data.len()) + .unwrap(); + let rent = Rent::default().minimum_balance(account_length); + let mut ixs = initialize_instructions(&payer, &authority, &account, data).to_vec(); + ixs.push(instruction::close_account(&account, &authority, &recipient)); + // The runtime only reaps a zero-lamport account at the end of the + // transaction, so fund it again before then. + ixs.push(system_instruction::transfer(&payer, &account, rent)); + // The record is closed, so the old authority must not be able to write. + ixs.push(instruction::write(&account, &authority, 0, &[222u8; 8])); + let checks = [ + [Check::success()], + [Check::success()], + [Check::success()], + [Check::success()], + [Check::success()], + [Check::err(ProgramError::InvalidAccountData)], + ]; + mollusk.process_and_validate_instruction_chain( + ixs.iter() + .zip(checks.iter().map(|c| c.as_ref())) + .collect::>() + .as_slice(), + &[ + ( + payer, + Account { + lamports: 1_000_000_000, + ..Default::default() + }, + ), + (authority, Account::default()), + (recipient, Account::default()), + (account, Account::default()), + mollusk_svm::program::keyed_account_for_system_program(), + ], + ); +}