Skip to content
Merged
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
3 changes: 2 additions & 1 deletion p-token/src/processor/close_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
.unwrap_or(&source_account.owner);

if !source_account.is_owned_by_system_program_or_incinerator() {
validate_owner(authority, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(authority, authority_info, remaining)? };
} else if destination_account_info.key() != &INCINERATOR_ID {
return Err(ProgramError::InvalidAccountData);
}
Expand Down
7 changes: 5 additions & 2 deletions p-token/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ fn check_account_owner(account_info: &AccountInfo) -> ProgramResult {

/// Validates owner(s) are present.
///
/// Note that `owner_account_info` will be immutable borrowed when it represents
///
/// # Safety
///
/// The `owner_account_info` will be immutable borrowed when it represents
/// a multisig account, therefore it should not have any mutable borrows when
/// calling this function.
#[inline(always)]
#[allow(clippy::arithmetic_side_effects)]
fn validate_owner(
unsafe fn validate_owner(
expected_owner: &Pubkey,
owner_account_info: &AccountInfo,
signers: &[AccountInfo],
Expand Down
3 changes: 2 additions & 1 deletion p-token/src/processor/revoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub fn process_revoke(accounts: &[AccountInfo]) -> ProgramResult {
return Err(TokenError::AccountFrozen.into());
}

validate_owner(&source_account.owner, owner_info, remaining)?;
// SAFETY: `owner_info` is not currently borrowed.
unsafe { validate_owner(&source_account.owner, owner_info, remaining)? }

source_account.clear_delegate();
source_account.set_delegated_amount(0);
Expand Down
12 changes: 8 additions & 4 deletions p-token/src/processor/set_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])

match authority_type {
AuthorityType::AccountOwner => {
validate_owner(&account.owner, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(&account.owner, authority_info, remaining)? };

if let Some(authority) = new_authority {
account.owner = *authority;
Expand All @@ -67,7 +68,8 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
}
AuthorityType::CloseAccount => {
let authority = account.close_authority().unwrap_or(&account.owner);
validate_owner(authority, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(authority, authority_info, remaining)? };

if let Some(authority) = new_authority {
account.set_close_authority(authority);
Expand All @@ -90,7 +92,8 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
// mint_authority.
let mint_authority = mint.mint_authority().ok_or(TokenError::FixedSupply)?;

validate_owner(mint_authority, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(mint_authority, authority_info, remaining)? };

if let Some(authority) = new_authority {
mint.set_mint_authority(authority);
Expand All @@ -105,7 +108,8 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
.freeze_authority()
.ok_or(TokenError::MintCannotFreeze)?;

validate_owner(freeze_authority, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(freeze_authority, authority_info, remaining)? };

if let Some(authority) = new_authority {
mint.set_freeze_authority(authority);
Expand Down
3 changes: 2 additions & 1 deletion p-token/src/processor/shared/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub fn process_approve(
}
}

validate_owner(&source_account.owner, owner_info, remaining)?;
// SAFETY: `owner_info` is not currently borrowed.
unsafe { validate_owner(&source_account.owner, owner_info, remaining)? };

// Sets the delegate and delegated amount.

Expand Down
6 changes: 4 additions & 2 deletions p-token/src/processor/shared/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub fn process_burn(
if !source_account.is_owned_by_system_program_or_incinerator() {
match source_account.delegate() {
Some(delegate) if authority_info.key() == delegate => {
validate_owner(delegate, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(delegate, authority_info, remaining)? };

let delegated_amount = source_account
.delegated_amount()
Expand All @@ -67,7 +68,8 @@ pub fn process_burn(
}
}
_ => {
validate_owner(&source_account.owner, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(&source_account.owner, authority_info, remaining)? };
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion p-token/src/processor/shared/mint_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ pub fn process_mint_to(
}

match mint.mint_authority() {
Some(mint_authority) => validate_owner(mint_authority, owner_info, remaining)?,
// SAFETY: `owner_info` is not currently borrowed.
Some(mint_authority) => unsafe { validate_owner(mint_authority, owner_info, remaining)? },
None => return Err(TokenError::FixedSupply.into()),
}

Expand Down
3 changes: 2 additions & 1 deletion p-token/src/processor/shared/toggle_account_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pub fn process_toggle_account_state(accounts: &[AccountInfo], freeze: bool) -> P
let mint = unsafe { load::<Mint>(mint_info.borrow_data_unchecked())? };

match mint.freeze_authority() {
Some(authority) => validate_owner(authority, authority_info, remaining),
// SAFETY: `authority_info` is not currently borrowed.
Some(authority) => unsafe { validate_owner(authority, authority_info, remaining) },
None => Err(TokenError::MintCannotFreeze.into()),
}?;

Expand Down
6 changes: 4 additions & 2 deletions p-token/src/processor/shared/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ pub fn process_transfer(
// Validates the authority (delegate or owner).

if source_account.delegate() == Some(authority_info.key()) {
validate_owner(authority_info.key(), authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(authority_info.key(), authority_info, remaining)? };

let delegated_amount = source_account
.delegated_amount()
Expand All @@ -142,7 +143,8 @@ pub fn process_transfer(
}
}
} else {
validate_owner(&source_account.owner, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(&source_account.owner, authority_info, remaining)? };
}

if self_transfer || amount == 0 {
Expand Down
9 changes: 6 additions & 3 deletions p-token/src/processor/withdraw_excess_lamports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ pub fn process_withdraw_excess_lamports(accounts: &[AccountInfo]) -> ProgramResu
return Err(TokenError::NativeNotSupported.into());
}

validate_owner(&account.owner, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(&account.owner, authority_info, remaining)? };
}
Mint::LEN => {
// SAFETY: `source_data` has the same length as `Mint`.
let mint = unsafe { load::<Mint>(source_data)? };

match mint.mint_authority() {
Some(mint_authority) => {
validate_owner(mint_authority, authority_info, remaining)?;
// SAFETY: `authority_info` is not currently borrowed.
unsafe { validate_owner(mint_authority, authority_info, remaining)? };
}
None if source_account_info == authority_info => {
// Comparing whether the AccountInfo's "point" to the same account or
Expand All @@ -58,7 +60,8 @@ pub fn process_withdraw_excess_lamports(accounts: &[AccountInfo]) -> ProgramResu
}
}
Multisig::LEN => {
validate_owner(source_account_info.key(), authority_info, remaining)?;
// SAFETY: `authority_info` is not currently mutably borrowed.
unsafe { validate_owner(source_account_info.key(), authority_info, remaining)? };
}
_ => return Err(TokenError::InvalidState.into()),
}
Expand Down