Skip to content

Commit 5ed13a2

Browse files
rawalexearchigup
authored andcommitted
Validate PREFIX_INFORMATION option size in RA
In vReceiveRA_ReadReply(), the parser casts RA option bytes to ICMPPrefixOption_IPv6_t * without verifying the option length is at least sizeof(ICMPPrefixOption_IPv6_t). A truncated option causes an out-of-bounds read when accessing prefix fields. Validate option length before the cast.
1 parent b0ee47d commit 5ed13a2

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

source/FreeRTOS_RA.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
const size_t uxLast = pxNetworkBuffer->xDataLength - uxNeededSize;
277277
uint8_t * pucBytes = &( pxNetworkBuffer->pucEthernetBuffer[ uxNeededSize ] );
278278
ICMPPrefixOption_IPv6_t * pxPrefixOption = NULL;
279+
BaseType_t xMalformed = pdFALSE;
279280

280281
while( ( uxIndex + 1U ) < uxLast )
281282
{
@@ -312,16 +313,29 @@
312313
break;
313314

314315
case ndICMP_PREFIX_INFORMATION: /* 3 */
315-
/* MISRA Ref 11.3.1 [Misaligned access] */
316-
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
317-
/* coverity[misra_c_2012_rule_11_3_violation] */
318-
pxPrefixOption = ( ( ICMPPrefixOption_IPv6_t * ) &( pucBytes[ uxIndex ] ) );
319-
320-
FreeRTOS_printf( ( "RA: Prefix len %d Life %u, %u (%pip)\n",
321-
pxPrefixOption->ucPrefixLength,
322-
( unsigned ) FreeRTOS_ntohl( pxPrefixOption->ulValidLifeTime ),
323-
( unsigned ) FreeRTOS_ntohl( pxPrefixOption->ulPreferredLifeTime ),
324-
( void * ) pxPrefixOption->ucPrefix ) );
316+
317+
if( uxLength < sizeof( ICMPPrefixOption_IPv6_t ) )
318+
{
319+
FreeRTOS_printf(
320+
( "RA: Prefix option too short ( %u < %u )\n",
321+
( unsigned ) uxLength,
322+
( unsigned ) sizeof( ICMPPrefixOption_IPv6_t ) ) );
323+
xMalformed = pdTRUE;
324+
}
325+
else
326+
{
327+
/* MISRA Ref 11.3.1 [Misaligned access] */
328+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
329+
/* coverity[misra_c_2012_rule_11_3_violation] */
330+
pxPrefixOption = ( ( ICMPPrefixOption_IPv6_t * ) &( pucBytes[ uxIndex ] ) );
331+
332+
FreeRTOS_printf( ( "RA: Prefix len %d Life %u, %u (%pip)\n",
333+
pxPrefixOption->ucPrefixLength,
334+
( unsigned ) FreeRTOS_ntohl( pxPrefixOption->ulValidLifeTime ),
335+
( unsigned ) FreeRTOS_ntohl( pxPrefixOption->ulPreferredLifeTime ),
336+
( void * ) pxPrefixOption->ucPrefix ) );
337+
}
338+
325339
break;
326340

327341
case ndICMP_REDIRECTED_HEADER: /* 4 */
@@ -345,6 +359,12 @@
345359
break;
346360
}
347361

362+
if( xMalformed != pdFALSE )
363+
{
364+
FreeRTOS_printf( ( "RA: Malformed packet.\n" ) );
365+
break;
366+
}
367+
348368
uxIndex = uxIndex + uxLength;
349369
} /* while( ( uxIndex + 1 ) < uxLast ) */
350370

0 commit comments

Comments
 (0)