Header bannerHeader banner
Advisory ID:
BRLY-LOGOFAIL-2023-027

[BRLY-LOGOFAIL-2023-027] Memory Corruption vulnerability in DXE driver

June 20, 2024
Severity:
High
CVSS Score
8.2
Public Disclosure Date:
June 19, 2024
CVE ID:
CVE-2023-5058

Summary

BINARLY efiXplorer team has discovered an OOB Write vulnerability in the decode routine during BMP file processing in Phoenix firmware.

Vendors Affected

Affected Products

Yoga Slim 7 Pro
,

Potential Impact

An attacker with local access can exploit this vulnerability to elevate privileges from ring 3 or ring 0 (depends on the operating system) to a DXE driver and execute arbitrary code. Malicious code installed as a result of this exploitation could survive operating system (OS) boot process and runtime, or modify NVRAM area on the SPI flash storage (to gain persistence). Additionally, threat actors could use this vulnerability to bypass OS security mechanisms (modify privileged memory or runtime variables), influence OS boot process, and in some cases allow an attacker to hook or modify EFI Runtime services.

Summary

BINARLY efiXplorer team has discovered an OOB Write vulnerability in the decode routine during BMP file processing in Phoenix firmware.

Vulnerability Information

     
  • BINARLY internal vulnerability identifier: BRLY-LOGOFAIL-2023-027  
  • Phoenix PSIRT assigned CVE identifier: CVE-2023-5058  
  • CVSS v3.1: 8.2 High AV:L/AC:L/PR:H/UI:N/S:C/C:H/I:H/A:H

Affected modules with confirmed impact by Binarly team

Module name Module GUID Module SHA256
SystemImageDecoderDxe 5F65D21A-8867-45D3-A41A-526F9FE2C598 86E6C85A8FF7C1DB8FF7292521223C546DD4F40F5168F7DA3134916BF52DA81D

Potential impact

An attacker with local access can exploit this vulnerability to elevate privileges from ring 3 or ring 0 (depending on the operating system) to a DXE driver and execute arbitrary code. Malicious code installed as a result of this exploitation could survive operating system (OS) boot process and runtime, or modify NVRAM area on the SPI flash storage (to gain persistence). Additionally, threat actors could use this vulnerability to bypass OS security mechanisms (modify privileged memory or runtime variables), influence OS boot process, and in some cases allow an attacker to hook or modify EFI Runtime services.

Vulnerability description

The pseudocode of the vulnerable function is shown below:

// local variable allocation has failed, the output may be wrong!
EFI_STATUS __fastcall DbDecodeImage(BUFFER_INDICATOR *Bi, EFI_IMAGE_INPUT *ImageInput)
{
  UINTN BufferSize; // rcx
  BMP_COLOR_PALETTE *Palette; // r15
  EFI_STATUS Status; // rsi
  void *Start; // rax
  void *Current; // rcx
  char *Start_plus_BMP_FILE_HEADER; // rax
  char *Start_plus_BufferSize; // rcx
  size_t ImageSize; // rdi
  __int64 BmpHeaderSize; // r12
  __int64 ColorAmount; // r14
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *mem; // rax
  UINT16 BitsPerPixel; // cx
  size_t PaletteSize; // r14 MAPDST
  void *Start_plus_BmpHeaderSize; // rax
  void *Start_plus_BufferSize_1; // rcx
  void *ImageBuffer; // rax MAPDST
  void *Start_plus_ImageOffset; // rcx
  UINT32 Height; // er8
  int i; // ebx
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *PixelPtr; // rdi
  __int64 ImageIndex; // r9
  UINT32 Width; // er11
  UINT32 j; // er10
  unsigned __int64 offset; // rcx
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL v29; // eax
  UINT8 v30; // al
  __int64 index; // rcx
  __int64 v32; // rcx
  unsigned __int64 v33; // rcx
  __int64 v34; // rax
  unsigned __int64 v35; // rcx
  __int64 BitIndex; // r12
  unsigned __int64 PaletteIndex; // rdx
  __int64 v38; // rax
  int v39; // eax
  BMP_FILE_HEADER Bfh; // [rsp+20h] [rbp-40h]
  BMP_INFO_HEADER Bih; // [rsp+30h] [rbp-30h]
  UINT64 ScratchedBufferSize; // [rsp+A0h] [rbp+40h] OVERLAPPED
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color; // [rsp+A0h] [rbp+40h] FORCED

  BufferSize = Bi->BufferSize;
  Bfh.ImageOffset = 0;
  Palette = 0i64;
  Bih.BitsPerPixel = 0;
  Status = 0i64;
  Bih.ImageSize = 0;
  *(_QWORD *)&Bih.HeaderSize = 0i64;
  Bih.Height = 0;
  if ( BufferSize <= 0x36 )
    return Status;
  Start = Bi->Start;
  Bi->Current = Bi->Start;
  ScratchedBufferSize = sizeof(BMP_FILE_HEADER);
  if ( (char *)Start + sizeof(BMP_FILE_HEADER) > (char *)Start + BufferSize )
    return Status;
  if ( BiReadBuffer(Bi, &Bfh, &ScratchedBufferSize, (char *)Start + sizeof(BMP_FILE_HEADER)) )
    return Status;
  Current = Bi->Start;
  ScratchedBufferSize = sizeof(BMP_INFO_HEADER);
  Start_plus_BMP_FILE_HEADER = (char *)Current + sizeof(BMP_FILE_HEADER);
  Start_plus_BufferSize = (char *)Current + Bi->BufferSize;
  Bi->Current = Start_plus_BMP_FILE_HEADER;
  if ( Start_plus_BMP_FILE_HEADER + sizeof(BMP_INFO_HEADER) > Start_plus_BufferSize
    || BiReadBuffer(Bi, &Bih, &ScratchedBufferSize, Start_plus_BMP_FILE_HEADER + sizeof(BMP_INFO_HEADER))
    || Bfh.BmpId[0] != 'B'
    || Bfh.BmpId[1] != 'M' )
  {
    return Status;
  }
  ImageSize = Bih.ImageSize;
  BmpHeaderSize = Bih.HeaderSize + sizeof(BMP_FILE_HEADER);
  ColorAmount = 1 << SLOBYTE(Bih.BitsPerPixel);
  ScratchedBufferSize = Bih.ImageSize;
  mem = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)malloc(4i64 * Bih.Height * Bih.Width);// Root Cause #1: integer overflow. Signed mul operation.
  ImageInput->Bitmap = mem;
  if ( !mem )
    return EFI_OUT_OF_RESOURCES;
  BitsPerPixel = Bih.BitsPerPixel;
  ImageInput->Height = Bih.Height;
  ImageInput->Width = Bih.Width;
  if ( !ImageSize )
  {
    ImageSize = Bih.Height * ((((BitsPerPixel * (unsigned __int64)Bih.Width) >> 3) + 3) & 0xFFFFFFFC);
    ScratchedBufferSize = Bih.Height * ((((BitsPerPixel * (unsigned __int64)Bih.Width) >> 3) + 3) & 0xFFFFFFFC);
  }
  if ( (unsigned __int16)(BitsPerPixel - 1) <= 7u )
  {
    PaletteSize = 4 * ColorAmount;
    Palette = (BMP_COLOR_PALETTE *)malloc(PaletteSize);
    if ( !Palette )
      return EFI_OUT_OF_RESOURCES;
    Start_plus_BmpHeaderSize = (char *)Bi->Start + BmpHeaderSize;
    Start_plus_BufferSize_1 = (char *)Bi->Start + Bi->BufferSize;
    Bi->Current = Start_plus_BmpHeaderSize;
    if ( (char *)Start_plus_BmpHeaderSize + PaletteSize <= Start_plus_BufferSize_1 )
      BiReadBuffer(Bi, Palette, &PaletteSize, (char *)Start_plus_BmpHeaderSize + PaletteSize);
  }
  ImageBuffer = malloc(ImageSize);
  if ( ImageBuffer )
  {
    Start_plus_ImageOffset = (char *)Bi->Start + Bfh.ImageOffset;
    Bi->Current = Start_plus_ImageOffset;
    if ( (void *)((unsigned __int64)Start_plus_ImageOffset + ImageSize) <= (char *)Bi->Start + Bi->BufferSize )
      BiReadBuffer(Bi, ImageBuffer, &ScratchedBufferSize, (char *)Start_plus_ImageOffset + ImageSize);
    Height = Bih.Height;
    i = 0;
    PixelPtr = ImageInput->Bitmap;
    ImageIndex = 0i64;
    Color.Reserved = 0;
    if ( !Bih.Height )
      goto exit;
    Width = Bih.Width;
    while ( 1 )
    {
      j = 0;
      if ( Width )
        break;
LABEL_40:
      if ( ImageIndex & 3 )
        v39 = 4 - (ImageIndex & 3);
      else
        v39 = 0;
      ImageIndex = (unsigned int)(v39 + ImageIndex);
      if ( ++i >= Height )
        goto exit;
    }
    while ( 1 )
    {
      if ( Bih.BitsPerPixel == 1 )              // 2 Color Mode
                                                // CRASH area
      {
        BitIndex = 7i64;
        while ( j < Width )
        {
          PaletteIndex = ((unsigned __int64)*((unsigned __int8 *)ImageBuffer + ImageIndex) >> BitIndex) & 1;
          *(_WORD *)&Color.Green = *(_WORD *)&Palette[PaletteIndex].Green;
          Color.Blue = Palette[PaletteIndex].Blue;
          v38 = j++;
          --BitIndex;
          PixelPtr[v38 + Width * (Height - i - 1)] = Color;// OOB write Root Cause #1
          Height = Bih.Height;
          Width = Bih.Width;
        }
        --j;
        ImageIndex = (unsigned int)(ImageIndex + 1);
        goto LABEL_39;
      }
      if ( Bih.BitsPerPixel == 4 )              // 16 Color Mode
        break;
      switch ( Bih.BitsPerPixel )
      {
        case 8u:                                // 256 Color Mode
          v32 = *((unsigned __int8 *)ImageBuffer + ImageIndex);
          ImageIndex = (unsigned int)(ImageIndex + 1);
          *(_WORD *)&Color.Green = *(_WORD *)&Palette[v32].Green;
          v30 = Palette[v32].Blue;
LABEL_30:
          index = Width * (Height - i - 1);
LABEL_33:
          Color.Blue = v30;
          offset = j + index;
          v29 = Color;
          goto LABEL_34;
        case 0x18u:                             // 16M Color Mode
          *(_WORD *)&Color.Green = *(_WORD *)((char *)ImageBuffer + ImageIndex + 1);
          v30 = *((_BYTE *)ImageBuffer + ImageIndex);
          ImageIndex = (unsigned int)(ImageIndex + 3);
          goto LABEL_30;
        case 0x20u:                             // CRASH case
          offset = j + (unsigned __int64)(Width * (Height - i - 1));
          v29 = *(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)((char *)ImageBuffer + ImageIndex);
          ImageIndex = (unsigned int)(ImageIndex + 4);
LABEL_34:
          PixelPtr[offset] = v29;               // OOB write Root Cause #1
          Height = Bih.Height;
          Width = Bih.Width;
          break;
      }
LABEL_39:
      if ( ++j >= Width )
        goto LABEL_40;
    }
    v33 = (unsigned __int64)*((unsigned __int8 *)ImageBuffer + ImageIndex) >> 4;
    *(_WORD *)&Color.Green = *(_WORD *)&Palette[v33].Green;
    Color.Blue = Palette[v33].Blue;
    v34 = j++;
    PixelPtr[v34 + Width * (Height - i - 1)] = Color;// OOB write Root Cause #1
    LOBYTE(v33) = *((_BYTE *)ImageBuffer + ImageIndex);
    ImageIndex = (unsigned int)(ImageIndex + 1);
    v35 = v33 & 0xF;
    *(_WORD *)&Color.Green = *(_WORD *)&Palette[v35].Green;
    v30 = Palette[v35].Blue;
    index = Bih.Width * (Bih.Height - i - 1);
    goto LABEL_33;
  }
  Status = EFI_OUT_OF_RESOURCES;
exit:
  if ( Palette )
    gEfiBootServices->FreePool(Palette);
  if ( ImageBuffer )
    gEfiBootServices->FreePool(ImageBuffer);
  return Status;
}

As we can see from the pseudocode, Width and Height are not checked properly and there is a signed operation of multiply between them. This could lead to: malloc(4 * (-1 * -1)) which in turn allocates very small buffer on a heap. However bigger integers Width and Height treated as unsigned will allow to create and OOB index for such a buffer.

This vulnerability could lead to the execution of arbitrary code during the DXE phase.

Disclosure timeline

This bug is subject to a 90 day disclosure deadline. After 90 days elapsed or a patch has been made broadly available (whichever is earlier), the bug report will become visible to the public.

Disclosure Activity Date (YYYY-mm-dd)
Lenovo PSIRT is notified 2023-06-21
Lenovo ID (LEN-132940) is assigned 2023-06-22
CERT/CC is notified 2023-07-10
Phoenix PSIRT confirmed reported issues 2023-09-10
Phoenix PSIRT assigned CVE ID 2023-11-27
Phoenix advisory release date 2023-12-06
BINARLY public disclosure date 2024-06-19

Acknowledgements

BINARLY efiXplorer team

Tags
Vulnerability
supply chain
FWHunt
See if you are impacted now with our Firmware Vulnerability Scanner