]> xenbits.xensource.com Git - ovmf.git/commitdiff
UefiCpuPkg/CpuPageTableLib:Add check for Mask and Attr
authorDun Tan <dun.tan@intel.com>
Fri, 24 Feb 2023 07:05:08 +0000 (15:05 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 27 Mar 2023 08:21:58 +0000 (08:21 +0000)
For different usage, check if the combination for Mask and
Attr is valid when creating or updating page table.

1.For non-present range
  1.1Mask.Present is 0 but some other attributes is provided.
     This case is invalid.
  1.2Mask.Present is 1 and Attr.Present is 0. In this case,all
     other attributes should not be provided.
  1.3Mask.Present is 1 and Attr.Present is 1. In this case,all
     attributes should be provided to intialize the attribute.

2.For present range
  2.1Mask.Present is 1 and Attr.Present is 0.In this case, all
     other attributes should not be provided.
All other usage for present range is permitted.
In the mentioned cases, 1.2 and 2.1 can be merged into 1 check.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Tested-by: Gerd Hoffmann <kraxel@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
UefiCpuPkg/Include/Library/CpuPageTableLib.h
UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c

index 5f44ece548915d530e7bd885bbd367d5ddc4f30e..4ef4a8b6af208a61fb3581278a35da23853df371 100644 (file)
@@ -77,6 +77,10 @@ typedef enum {
 \r
   @retval RETURN_UNSUPPORTED        PagingMode is not supported.\r
   @retval RETURN_INVALID_PARAMETER  PageTable, BufferSize, Attribute or Mask is NULL.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 0 but some other attributes are provided.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 1 but some other attributes are not provided.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 0 but some other attributes are provided.\r
+  @retval RETURN_INVALID_PARAMETER  For present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 0 but some other attributes are provided.\r
   @retval RETURN_INVALID_PARAMETER  *BufferSize is not multiple of 4KB.\r
   @retval RETURN_BUFFER_TOO_SMALL   The buffer is too small for page table creation/updating.\r
                                     BufferSize is updated to indicate the expected buffer size.\r
index f09bb63ad1351b39106ffd4daa9afdfebaff6a23..76c3719cdfc31d1c76370a45dd50c51306fb8a3c 100644 (file)
@@ -215,6 +215,44 @@ PageTableLibSetPnle (
   Pnle->Bits.CacheDisabled = 0;\r
 }\r
 \r
+/**\r
+  Check if the combination for Attribute and Mask is valid for non-present entry.\r
+  1.Mask.Present is 0 but some other attributes is provided. This case should be invalid.\r
+  2.Map non-present range to present. In this case, all attributes should be provided.\r
+\r
+  @param[in] Attribute    The attribute of the linear address range.\r
+  @param[in] Mask         The mask used for attribute to check.\r
+\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 0 but some other attributes are provided.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 1 but some other attributes are not provided.\r
+  @retval RETURN_SUCCESS            The combination for Attribute and Mask is valid.\r
+**/\r
+RETURN_STATUS\r
+IsAttributesAndMaskValidForNonPresentEntry (\r
+  IN     IA32_MAP_ATTRIBUTE  *Attribute,\r
+  IN     IA32_MAP_ATTRIBUTE  *Mask\r
+  )\r
+{\r
+  if ((Mask->Bits.Present == 1) && (Attribute->Bits.Present == 1)) {\r
+    //\r
+    // Creating new page table or remapping non-present range to present.\r
+    //\r
+    if ((Mask->Bits.ReadWrite == 0) || (Mask->Bits.UserSupervisor == 0) || (Mask->Bits.WriteThrough == 0) || (Mask->Bits.CacheDisabled == 0) ||\r
+        (Mask->Bits.Accessed == 0) || (Mask->Bits.Dirty == 0) || (Mask->Bits.Pat == 0) || (Mask->Bits.Global == 0) ||\r
+        (Mask->Bits.PageTableBaseAddress == 0) || (Mask->Bits.ProtectionKey == 0) || (Mask->Bits.Nx == 0))\r
+    {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+  } else if ((Mask->Bits.Present == 0) && (Mask->Uint64 > 1)) {\r
+    //\r
+    // Only change other attributes for non-present range is not permitted.\r
+    //\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
 /**\r
   Update page table to map [LinearAddress, LinearAddress + Length) with specified attribute in the specified level.\r
 \r
@@ -237,6 +275,8 @@ PageTableLibSetPnle (
                                     when a new physical base address is set.\r
   @param[in]      Mask              The mask used for attribute. The corresponding field in Attribute is ignored if that in Mask is 0.\r
 \r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 0 but some other attributes are provided.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 1 but some other attributes are not provided.\r
   @retval RETURN_SUCCESS            PageTable is created/updated successfully.\r
 **/\r
 RETURN_STATUS\r
@@ -260,6 +300,7 @@ PageTableLibMapInLevel (
   UINTN               Index;\r
   IA32_PAGING_ENTRY   *PagingEntry;\r
   UINTN               PagingEntryIndex;\r
+  UINTN               PagingEntryIndexEnd;\r
   IA32_PAGING_ENTRY   *CurrentPagingEntry;\r
   UINT64              RegionLength;\r
   UINT64              SubLength;\r
@@ -306,6 +347,14 @@ PageTableLibMapInLevel (
   //\r
 \r
   if (ParentPagingEntry->Pce.Present == 0) {\r
+    //\r
+    // [LinearAddress, LinearAddress + Length] contains non-present range.\r
+    //\r
+    Status = IsAttributesAndMaskValidForNonPresentEntry (Attribute, Mask);\r
+    if (RETURN_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
     //\r
     // The parent entry is CR3 or PML5E/PML4E/PDPTE/PDE.\r
     // It does NOT point to an existing page directory.\r
@@ -383,6 +432,27 @@ PageTableLibMapInLevel (
       ParentPagingEntry->Uint64 = ((UINTN)(VOID *)PagingEntry) | (ParentPagingEntry->Uint64 & (~IA32_PE_BASE_ADDRESS_MASK_40));\r
     }\r
   } else {\r
+    //\r
+    // If (LinearAddress + Length - 1) is not in the same ParentPagingEntry with (LinearAddress + Offset), then the remaining child PagingEntry\r
+    // starting from PagingEntryIndex of ParentPagingEntry is all covered by [LinearAddress + Offset, LinearAddress + Length - 1].\r
+    //\r
+    PagingEntryIndexEnd = (BitFieldRead64 (LinearAddress + Length - 1, BitStart + 9, 63) != BitFieldRead64 (LinearAddress + Offset, BitStart + 9, 63)) ? 511 :\r
+                          (UINTN)BitFieldRead64 (LinearAddress + Length - 1, BitStart, BitStart + 9 - 1);\r
+    PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)IA32_PNLE_PAGE_TABLE_BASE_ADDRESS (&ParentPagingEntry->Pnle);\r
+    for (Index = PagingEntryIndex; Index <= PagingEntryIndexEnd; Index++) {\r
+      if (PagingEntry[Index].Pce.Present == 0) {\r
+        //\r
+        // [LinearAddress, LinearAddress + Length] contains non-present range.\r
+        //\r
+        Status = IsAttributesAndMaskValidForNonPresentEntry (Attribute, Mask);\r
+        if (RETURN_ERROR (Status)) {\r
+          return Status;\r
+        }\r
+\r
+        break;\r
+      }\r
+    }\r
+\r
     //\r
     // It's a non-leaf entry\r
     //\r
@@ -430,7 +500,6 @@ PageTableLibMapInLevel (
         // Update child entries to use restrictive attribute inherited from parent.\r
         // e.g.: Set PDE[0-255].ReadWrite = 0\r
         //\r
-        PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)IA32_PNLE_PAGE_TABLE_BASE_ADDRESS (&ParentPagingEntry->Pnle);\r
         for (Index = 0; Index < 512; Index++) {\r
           if (PagingEntry[Index].Pce.Present == 0) {\r
             continue;\r
@@ -557,6 +626,10 @@ PageTableLibMapInLevel (
 \r
   @retval RETURN_UNSUPPORTED        PagingMode is not supported.\r
   @retval RETURN_INVALID_PARAMETER  PageTable, BufferSize, Attribute or Mask is NULL.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 0 but some other attributes are provided.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 1 but some other attributes are not provided.\r
+  @retval RETURN_INVALID_PARAMETER  For non-present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 0 but some other attributes are provided.\r
+  @retval RETURN_INVALID_PARAMETER  For present range, Mask->Bits.Present is 1, Attribute->Bits.Present is 0 but some other attributes are provided.\r
   @retval RETURN_INVALID_PARAMETER  *BufferSize is not multiple of 4KB.\r
   @retval RETURN_BUFFER_TOO_SMALL   The buffer is too small for page table creation/updating.\r
                                     BufferSize is updated to indicate the expected buffer size.\r
@@ -618,6 +691,14 @@ PageTableMap (
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // If to map [LinearAddress, LinearAddress + Length] as non-present,\r
+  // all attributes except Present should not be provided.\r
+  //\r
+  if ((Attribute->Bits.Present == 0) && (Mask->Bits.Present == 1) && (Mask->Uint64 > 1)) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
   MaxLeafLevel     = (IA32_PAGE_LEVEL)(UINT8)PagingMode;\r
   MaxLevel         = (IA32_PAGE_LEVEL)(UINT8)(PagingMode >> 8);\r
   MaxLinearAddress = LShiftU64 (1, 12 + MaxLevel * 9);\r