From cf4a8fa455c63feaffdb0fbc5b7c72729ffbb17f Mon Sep 17 00:00:00 2001 From: Wang Fan Date: Wed, 3 Jan 2018 10:18:29 +0800 Subject: [PATCH] MdeModulePkg/DxeNetLib: Add parameter check and ASSERT handling. * Library API should check the input parameters before use, or ASSERT to tell it has to meet some requirements. But in DxeNetLib, not all functions follows this rule. * ASSERT shouldn't be used as error handling, add some handling code for errors. * Add some ASSERT commence in function notes. Cc: Fu Siyuan Cc: Ye Ting Cc: Jiaxin Wu Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Wang Fan Reviewed-by: Jiaxin Wu --- MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 119 ++++++++++++++++++--- 1 file changed, 105 insertions(+), 14 deletions(-) diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index 0f00f79fe4..90f17b71b5 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -1,7 +1,7 @@ /** @file Network library. -Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.
+Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
(C) Copyright 2015 Hewlett Packard Enterprise Development LP
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License @@ -198,6 +198,7 @@ SyslogLocateSnp ( fill in the source MAC because it will try to locate a SNP each time it is called to avoid the problem if SNP is unloaded. This code snip is copied from MNP. + If Packet is NULL, then ASSERT(). @param[in] Packet The Syslog packet @param[in] Length The length of the packet @@ -219,6 +220,8 @@ SyslogSendPacket ( EFI_EVENT TimeoutEvent; UINT8 *TxBuf; + ASSERT (Packet != NULL); + Snp = SyslogLocateSnp (); if (Snp == NULL) { @@ -310,7 +313,7 @@ ON_EXIT: @param[in] BufLen The lenght of the Buf @param[out] Buf The buffer to put the packet data - @return The length of the syslog packet built. + @return The length of the syslog packet built, 0 represents no packet is built. **/ UINT32 @@ -324,6 +327,7 @@ SyslogBuildPacket ( OUT CHAR8 *Buf ) { + EFI_STATUS Status; ETHER_HEAD *Ether; IP4_HEAD *Ip4; EFI_UDP_HEADER *Udp4; @@ -379,8 +383,10 @@ SyslogBuildPacket ( // Build the syslog message body with Timestamp machine module Message // Pri = ((NET_SYSLOG_FACILITY & 31) << 3) | (Level & 7); - gRT->GetTime (&Time, NULL); - ASSERT ((Time.Month <= 12) && (Time.Month >= 1)); + Status = gRT->GetTime (&Time, NULL); + if (EFI_ERROR (Status)) { + return 0; + } // // Use %a to format the ASCII strings, %s to format UNICODE strings @@ -439,6 +445,8 @@ SyslogBuildPacket ( NetDebugASPrint ("State transit to %a\n", Name) ) + If Format is NULL, then ASSERT(). + @param Format The ASCII format string. @param ... The variable length parameter whose format is determined by the Format string. @@ -457,6 +465,8 @@ NetDebugASPrint ( VA_LIST Marker; CHAR8 *Buf; + ASSERT (Format != NULL); + Buf = (CHAR8 *) AllocatePool (NET_DEBUG_MSG_LEN); if (Buf == NULL) { @@ -483,7 +493,8 @@ NetDebugASPrint ( @param Message The user message to log. @retval EFI_INVALID_PARAMETER Any input parameter is invalid. - @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet. + @retval EFI_DEVICE_ERROR Device error occurs. @retval EFI_SUCCESS The log is discard because that it is more verbose than the mNetDebugLevelMax. Or, it has been sent out. **/ @@ -504,7 +515,7 @@ NetDebugOutput ( // // Check whether the message should be sent out // - if (Message == NULL) { + if (Message == NULL || File == NULL || Module == NULL) { return EFI_INVALID_PARAMETER; } @@ -537,9 +548,13 @@ NetDebugOutput ( NET_SYSLOG_PACKET_LEN, Packet ); + if (Len == 0) { + Status = EFI_DEVICE_ERROR; + } else { + mSyslogPacketSeq++; + Status = SyslogSendPacket (Packet, Len); + } - mSyslogPacketSeq++; - Status = SyslogSendPacket (Packet, Len); FreePool (Packet); ON_EXIT: @@ -675,6 +690,8 @@ NetIp4IsUnicast ( /** Check whether the incoming IPv6 address is a valid unicast address. + ASSERT if Ip6 is NULL. + If the address is a multicast address has binary 0xFF at the start, it is not a valid unicast address. If the address is unspecified ::, it is not a valid unicast address to be assigned to any node. If the address is loopback address @@ -695,6 +712,8 @@ NetIp6IsValidUnicast ( UINT8 Byte; UINT8 Index; + ASSERT (Ip6 != NULL); + if (Ip6->Addr[0] == 0xFF) { return FALSE; } @@ -717,6 +736,8 @@ NetIp6IsValidUnicast ( /** Check whether the incoming Ipv6 address is the unspecified address or not. + ASSERT if Ip6 is NULL. + @param[in] Ip6 - Ip6 address, in network order. @retval TRUE - Yes, unspecified @@ -731,6 +752,8 @@ NetIp6IsUnspecifiedAddr ( { UINT8 Index; + ASSERT (Ip6 != NULL); + for (Index = 0; Index < 16; Index++) { if (Ip6->Addr[Index] != 0) { return FALSE; @@ -743,6 +766,8 @@ NetIp6IsUnspecifiedAddr ( /** Check whether the incoming Ipv6 address is a link-local address. + ASSERT if Ip6 is NULL. + @param[in] Ip6 - Ip6 address, in network order. @retval TRUE - Yes, link-local address @@ -779,6 +804,9 @@ NetIp6IsLinkLocalAddr ( /** Check whether the Ipv6 address1 and address2 are on the connected network. + ASSERT if Ip1 or Ip2 is NULL. + ASSERT if PrefixLength exceeds IP6_PREFIX_MAX. + @param[in] Ip1 - Ip6 address1, in network order. @param[in] Ip2 - Ip6 address2, in network order. @param[in] PrefixLength - The prefix length of the checking net. @@ -815,7 +843,6 @@ NetIp6IsNetEqual ( if (Bit > 0) { Mask = (UINT8) (0xFF << (8 - Bit)); - ASSERT (Byte < 16); if ((Ip1->Addr[Byte] & Mask) != (Ip2->Addr[Byte] & Mask)) { return FALSE; } @@ -828,6 +855,8 @@ NetIp6IsNetEqual ( /** Switches the endianess of an IPv6 address + ASSERT if Ip6 is NULL. + This function swaps the bytes in a 128-bit IPv6 address to switch the value from little endian to big endian or vice versa. The byte swapped value is returned. @@ -846,6 +875,8 @@ Ip6Swap128 ( UINT64 High; UINT64 Low; + ASSERT (Ip6 != NULL); + CopyMem (&High, Ip6, sizeof (UINT64)); CopyMem (&Low, &Ip6->Addr[8], sizeof (UINT64)); @@ -893,6 +924,8 @@ NetRandomInitSeed ( /** Extract a UINT32 from a byte stream. + ASSERT if Buf is NULL. + Copy a UINT32 from a byte stream, then converts it from Network byte order to host byte order. Use this function to avoid alignment error. @@ -909,6 +942,8 @@ NetGetUint32 ( { UINT32 Value; + ASSERT (Buf != NULL); + CopyMem (&Value, Buf, sizeof (UINT32)); return NTOHL (Value); } @@ -917,6 +952,8 @@ NetGetUint32 ( /** Put a UINT32 to the byte stream in network byte order. + ASSERT if Buf is NULL. + Converts a UINT32 from host byte order to network byte order. Then copy it to the byte stream. @@ -931,6 +968,8 @@ NetPutUint32 ( IN UINT32 Data ) { + ASSERT (Buf != NULL); + Data = HTONL (Data); CopyMem (Buf, &Data, sizeof (UINT32)); } @@ -1029,6 +1068,8 @@ NetListRemoveTail ( /** Insert a new node entry after a designated node entry of a doubly linked list. + ASSERT if PrevEntry or NewEntry is NULL. + Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry of the doubly linked list. @@ -1043,6 +1084,8 @@ NetListInsertAfter ( IN OUT LIST_ENTRY *NewEntry ) { + ASSERT (PrevEntry != NULL && NewEntry != NULL); + NewEntry->BackLink = PrevEntry; NewEntry->ForwardLink = PrevEntry->ForwardLink; PrevEntry->ForwardLink->BackLink = NewEntry; @@ -1053,6 +1096,8 @@ NetListInsertAfter ( /** Insert a new node entry before a designated node entry of a doubly linked list. + ASSERT if PostEntry or NewEntry is NULL. + Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry of the doubly linked list. @@ -1067,6 +1112,8 @@ NetListInsertBefore ( IN OUT LIST_ENTRY *NewEntry ) { + ASSERT (PostEntry != NULL && NewEntry != NULL); + NewEntry->ForwardLink = PostEntry; NewEntry->BackLink = PostEntry->BackLink; PostEntry->BackLink->ForwardLink = NewEntry; @@ -1265,7 +1312,6 @@ NetMapClean ( If Map is NULL, then ASSERT(). - @param[in] Map The net map to test. @return TRUE if the netmap is empty, otherwise FALSE. @@ -1285,6 +1331,8 @@ NetMapIsEmpty ( /** Return the number of the pairs in the netmap. + If Map is NULL, then ASSERT(). + @param[in] Map The netmap to get the entry number. @return The entry number in the netmap. @@ -1296,6 +1344,7 @@ NetMapGetCount ( IN NET_MAP *Map ) { + ASSERT (Map != NULL); return Map->Count; } @@ -1360,6 +1409,7 @@ NetMapAllocItem ( pairs in the netmap increase by 1. If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). @param[in, out] Map The netmap to insert into. @param[in] Key The user's key. @@ -1379,7 +1429,7 @@ NetMapInsertHead ( { NET_MAP_ITEM *Item; - ASSERT (Map != NULL); + ASSERT (Map != NULL && Key != NULL); Item = NetMapAllocItem (Map); @@ -1404,6 +1454,7 @@ NetMapInsertHead ( pairs in the netmap increase by 1. If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). @param[in, out] Map The netmap to insert into. @param[in] Key The user's key. @@ -1423,7 +1474,7 @@ NetMapInsertTail ( { NET_MAP_ITEM *Item; - ASSERT (Map != NULL); + ASSERT (Map != NULL && Key != NULL); Item = NetMapAllocItem (Map); @@ -1444,6 +1495,9 @@ NetMapInsertTail ( /** Check whether the item is in the Map and return TRUE if it is. + If Map is NULL, then ASSERT(). + If Item is NULL, then ASSERT(). + @param[in] Map The netmap to search within. @param[in] Item The item to search. @@ -1458,6 +1512,8 @@ NetItemInMap ( { LIST_ENTRY *ListEntry; + ASSERT (Map != NULL && Item != NULL); + NET_LIST_FOR_EACH (ListEntry, &Map->Used) { if (ListEntry == &Item->Link) { return TRUE; @@ -1475,6 +1531,7 @@ NetItemInMap ( item with the key to search. It returns the point to the item contains the Key if found. If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). @param[in] Map The netmap to search within. @param[in] Key The key to search. @@ -1492,7 +1549,7 @@ NetMapFindKey ( LIST_ENTRY *Entry; NET_MAP_ITEM *Item; - ASSERT (Map != NULL); + ASSERT (Map != NULL && Key != NULL); NET_LIST_FOR_EACH (Entry, &Map->Used) { Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); @@ -2095,6 +2152,9 @@ NetLibGetVlanHandle ( /** Get MAC address associated with the network service handle. + If MacAddress is NULL, then ASSERT(). + If AddressSize is NULL, then ASSERT(). + There should be MNP Service Binding Protocol installed on the input ServiceHandle. If SNP is installed on the ServiceHandle or its parent handle, MAC address will be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP. @@ -2199,6 +2259,8 @@ NetLibGetMacAddress ( Convert MAC address of the NIC associated with specified Service Binding Handle to a unicode string. Callers are responsible for freeing the string storage. + If MacString is NULL, then ASSERT(). + Locate simple network protocol associated with the Service Binding Handle and get the mac address from SNP. Then convert the mac address into a unicode string. It takes 2 unicode characters to represent a 1 byte binary buffer. @@ -2298,6 +2360,8 @@ NetLibGetMacString ( /** Detect media status for specified network device. + If MediaPresent is NULL, then ASSERT(). + The underlying UNDI driver may or may not support reporting media status from GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine will try to invoke Snp->GetStatus() to get the media status: if media already @@ -2406,6 +2470,10 @@ NetLibDetectMedia ( Snp->Mode->MCastFilter ); ASSERT (MCastFilter != NULL); + if (MCastFilter == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } ResetMCastFilters = FALSE; } @@ -2737,6 +2805,8 @@ ON_EXIT: /** Create an IPv4 device path node. + If Node is NULL, then ASSERT(). + The header type of IPv4 device path node is MESSAGING_DEVICE_PATH. The header subtype of IPv4 device path node is MSG_IPv4_DP. Get other info from parameters to make up the whole IPv4 device path node. @@ -2764,6 +2834,8 @@ NetLibCreateIPv4DPathNode ( IN BOOLEAN UseDefaultAddress ) { + ASSERT (Node != NULL); + Node->Header.Type = MESSAGING_DEVICE_PATH; Node->Header.SubType = MSG_IPv4_DP; SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH)); @@ -2794,6 +2866,10 @@ NetLibCreateIPv4DPathNode ( /** Create an IPv6 device path node. + If Node is NULL, then ASSERT(). + If LocalIp is NULL, then ASSERT(). + If RemoteIp is NULL, then ASSERT(). + The header type of IPv6 device path node is MESSAGING_DEVICE_PATH. The header subtype of IPv6 device path node is MSG_IPv6_DP. Get other info from parameters to make up the whole IPv6 device path node. @@ -2819,6 +2895,8 @@ NetLibCreateIPv6DPathNode ( IN UINT16 Protocol ) { + ASSERT (Node != NULL && LocalIp != NULL && RemoteIp != NULL); + Node->Header.Type = MESSAGING_DEVICE_PATH; Node->Header.SubType = MSG_IPv6_DP; SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH)); @@ -2843,6 +2921,8 @@ NetLibCreateIPv6DPathNode ( /** Find the UNDI/SNP handle from controller and protocol GUID. + If ProtocolGuid is NULL, then ASSERT(). + For example, IP will open a MNP child to transmit/receive packets, when MNP is stopped, IP should also be stopped. IP needs to find its own private data which is related the IP's @@ -2870,6 +2950,8 @@ NetLibGetNicHandle ( UINTN OpenCount; UINTN Index; + ASSERT (ProtocolGuid != NULL); + Status = gBS->OpenProtocolInformation ( Controller, ProtocolGuid, @@ -3151,6 +3233,8 @@ NetLibIp6ToStr ( /** This function obtains the system guid from the smbios table. + If SystemGuid is NULL, then ASSERT(). + @param[out] SystemGuid The pointer of the returned system guid. @retval EFI_SUCCESS Successfully obtained the system guid. @@ -3170,6 +3254,8 @@ NetLibGetSystemGuid ( SMBIOS_STRUCTURE_POINTER SmbiosEnd; CHAR8 *String; + ASSERT (SystemGuid != NULL); + SmbiosTable = NULL; Status = EfiGetSystemConfigurationTable (&gEfiSmbios3TableGuid, (VOID **) &Smbios30Table); if (!(EFI_ERROR (Status) || Smbios30Table == NULL)) { @@ -3236,7 +3322,10 @@ NetLibGetSystemGuid ( } /** - Create Dns QName according the queried domain name. + Create Dns QName according the queried domain name. + + If DomainName is NULL, then ASSERT(). + QName is a domain name represented as a sequence of labels, where each label consists of a length octet followed by that number of octets. The QName terminates with the zero @@ -3262,6 +3351,8 @@ NetLibCreateDnsQName ( UINTN Len; UINTN Index; + ASSERT (DomainName != NULL); + QueryName = NULL; QueryNameSize = 0; Header = NULL; -- 2.39.2