efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c of Linux 5.1
PATCH 1/1 efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c of Linux 5.1
save_pgd is allocated by kmalloc_array. And it is dereferenced in the following codes. However, memory allocation functions such as kmalloc_array may fail. Dereferencing this save_pgd null pointer may cause the kernel go wrong. Thus we should check this allocation and add error handling code.
---
arch/x86/platform/efi/efi_64.c +5 -0
@@ -91,6 +91,8 @@ pgd_t * __init efi_call_phys_prolog(void) n_pgds =DIV_ROUND_UP ((max_pfn <<PAGE_SHIFT ),PGDIR_SIZE ); save_pgd =kmalloc_array (n_pgds,sizeof (*save_pgd),GFP_KERNEL ); +if (!save_pgd) + goto err;/* * Build 1:1 identity mapping for efi=old_map usage. Note that
@@ -142,6 +144,9 @@ pgd_t * __init efi_call_phys_prolog(void)__flush_tlb_all ();return save_pgd; +err: +__flush_tlb_all (); +return ERR_PTR (-ENOMEM ); }void __init efi_call_phys_epilog (pgd_t *save_pgd)
---
Hello Gen,
Thanks for the patch.
Quoted 24 lines · Gen Zhang
save_pgdis allocated bykmalloc_array. And it is dereferenced in the following codes. However, memory allocation functions such askmalloc_arraymay fail. Dereferencing thissave_pgdnull pointer may cause the kernel go wrong. Thus we should check this allocation and add error handling code.Signed-off-by Gen Zhang <[email protected]>---
arch/x86/platform/efi/efi_64.c +2
-0@@ -91,6 +91,8 @@ pgd_t * __init efi_call_phys_prolog(void) n_pgds =DIV_ROUND_UP ((max_pfn <<PAGE_SHIFT ),PGDIR_SIZE ); save_pgd =kmalloc_array (n_pgds,sizeof (*save_pgd),GFP_KERNEL ); +if (!save_pgd) + goto err;/* * Build 1:1 identity mapping for efi=old_map usage. Note that
arch/x86/platform/efi/efi_64.c pgd_t * __init efi_call_phys_prolog(void) · line 148
Quoted 5 lines
arch/x86/platform/efi/efi_64.c +2
-0@@ -142,6 +144,9 @@ pgd_t * __init efi_call_phys_prolog(void)__flush_tlb_all ();return save_pgd; +err: +__flush_tlb_all ();
What is the point of the goto and the TLB flush?
arch/x86/platform/efi/efi_64.c pgd_t * __init efi_call_phys_prolog(void) · line 149
Quoted 1 line
arch/x86/platform/efi/efi_64.c +1
-0+return ERR_PTR (-ENOMEM );
Returning an error here is not going to make much difference, given that the caller of efi_call_phys_prolog() does not bother to check it, and passes the result straight into efi_call_phys_epilog(), which happily attempts to dereference it.
So if you want to fix this properly, please fix it at the call site as well. I'd prefer to avoid ERR_PTR() and just return NULL for a failed allocation though.
Quoted 4 lines
arch/x86/platform/efi/efi_64.c +0
-1}void __init efi_call_phys_epilog (pgd_t *save_pgd) ---
Quoted 8 lines · Ard Biesheuvel
Returning an error here is not going to make much difference, given that the caller of
efi_call_phys_prolog()does not bother to check it, and passes the result straight intoefi_call_phys_epilog(), which happily attempts to dereference it.So if you want to fix this properly, please fix it at the call site as well. I'd prefer to avoid
ERR_PTR()and just return NULL for a failed allocation though.
Hi Ard,
Thanks for your timely reply!
I think returning NULL in efi_call_phys_prolog() and checking in efi_call_phys_epilog() is much better. But I am confused what to return in efi_call_phys_epilog() if save_pgd is NULL. Definitely not return -ENOMEM, because efi_call_phys_epilog() returns unsigned long. Could please light on me to fix this problem?
Thanks
Gen
Quoted 10 lines · Gen Zhang
Quoted 8 lines · Ard Biesheuvel
Returning an error here is not going to make much difference, given that the caller of
efi_call_phys_prolog()does not bother to check it, and passes the result straight intoefi_call_phys_epilog(), which happily attempts to dereference it.So if you want to fix this properly, please fix it at the call site as well. I'd prefer to avoid
ERR_PTR()and just return NULL for a failed allocation though.
Quoted 7 lines
Hi Ard,
Thanks for your timely reply!
I think returning NULL inefi_call_phys_prolog()and checking inefi_call_phys_epilog()is much better. But I am confused what to return inefi_call_phys_epilog()ifsave_pgdis NULL. Definitely not return-ENOMEM, becauseefi_call_phys_epilog()returns unsigned long. Could please light on me to fix this problem?
If efi_call_phys_prolog() returns NULL, the calling function should abort and never call efi_call_phys_epilog().
Quoted 2 lines · Ard Biesheuvel
If
efi_call_phys_prolog()returns NULL, the calling function should abort and never callefi_call_phys_epilog().
Hi Ard,
I edit the patch and it is as following. Returning EFI_ABORTED would be proper, because the return value (status) is checked in __efi_enter_virtual_mode(). And returning EFI_ABORTED can abort the process.
Thanks
Gen
save_pgd is allocated by kmalloc_array. And it is dereferenced in the following codes. However, memory allocation functions such as kmalloc_array may fail. Dereferencing this save_pgd null pointer may cause the kernel go wrong. Thus we should check this allocation.
---
arch/x86/platform/efi/efi.c +2 -0
@@ -85,6 +85,8 @@ static efi_status_t __init phys_efi_set_virtual_address_map(pgd_t *save_pgd; save_pgd =efi_call_phys_prolog (); +if (!save_pgd) +return EFI_ABORTED ;/* Disable interrupts around EFI calls: */ local_irq_save (flags);
arch/x86/platform/efi/efi_64.c +2 -0
@@ -91,6 +91,8 @@ pgd_t * __init efi_call_phys_prolog(void) n_pgds =DIV_ROUND_UP ((max_pfn <<PAGE_SHIFT ),PGDIR_SIZE ); save_pgd =kmalloc_array (n_pgds,sizeof (*save_pgd),GFP_KERNEL ); +if (!save_pgd) +return NULL ;/* * Build 1:1 identity mapping for efi=old_map usage. Note that
---
Re: [PATCH v2] efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c
In reply to Ard BiesheuvelQuoted 20 lines · Ard Biesheuvel
Quoted 10 lines · Gen Zhang
Quoted 8 lines · Ard Biesheuvel
Returning an error here is not going to make much difference, given that the caller of
efi_call_phys_prolog()does not bother to check it, and passes the result straight intoefi_call_phys_epilog(), which happily attempts to dereference it.So if you want to fix this properly, please fix it at the call site as well. I'd prefer to avoid
ERR_PTR()and just return NULL for a failed allocation though.Quoted 7 lines
Hi Ard,
Thanks for your timely reply!
I think returning NULL inefi_call_phys_prolog()and checking inefi_call_phys_epilog()is much better. But I am confused what to return inefi_call_phys_epilog()ifsave_pgdis NULL. Definitely not return-ENOMEM, becauseefi_call_phys_epilog()returns unsigned long. Could please light on me to fix this problem?
Quoted 2 lines
If
efi_call_phys_prolog()returns NULL, the calling function should abort and never callefi_call_phys_epilog().
In efi_call_phys_prolog(), save_pgd is allocated by kmalloc_array(). And it is dereferenced in the following codes. However, memory allocation functions such as kmalloc_array() may fail. Dereferencing this save_pgd null pointer may cause the kernel go wrong. Thus we should check this allocation.
Further, if efi_call_phys_prolog() returns NULL, we should abort the process in phys_efi_set_virtual_address_map(), and return EFI_ABORTED.
---
arch/x86/platform/efi/efi.c +2 -0
@@ -85,6 +85,8 @@ static efi_status_t __init phys_efi_set_virtual_address_map(pgd_t *save_pgd; save_pgd =efi_call_phys_prolog (); +if (!save_pgd) +return EFI_ABORTED ;/* Disable interrupts around EFI calls: */ local_irq_save (flags);
arch/x86/platform/efi/efi_64.c +2 -0
@@ -91,6 +91,8 @@ pgd_t * __init efi_call_phys_prolog(void) n_pgds =DIV_ROUND_UP ((max_pfn <<PAGE_SHIFT ),PGDIR_SIZE ); save_pgd =kmalloc_array (n_pgds,sizeof (*save_pgd),GFP_KERNEL ); +if (!save_pgd) +return NULL ;/* * Build 1:1 identity mapping for efi=old_map usage. Note that
---
PATCH v2 1/1 efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c
In reply to Ard BiesheuvelIn efi_call_phys_prolog(), save_pgd is allocated by kmalloc_array(). And it is dereferenced in the following codes. However, memory allocation functions such as kmalloc_array() may fail. Dereferencing this save_pgd null pointer may cause the kernel go wrong. Thus we should check this allocation.
Further, if efi_call_phys_prolog() returns NULL, we should abort the process in phys_efi_set_virtual_address_map(), and return EFI_ABORTED.
---
arch/x86/platform/efi/efi.c +2 -0
@@ -85,6 +85,8 @@ static efi_status_t __init phys_efi_set_virtual_address_map(pgd_t *save_pgd; save_pgd =efi_call_phys_prolog (); +if (!save_pgd) +return EFI_ABORTED ;/* Disable interrupts around EFI calls: */ local_irq_save (flags);
arch/x86/platform/efi/efi_64.c +2 -0
@@ -91,6 +91,8 @@ pgd_t * __init efi_call_phys_prolog(void) n_pgds =DIV_ROUND_UP ((max_pfn <<PAGE_SHIFT ),PGDIR_SIZE ); save_pgd =kmalloc_array (n_pgds,sizeof (*save_pgd),GFP_KERNEL ); +if (!save_pgd) +return NULL ;/* * Build 1:1 identity mapping for efi=old_map usage. Note that
---
Re: [PATCH v2] efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c
Quoted 30 lines · Gen Zhang
In
efi_call_phys_prolog(),save_pgdis allocated bykmalloc_array(). And it is dereferenced in the following codes. However, memory allocation functions such askmalloc_array()may fail. Dereferencing thissave_pgdnull pointer may cause the kernel go wrong. Thus we should check this allocation.
Further, ifefi_call_phys_prolog()returns NULL, we should abort the process inphys_efi_set_virtual_address_map(), and returnEFI_ABORTED.Signed-off-by Gen Zhang <[email protected]>---
arch/x86/platform/efi/efi.c +2
-0@@ -85,6 +85,8 @@ static efi_status_t __init phys_efi_set_virtual_address_map(pgd_t *save_pgd; save_pgd =efi_call_phys_prolog (); +if (!save_pgd) +return EFI_ABORTED ;/* Disable interrupts around EFI calls: */ local_irq_save (flags);arch/x86/platform/efi/efi_64.c +0
-0
arch/x86/platform/efi/efi_64.c pgd_t * __init efi_call_phys_prolog(void)
Quoted 9 lines
arch/x86/platform/efi/efi_64.c +2
-1@@ -91,6 +91,8 @@ pgd_t * __init efi_call_phys_prolog(void) n_pgds =DIV_ROUND_UP ((max_pfn <<PAGE_SHIFT ),PGDIR_SIZE ); save_pgd =kmalloc_array (n_pgds,sizeof (*save_pgd),GFP_KERNEL ); +if (!save_pgd) +return NULL ;/* * Build 1:1 identity mapping for efi=old_map usage. Note that ---
Apologies for only spotting this now, but I seem to have given some bad advice.
efi_call_phys_prolog() in efi_64.c will also return NULL if (!efi_enabled(EFI_OLD_MEMMAP)), but this is not an error condition. So that occurrence has to be updated: please return efi_mm.pgd instead.
Re: [PATCH v2] efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c
Quoted 5 lines · Ard Biesheuvel
Apologies for only spotting this now, but I seem to have given some bad advice.
efi_call_phys_prolog()inefi_64.cwill also return NULL if (!efi_enabled(EFI_OLD_MEMMAP)), but this is not an error condition. So that occurrence has to be updated: please return efi_mm.pgd instead.
Thanks for your reply, Ard. You mean that we should return efi_mm.pgd when allcoation fails? And we should delete return EFI_ABORTED on the caller site, right? In that case, how should we handle the NULL pointer returned by condition if(!efi_enabled(EFI_OLD_MEMMAP)) on the caller site?
Thanks
Gen
Re: [PATCH v2] efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c
Quoted 4 lines · Gen Zhang
Quoted 2 lines · Ard Biesheuvel
Apologies for only spotting this now, but I seem to have given some bad advice.
Quoted 9 lines
Quoted 3 lines
efi_call_phys_prolog()inefi_64.cwill also return NULL if (!efi_enabled(EFI_OLD_MEMMAP)), but this is not an error condition. So that occurrence has to be updated: please return efi_mm.pgd instead.Thanks for your reply, Ard. You mean that we should return efi_mm.pgd when allcoation fails? And we should delete return
EFI_ABORTEDon the caller site, right? In that case, how should we handle the NULL pointer returned by condition if(!efi_enabled(EFI_OLD_MEMMAP)) on the caller site?
No, the other way around. I have already updated the patch, so don't worry about it.
Re: [PATCH v2] efi_64: Fix a missing-check bug in arch/x86/platform/efi/efi_64.c
Quoted 15 lines · Ard Biesheuvel
Quoted 4 lines · Gen Zhang
Quoted 2 lines · Ard Biesheuvel
Apologies for only spotting this now, but I seem to have given some bad advice.
Quoted 9 lines
Quoted 3 lines
efi_call_phys_prolog()inefi_64.cwill also return NULL if (!efi_enabled(EFI_OLD_MEMMAP)), but this is not an error condition. So that occurrence has to be updated: please return efi_mm.pgd instead.Thanks for your reply, Ard. You mean that we should return efi_mm.pgd when allcoation fails? And we should delete return
EFI_ABORTEDon the caller site, right? In that case, how should we handle the NULL pointer returned by condition if(!efi_enabled(EFI_OLD_MEMMAP)) on the caller site?
Quoted 4 lines
No, the other way around. I have already updated the patch, so don't worry about it.
Thanks for your reply and update, Ard! That's really nice of you.
Thanks
Gen