Skip to content

Commit ceac135

Browse files
committed
Further PR refinements per comments
1 parent dcbdfb5 commit ceac135

3 files changed

Lines changed: 157 additions & 55 deletions

File tree

‎History.txt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ Documentation and download available at https://www.FreeRTOS.org/
22

33
Changes to be included in the upcoming release:
44
+ Add Router Advertisement validations for RFC 4861 section 6.1.2
5-
conformance: the IP source address must be link-local, the IP hop
6-
limit must be 255, and the ICMP code must be 0 (#1369). We thank
7-
Causal Security (https://causalsecurity.com) for their help
8-
identifying this issue, and @htibosch for their collaboration.
5+
(#1369). A malformed option now discards the whole message, so an RA
6+
that previously drove SLAAC may now be rejected. Thanks to Causal
7+
Security (https://causalsecurity.com) and @htibosch for their help.
98
+ Rename the ICMP header field ucTypeOfService to ucCode in
109
ICMPHeader_t, ICMPHeader_IPv6_t, ICMPEcho_IPv6_t,
1110
ICMPRouterSolicitation_IPv6_t and ICMPRouterAdvertisement_IPv6_t

‎source/FreeRTOS_RA.c‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@
270270
* options are bounded by this value rather than by the
271271
* length of the received Ethernet frame.
272272
*
273+
* @note vReceiveRA() has already established that uxPayloadLength is at least
274+
* sizeof( ICMPRouterAdvertisement_IPv6_t ) and that the message it describes
275+
* fits inside the received frame. Both properties are relied upon here: the
276+
* subtraction below cannot underflow, and the resulting bound cannot reach
277+
* past the end of the Ethernet buffer.
278+
*
273279
* @returns Returns the ICMP prefix option pointer, pointing to its location in the
274280
* input RA reply message buffer, or NULL when no prefix option was found
275281
* or when any option in the message was malformed.
@@ -280,31 +286,12 @@
280286
size_t uxIndex = 0U;
281287
const size_t uxICMPSize = sizeof( ICMPRouterAdvertisement_IPv6_t );
282288
const size_t uxNeededSize = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER + uxICMPSize;
283-
size_t uxLast = 0U;
289+
/* uxLast is the number of option bytes that the ICMP length accounts for. */
290+
const size_t uxLast = uxPayloadLength - uxICMPSize;
284291
uint8_t * pucBytes = &( pxNetworkBuffer->pucEthernetBuffer[ uxNeededSize ] );
285292
ICMPPrefixOption_IPv6_t * pxPrefixOption = NULL;
286293
BaseType_t xMalformed = pdFALSE;
287294

288-
if( pxNetworkBuffer->xDataLength > uxNeededSize )
289-
{
290-
const size_t uxReceived = pxNetworkBuffer->xDataLength - uxNeededSize;
291-
292-
if( uxPayloadLength > uxICMPSize )
293-
{
294-
uxLast = uxPayloadLength - uxICMPSize;
295-
}
296-
297-
if( uxLast > uxReceived )
298-
{
299-
/* usPayloadLength claims more data than was actually received. Never
300-
* read beyond the buffer. */
301-
FreeRTOS_printf( ( "RA: Payload length ( %u ) exceeds the buffer ( %u )\n",
302-
( unsigned ) uxLast,
303-
( unsigned ) uxReceived ) );
304-
uxLast = uxReceived;
305-
}
306-
}
307-
308295
while( ( uxIndex + 1U ) < uxLast )
309296
{
310297
uint8_t ucType = pucBytes[ uxIndex ];
@@ -424,7 +411,7 @@
424411
const ICMPPacket_IPv6_t * pxICMPPacket = ( ( const ICMPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer );
425412
const ICMPPrefixOption_IPv6_t * pxPrefixOption = NULL;
426413
const size_t uxICMPSize = sizeof( ICMPRouterAdvertisement_IPv6_t );
427-
const size_t uxNeededSize = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER + uxICMPSize;
414+
const size_t uxIPHeaderBytes = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER;
428415

429416
/* MISRA Ref 11.3.1 [Misaligned access] */
430417
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
@@ -434,10 +421,19 @@
434421
/* Number of bytes following the IPv6 header. The ICMP length is derived from the IP length. */
435422
const size_t uxPayloadLength = ( size_t ) FreeRTOS_ntohs( pxICMPPacket->xIPHeader.usPayloadLength );
436423

437-
/* A Router Advertisement was received, handle it here. */
438-
if( uxNeededSize > pxNetworkBuffer->xDataLength )
424+
/* A Router Advertisement was received, handle it here.
425+
*
426+
* The IPv6 ingress checks guarantee that at least the Ethernet and IPv6
427+
* headers were received, which is what makes reading usPayloadLength above
428+
* safe. Nothing past those headers is read until the two length checks below
429+
* have both passed; together they establish that the ICMP message described
430+
* by the IP header was received in full, and therefore that xDataLength is at
431+
* least ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER + uxICMPSize. */
432+
if( ( uxPayloadLength + uxIPHeaderBytes ) > pxNetworkBuffer->xDataLength )
439433
{
440-
FreeRTOS_printf( ( "vReceiveRA: The buffer provided is too small\n" ) );
434+
FreeRTOS_printf( ( "vReceiveRA: ICMP length %u exceeds the %u bytes received\n",
435+
( unsigned ) uxPayloadLength,
436+
( unsigned ) pxNetworkBuffer->xDataLength ) );
441437
}
442438
/* First verify it's validity. */
443439
else if( uxPayloadLength < uxICMPSize )

‎test/unit-test/FreeRTOS_RA/FreeRTOS_RA_utest.c‎

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ void test_vReceiveNA_bIPAddressInUse( void )
476476
}
477477

478478
/**
479-
* @brief This function verify the handling
480-
* of incorrect data length.
479+
* @brief This function verify the handling of incorrect data length: a frame too
480+
* short to hold the Router Advertisement header must be discarded.
481481
*/
482482
void test_vReceiveRA_IncorrectDataLength( void )
483483
{
@@ -494,7 +494,12 @@ void test_vReceiveRA_IncorrectDataLength( void )
494494

495495
xICMPPacket.xIPHeader.ucHopLimit = raDEFAULT_HOP_LIMIT;
496496

497-
prvSetIPv6PayloadLength( pxNetworkBuffer );
497+
/* Claim a payload that is far larger than the frame, so that the length check
498+
* is the only thing that can reject this message: every later check would pass.
499+
* Deriving usPayloadLength from xDataLength instead would make the 16 octet
500+
* minimum reject the packet first, and this test would then pass even if the
501+
* check under test were removed. */
502+
xICMPPacket.xIPHeader.usPayloadLength = FreeRTOS_htons( 0xFFFFU );
498503

499504
vReceiveRA( pxNetworkBuffer );
500505
}
@@ -508,11 +513,15 @@ void test_vReceiveRA_IncorrectDataLength( void )
508513
* accept path would bail out of vReceiveRA_ReadReply() with a NULL prefix and never
509514
* reach FreeRTOS_FirstEndPoint() either, so a broken check would go unnoticed.
510515
*
511-
* Because a valid prefix is present, reaching the end-point iteration is the
512-
* observable difference between accepting and discarding the message. The discard
513-
* tests deliberately register no FreeRTOS_FirstEndPoint() expectation, so if the
514-
* check under test were removed, CMock would fail the test on the unexpected call.
515-
* See test_vReceiveRA_vRAProcess() for the accepting counterpart of this fixture.
516+
* Each discard test registers only the mock calls that precede the check it covers,
517+
* so removing that check makes the next unmocked call fail the test. For the length,
518+
* hop limit and ICMP code tests that call is xIPv6_GetIPType(); for the source
519+
* address test the message is otherwise acceptable, so it is FreeRTOS_FirstEndPoint().
520+
*
521+
* test_vReceiveRA_vRAProcess() builds its packet with this same fixture and asserts
522+
* that the end-point is updated, which is what keeps the fixture honest: if a change
523+
* here made the packet unacceptable, that test would fail rather than silently
524+
* disarming every discard test below.
516525
*/
517526
static void prvBuildAcceptableRA( NetworkBufferDescriptor_t * pxNetworkBuffer,
518527
EthernetPacketICMPv6RouterAdvertisementPrefixOption_t * pxICMPPacket,
@@ -531,6 +540,18 @@ static void prvBuildAcceptableRA( NetworkBufferDescriptor_t * pxNetworkBuffer,
531540
pxICMPPacket->xAdvertisement.usLifetime = pdTRUE_UNSIGNED;
532541
pxICMPPacket->xIPHeader.ucHopLimit = raDEFAULT_HOP_LIMIT;
533542

543+
/* The source and destination addresses must differ, and must not be all zeroes.
544+
* CMock compares the pointed-to bytes rather than the pointer, so an
545+
* xIPv6_GetIPType_ExpectAndReturn() on the source address only pins down which
546+
* of the two the code classifies when their contents are distinguishable.
547+
* fe80::1 as the source, ff02::1 as the destination. */
548+
pxICMPPacket->xIPHeader.xSourceAddress.ucBytes[ 0 ] = 0xFEU;
549+
pxICMPPacket->xIPHeader.xSourceAddress.ucBytes[ 1 ] = 0x80U;
550+
pxICMPPacket->xIPHeader.xSourceAddress.ucBytes[ 15 ] = 0x01U;
551+
pxICMPPacket->xIPHeader.xDestinationAddress.ucBytes[ 0 ] = 0xFFU;
552+
pxICMPPacket->xIPHeader.xDestinationAddress.ucBytes[ 1 ] = 0x02U;
553+
pxICMPPacket->xIPHeader.xDestinationAddress.ucBytes[ 15 ] = 0x01U;
554+
534555
pxPrefixOption = &( pxICMPPacket->xPrefixOption );
535556
pxPrefixOption->ucType = ndICMP_PREFIX_INFORMATION;
536557
pxPrefixOption->ucLength = 4;
@@ -1138,6 +1159,12 @@ void test_vReceiveRA_vRAProccess( void )
11381159

11391160
/**
11401161
* @brief This function verify vReceiveRA success case.
1162+
*
1163+
* This is the accepting counterpart of prvBuildAcceptableRA(): it drives the exact
1164+
* packet that fixture produces all the way through to the end-point update. That
1165+
* makes it the guard for every discard test built on the same fixture - if a change
1166+
* to the fixture stopped the packet from being accepted, this test fails instead of
1167+
* those tests quietly ceasing to exercise their checks.
11411168
*/
11421169
void test_vReceiveRA_vRAProcess( void )
11431170
{
@@ -1146,41 +1173,25 @@ void test_vReceiveRA_vRAProcess( void )
11461173
NetworkInterface_t xInterface;
11471174
NetworkEndPoint_t xEndPoint, * pxEndPoint = &xEndPoint;
11481175
ICMPPrefixOption_IPv6_t * pxPrefixOption;
1149-
ICMPRouterAdvertisement_IPv6_t * pxAdvertisement;
11501176

1151-
memset( &xNetworkBuffer, 0, sizeof( NetworkBufferDescriptor_t ) );
1152-
memset( &xICMPPacket, 0, sizeof( xICMPPacket ) );
1153-
memset( &xInterface, 0, sizeof( NetworkInterface_t ) );
11541177
memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
11551178

11561179
pxNetworkBuffer = &xNetworkBuffer;
1157-
pxNetworkBuffer->pucEthernetBuffer = ( uint8_t * ) &xICMPPacket;
1158-
pxNetworkBuffer->pxInterface = &xInterface;
1159-
pxNetworkBuffer->xDataLength = raHeaderBytesRA + sizeof( ICMPPrefixOption_IPv6_t );
1160-
pxAdvertisement = &xICMPPacket.xAdvertisement;
1161-
pxAdvertisement->usLifetime = pdTRUE_UNSIGNED;
1180+
prvBuildAcceptableRA( pxNetworkBuffer, &xICMPPacket, &xInterface );
11621181

11631182
pxPrefixOption = &xICMPPacket.xPrefixOption;
1164-
pxPrefixOption->ucType = ndICMP_PREFIX_INFORMATION;
1165-
pxPrefixOption->ucLength = 4;
1166-
pxPrefixOption->ucPrefixLength = 64;
1167-
11681183

11691184
pxEndPoint->bits.bWantRA = pdTRUE_UNSIGNED;
11701185
pxEndPoint->xRAData.eRAState = eRAStateWait;
11711186

1172-
xICMPPacket.xIPHeader.ucHopLimit = raDEFAULT_HOP_LIMIT;
1173-
1174-
xIPv6_GetIPType_ExpectAnyArgsAndReturn( eIPv6_LinkLocal );
1187+
xIPv6_GetIPType_ExpectAndReturn( &( xICMPPacket.xIPHeader.xSourceAddress ), eIPv6_LinkLocal );
11751188

11761189
FreeRTOS_FirstEndPoint_ExpectAnyArgsAndReturn( pxEndPoint );
11771190
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );
11781191

11791192
pxGetNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( NULL );
11801193
vDHCP_RATimerReload_ExpectAnyArgs();
11811194

1182-
prvSetIPv6PayloadLength( pxNetworkBuffer );
1183-
11841195
vReceiveRA( pxNetworkBuffer );
11851196

11861197

@@ -1226,6 +1237,15 @@ void test_vReceiveRA_PrefixFollowedByZeroLengthOption( void )
12261237
xFrame.xPacket.xAdvertisement.usLifetime = pdTRUE_UNSIGNED;
12271238
xFrame.xPacket.xIPHeader.ucHopLimit = raDEFAULT_HOP_LIMIT;
12281239

1240+
/* Distinguishable source and destination addresses, so that the expectation
1241+
* below pins down which of the two is classified. See prvBuildAcceptableRA(). */
1242+
xFrame.xPacket.xIPHeader.xSourceAddress.ucBytes[ 0 ] = 0xFEU;
1243+
xFrame.xPacket.xIPHeader.xSourceAddress.ucBytes[ 1 ] = 0x80U;
1244+
xFrame.xPacket.xIPHeader.xSourceAddress.ucBytes[ 15 ] = 0x01U;
1245+
xFrame.xPacket.xIPHeader.xDestinationAddress.ucBytes[ 0 ] = 0xFFU;
1246+
xFrame.xPacket.xIPHeader.xDestinationAddress.ucBytes[ 1 ] = 0x02U;
1247+
xFrame.xPacket.xIPHeader.xDestinationAddress.ucBytes[ 15 ] = 0x01U;
1248+
12291249
/* A perfectly valid prefix option ... */
12301250
pxPrefixOption = &xFrame.xPacket.xPrefixOption;
12311251
pxPrefixOption->ucType = ndICMP_PREFIX_INFORMATION;
@@ -1245,6 +1265,68 @@ void test_vReceiveRA_PrefixFollowedByZeroLengthOption( void )
12451265
vReceiveRA( pxNetworkBuffer );
12461266
}
12471267

1268+
/**
1269+
* @brief A Router Advertisement carrying a valid Prefix Information option
1270+
* followed by an option that runs past the end of the message must be
1271+
* discarded in its entirety.
1272+
*
1273+
* This is the overrun counterpart of test_vReceiveRA_PrefixFollowedByZeroLengthOption().
1274+
* The trailing option declares a length of 2 - sixteen octets - while only eight
1275+
* octets of the message remain. Returning the prefix that parsed before it would let
1276+
* an attacker drive SLAAC with a truncated RA, so the end-point must be left
1277+
* untouched.
1278+
*/
1279+
void test_vReceiveRA_PrefixFollowedByOverrunOption( void )
1280+
{
1281+
NetworkBufferDescriptor_t * pxNetworkBuffer, xNetworkBuffer;
1282+
1283+
struct
1284+
{
1285+
EthernetPacketICMPv6RouterAdvertisementPrefixOption_t xPacket;
1286+
uint8_t ucTrailingOption[ 8 ];
1287+
}
1288+
xFrame;
1289+
NetworkInterface_t xInterface;
1290+
ICMPPrefixOption_IPv6_t * pxPrefixOption;
1291+
1292+
memset( &xNetworkBuffer, 0, sizeof( NetworkBufferDescriptor_t ) );
1293+
memset( &xFrame, 0, sizeof( xFrame ) );
1294+
memset( &xInterface, 0, sizeof( NetworkInterface_t ) );
1295+
1296+
pxNetworkBuffer = &xNetworkBuffer;
1297+
pxNetworkBuffer->pucEthernetBuffer = ( uint8_t * ) &xFrame;
1298+
pxNetworkBuffer->pxInterface = &xInterface;
1299+
pxNetworkBuffer->xDataLength = sizeof( xFrame );
1300+
1301+
xFrame.xPacket.xAdvertisement.usLifetime = pdTRUE_UNSIGNED;
1302+
xFrame.xPacket.xIPHeader.ucHopLimit = raDEFAULT_HOP_LIMIT;
1303+
1304+
xFrame.xPacket.xIPHeader.xSourceAddress.ucBytes[ 0 ] = 0xFEU;
1305+
xFrame.xPacket.xIPHeader.xSourceAddress.ucBytes[ 1 ] = 0x80U;
1306+
xFrame.xPacket.xIPHeader.xSourceAddress.ucBytes[ 15 ] = 0x01U;
1307+
xFrame.xPacket.xIPHeader.xDestinationAddress.ucBytes[ 0 ] = 0xFFU;
1308+
xFrame.xPacket.xIPHeader.xDestinationAddress.ucBytes[ 1 ] = 0x02U;
1309+
xFrame.xPacket.xIPHeader.xDestinationAddress.ucBytes[ 15 ] = 0x01U;
1310+
1311+
/* A perfectly valid prefix option ... */
1312+
pxPrefixOption = &xFrame.xPacket.xPrefixOption;
1313+
pxPrefixOption->ucType = ndICMP_PREFIX_INFORMATION;
1314+
pxPrefixOption->ucLength = 4;
1315+
pxPrefixOption->ucPrefixLength = 64;
1316+
1317+
/* ... followed by an option claiming 16 octets when only 8 remain. */
1318+
xFrame.ucTrailingOption[ 0 ] = ndICMP_MTU_OPTION;
1319+
xFrame.ucTrailingOption[ 1 ] = 2U;
1320+
1321+
xIPv6_GetIPType_ExpectAndReturn( &( xFrame.xPacket.xIPHeader.xSourceAddress ), eIPv6_LinkLocal );
1322+
1323+
prvSetIPv6PayloadLength( pxNetworkBuffer );
1324+
1325+
/* No FreeRTOS_FirstEndPoint() expectation is registered: were the prefix still
1326+
* returned, the unexpected call would fail this test. */
1327+
vReceiveRA( pxNetworkBuffer );
1328+
}
1329+
12481330
/**
12491331
* @brief Options that lie beyond the IPv6 payload length must not be parsed.
12501332
*
@@ -1291,6 +1373,31 @@ void test_vReceiveRA_PayloadLengthTooSmall( void )
12911373
vReceiveRA( pxNetworkBuffer );
12921374
}
12931375

1376+
/**
1377+
* @brief An ICMP length that reaches beyond the received frame must be rejected
1378+
* rather than trimmed to fit.
1379+
*
1380+
* The IPv6 ingress checks already drop such a packet before vReceiveRA() is reached,
1381+
* so this is a backstop. It is still worth driving: trimming the bound and then
1382+
* carrying on would accept an RA whose own IP header says it was truncated, which is
1383+
* exactly the inconsistency the length check exists to reject.
1384+
*/
1385+
void test_vReceiveRA_PayloadLengthExceedsFrame( void )
1386+
{
1387+
NetworkBufferDescriptor_t * pxNetworkBuffer, xNetworkBuffer;
1388+
EthernetPacketICMPv6RouterAdvertisementPrefixOption_t xICMPPacket;
1389+
NetworkInterface_t xInterface;
1390+
1391+
pxNetworkBuffer = &xNetworkBuffer;
1392+
prvBuildAcceptableRA( pxNetworkBuffer, &xICMPPacket, &xInterface );
1393+
1394+
/* Claim one octet more of payload than the frame actually carries. */
1395+
xICMPPacket.xIPHeader.usPayloadLength =
1396+
FreeRTOS_htons( ( uint16_t ) ( ( pxNetworkBuffer->xDataLength - ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER ) ) + 1U ) );
1397+
1398+
vReceiveRA( pxNetworkBuffer );
1399+
}
1400+
12941401
/**
12951402
* @brief This function verify RA state machine
12961403
* with RA NULL endpoint.

0 commit comments

Comments
 (0)