public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Mike Maslenkin" <mike.maslenkin@gmail.com>
To: devel@edk2.groups.io
Cc: richardho@ami.com, rebecca@bsdio.com,
	Mike Maslenkin <mike.maslenkin@gmail.com>
Subject: [edk2-devel] [PATCH 1/2] MdeModulePkg: UsbNetwork: fix Ethernet functional descriptor processing
Date: Sat, 26 Aug 2023 04:57:59 +0300	[thread overview]
Message-ID: <20230826015800.74524-2-mike.maslenkin@gmail.com> (raw)
In-Reply-To: <20230826015800.74524-1-mike.maslenkin@gmail.com>

This patch fixes wrong condition because of UINT16 value to integer
promotion. NumberMcFilters is UINT16 value, so when bitwise shift operator
applied to small integer type, the operation is preceded by integral
promotion. This is described in MISRA-C:2004 guideline as Rule 10.5:
"If the bitwise operators ~ and << are applied to an operand of underlying
type unsigned char or unsigned short, the result shall be immediately cast
to the underlying type of the operand."

A simple fix for this issue would be the following:
  if ((UINT16)(UsbEthFunDescriptor.NumberMcFilters << 1) == 0)

But this patch proposes to use bitwise AND operation with a proper bit mask
rather than shifting to prevent similar mistakes in future.

Cc: Richard Ho <richardho@ami.com>
Cc: Rebecca Cran <rebecca@bsdio.com>
Signed-off-by: Mike Maslenkin <mike.maslenkin@gmail.com>
---
 MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c | 2 +-
 MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcEcm/UsbEcmFunction.c  | 2 +-
 MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcNcm/UsbNcmFunction.c  | 2 +-
 MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndisFunction.c | 4 ++--
 MdeModulePkg/Include/Protocol/UsbEthernetProtocol.h         | 2 ++
 5 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c b/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c
index daa30f081500..62df4e92ea01 100644
--- a/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c
+++ b/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c
@@ -829,7 +829,7 @@ SetFilter (
     }
 
     Nic->UsbEth->UsbEthFunDescriptor (Nic->UsbEth, &UsbEthFunDescriptor);
-    if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
+    if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
       Nic->RxFilter |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
       Nic->UsbEth->SetUsbEthPacketFilter (Nic->UsbEth, Nic->RxFilter);
     } else {
diff --git a/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcEcm/UsbEcmFunction.c b/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcEcm/UsbEcmFunction.c
index 63003e07ff52..29f4508a38ce 100644
--- a/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcEcm/UsbEcmFunction.c
+++ b/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcEcm/UsbEcmFunction.c
@@ -628,7 +628,7 @@ SetUsbEthMcastFilter (
     return Status;
   }
 
-  if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
+  if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
     return EFI_UNSUPPORTED;
   }
 
diff --git a/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcNcm/UsbNcmFunction.c b/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcNcm/UsbNcmFunction.c
index 2a2454f466f7..baa2225bf8a8 100644
--- a/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcNcm/UsbNcmFunction.c
+++ b/MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcNcm/UsbNcmFunction.c
@@ -714,7 +714,7 @@ SetUsbEthMcastFilter (
     return Status;
   }
 
-  if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
+  if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
     return EFI_UNSUPPORTED;
   }
 
diff --git a/MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndisFunction.c b/MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndisFunction.c
index b3632233add1..2c0dcae4cf96 100644
--- a/MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndisFunction.c
+++ b/MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndisFunction.c
@@ -661,7 +661,7 @@ SetUsbRndisMcastFilter (
     return Status;
   }
 
-  if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
+  if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
     return EFI_UNSUPPORTED;
   }
 
@@ -856,7 +856,7 @@ RndisUndiReceiveFilter (
     }
 
     Nic->UsbEth->UsbEthFunDescriptor (Nic->UsbEth, &UsbEthFunDescriptor);
-    if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
+    if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
       Nic->RxFilter |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
       DEBUG ((DEBUG_INFO, "SetUsbEthPacketFilter Nic %lx Nic->UsbEth %lx ", Nic, Nic->UsbEth));
       Nic->UsbEth->SetUsbEthPacketFilter (Nic->UsbEth, Nic->RxFilter);
diff --git a/MdeModulePkg/Include/Protocol/UsbEthernetProtocol.h b/MdeModulePkg/Include/Protocol/UsbEthernetProtocol.h
index 800945d4b397..4cc2cee1167d 100644
--- a/MdeModulePkg/Include/Protocol/UsbEthernetProtocol.h
+++ b/MdeModulePkg/Include/Protocol/UsbEthernetProtocol.h
@@ -42,6 +42,8 @@ typedef struct _EDKII_USB_ETHERNET_PROTOCOL EDKII_USB_ETHERNET_PROTOCOL;
 #define NETWORK_CONNECTED   0x01
 #define NETWORK_DISCONNECT  0x00
 
+#define MAC_FILTERS_MASK  0x7FFF
+
 // USB Header functional Descriptor
 typedef struct {
   UINT8     FunctionLength;
-- 
2.32.0 (Apple Git-132)



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108040): https://edk2.groups.io/g/devel/message/108040
Mute This Topic: https://groups.io/mt/100968487/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  reply	other threads:[~2023-08-26  1:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-26  1:57 [edk2-devel] [PATCH 0/2] MdeModulePkg: small improvements to UsbNetwork Mike Maslenkin
2023-08-26  1:57 ` Mike Maslenkin [this message]
2023-08-26  1:58 ` [edk2-devel] [PATCH 2/2] MdeModulePkg: UsbRndis: get rid of magic values Mike Maslenkin
2023-08-30  2:05 ` [edk2-devel] [PATCH 0/2] MdeModulePkg: small improvements to UsbNetwork RichardHo [何明忠] via groups.io
2023-08-31 12:32   ` Rebecca Cran
2023-10-08 10:20     ` Mike Maslenkin
2023-10-16 21:34       ` Rebecca Cran
2023-10-16 22:52 ` Rebecca Cran

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230826015800.74524-2-mike.maslenkin@gmail.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox