public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Gary Lin <glin@suse.com>
To: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: edk2-devel@lists.01.org, Ye Ting <ting.ye@intel.com>,
	Wang Fan <fan.wang@intel.com>, Fu Siyuan <siyuan.fu@intel.com>
Subject: Re: [Patch 1/5 v2] MdeModulePkg/DxeHttpLib: Add boundary condition check.
Date: Tue, 26 Dec 2017 12:48:02 +0800	[thread overview]
Message-ID: <20171226044802.sof2jzvzv76blccs@GaryWorkstation> (raw)
In-Reply-To: <1514255807-4580-1-git-send-email-jiaxin.wu@intel.com>

On Tue, Dec 26, 2017 at 10:36:47AM +0800, Jiaxin Wu wrote:
> v2:
> * Fix GCC the build error.
> 
> This patch is to add the boundary condition check to make sure
> the accessed buffer is valid.
> 
> Cc: Gary Lin <glin@suse.com>
> Cc: Ye Ting <ting.ye@intel.com>
> Cc: Fu Siyuan <siyuan.fu@intel.com>
> Cc: Wang Fan <fan.wang@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Wu Jiaxin <jiaxin.wu@intel.com>
> ---
>  MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.c | 38 +++++++++++++++++++++++++---
>  1 file changed, 34 insertions(+), 4 deletions(-)
> 
> diff --git a/MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.c b/MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.c
> index caddbb7..915b81d 100644
> --- a/MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.c
> +++ b/MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.c
> @@ -54,11 +54,11 @@ UriPercentDecode (
>    Index = 0;
>    Offset = 0;
>    HexStr[2] = '\0';
>    while (Index < BufferLength) {
>      if (Buffer[Index] == '%') {
> -      if (!NET_IS_HEX_CHAR (Buffer[Index+1]) || !NET_IS_HEX_CHAR (Buffer[Index+2])) {
> +      if (Index + 1 >= BufferLength || Index + 2 >= BufferLength || !NET_IS_HEX_CHAR (Buffer[Index+1]) || !NET_IS_HEX_CHAR (Buffer[Index+2])) {
>          return EFI_INVALID_PARAMETER;
>        }
>        HexStr[0] = Buffer[Index+1];
>        HexStr[1] = Buffer[Index+2];
>        ResultBuffer[Offset] = (CHAR8) AsciiStrHexToUintn (HexStr);
> @@ -1556,20 +1556,31 @@ HttpGetFieldNameAndValue (
>    )
>  {
>    CHAR8  *FieldNameStr;
>    CHAR8  *FieldValueStr;
>    CHAR8  *StrPtr;
> +  CHAR8  *EndofHeader;
>  
>    if (String == NULL || FieldName == NULL || FieldValue == NULL) {
>      return NULL;
>    }
>  
>    *FieldName    = NULL;
>    *FieldValue   = NULL;
>    FieldNameStr  = NULL;
>    FieldValueStr = NULL;
>    StrPtr        = NULL;
> +  EndofHeader   = NULL;
> +
> +
> +  //
> +  // Check whether the raw HTTP header string is valid or not.
> +  //
> +  EndofHeader = AsciiStrStr (String, "\r\n\r\n");
> +  if (EndofHeader == NULL) {
> +    return NULL;
> +  }
>  
>    //
>    // Each header field consists of a name followed by a colon (":") and the field value.
>    //
>    FieldNameStr = String;
> @@ -1583,17 +1594,36 @@ HttpGetFieldNameAndValue (
>    //
>    *(FieldValueStr - 1) = 0;
>  
>    //
>    // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
> +  // Note: LWS  = [CRLF] 1*(SP|HT), it can be '\r\n ' or '\r\n\t' or ' ' or '\t'.
> +  //       CRLF = '\r\n'.
> +  //       SP   = ' '.
> +  //       HT   = '\t' (Tab).
>    //
>    while (TRUE) {
>      if (*FieldValueStr == ' ' || *FieldValueStr == '\t') {
> +      //
> +      // Boundary condition check. 
Trailing whitespaces (nitpicking :-)

> +      //
> +      if ((UINTN)EndofHeader - (UINTN)(FieldValueStr) < 1) {
> +        return NULL;  
ditto

> +      }
> +      
ditto

>        FieldValueStr ++;
> -    } else if (*FieldValueStr == '\r' && *(FieldValueStr + 1) == '\n' &&
> -               (*(FieldValueStr + 2) == ' ' || *(FieldValueStr + 2) == '\t')) {
> -      FieldValueStr = FieldValueStr + 3;
> +    } else if (*FieldValueStr == '\r') {
> +      //
> +      // Boundary condition check. 
ditto

> +      //
> +      if ((UINTN)EndofHeader - (UINTN)(FieldValueStr) < 3) {
> +        return NULL;  
ditto

> +      }
> +
> +      if (*(FieldValueStr + 1) == '\n' && (*(FieldValueStr + 2) == ' ' || *(FieldValueStr + 2) == '\t')) {
> +        FieldValueStr = FieldValueStr + 3;
> +      }
>      } else {
>        break;
>      }
>    }
>  
This patch fixes the gcc build.

Tested-by: Gary Lin <glin@suse.com>

Gary Lin

> -- 
> 1.9.5.msysgit.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
> 


      reply	other threads:[~2017-12-26  4:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-26  2:36 [Patch 1/5 v2] MdeModulePkg/DxeHttpLib: Add boundary condition check Jiaxin Wu
2017-12-26  4:48 ` Gary Lin [this message]

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=20171226044802.sof2jzvzv76blccs@GaryWorkstation \
    --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