@@ -111,6 +111,8 @@ static uint8_t *mb2_find_tag_by_type(uint8_t *tags, uint32_t tags_len,
111111 while ((uint8_t * )tag + sizeof (* tag ) <= end && tag -> type != 0 ) {
112112 if (tag -> size < sizeof (* tag ))
113113 return NULL ;
114+ if (tag -> size > ((uint32_t )(end - (uint8_t * )tag )))
115+ return NULL ;
114116 if (tag -> type == type )
115117 return (uint8_t * )tag ;
116118 tag = (struct mb2_tag * )mb2_align_address_up ((uint8_t * )tag + tag -> size ,
@@ -315,17 +317,26 @@ static void mb2_parse_info_request_tag(void* tag) {
315317 }
316318}
317319
318- static void mb2_dump_tags (void * mbTags ) {
320+ static void mb2_dump_tags (void * mbTags , uint32_t tags_len ) {
319321 struct mb2_tag * tag = (struct mb2_tag * )mbTags ;
322+ uint8_t * end = (uint8_t * )mbTags + tags_len ;
320323
321- while (tag -> type != 0 ) {
324+ while (( uint8_t * ) tag + sizeof ( * tag ) <= end && tag -> type != 0 ) {
322325 MB2_DEBUG_PRINTF ("Tag Type: %u\r\n" , tag -> type );
323326 MB2_DEBUG_PRINTF ("Tag Flags: 0x%x\r\n" , tag -> flags );
324327 MB2_DEBUG_PRINTF ("Tag Size: %u\r\n" , tag -> size );
325328
329+ if (tag -> size < sizeof (* tag ))
330+ return ;
331+
332+ if (tag -> size > ((uint32_t )(end - (uint8_t * )tag )))
333+ return ;
334+
326335 if (tag -> type == MB2_TAG_TYPE_INFO_REQ )
327336 mb2_parse_info_request_tag (tag );
328337
338+ if (tag -> size < sizeof (* tag ))
339+ break ;
329340 tag = (struct mb2_tag * )mb2_align_address_up ((uint8_t * )tag + tag -> size ,
330341 8 );
331342 }
@@ -341,7 +352,9 @@ static void mb2_dump_header(void* mbHeader) {
341352 MB2_DEBUG_PRINTF ("Checksum: 0x%x\r\n" , header -> checksum );
342353
343354 tags = (uint8_t * )header + sizeof (* header );
344- mb2_dump_tags (tags );
355+ if (header -> header_length < sizeof (struct mb2_header ))
356+ MB2_DEBUG_PRINTF ("Invalid header length\r\n" );
357+ mb2_dump_tags (tags , header -> header_length - sizeof (* header ));
345358}
346359#endif /* DEBUG_MB2 */
347360
0 commit comments