Skip to content

Commit f6c572f

Browse files
authored
Add bounds check to upd_test num_ parameter (#154)
1 parent a81c6c0 commit f6c572f

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

program/src/oracle/oracle.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ static uint64_t upd_test( SolParameters *prm, SolAccountInfo *ka )
454454
if ( prm->data_len != sizeof( cmd_upd_test_t ) ||
455455
px->magic_ != PC_MAGIC ||
456456
px->ver_ != cmd->ver_ ||
457-
px->type_ != PC_ACCTYPE_TEST ) {
457+
px->type_ != PC_ACCTYPE_TEST ||
458+
cmd->num_ > PC_COMP_SIZE ) {
458459
return ERROR_INVALID_ARGUMENT;
459460
}
460461

program/src/oracle/test_oracle.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,69 @@ Test(oracle, pc_size ) {
337337
PC_COMP_SIZE * sizeof( pc_price_comp_t ) );
338338
}
339339

340+
Test( oracle, upd_test ) {
341+
342+
// Initialize the test account
343+
SolPubkey p_id = {.x = { 0xff, }};
344+
SolPubkey pkey = {.x = { 1, }};
345+
SolPubkey mkey = {.x = { 2, }};
346+
uint64_t pqty = 100;
347+
pc_map_table_t mptr[1];
348+
sol_memset( mptr, 0, sizeof( pc_price_t ) );
349+
SolAccountInfo acc[] = {{
350+
.key = &pkey,
351+
.lamports = &pqty,
352+
.data_len = 0,
353+
.data = NULL,
354+
.owner = NULL,
355+
.rent_epoch = 0,
356+
.is_signer = true,
357+
.is_writable = true,
358+
.executable = false
359+
},{
360+
.key = &mkey,
361+
.lamports = &pqty,
362+
.data_len = sizeof( pc_price_t ),
363+
.data = (uint8_t*)mptr,
364+
.owner = &p_id,
365+
.rent_epoch = 0,
366+
.is_signer = true,
367+
.is_writable = true,
368+
.executable = false
369+
}};
370+
cmd_hdr_t hdata = {
371+
.ver_ = PC_VERSION,
372+
.cmd_ = e_cmd_init_test,
373+
};
374+
SolParameters prm = {
375+
.ka = acc,
376+
.ka_num = 2,
377+
.data = (const uint8_t*)&hdata,
378+
.data_len = sizeof( hdata ),
379+
.program_id = &p_id
380+
};
381+
cr_assert( SUCCESS == dispatch( &prm, acc ) );
382+
383+
// Try and send an upd_test instruction with an invalid num_ parameter
384+
cmd_upd_test_t idata = {
385+
.ver_ = PC_VERSION,
386+
.cmd_ = e_cmd_upd_test,
387+
.num_ = PC_COMP_SIZE + 1,
388+
.expo_ = -8,
389+
.slot_diff_ = {1, 1},
390+
.price_ = {10, 10},
391+
.conf_ = {20, 20}
392+
};
393+
prm.data = (const uint8_t*)&idata;
394+
prm.data_len = sizeof( idata );
395+
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
396+
397+
// Send an upd_test instruction with a valid num_ parameter
398+
idata.num_ = 2;
399+
cr_assert( SUCCESS == dispatch( &prm, acc ) );
400+
401+
}
402+
340403
Test( oracle, upd_price ) {
341404
cmd_upd_price_t idata = {
342405
.ver_ = PC_VERSION,

0 commit comments

Comments
 (0)