@@ -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 */
482482void 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 */
517526static 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 */
11421169void 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