Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
32 changes: 20 additions & 12 deletions program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<RecordData>(
&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::<RecordData>(
&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(())
}

Expand Down
48 changes: 48 additions & 0 deletions program/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RecordData>()
.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::<Vec<_>>()
.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(),
],
);
}
Loading