Skip to contentSkip to conversation

EFI: refine cfgfile buffer allocation

PATCH In review Jan Beulich 2 participants 7 messages Sep 21 – Oct 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 EFI: refine cfgfile buffer allocation

Use of AllocateMaxAddress requires that the variable pointed to by the last argument of ->AllocatePages() is initialized. For cfgfile buffers we don't need AllocateMaxAddress though, at which point initialization of "addr" also isn't necessary anymore.

Mirror the lack of address constraint also to the main / central buffer allocation in read_file().

Fixes df75f77092c1 ("EFI: avoid OOB config file reads")
Assisted-by Sashiko + Opus 5.0
xen/common/efi/boot.c +8 -3
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
     what = L"Allocation";
     file->addr = min(1UL << (32 + PAGE_SHIFT),
                      HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,
+                                EfiLoaderData,
                                 PFN_UP(size + (file == &cfg)), &file->addr);
     if ( EFI_ERROR(ret) )
         goto fail;
@@ -931,7 +936,7 @@ static bool __init read_section(const EF
     if ( file == &cfg && file->size && !iscntrl(file->str[file->size - 1]) )
     {
         EFI_PHYSICAL_ADDRESS addr;
-        EFI_STATUS ret = efi_bs->AllocatePages(AllocateMaxAddress,
+        EFI_STATUS ret = efi_bs->AllocatePages(AllocateAnyPages,
                                                EfiLoaderData,
                                                PFN_UP(file->size + 1), &addr);
 
#1 lore raw
Quoted 17 lines · Jan Beulich

Use of AllocateMaxAddress requires that the variable pointed to by the last argument of ->AllocatePages() is initialized. For cfgfile buffers we don't need AllocateMaxAddress though, at which point initialization of "addr" also isn't necessary anymore.

Mirror the lack of address constraint also to the main / central buffer allocation in read_file().

Fixes df75f77092c1 ("EFI: avoid OOB config file reads")
Assisted-by Sashiko + Opus 5.0
xen/common/efi/boot.c +0 -0
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
      what = L"Allocation";

xen/common/efi/boot.c static bool __init read_file(EFI_FILE_HA · line 886

Quoted 10 lines
xen/common/efi/boot.c +6 -2
      file->addr = min(1UL << (32 + PAGE_SHIFT),
                       HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,

You could avoid the negation here, unless it was intentional?

Ross

#2 lore raw
Quoted 17 lines · Ross Lagerwall
Quoted 4 lines · Jan Beulich
xen/common/efi/boot.c +0 -0
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
      what = L"Allocation";

xen/common/efi/boot.c static bool __init read_file(EFI_FILE_HA · line 886

Quoted 10 lines
xen/common/efi/boot.c +6 -2
      file->addr = min(1UL << (32 + PAGE_SHIFT),
                       HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,

You could avoid the negation here, unless it was intentional?

Use of != was intentional, but that's not a "negation", so I'm not quite sure I understand what you're referring to.

Jan

#3 lore raw
Quoted 19 lines · Jan Beulich
Quoted 17 lines · Ross Lagerwall
Quoted 4 lines · Jan Beulich
xen/common/efi/boot.c +0 -0
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
       what = L"Allocation";

xen/common/efi/boot.c static bool __init read_file(EFI_FILE_HA · line 886

Quoted 10 lines
xen/common/efi/boot.c +6 -2
       file->addr = min(1UL << (32 + PAGE_SHIFT),
                        HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,

You could avoid the negation here, unless it was intentional?

Quoted 2 lines

Use of != was intentional, but that's not a "negation", so I'm not quite sure I understand what you're referring to.

Sorry, I meant !=.

For the cfgfile, I don't think you need to allocate whole pages so it might be better to use AllocatePool/FreePool for those allocations.

Ross

#4 lore raw
Quoted 20 lines · Ross Lagerwall
Quoted 19 lines · Jan Beulich
Quoted 17 lines · Ross Lagerwall
Quoted 4 lines · Jan Beulich
xen/common/efi/boot.c +0 -0
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
       what = L"Allocation";

xen/common/efi/boot.c static bool __init read_file(EFI_FILE_HA · line 886

Quoted 10 lines
xen/common/efi/boot.c +6 -2
       file->addr = min(1UL << (32 + PAGE_SHIFT),
                        HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,

You could avoid the negation here, unless it was intentional?

Quoted 7 lines
Quoted 2 lines

Use of != was intentional, but that's not a "negation", so I'm not quite sure I understand what you're referring to.

Sorry, I meant !=.

For the cfgfile, I don't think you need to allocate whole pages so it might be better to use AllocatePool/FreePool for those allocations.

Yet that would further increase the difference between that and the other allocations. See e.g. free_cfg(), which right now works uniformly.

Jan

#5 lore raw
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Ross Lagerwall
Quoted 21 lines · Jan Beulich
Quoted 17 lines · Ross Lagerwall
Quoted 4 lines · Jan Beulich
xen/common/efi/boot.c +0 -0
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
        what = L"Allocation";

xen/common/efi/boot.c static bool __init read_file(EFI_FILE_HA · line 886

Quoted 10 lines
xen/common/efi/boot.c +6 -2
        file->addr = min(1UL << (32 + PAGE_SHIFT),
                         HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,

You could avoid the negation here, unless it was intentional?

Use of != was intentional, but that's not a "negation", so I'm not quite sure I understand what you're referring to.

Quoted 7 lines
Quoted 4 lines

Sorry, I meant !=.

For the cfgfile, I don't think you need to allocate whole pages so it might be better to use AllocatePool/FreePool for those allocations.

Yet that would further increase the difference between that and the other allocations. See e.g. free_cfg(), which right now works uniformly.

free_cfg() is already a special case for freeing the cfgfile and it is handled differently from the other allocations (in blexit()) so switching the FreePages to FreePool in free_cfg() is surely no less uniform.

Ross

#6 lore raw
In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

Use of AllocateMaxAddress requires that the variable pointed to by the last argument of ->AllocatePages() is initialized. For cfgfile buffers we don't need AllocateMaxAddress though, at which point initialization of "addr" also isn't necessary anymore.

Quoted 7 lines

Mirror the lack of address constraint also to the main / central buffer allocation in read_file().

Fixes df75f77092c1 ("EFI: avoid OOB config file reads")
Assisted-by Sashiko + Opus 5.0

May I please ask for maintainer feedback regarding this patch? If this small variant is acceptable, I'd like to get it in. If the larger form Ross suggested is going to be a requirement, I'd like to know so I can plan the re-work accordingly.

Thanks, Jan

Quoted 27 lines
xen/common/efi/boot.c +8 -3
@@ -878,8 +878,13 @@ static bool __init read_file(EFI_FILE_HA
     what = L"Allocation";
     file->addr = min(1UL << (32 + PAGE_SHIFT),
                      HYPERVISOR_VIRT_END - DIRECTMAP_VIRT_START);
-    /* For config files allocate an extra byte to put a NUL there. */
-    ret = efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+    /*
+     * For config files allocate an extra byte to put a NUL there.  There's
+     * also no constraint on addresses for them.
+     */
+    ret = efi_bs->AllocatePages(file != &cfg ? AllocateMaxAddress
+                                             : AllocateAnyPages,
+                                EfiLoaderData,
                                 PFN_UP(size + (file == &cfg)), &file->addr);
     if ( EFI_ERROR(ret) )
         goto fail;
@@ -931,7 +936,7 @@ static bool __init read_section(const EF
     if ( file == &cfg && file->size && !iscntrl(file->str[file->size - 1]) )
     {
         EFI_PHYSICAL_ADDRESS addr;
-        EFI_STATUS ret = efi_bs->AllocatePages(AllocateMaxAddress,
+        EFI_STATUS ret = efi_bs->AllocatePages(AllocateAnyPages,
                                                EfiLoaderData,
                                                PFN_UP(file->size + 1), &addr);