public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 0/6] Refine type cast for pointer subtraction
@ 2017-01-24  7:50 Hao Wu
  2017-01-24  7:50 ` [PATCH 1/6] MdeModulePkg: " Hao Wu
  0 siblings, 1 reply; 3+ messages in thread
From: Hao Wu @ 2017-01-24  7:50 UTC (permalink / raw)
  To: edk2-devel; +Cc: Hao Wu

Please note that this patch is maily for feedback collection and the only
the MdeModulePkg part of the series is sent out firstly.


For pointer subtraction, the result is of type "ptrdiff_t". According to
the C11 spec, ptrdiff_t is a signed integer type but its size is
implementation-defined.

There are cases that the result of pointer subtraction is type casted to
UINTN, like:

UINT8  *Ptr1, *Ptr2;
UINTN  PtrDiff;
...
PtrDiff = (UINTN) (Ptr1 - Ptr2);

Some static code checkers may warn that the pointer subtraction might
overflow first and then the result is being cast to a bigger size.

The series will cast each pointer to UINTN first and then perform the
subtraction:

PtrDiff = (UINTN) Ptr1 - (UINTN) Ptr2;


Hao Wu (6):
  MdeModulePkg: Refine type cast for pointer subtraction
  CryptoPkg: Refine type cast for pointer subtraction
  IntelFrameworkModulePkg: Refine type cast for pointer subtraction
  NetworkPkg: Refine type cast for pointer subtraction
  SecurityPkg: Refine type cast for pointer subtraction
  ShellPkg: Refine type cast for pointer subtraction

 .../BaseCryptLib/SysCall/RuntimeMemAllocation.c        |  6 +++---
 IntelFrameworkModulePkg/Csm/LegacyBiosDxe/LegacyPci.c  |  4 ++--
 .../Library/GenericBdsLib/BdsBoot.c                    |  4 ++--
 .../Library/GenericBdsLib/BdsConsole.c                 |  4 ++--
 .../Library/GenericBdsLib/BdsMisc.c                    |  4 ++--
 .../Library/LegacyBootManagerLib/LegacyBm.c            |  4 ++--
 .../Universal/BdsDxe/BootMaint/BootOption.c            |  6 +++---
 MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c   |  4 ++--
 MdeModulePkg/Include/Library/NetLib.h                  |  6 +++---
 MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c  |  4 ++--
 .../Library/DxePrintLibPrint2Protocol/PrintLib.c       |  2 +-
 MdeModulePkg/Library/FileExplorerLib/FileExplorer.c    |  6 +++---
 .../Library/PiDxeS3BootScriptLib/BootScriptSave.c      |  4 ++--
 MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c |  4 ++--
 MdeModulePkg/Universal/DebugPortDxe/DebugPort.c        |  4 ++--
 .../FaultTolerantWriteDxe/FaultTolerantWrite.c         |  4 ++--
 MdeModulePkg/Universal/HiiDatabaseDxe/Image.c          |  4 ++--
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c  | 10 +++++-----
 NetworkPkg/HttpDxe/HttpImpl.c                          |  6 +++---
 .../DxeImageVerificationLib/DxeImageVerificationLib.c  | 18 +++++++++---------
 .../DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c        | 10 +++++-----
 SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c          |  6 +++---
 SecurityPkg/Tcg/Tcg2Dxe/MeasureBootPeCoff.c            | 10 +++++-----
 SecurityPkg/Tcg/TrEEDxe/MeasureBootPeCoff.c            | 10 +++++-----
 .../SecureBootConfigDxe/SecureBootConfigImpl.c         | 14 +++++++-------
 ShellPkg/Application/Shell/ShellParametersProtocol.c   |  6 +++---
 ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c |  4 ++--
 .../UefiShellDebug1CommandsLib/SmbiosView/SmbiosView.c |  4 ++--
 ShellPkg/Library/UefiShellDriver1CommandsLib/Dh.c      |  4 ++--
 ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c       |  6 +++---
 30 files changed, 91 insertions(+), 91 deletions(-)

-- 
1.9.5.msysgit.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/6] MdeModulePkg: Refine type cast for pointer subtraction
  2017-01-24  7:50 [PATCH 0/6] Refine type cast for pointer subtraction Hao Wu
@ 2017-01-24  7:50 ` Hao Wu
  2017-01-24 10:14   ` Laszlo Ersek
  0 siblings, 1 reply; 3+ messages in thread
From: Hao Wu @ 2017-01-24  7:50 UTC (permalink / raw)
  To: edk2-devel; +Cc: Hao Wu

For pointer subtraction, the result is of type "ptrdiff_t". According to
the C11 spec, ptrdiff_t is a signed integer type but its size is
implementation-defined.

There are cases that the result of pointer subtraction is type casted to
UINTN, like:

UINT8  *Ptr1, *Ptr2;
UINTN  PtrDiff;
...
PtrDiff = (UINTN) (Ptr1 - Ptr2);

Some static code checkers may warn that the pointer subtraction might
overflow first and then the result is being cast to a bigger size.

The commit will cast each pointer to UINTN first and then perform the
subtraction:

PtrDiff = (UINTN) Ptr1 - (UINTN) Ptr2;

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
---
 MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c           |  4 ++--
 MdeModulePkg/Include/Library/NetLib.h                          |  6 +++---
 MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c          |  4 ++--
 MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c      |  2 +-
 MdeModulePkg/Library/FileExplorerLib/FileExplorer.c            |  6 +++---
 MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c     |  4 ++--
 MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c         |  4 ++--
 MdeModulePkg/Universal/DebugPortDxe/DebugPort.c                |  4 ++--
 .../Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c       |  4 ++--
 MdeModulePkg/Universal/HiiDatabaseDxe/Image.c                  |  4 ++--
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c          | 10 +++++-----
 11 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
index 2bc4f8c..d2ad94e 100644
--- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
+++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
@@ -1,7 +1,7 @@
 /** @file
   PCI Rom supporting funtions implementation for PCI Bus module.
 
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
@@ -776,7 +776,7 @@ ProcessOpRomImage (
 NextImage:
     RomBarOffset += ImageSize;
 
-  } while (((Indicator & 0x80) == 0x00) && ((UINTN) (RomBarOffset - (UINT8 *) RomBar) < PciDevice->RomSize));
+  } while (((Indicator & 0x80) == 0x00) && (((UINTN) RomBarOffset - (UINTN) RomBar) < PciDevice->RomSize));
 
   return RetStatus;
 }
diff --git a/MdeModulePkg/Include/Library/NetLib.h b/MdeModulePkg/Include/Library/NetLib.h
index 09ead09..3b8ff1a 100644
--- a/MdeModulePkg/Include/Library/NetLib.h
+++ b/MdeModulePkg/Include/Library/NetLib.h
@@ -2,7 +2,7 @@
   This library is only intended to be used by UEFI network stack modules.
   It provides basic functions for the UEFI network stack.
 
-Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at<BR>
@@ -1610,10 +1610,10 @@ typedef struct {
   (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
 
 #define NET_HEADSPACE(BlockOp)  \
-  (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
+  ((UINTN)((BlockOp)->Head) - (UINTN)((BlockOp)->BlockHead))
 
 #define NET_TAILSPACE(BlockOp)  \
-  (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
+  ((UINTN)((BlockOp)->BlockTail) - (UINTN)((BlockOp)->Tail))
 
 /**
   Allocate a single block NET_BUF. Upon allocation, all the
diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
index 71e05bd..d7abcc8 100644
--- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
+++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
@@ -10,7 +10,7 @@
   ValidateFmpCapsule(), DisplayCapsuleImage(), ConvertBmpToGopBlt() will
   receive untrusted input and do basic validation.
 
-  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -547,7 +547,7 @@ ConvertBmpToGopBlt (
 
     }
 
-    ImageIndex = (UINTN) (Image - ImageHeader);
+    ImageIndex = (UINTN) Image - (UINTN) ImageHeader;
     if ((ImageIndex % 4) != 0) {
       //
       // Bmp Image starts each row on a 32-bit boundary!
diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
index 0137868..7fda260 100644
--- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
+++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
@@ -205,7 +205,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
     //
     // If BASE_LIST is larger than Size, then return FALSE
     //
-    if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {
+    if (((UINTN)BaseListMarker - (UINTN)BaseListStart) > Size) {
       DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));
       return FALSE;
     }
diff --git a/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c b/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
index 41a22aa..d4da998 100644
--- a/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
+++ b/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
@@ -1,7 +1,7 @@
 /** @file
 File explorer related functions.
 
-Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials are licensed and made available under
 the terms and conditions of the BSD License that accompanies this distribution.
 The full text of the license may be found at
@@ -657,7 +657,7 @@ LibAppendFileName (
       // that overlap.
       //
       StrCpyS (TmpStr, MaxLen, Ptr + 3);
-      StrCpyS (LastSlash, MaxLen - (UINTN) (LastSlash - Str), TmpStr);
+      StrCpyS (LastSlash, MaxLen - ((UINTN) LastSlash - (UINTN) Str) / sizeof (CHAR16), TmpStr);
       Ptr = LastSlash;
     } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {
       //
@@ -669,7 +669,7 @@ LibAppendFileName (
       // that overlap.
       //
       StrCpyS (TmpStr, MaxLen, Ptr + 2);
-      StrCpyS (Ptr, MaxLen - (UINTN) (Ptr - Str), TmpStr);
+      StrCpyS (Ptr, MaxLen - ((UINTN) Ptr - (UINTN) Str) / sizeof (CHAR16), TmpStr);
       Ptr = LastSlash;
     } else if (*Ptr == '\\') {
       LastSlash = Ptr;
diff --git a/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c b/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
index 5698c91..1f8aaf4 100644
--- a/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
+++ b/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
@@ -1,7 +1,7 @@
 /** @file
   Save the S3 data to S3 boot script.
 
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions
@@ -2025,7 +2025,7 @@ S3BootScriptCalculateInsertAddress (
    // calculate the Position offset
    //
    if (Position != NULL) {
-     PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);
+     PositionOffset = (UINTN)Position - (UINTN)S3TableBase;
 
      //
      // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
index 6f705bd..116cf28 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
+++ b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
@@ -1,7 +1,7 @@
 /** @file
   Load option library functions which relate with creating and processing load options.
 
-Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
@@ -912,7 +912,7 @@ EfiBootManagerVariableToLoadOptionEx (
   FilePath = (EFI_DEVICE_PATH_PROTOCOL *) VariablePtr;
   VariablePtr += FilePathSize;
 
-  OptionalDataSize = (UINT32) (VariableSize - (UINTN) (VariablePtr - Variable));
+  OptionalDataSize = (UINT32) (VariableSize - ((UINTN) VariablePtr - (UINTN) Variable));
   if (OptionalDataSize == 0) {
     OptionalData = NULL;
   } else {
diff --git a/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c b/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
index 298b6b2..dcb623c 100644
--- a/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
+++ b/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
@@ -4,7 +4,7 @@
   ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM
   INTERRUPT CONTEXT
 
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
@@ -569,7 +569,7 @@ DebugPortRead (
     LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);
   } while (LocalBufferSize != 0 && Timeout > 0);
 
-  *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);
+  *BufferSize = (UINTN) BufferPtr - (UINTN) Buffer;
 
   return Status;
 }
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
index 2e3e8c7..49e747b 100644
--- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
+++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
@@ -3,7 +3,7 @@
   These are the common Fault Tolerant Write (FTW) functions that are shared 
   by DXE FTW driver and SMM FTW driver.
 
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials                          
 are licensed and made available under the terms and conditions of the BSD License         
 which accompanies this distribution.  The full text of the license may be found at        
@@ -373,7 +373,7 @@ FtwWrite (
   //
   // If Record is out of the range of Header, return access denied.
   //
-  if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {
+  if (((UINTN) Record - (UINTN) Header) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {
     return EFI_ACCESS_DENIED;
   }
 
diff --git a/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c b/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
index 1668828..e2fa16e 100644
--- a/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
+++ b/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
@@ -2,7 +2,7 @@
 Implementation for EFI_HII_IMAGE_PROTOCOL.
 
 
-Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
@@ -1124,7 +1124,7 @@ HiiSetImage (
     return EFI_OUT_OF_RESOURCES;
   }
 
-  Part1Size = (UINT32) (UINTN) ((UINT8 *) CurrentImageBlock - (UINT8 *) ImagePackage->ImageBlock);
+  Part1Size = (UINT32) ((UINTN) CurrentImageBlock - (UINTN) ImagePackage->ImageBlock);
   Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;
   CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);
 
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index f5b6a5f..b0c7434 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -16,7 +16,7 @@
   VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
   integer overflow. It should also check attribute to avoid authentication bypass.
 
-Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
@@ -1170,7 +1170,7 @@ Reclaim (
   // Install the new variable if it is not NULL.
   //
   if (NewVariable != NULL) {
-    if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
+    if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
       //
       // No enough space to store the new variable.
       //
@@ -1211,8 +1211,8 @@ Reclaim (
     // If volatile variable store, just copy valid buffer.
     //
     SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
-    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));
-    *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
+    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);
+    *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
     Status  = EFI_SUCCESS;
   } else {
     //
@@ -1223,7 +1223,7 @@ Reclaim (
               (VARIABLE_STORE_HEADER *) ValidBuffer
               );
     if (!EFI_ERROR (Status)) {
-      *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
+      *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
       mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
       mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
       mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;
-- 
1.9.5.msysgit.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/6] MdeModulePkg: Refine type cast for pointer subtraction
  2017-01-24  7:50 ` [PATCH 1/6] MdeModulePkg: " Hao Wu
@ 2017-01-24 10:14   ` Laszlo Ersek
  0 siblings, 0 replies; 3+ messages in thread
From: Laszlo Ersek @ 2017-01-24 10:14 UTC (permalink / raw)
  To: Hao Wu; +Cc: edk2-devel

On 01/24/17 08:50, Hao Wu wrote:
> For pointer subtraction, the result is of type "ptrdiff_t". According to
> the C11 spec, ptrdiff_t is a signed integer type but its size is
> implementation-defined.
> 
> There are cases that the result of pointer subtraction is type casted to
> UINTN, like:
> 
> UINT8  *Ptr1, *Ptr2;
> UINTN  PtrDiff;
> ...
> PtrDiff = (UINTN) (Ptr1 - Ptr2);
> 
> Some static code checkers may warn that the pointer subtraction might
> overflow first and then the result is being cast to a bigger size.
> 
> The commit will cast each pointer to UINTN first and then perform the
> subtraction:
> 
> PtrDiff = (UINTN) Ptr1 - (UINTN) Ptr2;

The main point here is that pointer subtraction is only allowed between
pointers to elements of the same (single-dimensional) array. For the
purposes of this requirement, the subtrahend and/or the minuend can
point one past the last element in the array (but such a pointer cannot
be dereferenced). Also, a non-array object is treated identically to an
array with 1 element.

In the above permitted (defined) cases, no overflow will ever occur.
That leaves us with the following concerns:

- The well-defined difference (of type ptrdiff_t) might be negative;
casting that to UINTN probably doesn't have the intended result. This
can be excluded by verifying (manually) that the minuend pointer always
points past the subtrahend pointer.

- If the difference is not defined (because the above requirement is not
satisfied), then the pointers should indeed be converted to some integer
type before the subtraction. This raises further questions:

  - if the integer type we choose is signed, then the subtraction
    (between the signed integers) could underflow, which would be
    undefined.

  - if the integer type we choose is unsigned (and has at least the
    rank of int), then the subtraction could underflow with
    well-defined behavior, but the result would still be non-intuitive.

Considering our implementation details, casting both operands to UINTN
and then doing the subtraction seems fine, as long as we can ensure that
the subtraction doesn't wrap around (in order to prevent non-intuitive
results).

... I wrote up this wall of text only to suggest a more precise commit
message. If you have access to the C11 std (or a late enough draft of
it), then it's best to quote the standard directly. My point is that in
the following example,

  UINT8 Blah[2][2];
  UINTN Diff;

  Diff = &Blah[1][1] - &Blah[0][0];

there is no underflow of the specific kind that the commit message
suggests, but the subtraction is still undefined, because the operands
don't point into (or one past) the same array. The minuend points into
the Blah[1] array, while the subtrahend points into the Blah[0] array.

Not volunteering to review this patch though :)

Thanks
Laszlo


> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Hao Wu <hao.a.wu@intel.com>
> ---
>  MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c           |  4 ++--
>  MdeModulePkg/Include/Library/NetLib.h                          |  6 +++---
>  MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c          |  4 ++--
>  MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c      |  2 +-
>  MdeModulePkg/Library/FileExplorerLib/FileExplorer.c            |  6 +++---
>  MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c     |  4 ++--
>  MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c         |  4 ++--
>  MdeModulePkg/Universal/DebugPortDxe/DebugPort.c                |  4 ++--
>  .../Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c       |  4 ++--
>  MdeModulePkg/Universal/HiiDatabaseDxe/Image.c                  |  4 ++--
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c          | 10 +++++-----
>  11 files changed, 26 insertions(+), 26 deletions(-)
> 
> diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> index 2bc4f8c..d2ad94e 100644
> --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> @@ -1,7 +1,7 @@
>  /** @file
>    PCI Rom supporting funtions implementation for PCI Bus module.
>  
> -Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD License
>  which accompanies this distribution.  The full text of the license may be found at
> @@ -776,7 +776,7 @@ ProcessOpRomImage (
>  NextImage:
>      RomBarOffset += ImageSize;
>  
> -  } while (((Indicator & 0x80) == 0x00) && ((UINTN) (RomBarOffset - (UINT8 *) RomBar) < PciDevice->RomSize));
> +  } while (((Indicator & 0x80) == 0x00) && (((UINTN) RomBarOffset - (UINTN) RomBar) < PciDevice->RomSize));
>  
>    return RetStatus;
>  }
> diff --git a/MdeModulePkg/Include/Library/NetLib.h b/MdeModulePkg/Include/Library/NetLib.h
> index 09ead09..3b8ff1a 100644
> --- a/MdeModulePkg/Include/Library/NetLib.h
> +++ b/MdeModulePkg/Include/Library/NetLib.h
> @@ -2,7 +2,7 @@
>    This library is only intended to be used by UEFI network stack modules.
>    It provides basic functions for the UEFI network stack.
>  
> -Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD License
>  which accompanies this distribution.  The full text of the license may be found at<BR>
> @@ -1610,10 +1610,10 @@ typedef struct {
>    (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
>  
>  #define NET_HEADSPACE(BlockOp)  \
> -  (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
> +  ((UINTN)((BlockOp)->Head) - (UINTN)((BlockOp)->BlockHead))
>  
>  #define NET_TAILSPACE(BlockOp)  \
> -  (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
> +  ((UINTN)((BlockOp)->BlockTail) - (UINTN)((BlockOp)->Tail))
>  
>  /**
>    Allocate a single block NET_BUF. Upon allocation, all the
> diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> index 71e05bd..d7abcc8 100644
> --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> @@ -10,7 +10,7 @@
>    ValidateFmpCapsule(), DisplayCapsuleImage(), ConvertBmpToGopBlt() will
>    receive untrusted input and do basic validation.
>  
> -  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
>    This program and the accompanying materials
>    are licensed and made available under the terms and conditions of the BSD License
>    which accompanies this distribution.  The full text of the license may be found at
> @@ -547,7 +547,7 @@ ConvertBmpToGopBlt (
>  
>      }
>  
> -    ImageIndex = (UINTN) (Image - ImageHeader);
> +    ImageIndex = (UINTN) Image - (UINTN) ImageHeader;
>      if ((ImageIndex % 4) != 0) {
>        //
>        // Bmp Image starts each row on a 32-bit boundary!
> diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
> index 0137868..7fda260 100644
> --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
> +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
> @@ -205,7 +205,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
>      //
>      // If BASE_LIST is larger than Size, then return FALSE
>      //
> -    if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {
> +    if (((UINTN)BaseListMarker - (UINTN)BaseListStart) > Size) {
>        DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));
>        return FALSE;
>      }
> diff --git a/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c b/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
> index 41a22aa..d4da998 100644
> --- a/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
> +++ b/MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
> @@ -1,7 +1,7 @@
>  /** @file
>  File explorer related functions.
>  
> -Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials are licensed and made available under
>  the terms and conditions of the BSD License that accompanies this distribution.
>  The full text of the license may be found at
> @@ -657,7 +657,7 @@ LibAppendFileName (
>        // that overlap.
>        //
>        StrCpyS (TmpStr, MaxLen, Ptr + 3);
> -      StrCpyS (LastSlash, MaxLen - (UINTN) (LastSlash - Str), TmpStr);
> +      StrCpyS (LastSlash, MaxLen - ((UINTN) LastSlash - (UINTN) Str) / sizeof (CHAR16), TmpStr);
>        Ptr = LastSlash;
>      } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {
>        //
> @@ -669,7 +669,7 @@ LibAppendFileName (
>        // that overlap.
>        //
>        StrCpyS (TmpStr, MaxLen, Ptr + 2);
> -      StrCpyS (Ptr, MaxLen - (UINTN) (Ptr - Str), TmpStr);
> +      StrCpyS (Ptr, MaxLen - ((UINTN) Ptr - (UINTN) Str) / sizeof (CHAR16), TmpStr);
>        Ptr = LastSlash;
>      } else if (*Ptr == '\\') {
>        LastSlash = Ptr;
> diff --git a/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c b/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> index 5698c91..1f8aaf4 100644
> --- a/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> +++ b/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> @@ -1,7 +1,7 @@
>  /** @file
>    Save the S3 data to S3 boot script.
>  
> -  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
>  
>    This program and the accompanying materials
>    are licensed and made available under the terms and conditions
> @@ -2025,7 +2025,7 @@ S3BootScriptCalculateInsertAddress (
>     // calculate the Position offset
>     //
>     if (Position != NULL) {
> -     PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);
> +     PositionOffset = (UINTN)Position - (UINTN)S3TableBase;
>  
>       //
>       // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
> diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> index 6f705bd..116cf28 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> @@ -1,7 +1,7 @@
>  /** @file
>    Load option library functions which relate with creating and processing load options.
>  
> -Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
>  (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD License
> @@ -912,7 +912,7 @@ EfiBootManagerVariableToLoadOptionEx (
>    FilePath = (EFI_DEVICE_PATH_PROTOCOL *) VariablePtr;
>    VariablePtr += FilePathSize;
>  
> -  OptionalDataSize = (UINT32) (VariableSize - (UINTN) (VariablePtr - Variable));
> +  OptionalDataSize = (UINT32) (VariableSize - ((UINTN) VariablePtr - (UINTN) Variable));
>    if (OptionalDataSize == 0) {
>      OptionalData = NULL;
>    } else {
> diff --git a/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c b/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
> index 298b6b2..dcb623c 100644
> --- a/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
> +++ b/MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
> @@ -4,7 +4,7 @@
>    ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM
>    INTERRUPT CONTEXT
>  
> -Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD License
>  which accompanies this distribution.  The full text of the license may be found at
> @@ -569,7 +569,7 @@ DebugPortRead (
>      LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);
>    } while (LocalBufferSize != 0 && Timeout > 0);
>  
> -  *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);
> +  *BufferSize = (UINTN) BufferPtr - (UINTN) Buffer;
>  
>    return Status;
>  }
> diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
> index 2e3e8c7..49e747b 100644
> --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
> +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
> @@ -3,7 +3,7 @@
>    These are the common Fault Tolerant Write (FTW) functions that are shared 
>    by DXE FTW driver and SMM FTW driver.
>  
> -Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials                          
>  are licensed and made available under the terms and conditions of the BSD License         
>  which accompanies this distribution.  The full text of the license may be found at        
> @@ -373,7 +373,7 @@ FtwWrite (
>    //
>    // If Record is out of the range of Header, return access denied.
>    //
> -  if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {
> +  if (((UINTN) Record - (UINTN) Header) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {
>      return EFI_ACCESS_DENIED;
>    }
>  
> diff --git a/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c b/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
> index 1668828..e2fa16e 100644
> --- a/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
> +++ b/MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
> @@ -2,7 +2,7 @@
>  Implementation for EFI_HII_IMAGE_PROTOCOL.
>  
>  
> -Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD License
>  which accompanies this distribution.  The full text of the license may be found at
> @@ -1124,7 +1124,7 @@ HiiSetImage (
>      return EFI_OUT_OF_RESOURCES;
>    }
>  
> -  Part1Size = (UINT32) (UINTN) ((UINT8 *) CurrentImageBlock - (UINT8 *) ImagePackage->ImageBlock);
> +  Part1Size = (UINT32) ((UINTN) CurrentImageBlock - (UINTN) ImagePackage->ImageBlock);
>    Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;
>    CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);
>  
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index f5b6a5f..b0c7434 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -16,7 +16,7 @@
>    VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
>    integer overflow. It should also check attribute to avoid authentication bypass.
>  
> -Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
>  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD License
> @@ -1170,7 +1170,7 @@ Reclaim (
>    // Install the new variable if it is not NULL.
>    //
>    if (NewVariable != NULL) {
> -    if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
> +    if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
>        //
>        // No enough space to store the new variable.
>        //
> @@ -1211,8 +1211,8 @@ Reclaim (
>      // If volatile variable store, just copy valid buffer.
>      //
>      SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
> -    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));
> -    *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
> +    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);
> +    *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
>      Status  = EFI_SUCCESS;
>    } else {
>      //
> @@ -1223,7 +1223,7 @@ Reclaim (
>                (VARIABLE_STORE_HEADER *) ValidBuffer
>                );
>      if (!EFI_ERROR (Status)) {
> -      *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
> +      *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
>        mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
>        mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
>        mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;
> 



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-01-24 10:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-24  7:50 [PATCH 0/6] Refine type cast for pointer subtraction Hao Wu
2017-01-24  7:50 ` [PATCH 1/6] MdeModulePkg: " Hao Wu
2017-01-24 10:14   ` Laszlo Ersek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox