Repository navigation
program: Close the record account instead of only moving its lamports - #395
Open
Fahad-Dezloper wants to merge 1 commit into
Open
Fahad-Dezloper wants to merge 1 commit into
Fahad-Dezloper wants to merge 1 commit into
Conversation
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CloseAccountmoves the lamports out of the record and stops there. The account data is never touched, soversionstays1,is_initialized()stays true, and the stored authority survives.Write,SetAuthorityandReallocateall gate onis_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, thenWritewith the same authority: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-2022avoids this by endingprocess_close_accountwithclose_unchecked(), which zeroes the lamports, reallocs the data to zero and reassigns the account to the system program.Summary of Changes
resize(0)the account andassignit to the system program. Funding it afterwards leaves an ordinary empty system account. The existingdata.borrow_mut()is scoped to the validation block so the borrow is released beforeresize; the checks and their error values are unchanged.solana-sdk-idsforsystem_program::ID. It was already inCargo.locktransitively, so the lock change is one line adding it tospl-record, with no version resolution changes.close_account_cannot_be_revived, covering the chain above and expecting the finalWriteto fail withInvalidAccountData. OnmainthatWritesucceeds.make test-program(11 unit, 17 functional),make format-check-programandmake clippy-programare clean. I did not run the JS clients.