From: "GuoMinJ" <newexplorerj@gmail.com>
To: devel@edk2.groups.io, lersek@redhat.com
Cc: philmd@redhat.com, Jian J Wang <jian.j.wang@intel.com>,
Hao A Wu <hao.a.wu@intel.com>, Eric Dong <eric.dong@intel.com>
Subject: Re: [edk2-devel] [RFC PATCH 1/1] MdeModulePkg/PiDxeS3BootScriptLib: Use SafeIntLib to avoid truncation
Date: Tue, 18 Feb 2020 15:10:29 +0800 [thread overview]
Message-ID: <CAGcZ9yW3mhAYLwdSR2rmWWYeom9Dg7468M0QV-rHZSKM_1tYwA@mail.gmail.com> (raw)
In-Reply-To: <115eb81f-59ca-398c-bac8-445e9a516849@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 6996 bytes --]
I agree with this comment.
Laszlo Ersek <lersek@redhat.com> 于2020年2月17日周一 下午5:32写道:
> On 02/13/20 19:33, Philippe Mathieu-Daudé wrote:
> > On 2/13/20 7:29 PM, Philippe Mathieu-Daude wrote:
> >> Math expressions written in terms of SafeIntLib function calls
> >> are easily readable, making review trivial. Convert the truncation
> >> checks added by commit 322ac05f8 to SafeIntLib calls.
> >>
> >> Cc: Jian J Wang <jian.j.wang@intel.com>
> >> Cc: Hao A Wu <hao.a.wu@intel.com>
> >> Cc: Eric Dong <eric.dong@intel.com>
> >> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> >> Signed-off-by: Philippe Mathieu-Daude <philmd@redhat.com>
> >> ---
> >> .../DxeS3BootScriptLib.inf | 1 +
> >> .../InternalBootScriptLib.h | 1 +
> >> .../PiDxeS3BootScriptLib/BootScriptSave.c | 114 +++++++++++-------
> >> 3 files changed, 73 insertions(+), 43 deletions(-)
> >>
> >> diff --git
> >> a/MdeModulePkg/Library/PiDxeS3BootScriptLib/DxeS3BootScriptLib.inf
> >> b/MdeModulePkg/Library/PiDxeS3BootScriptLib/DxeS3BootScriptLib.inf
> >> index 2b894c99da55..698039fe8e69 100644
> >> --- a/MdeModulePkg/Library/PiDxeS3BootScriptLib/DxeS3BootScriptLib.inf
> >> +++ b/MdeModulePkg/Library/PiDxeS3BootScriptLib/DxeS3BootScriptLib.inf
> >> @@ -40,15 +40,16 @@ [Packages]
> >> [LibraryClasses]
> >> UefiBootServicesTableLib
> >> BaseLib
> >> BaseMemoryLib
> >> TimerLib
> >> DebugLib
> >> PcdLib
> >> UefiLib
> >> SmbusLib
> >> PciSegmentLib
> >> IoLib
> >> LockBoxLib
> >> + SafeIntLib
> >> [Protocols]
> >> gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES
> >> diff --git
> >> a/MdeModulePkg/Library/PiDxeS3BootScriptLib/InternalBootScriptLib.h
> >> b/MdeModulePkg/Library/PiDxeS3BootScriptLib/InternalBootScriptLib.h
> >> index 9485994087d0..7513220c15ac 100644
> >> --- a/MdeModulePkg/Library/PiDxeS3BootScriptLib/InternalBootScriptLib.h
> >> +++ b/MdeModulePkg/Library/PiDxeS3BootScriptLib/InternalBootScriptLib.h
> >> @@ -1,49 +1,50 @@
> >> /** @file
> >> Support for S3 boot script lib. This file defined some internal
> >> macro and internal
> >> data structure
> >> Copyright (c) 2006 - 2016, Intel Corporation. All rights
> >> reserved.<BR>
> >> SPDX-License-Identifier: BSD-2-Clause-Patent
> >> **/
> >> #ifndef __INTERNAL_BOOT_SCRIPT_LIB__
> >> #define __INTERNAL_BOOT_SCRIPT_LIB__
> >> #include <PiDxe.h>
> >> #include <Guid/EventGroup.h>
> >> #include <Protocol/SmmBase2.h>
> >> #include <Protocol/DxeSmmReadyToLock.h>
> >> #include <Protocol/SmmReadyToLock.h>
> >> #include <Protocol/SmmExitBootServices.h>
> >> #include <Protocol/SmmLegacyBoot.h>
> >> #include <Library/S3BootScriptLib.h>
> >> #include <Library/UefiBootServicesTableLib.h>
> >> #include <Library/BaseLib.h>
> >> #include <Library/PcdLib.h>
> >> #include <Library/SmbusLib.h>
> >> #include <Library/IoLib.h>
> >> #include <Library/PciSegmentLib.h>
> >> #include <Library/DebugLib.h>
> >> #include <Library/BaseMemoryLib.h>
> >> #include <Library/TimerLib.h>
> >> #include <Library/UefiLib.h>
> >> #include <Library/LockBoxLib.h>
> >> +#include <Library/SafeIntLib.h>
> >> #include "BootScriptInternalFormat.h"
> >> #define MAX_IO_ADDRESS 0xFFFF
> >> //
> >> // Macro to convert a UEFI PCI address + segment to a PCI Segment
> >> Library PCI address
> >> //
> >> #define PCI_ADDRESS_ENCODE(S, A) PCI_SEGMENT_LIB_ADDRESS( \
> >> S, \
> >> ((((UINTN)(A)) & 0xff000000) >>
> >> 24), \
> >> ((((UINTN)(A)) & 0x00ff0000) >>
> >> 16), \
> >> ((((UINTN)(A)) & 0xff00) >> 8), \
> >> ((RShiftU64 ((A), 32) & 0xfff) |
> >> ((A) & 0xff)) \
> >> )
> >> diff --git
> >> a/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> >> b/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> >> index 9315fc9f0188..d229263638fc 100644
> >> --- a/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> >> +++ b/MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
> >> @@ -995,55 +995,60 @@ EFIAPI
> >> S3BootScriptSaveIoWrite (
> >> IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
> >> IN UINT64 Address,
> >> IN UINTN Count,
> >> IN VOID *Buffer
> >> )
> >> {
> >> + EFI_STATUS Status;
> >> UINT8 Length;
> >> UINT8 *Script;
> >> UINT8 WidthInByte;
> >> EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite;
> >> - WidthInByte = (UINT8) (0x01 << (Width & 0x03));
> >> + Status = SafeUintnToUint8 (Count, &Length);
> >> + if (EFI_ERROR (Status)) {
> >> + return RETURN_OUT_OF_RESOURCES;
> >> + }
> >> +
> >> + Status = SafeUint8Mult (Length, 0x01 << (Width & 0x03), &Length);
> >> + if (EFI_ERROR (Status)) {
> >> + return RETURN_OUT_OF_RESOURCES;
> >> + }
> >> - //
> >> - // Truncation check
> >> - //
> >> - if ((Count > MAX_UINT8) ||
> >> - (WidthInByte * Count > MAX_UINT8 - sizeof
> >> (EFI_BOOT_SCRIPT_IO_WRITE))) {
> >> + Status = SafeUint8Add (Length, sizeof (EFI_BOOT_SCRIPT_IO_WRITE),
> >> &Length);
> >> + if (EFI_ERROR (Status)) {
> >> return RETURN_OUT_OF_RESOURCES;
> >> }
> >> - Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte *
> >> Count));
> >> Script = S3BootScriptGetEntryAddAddress (Length);
> >> if (Script == NULL) {
> >> return RETURN_OUT_OF_RESOURCES;
> >> }
> >> //
> >> // save script data
> >> //
> >> ScriptIoWrite.OpCode = EFI_BOOT_SCRIPT_IO_WRITE_OPCODE;
> >> ScriptIoWrite.Length = Length;
> >> ScriptIoWrite.Width = Width;
> >> ScriptIoWrite.Address = Address;
> >> ScriptIoWrite.Count = (UINT32) Count;
> >> CopyMem ((VOID*)Script, (VOID*)&ScriptIoWrite,
> >> sizeof(EFI_BOOT_SCRIPT_IO_WRITE));
> >> CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE)),
> >> Buffer, WidthInByte * Count);
> >> SyncBootScript (Script);
> >> return RETURN_SUCCESS;
> >> }
> >
> > Oops wrong version (WidthInByte is uninitialized).
>
> Also -- if the MdeModulePkg maintainers like the appraoch in general --,
> you could eliminate the "WidthInByte * Count" multiplication from the
> CopyMem() call altogether. Instead, you could save the SafeUint8Mult()
> result in an intermediate variable, and reuse it here.
>
> Just an idea (again, only for the case when the MdeModulePkg actually
> like this refactoring).
>
> Thanks!
> Laszlo
>
>
>
>
>
[-- Attachment #2: Type: text/html, Size: 9763 bytes --]
prev parent reply other threads:[~2020-02-18 7:10 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-13 18:29 [RFC PATCH 0/1] MdeModulePkg/PiDxeS3BootScriptLib: Use SafeIntLib to avoid truncation Philippe Mathieu-Daudé
2020-02-13 18:29 ` [RFC PATCH 1/1] " Philippe Mathieu-Daudé
2020-02-13 18:33 ` Philippe Mathieu-Daudé
2020-02-17 9:32 ` [edk2-devel] " Laszlo Ersek
2020-02-18 6:51 ` GuoMinJ
2020-02-18 7:04 ` GuoMinJ
2020-02-18 7:10 ` GuoMinJ [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=CAGcZ9yW3mhAYLwdSR2rmWWYeom9Dg7468M0QV-rHZSKM_1tYwA@mail.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