Skip to content

Commit

Permalink
USBH: check remaining bytes before dereferencing buffer
Browse files Browse the repository at this point in the history
To avoid accessing unimplemented memory. We rely on the lazy evaluation
of the C language.
  • Loading branch information
dismirlian committed Oct 2, 2019
1 parent 2cd41f9 commit fa38805
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions os/hal/src/usbh/hal_usbh_desciter.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
void cfg_iter_init(generic_iterator_t *icfg, const uint8_t *buff, uint16_t rem) {
icfg->valid = 0;

if ((buff[0] < 2) || (rem < 2) || (rem < buff[0])
if ((rem < 2) || (buff[0] < 2) || (rem < buff[0])
|| (buff[0] < USBH_DT_CONFIG_SIZE)
|| (buff[1] != USBH_DT_CONFIG))
return;
Expand All @@ -45,14 +45,14 @@ void if_iter_next(if_iterator_t *iif) {

iif->valid = 0;

if ((curr[0] < 2) || (rem < 2) || (rem < curr[0]))
if ((rem < 2) || (curr[0] < 2) || (rem < curr[0]))
return;

for (;;) {
rem -= curr[0];
curr += curr[0];

if ((curr[0] < 2) || (rem < 2) || (rem < curr[0]))
if ((rem < 2) || (curr[0] < 2) || (rem < curr[0]))
return;

if (curr[1] == USBH_DT_INTERFACE_ASSOCIATION) {
Expand Down Expand Up @@ -92,14 +92,14 @@ void ep_iter_next(generic_iterator_t *iep) {

iep->valid = 0;

if ((curr[0] < 2) || (rem < 2) || (rem < curr[0]))
if ((rem < 2) || (curr[0] < 2) || (rem < curr[0]))
return;

for (;;) {
rem -= curr[0];
curr += curr[0];

if ((curr[0] < 2) || (rem < 2) || (rem < curr[0]))
if ((rem < 2) || (curr[0] < 2) || (rem < curr[0]))
return;

if ((curr[1] == USBH_DT_INTERFACE_ASSOCIATION)
Expand Down Expand Up @@ -131,13 +131,13 @@ void cs_iter_next(generic_iterator_t *ics) {

ics->valid = 0;

if ((curr[0] < 2) || (rem < 2) || (rem < curr[0]))
if ((rem < 2) || (curr[0] < 2) || (rem < curr[0]))
return;

rem -= curr[0];
curr += curr[0];

if ((curr[0] < 2) || (rem < 2) || (rem < curr[0]))
if ((rem < 2) || (curr[0] < 2) || (rem < curr[0]))
return;

if ((curr[1] == USBH_DT_INTERFACE_ASSOCIATION)
Expand Down

0 comments on commit fa38805

Please sign in to comment.