-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathadmin.rs
More file actions
431 lines (389 loc) · 16.8 KB
/
admin.rs
File metadata and controls
431 lines (389 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::collections::HashMap;
use solana_sdk::{account::AccountSharedData, pubkey::Pubkey};
use solana_svm::{
account_loader::{LoadedTransaction, TransactionCheckResult},
transaction_error_metrics::TransactionErrorMetrics,
transaction_execution_result::{ExecutedTransaction, TransactionExecutionDetails},
transaction_processing_result::{ProcessedTransaction, TransactionProcessingResult},
transaction_processor::{
LoadAndExecuteSanitizedTransactionsOutput, TransactionProcessingConfig,
TransactionProcessingEnvironment,
},
};
use solana_svm_callback::TransactionProcessingCallback;
use solana_svm_transaction::svm_transaction::SVMTransaction;
use solana_timings::ExecuteTimings;
use spl_token::solana_program::program_option::COption;
use spl_token::solana_program::program_pack::Pack;
use spl_token::state::Mint;
use tracing::warn;
const SPL_TOKEN_ID: Pubkey = spl_token::id();
// SPL Token instruction types
const INSTRUCTION_INITIALIZE_MINT: u8 = 0;
/// This VM is used to execute admin transactions
#[derive(Default)]
pub struct AdminVm {}
impl AdminVm {
/// Creates a new SPL Token Mint account with the given parameters
fn create_mint_account(
decimals: u8,
mint_authority: &[u8],
freeze_authority: Option<&[u8]>,
) -> AccountSharedData {
// Parse mint authority pubkey
let mint_auth_pubkey =
Pubkey::new_from_array(mint_authority.try_into().expect("Invalid mint authority"));
// Parse freeze authority if provided
let freeze_auth_pubkey = freeze_authority
.map(|auth| Pubkey::new_from_array(auth.try_into().expect("Invalid freeze authority")));
// Create the Mint struct using official SPL Token types
let mint = Mint {
mint_authority: COption::Some(mint_auth_pubkey),
supply: 0,
decimals,
is_initialized: true,
freeze_authority: freeze_auth_pubkey
.map(COption::Some)
.unwrap_or(COption::None),
};
// Pack the mint data using the official Pack trait
let mut mint_data = vec![0u8; Mint::LEN];
Mint::pack(mint, &mut mint_data).expect("Failed to pack mint");
let mut account = AccountSharedData::new(0, Mint::LEN, &spl_token::id());
account.set_data_from_slice(&mint_data);
account
}
/// Creates an ExecutedTransaction result
fn create_executed_transaction(
accounts: Vec<(Pubkey, AccountSharedData)>,
) -> ExecutedTransaction {
ExecutedTransaction {
loaded_transaction: LoadedTransaction {
accounts,
..Default::default()
},
execution_details: TransactionExecutionDetails {
status: Ok(()),
log_messages: None,
inner_instructions: None,
return_data: None,
executed_units: 0,
accounts_data_len_delta: 0,
},
programs_modified_by_tx: HashMap::new(),
}
}
pub fn load_and_execute_sanitized_transactions<CB: TransactionProcessingCallback>(
&self,
_callbacks: &CB,
sanitized_txs: &[impl SVMTransaction],
_check_results: Vec<TransactionCheckResult>,
_environment: &TransactionProcessingEnvironment,
_config: &TransactionProcessingConfig,
) -> LoadAndExecuteSanitizedTransactionsOutput {
let mut processing_results: Vec<TransactionProcessingResult> = vec![];
for tx in sanitized_txs {
let mut accounts = vec![];
for (program_id, instruction) in tx.program_instructions_iter() {
match *program_id {
SPL_TOKEN_ID => {
let Some(instruction_type) = instruction.data.first() else {
warn!("[admin-vm] SPL Token instruction has empty data, skipping");
continue;
};
match *instruction_type {
INSTRUCTION_INITIALIZE_MINT => {
// Parse InitializeMint instruction
if !instruction.accounts.is_empty() && instruction.data.len() >= 34
{
// Parse instruction data
// TODO: Instruction data could be invalid,
// so we need to handle it without causing a
// panic
let account_keys = tx.account_keys();
let mint_index = instruction.accounts[0] as usize;
let mint_pubkey = account_keys.get(mint_index).unwrap();
// Extract parameters from instruction data
let decimals = instruction.data[1];
let mint_authority = &instruction.data[2..34];
// Check for optional freeze authority
let freeze_authority = if instruction.data.len() > 34
&& instruction.data[34] == 1
{
Some(&instruction.data[35..67])
} else {
None
};
// Create the mint account
let mint_account = Self::create_mint_account(
decimals,
mint_authority,
freeze_authority,
);
accounts.push((*mint_pubkey, mint_account));
}
}
_ => {
warn!(
"[admin-vm] Unsupported SPL token instruction type: {}",
instruction_type
);
}
}
}
_ => {
warn!("[admin-vm] Unsupported program ID: {}", program_id);
}
}
}
// Create successful processing result
let executed_tx = Self::create_executed_transaction(accounts);
processing_results.push(Ok(ProcessedTransaction::Executed(Box::new(executed_tx))))
}
LoadAndExecuteSanitizedTransactionsOutput {
// TODO: Not implemented
error_metrics: TransactionErrorMetrics::default(),
// TODO: No implemented
execute_timings: ExecuteTimings::default(),
// TODO: Not implemented
balance_collector: None,
processing_results,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::account::ReadableAccount;
use spl_token::solana_program::program_pack::Pack;
use spl_token::state::Mint;
#[test]
fn test_create_mint_account_roundtrip() {
let authority = Pubkey::new_unique();
let account = AdminVm::create_mint_account(6, &authority.to_bytes(), None);
let mint = Mint::unpack(account.data()).unwrap();
assert_eq!(mint.decimals, 6);
assert!(mint.is_initialized);
assert_eq!(mint.supply, 0);
assert_eq!(mint.mint_authority, COption::Some(authority));
assert_eq!(mint.freeze_authority, COption::None);
}
#[test]
fn test_initialize_mint_with_freeze_authority() {
let authority = Pubkey::new_unique();
let freeze = Pubkey::new_unique();
let account =
AdminVm::create_mint_account(9, &authority.to_bytes(), Some(&freeze.to_bytes()));
let mint = Mint::unpack(account.data()).unwrap();
assert_eq!(mint.decimals, 9);
assert_eq!(mint.freeze_authority, COption::Some(freeze));
}
// Shared dummy callback for all admin VM tests
struct DummyCb;
impl solana_svm_callback::TransactionProcessingCallback for DummyCb {
fn get_account_shared_data(&self, _pubkey: &Pubkey) -> Option<AccountSharedData> {
None
}
fn account_matches_owners(&self, _account: &Pubkey, _owners: &[Pubkey]) -> Option<usize> {
None
}
}
impl solana_svm_callback::InvokeContextCallback for DummyCb {}
fn run_admin_vm(
txs: &[solana_sdk::transaction::SanitizedTransaction],
) -> LoadAndExecuteSanitizedTransactionsOutput {
let vm = AdminVm::default();
let check_results = crate::processor::get_transaction_check_results(txs.len());
let env = solana_svm::transaction_processor::TransactionProcessingEnvironment::default();
let config = solana_svm::transaction_processor::TransactionProcessingConfig::default();
vm.load_and_execute_sanitized_transactions(&DummyCb, txs, check_results, &env, &config)
}
/// Build a SanitizedTransaction with a single instruction targeting the given
/// program_id, with the given accounts and data.
fn make_spl_tx(
program_id: Pubkey,
accounts_indices: &[u8],
data: Vec<u8>,
) -> solana_sdk::transaction::SanitizedTransaction {
use solana_sdk::{
instruction::{AccountMeta, Instruction},
message::Message,
signature::{Keypair, Signer},
transaction::Transaction,
};
use std::collections::HashSet;
let payer = Keypair::new();
let mint = Pubkey::new_unique();
// Build account metas: payer (signer), mint, program
let mut account_metas = vec![
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(mint, false),
];
// If test wants more accounts, add dummies
for _ in 2..accounts_indices.iter().copied().max().unwrap_or(0) as usize + 1 {
account_metas.push(AccountMeta::new(Pubkey::new_unique(), false));
}
let ix = Instruction {
program_id,
accounts: account_metas,
data,
};
let msg = Message::new(&[ix], Some(&payer.pubkey()));
let tx = Transaction::new(&[&payer], msg, solana_sdk::hash::Hash::default());
solana_sdk::transaction::SanitizedTransaction::try_from_legacy_transaction(
tx,
&HashSet::new(),
)
.unwrap()
}
#[test]
fn test_load_and_execute_unsupported_program() {
let from = solana_sdk::signature::Keypair::new();
let to = Pubkey::new_unique();
let tx = crate::test_helpers::create_test_sanitized_transaction(&from, &to, 100);
let output = run_admin_vm(&[tx]);
// Should still produce a result (with empty accounts since program is unsupported)
assert_eq!(output.processing_results.len(), 1);
let result = output
.processing_results
.into_iter()
.next()
.unwrap()
.unwrap();
match result {
ProcessedTransaction::Executed(executed) => {
assert!(executed.loaded_transaction.accounts.is_empty());
}
_ => panic!("Expected Executed variant"),
}
}
#[test]
fn test_spl_empty_data_skips_gracefully() {
// SPL Token instruction with empty data → skips without panic
let tx = make_spl_tx(spl_token::id(), &[1], vec![]);
let output = run_admin_vm(&[tx]);
assert_eq!(output.processing_results.len(), 1);
let result = output
.processing_results
.into_iter()
.next()
.unwrap()
.unwrap();
match result {
ProcessedTransaction::Executed(executed) => {
assert!(executed.loaded_transaction.accounts.is_empty());
}
_ => panic!("Expected Executed variant"),
}
}
#[test]
fn test_spl_compiled_indices_prevent_oob() {
// InitializeMint instruction where accounts[0] = 255 (way out of bounds)
// data: [0 (InitializeMint), decimals, 32 bytes mint_authority]
let mut data = vec![0u8; 34]; // type=0, decimals=6, then 32 bytes authority
data[1] = 6;
data[2..34].copy_from_slice(&Pubkey::new_unique().to_bytes());
// Build tx with program_id = spl_token, but the instruction's account index
// points beyond the transaction's account_keys
use solana_sdk::{
instruction::{AccountMeta, Instruction},
message::Message,
signature::{Keypair, Signer},
transaction::Transaction,
};
use std::collections::HashSet;
let payer = Keypair::new();
// Only 2 account keys total (payer + program), but instruction.accounts[0] = 1
// which is valid. Let's use accounts[0] = 200 to trigger OOB.
// We need raw control: create instruction with accounts = [200]
let ix = Instruction {
program_id: spl_token::id(),
accounts: vec![AccountMeta::new(payer.pubkey(), true)],
data,
};
let msg = Message::new(&[ix], Some(&payer.pubkey()));
let tx = Transaction::new(&[&payer], msg, solana_sdk::hash::Hash::default());
let sanitized = solana_sdk::transaction::SanitizedTransaction::try_from_legacy_transaction(
tx,
&HashSet::new(),
)
.unwrap();
// The instruction.accounts[0] in the compiled instruction will be a valid
// index since Message compilation maps AccountMeta → indices. So OOB via
// SanitizedTransaction is hard to achieve. Instead, verify the short-data
// path handles correctly (data.len() >= 34 but accounts is empty).
// This is the more realistic attack vector.
let output = run_admin_vm(&[sanitized]);
// With valid compiled indices this won't panic — the accounts[0] maps correctly.
assert_eq!(output.processing_results.len(), 1);
}
#[test]
fn test_spl_short_data_skips_mint_creation() {
// InitializeMint type byte (0) but data too short (< 34 bytes)
let data = vec![0u8; 10]; // type=0, but only 10 bytes total
let tx = make_spl_tx(spl_token::id(), &[1], data);
let output = run_admin_vm(&[tx]);
assert_eq!(output.processing_results.len(), 1);
let result = output
.processing_results
.into_iter()
.next()
.unwrap()
.unwrap();
match result {
ProcessedTransaction::Executed(executed) => {
// Short data → guard clause skips mint creation → empty accounts
assert!(executed.loaded_transaction.accounts.is_empty());
}
_ => panic!("Expected Executed variant"),
}
}
#[test]
fn test_spl_unsupported_instruction_type() {
// SPL Token Transfer instruction (type = 3) → logs warning, empty accounts
let data = vec![3u8; 10]; // type=3 (Transfer)
let tx = make_spl_tx(spl_token::id(), &[1], data);
let output = run_admin_vm(&[tx]);
assert_eq!(output.processing_results.len(), 1);
let result = output
.processing_results
.into_iter()
.next()
.unwrap()
.unwrap();
match result {
ProcessedTransaction::Executed(executed) => {
assert!(executed.loaded_transaction.accounts.is_empty());
}
_ => panic!("Expected Executed variant"),
}
}
#[test]
fn test_spl_valid_initialize_mint() {
// Valid InitializeMint: type=0, decimals=9, 32-byte authority
let authority = Pubkey::new_unique();
let mut data = vec![0u8; 34];
data[1] = 9; // decimals
data[2..34].copy_from_slice(&authority.to_bytes());
let tx = make_spl_tx(spl_token::id(), &[1], data);
let output = run_admin_vm(&[tx]);
assert_eq!(output.processing_results.len(), 1);
let result = output
.processing_results
.into_iter()
.next()
.unwrap()
.unwrap();
match result {
ProcessedTransaction::Executed(executed) => {
// Should have created one mint account
assert_eq!(executed.loaded_transaction.accounts.len(), 1);
let (_, account) = &executed.loaded_transaction.accounts[0];
let mint = Mint::unpack(account.data()).unwrap();
assert_eq!(mint.decimals, 9);
assert_eq!(mint.mint_authority, COption::Some(authority));
}
_ => panic!("Expected Executed variant"),
}
}
}