public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zeng, Star" <star.zeng@intel.com>
To: Ruiyu Ni <ruiyu.ni@intel.com>, edk2-devel@lists.01.org
Cc: Jiewen Yao <jiewen.yao@intel.com>, star.zeng@intel.com
Subject: Re: [PATCH 03/11] MdeModulePkg/UsbBus: Fix out-of-bound read access to descriptors
Date: Tue, 16 Oct 2018 11:38:42 +0800	[thread overview]
Message-ID: <a9589b65-07a2-dea5-adc5-f847218375c0@intel.com> (raw)
In-Reply-To: <20181015063833.61304-4-ruiyu.ni@intel.com>

On 2018/10/15 14:38, Ruiyu Ni wrote:
> Today's implementation reads the Type/Length field in the USB
> descriptors data without checking whether the offset to read is
> beyond the data boundary.
> 
> The patch fixes this issue.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> Cc: Star Zeng <star.zeng@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>

Ray,

Thanks for the patch.
I have two minor comments.
You can take them as you wish as I have no strong opinion for them. :)
Reviewed-by: Star Zeng <star.zeng@intel.com>

> ---
>   MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c | 50 ++++++++++++++++++++++++--------
>   1 file changed, 38 insertions(+), 12 deletions(-)
> 
> diff --git a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c
> index 061e4622e8..a93060deea 100644
> --- a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c
> +++ b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c
> @@ -177,6 +177,17 @@ UsbCreateDesc (
>       DescLen = sizeof (EFI_USB_ENDPOINT_DESCRIPTOR);
>       CtrlLen = sizeof (USB_ENDPOINT_DESC);
>       break;
> +
> +  default:
> +    ASSERT (FALSE);
> +    return NULL;
> +  }
> +
> +  //
> +  // Total length is too small that cannot hold the single descriptor header plus data.
> +  //
> +  if (Len <= sizeof (USB_DESC_HEAD)) {

Add debug message here like others below for error cases?

> +    return NULL;
>     }
>   
>     //
> @@ -184,24 +195,39 @@ UsbCreateDesc (
>     // format. Skip the descriptor that isn't of this Type
>     //
>     Offset = 0;
> -  Head   = (USB_DESC_HEAD*)DescBuf;
> +  Head   = (USB_DESC_HEAD *)DescBuf;
> +  while (Offset < Len - sizeof (USB_DESC_HEAD)) {
> +    //
> +    // Above condition make sure Head->Len and Head->Type are safe to access
> +    //
> +    Head = (USB_DESC_HEAD *)&DescBuf[Offset];
>   
> -  while ((Offset < Len) && (Head->Type != Type)) {
> -    Offset += Head->Len;
> -    if (Len <= Offset) {
> -      DEBUG (( EFI_D_ERROR, "UsbCreateDesc: met mal-format descriptor, Beyond boundary!\n"));
> +    if (Head->Len == 0) {
> +      DEBUG ((DEBUG_ERROR, "UsbCreateDesc: met mal-format descriptor, Head->Len = 0!\n"));
>         return NULL;
>       }
> -    Head    = (USB_DESC_HEAD*)(DescBuf + Offset);
> -    if (Head->Len == 0) {
> -      DEBUG (( EFI_D_ERROR, "UsbCreateDesc: met mal-format descriptor, Head->Len = 0!\n"));
> +
> +    //
> +    // Make sure no overflow when adding Head->Len to Offset.
> +    //
> +    if (Head->Len > MAX_UINTN - Offset) {
> +      DEBUG ((DEBUG_ERROR, "UsbCreateDesc: met mal-format descriptor, Head->Len = %d!\n", Head->Len));
>         return NULL;
>       }
> +
> +    Offset += Head->Len;
> +
> +    if (Head->Type == Type) {
> +      break;
> +    }
>     }
>   
> -  if ((Len <= Offset)      || (Len < Offset + Head->Len) ||
> -      (Head->Type != Type) || (Head->Len < DescLen)) {
> -    DEBUG (( EFI_D_ERROR, "UsbCreateDesc: met mal-format descriptor\n"));
> +  //
> +  // Head->Len is invalid resulting data beyond boundary, or
> +  // Descriptor cannot be found: No such type.
> +  //
> +  if ((Len < Offset) || (Head->Type != Type) || (Head->Len < DescLen)) {
> +    DEBUG (( EFI_D_ERROR, "UsbCreateDesc: met mal-format descriptor. Offset/Len = %d/%d, Header(T/L) = %d/%d\n", Offset, Len, Head->Type, Head->Len));

How about splitting the condition check and debug message to two? :)

if (Len < Offset) { // It is boundary check.

if ((Head->Type != Type) || (Head->Len < DescLen)) { // It is content check.


Thanks,
Star

>       return NULL;
>     }
>   
> @@ -212,7 +238,7 @@ UsbCreateDesc (
>   
>     CopyMem (Desc, Head, (UINTN) DescLen);
>   
> -  *Consumed = Offset + Head->Len;
> +  *Consumed = Offset;
>   
>     return Desc;
>   }
> 



  reply	other threads:[~2018-10-16  3:39 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-15  6:38 [PATCH 00/11] Need to validate data from HW Ruiyu Ni
2018-10-15  6:38 ` [PATCH 01/11] MdeModulePkg/UsbMass: Merge UsbBoot(Read|Write)Blocks(16) Ruiyu Ni
2018-10-16  3:06   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 02/11] MdeModulePkg/UsbMass: Fix integer overflow when BlockSize is 1 Ruiyu Ni
2018-10-16  3:19   ` Zeng, Star
2018-10-16  3:32     ` Ni, Ruiyu
2018-10-15  6:38 ` [PATCH 03/11] MdeModulePkg/UsbBus: Fix out-of-bound read access to descriptors Ruiyu Ni
2018-10-16  3:38   ` Zeng, Star [this message]
2018-10-15  6:38 ` [PATCH 04/11] MdeModulePkg/UsbBus: Reject descriptor whose length is bad Ruiyu Ni
2018-10-16  3:42   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 05/11] MdeModulePkg/Usb: Make sure data from HW is no more than expected Ruiyu Ni
2018-10-15 11:29   ` Wu, Hao A
2018-10-16  4:26   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 06/11] SourceLevelDebugPkg/Usb3: Make sure data from HW can fit in buffer Ruiyu Ni
2018-10-15 11:31   ` Wu, Hao A
2018-10-15  6:38 ` [PATCH 07/11] MdeModulePkg/UsbKb: Don't access key codes when length is wrong Ruiyu Ni
2018-10-16  4:52   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 08/11] MdeModulePkg/AbsPointer: " Ruiyu Ni
2018-10-16  5:01   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 09/11] MdeModulePkg/UsbMouse: " Ruiyu Ni
2018-10-16  5:03   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 10/11] MdeModulePkg/UsbBus: Deny when the string descriptor length is odd Ruiyu Ni
2018-10-16  5:11   ` Zeng, Star
2018-10-15  6:38 ` [PATCH 11/11] MdeModulePkg/Bus/Ufs: Ensure device not return more data than expected Ruiyu Ni
2018-10-16  5:27   ` Zeng, Star

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=a9589b65-07a2-dea5-adc5-f847218375c0@intel.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