Skip to content

program: Close the record account instead of only moving its lamports - #395

Open
Fahad-Dezloper wants to merge 1 commit into
solana-program:mainfrom
Fahad-Dezloper:program-close-account-revival
Open

Fahad-Dezloper wants to merge 1 commit into
solana-program:mainfrom
Fahad-Dezloper:program-close-account-revival

Conversation

@Fahad-Dezloper

@Fahad-Dezloper Fahad-Dezloper commented Nov 30, 2026 •

Copy link
Copy Markdown

Problem

CloseAccount moves the lamports out of the record and stops there. The account data is never touched, so version stays 1, is_initialized() stays true, and the stored authority survives. Write, SetAuthority and Reallocate all gate on is_initialized(), so all three still accept the closed record.

A zero-lamport account is only reaped at the end of the transaction, not between its instructions, so until then the closed record is still live, and funding it again before the transaction ends keeps it alive for good. Initialize, Write, CloseAccount, a system transfer back to the record, then Write with the same authority:

Program log: RecordInstruction::CloseAccount
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 success
Program 11111111111111111111111111111111 success
Program log: RecordInstruction::Write
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 success

That last write needs the authority's signature, so it is not an unauthorised write. What breaks is the meaning of "closed": a caller that closes a record to revoke access and discard the data gets neither. token-2022 avoids this by ending process_close_account with close_unchecked(), which zeroes the lamports, reallocs the data to zero and reassigns the account to the system program.

Summary of Changes

  • After the lamports are moved, resize(0) the account and assign it to the system program. Funding it afterwards leaves an ordinary empty system account. The existing data.borrow_mut() is scoped to the validation block so the borrow is released before resize; the checks and their error values are unchanged.
  • Add solana-sdk-ids for system_program::ID. It was already in Cargo.lock transitively, so the lock change is one line adding it to spl-record, with no version resolution changes.
  • Add close_account_cannot_be_revived, covering the chain above and expecting the final Write to fail with InvalidAccountData. On main that Write succeeds.

make test-program (11 unit, 17 functional), make format-check-program and make clippy-program are clean. I did not run the JS clients.

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.
@joncinque

Copy link
Copy Markdown
Contributor

This makes sense to me overall. Thankfully it's not exploitable since we don't update the account data in any way, but it would be better to prevent this kind of unexpected behavior.

Can you take a look @samkim-crypto ?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants