1From 06aa91f79f902752cb7e5d22ac0ea8e13bffd056 Mon Sep 17 00:00:00 2001 2From: Alexey Makhalov <amakhalov@vmware.com> 3Date: Fri, 17 Jul 2020 05:17:26 +0000 4Subject: [PATCH] relocator: Fix grub_relocator_alloc_chunk_align() top 5 memory allocation 6MIME-Version: 1.0 7Content-Type: text/plain; charset=UTF-8 8Content-Transfer-Encoding: 8bit 9 10Current implementation of grub_relocator_alloc_chunk_align() 11does not allow allocation of the top byte. 12 13Assuming input args are: 14 max_addr = 0xfffff000; 15 size = 0x1000; 16 17And this is valid. But following overflow protection will 18unnecessarily move max_addr one byte down (to 0xffffefff): 19 if (max_addr > ~size) 20 max_addr = ~size; 21 22~size + 1 will fix the situation. In addition, check size 23for non zero to do not zero max_addr. 24 25Signed-off-by: Alexey Makhalov <amakhalov@vmware.com> 26Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com> 27Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com> 28--- 29 grub-core/lib/relocator.c | 4 ++-- 30 1 file changed, 2 insertions(+), 2 deletions(-) 31 32diff --git a/grub-core/lib/relocator.c b/grub-core/lib/relocator.c 33index 5847aac36..f2c1944c2 100644 34--- a/grub-core/lib/relocator.c 35+++ b/grub-core/lib/relocator.c 36@@ -1386,8 +1386,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, 37 }; 38 grub_addr_t min_addr2 = 0, max_addr2; 39 40- if (max_addr > ~size) 41- max_addr = ~size; 42+ if (size && (max_addr > ~size)) 43+ max_addr = ~size + 1; 44 45 #ifdef GRUB_MACHINE_PCBIOS 46 if (min_addr < 0x1000) 47-- 482.26.2 49 50