Skip to content

Commit 97142f5

Browse files
committed
Added check of receive buffer length
1 parent 7ab50c8 commit 97142f5

File tree

5 files changed

+100
-35
lines changed

5 files changed

+100
-35
lines changed

firmware/cdc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ static int cdc_send_ready()
3939
return CDC_IsPacketSent();
4040
}
4141

42-
static void cdc_peek(uint8_t **data)
42+
static uint32_t cdc_peek(uint8_t **data)
4343
{
44-
*data = USB_Data_Peek();
44+
return USB_Data_Peek(data);
4545
}
4646

4747
static void cdc_consume()
4848
{
49-
USB_Data_Get();
49+
uint8_t *data;
50+
51+
USB_Data_Get(&data);
5052
USB_DataRx_Sched();
5153
}
5254

firmware/nand_programmer.c

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ typedef struct
198198
typedef struct
199199
{
200200
uint8_t *rx_buf;
201+
uint32_t rx_buf_len;
201202
uint32_t addr;
202203
uint32_t len;
203204
int addr_is_set;
@@ -398,13 +399,21 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
398399
{
399400
int ret;
400401
uint32_t addr, page, pages_in_block, len, total_len;
401-
np_erase_cmd_t *erase_cmd = (np_erase_cmd_t *)prog->rx_buf;
402-
bool is_bad = false, skip_bb = erase_cmd->flags.skip_bb;
402+
np_erase_cmd_t *erase_cmd;
403+
bool skip_bb, is_bad = false;
403404

405+
DEBUG_PRINT("Erase at 0x%lx %lx bytes command\r\n", addr, len);
406+
407+
if (prog->rx_buf_len < sizeof(np_erase_cmd_t))
408+
{
409+
ERROR_PRINT("Wrong buffer length for erase command %lu\r\n",
410+
prog->rx_buf_len);
411+
return NP_ERR_LEN_INVALID;
412+
}
413+
erase_cmd = (np_erase_cmd_t *)prog->rx_buf;
404414
total_len = len = erase_cmd->len;
405415
addr = erase_cmd->addr;
406-
407-
DEBUG_PRINT("Erase at 0x%lx %lx bytes command\r\n", addr, len);
416+
skip_bb = erase_cmd->flags.skip_bb;
408417

409418
if (skip_bb && !prog->bb_is_read && (ret = _np_cmd_read_bad_blocks(prog)))
410419
return ret;
@@ -496,13 +505,20 @@ static int np_cmd_nand_write_start(np_prog_t *prog)
496505
{
497506
int ret;
498507
uint32_t addr, len;
508+
np_write_start_cmd_t *write_start_cmd;
499509

500-
np_write_start_cmd_t *write_start_cmd =
501-
(np_write_start_cmd_t *)prog->rx_buf;
510+
DEBUG_PRINT("Write at 0x%lx 0x%lx bytes command\r\n", addr, len);
511+
512+
if (prog->rx_buf_len < sizeof(np_write_start_cmd_t))
513+
{
514+
ERROR_PRINT("Wrong buffer length for write start command %lu\r\n",
515+
prog->rx_buf_len);
516+
return NP_ERR_LEN_INVALID;
517+
}
502518

519+
write_start_cmd = (np_write_start_cmd_t *)prog->rx_buf;
503520
addr = write_start_cmd->addr;
504521
len = write_start_cmd->len;
505-
DEBUG_PRINT("Write at 0x%lx 0x%lx bytes command\r\n", addr, len);
506522

507523
if (addr + len > prog->chip_info.size)
508524
{
@@ -611,15 +627,30 @@ static int np_nand_write(np_prog_t *prog)
611627
static int np_cmd_nand_write_data(np_prog_t *prog)
612628
{
613629
uint32_t write_len, bytes_left, len;
614-
np_write_data_cmd_t *write_data_cmd = (np_write_data_cmd_t *)prog->rx_buf;
630+
np_write_data_cmd_t *write_data_cmd;
631+
632+
if (prog->rx_buf_len < sizeof(np_write_data_cmd_t))
633+
{
634+
ERROR_PRINT("Wrong buffer length for write data command %lu\r\n",
635+
prog->rx_buf_len);
636+
return NP_ERR_LEN_INVALID;
637+
}
615638

639+
write_data_cmd = (np_write_data_cmd_t *)prog->rx_buf;
616640
len = write_data_cmd->len;
617641
if (len + sizeof(np_write_data_cmd_t) > NP_PACKET_BUF_SIZE)
618642
{
619643
ERROR_PRINT("Data size is wrong 0x%lx\r\n", len);
620644
return NP_ERR_CMD_DATA_SIZE;
621645
}
622646

647+
if (len + sizeof(np_write_data_cmd_t) != prog->rx_buf_len)
648+
{
649+
ERROR_PRINT("Buffer len 0x%lx is bigger then command 0x%lx\r\n",
650+
prog->rx_buf_len, len + sizeof(np_write_data_cmd_t));
651+
return NP_ERR_CMD_DATA_SIZE;
652+
}
653+
623654
if (!prog->addr_is_set)
624655
{
625656
ERROR_PRINT("Write address is not set\r\n");
@@ -763,15 +794,25 @@ static int _np_cmd_nand_read(np_prog_t *prog)
763794
int ret;
764795
uint32_t addr, len, send_len;
765796
static np_page_t page;
797+
np_read_cmd_t *read_cmd;
798+
bool skip_bb;
766799
uint32_t resp_header_size = offsetof(np_resp_t, data);
767800
uint32_t tx_data_len = sizeof(np_packet_send_buf) - resp_header_size;
768-
np_read_cmd_t *read_cmd = (np_read_cmd_t *)prog->rx_buf;
769-
bool skip_bb = read_cmd->flags.skip_bb;
770801
np_resp_t *resp = (np_resp_t *)np_packet_send_buf;
771802

803+
DEBUG_PRINT("Read at 0x%lx 0x%lx bytes command\r\n", addr, len);
804+
805+
if (prog->rx_buf_len < sizeof(np_read_cmd_t))
806+
{
807+
ERROR_PRINT("Wrong buffer length for read command %lu\r\n",
808+
prog->rx_buf_len);
809+
return NP_ERR_LEN_INVALID;
810+
}
811+
812+
read_cmd = (np_read_cmd_t *)prog->rx_buf;
772813
addr = read_cmd->addr;
773814
len = read_cmd->len;
774-
DEBUG_PRINT("Read at 0x%lx 0x%lx bytes command\r\n", addr, len);
815+
skip_bb = read_cmd->flags.skip_bb;
775816

776817
if (addr + len > prog->chip_info.size)
777818
{
@@ -881,10 +922,19 @@ static int np_cmd_nand_read(np_prog_t *prog)
881922

882923
static int np_cmd_nand_conf(np_prog_t *prog)
883924
{
884-
np_conf_cmd_t *conf_cmd = (np_conf_cmd_t *)prog->rx_buf;
925+
np_conf_cmd_t *conf_cmd;
885926

886927
DEBUG_PRINT("Chip configure command\r\n");
887928

929+
if (prog->rx_buf_len < sizeof(np_conf_cmd_t))
930+
{
931+
ERROR_PRINT("Wrong buffer length for configuration command %lu\r\n",
932+
prog->rx_buf_len);
933+
return NP_ERR_LEN_INVALID;
934+
}
935+
936+
conf_cmd = (np_conf_cmd_t *)prog->rx_buf;
937+
888938
prog->chip_info.page_size = conf_cmd->page_size;
889939
prog->chip_info.block_size = conf_cmd->block_size;
890940
prog->chip_info.size = conf_cmd->size;
@@ -983,7 +1033,15 @@ static bool np_cmd_is_valid(np_cmd_code_t code)
9831033

9841034
static int np_cmd_handler(np_prog_t *prog)
9851035
{
986-
np_cmd_t *cmd = (np_cmd_t *)prog->rx_buf;
1036+
np_cmd_t *cmd;
1037+
1038+
if (prog->rx_buf_len < sizeof(np_cmd_t))
1039+
{
1040+
ERROR_PRINT("Wrong buffer length for command %lu\r\n",
1041+
prog->rx_buf_len);
1042+
return NP_ERR_LEN_INVALID;
1043+
}
1044+
cmd = (np_cmd_t *)prog->rx_buf;
9871045

9881046
if (!prog->chip_is_conf && cmd->code != NP_CMD_NAND_CONF &&
9891047
cmd->code != NP_CMD_VERSION_GET)
@@ -1007,9 +1065,8 @@ static void np_packet_handler(np_prog_t *prog)
10071065

10081066
do
10091067
{
1010-
np_comm_cb->peek(&prog->rx_buf);
1011-
1012-
if (!prog->rx_buf)
1068+
prog->rx_buf_len = np_comm_cb->peek(&prog->rx_buf);
1069+
if (!prog->rx_buf_len)
10131070
break;
10141071

10151072
ret = np_cmd_handler(prog);

firmware/nand_programmer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ typedef struct
1010
{
1111
int (*send)(uint8_t *data, uint32_t len);
1212
int (*send_ready)();
13-
void (*peek)(uint8_t **data);
13+
uint32_t (*peek)(uint8_t **data);
1414
void (*consume)();
1515
} np_comm_cb_t;
1616

firmware/usb_cdc/hw_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ uint32_t CDC_Receive_DATA(void);
6262
uint32_t CDC_ReceiveDataLen(void);
6363
void CDC_ReceiveDataAck(void);
6464
int CDC_IsPacketSent(void);
65-
uint8_t *USB_Data_Peek(void);
66-
uint8_t *USB_Data_Get(void);
65+
uint32_t USB_Data_Peek(uint8_t **data);
66+
uint32_t USB_Data_Get(uint8_t **data);
6767
void USB_DataRx_Sched(void);
6868
/* External variables --------------------------------------------------------*/
6969

firmware/usb_cdc/usb_endp.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,40 @@ void EP1_IN_Callback (void)
6969
#define PACKET_SIZE 64
7070
#define CIRC_BUF_SIZE 34 /* 62 * 34 = ~2K of data (max. NAND page) */
7171

72-
typedef uint8_t packet_t[PACKET_SIZE];
72+
typedef uint8_t packet_buf_t[PACKET_SIZE];
73+
74+
typedef struct
75+
{
76+
packet_buf_t pbuf;
77+
uint32_t len;
78+
} packet_t;
7379

7480
static packet_t circ_buf[CIRC_BUF_SIZE];
7581
static uint8_t head, size, tail = CIRC_BUF_SIZE - 1;
7682

77-
uint8_t *USB_Data_Peek(void)
83+
uint32_t USB_Data_Peek(uint8_t **data)
7884
{
79-
uint8_t *data;
80-
8185
if (!size)
82-
return NULL;
86+
return 0;
8387

84-
data = circ_buf[head];
88+
*data = circ_buf[head].pbuf;
8589

86-
return data;
90+
return circ_buf[head].len;
8791
}
8892

89-
uint8_t *USB_Data_Get(void)
93+
uint32_t USB_Data_Get(uint8_t **data)
9094
{
91-
uint8_t *data;
95+
uint32_t len;
9296

9397
if (!size)
94-
return NULL;
98+
return 0;
9599

96-
data = circ_buf[head];
100+
*data = circ_buf[head].pbuf;
101+
len = circ_buf[head].len;
97102
head = (head + 1) % CIRC_BUF_SIZE;
98103
size--;
99104

100-
return data;
105+
return len;
101106
}
102107

103108
static inline void USB_DataRx_Sched_Internal(void)
@@ -119,7 +124,8 @@ void EP3_OUT_Callback(void)
119124
if (size < CIRC_BUF_SIZE)
120125
{
121126
tail = (tail + 1) % CIRC_BUF_SIZE;
122-
PMAToUserBufferCopy(circ_buf[tail], ENDP3_RXADDR, Receive_length);
127+
PMAToUserBufferCopy(circ_buf[tail].pbuf, ENDP3_RXADDR, Receive_length);
128+
circ_buf[tail].len = Receive_length;
123129
size++;
124130
USB_DataRx_Sched_Internal();
125131
}

0 commit comments

Comments
 (0)