Skip to content

Commit afef947

Browse files
committed
Add more ownership checks
1 parent affb0ef commit afef947

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

p-token/src/processor/batch.rs

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,52 @@ pub fn process_batch(mut accounts: &[AccountInfo], mut instruction_data: &[u8])
4646
)
4747
};
4848

49-
// `Transfer` and `TransferChecked` instructions require specific account
50-
// ownership checks when executed in a batch since account ownership is
51-
// checked by the runtime at the end of the batch processing only.
52-
match ix_data.first() {
53-
// 3 - Transfer
54-
Some(3) => {
55-
let [source_account_info, destination_account_info, _remaining @ ..] = ix_accounts
56-
else {
57-
return Err(ProgramError::NotEnoughAccountKeys);
58-
};
59-
60-
check_account_owner(source_account_info)?;
61-
check_account_owner(destination_account_info)?;
62-
}
63-
// 12 - TransferChecked
64-
Some(12) => {
65-
let [source_account_info, _, destination_account_info, _remaining @ ..] =
66-
ix_accounts
67-
else {
68-
return Err(ProgramError::NotEnoughAccountKeys);
69-
};
70-
71-
check_account_owner(source_account_info)?;
72-
check_account_owner(destination_account_info)?;
49+
// Few Instructions require specific account ownership checks when executed
50+
// in a batch since ownership is only enforced by the runtime at the end of
51+
// the batch processing.
52+
//
53+
// Instructions that do not appear in the list below do not require
54+
// ownership checks since they either do not modify accounts or the ownership
55+
// is already checked explicitly.
56+
if let Some(&discriminator) = ix_data.first() {
57+
match discriminator {
58+
// 3 - Transfer
59+
// 7 - MintTo
60+
// 8 - Burn
61+
// 14 - MintToChecked
62+
// 15 - BurnChecked
63+
3 | 7 | 8 | 14 | 15 => {
64+
let [a0, a1, ..] = ix_accounts else {
65+
return Err(ProgramError::NotEnoughAccountKeys);
66+
};
67+
check_account_owner(a0)?;
68+
check_account_owner(a1)?;
69+
}
70+
// 12 - TransferChecked
71+
12 => {
72+
let [a0, _, a2, ..] = ix_accounts else {
73+
return Err(ProgramError::NotEnoughAccountKeys);
74+
};
75+
check_account_owner(a0)?;
76+
check_account_owner(a2)?;
77+
}
78+
// 4 - Approve
79+
// 5 - Revoke
80+
// 6 - SetAuthority
81+
// 9 - CloseAccount
82+
// 10 - FreezeAccount
83+
// 11 - ThawAccount
84+
// 13 - ApproveChecked
85+
// 22 - InitializeImmutableOwner
86+
// 38 - WithdrawExcessLamports
87+
4..=13 | 22 | 38 => {
88+
let [a0, ..] = ix_accounts else {
89+
return Err(ProgramError::NotEnoughAccountKeys);
90+
};
91+
check_account_owner(a0)?;
92+
}
93+
_ => {}
7394
}
74-
_ => (),
7595
}
7696

7797
inner_process_instruction(ix_accounts, ix_data)?;

0 commit comments

Comments
 (0)