Re: [PATCH] efi: fix for_each_efi_memory_desc_in_map() for empty memmaps

From: Mark Salter
Date: Wed May 25 2016 - 17:00:38 EST


On Wed, 2016-05-25 at 21:48 +0100, Matt Fleming wrote:
> (Cc'ing Mark, the original author)
>
> On Wed, 25 May, at 04:36:55PM, Vitaly Kuznetsov wrote:
> >
> > Commit 78ce248faa3c ("efi: Iterate over efi.memmap in
> > for_each_efi_memory_desc()") introduced a regression for systems booted
> > with 'noefi' kernel option. In particular, I observe early kernel hang in
> > efi_find_mirror() on for_each_efi_memory_desc() call. As we don't have
> > efi memmap we enter this iterator with the following parameters:
> >
> > efi.memmap.map = 0, efi.memmap.map_end = 0, efi.memmap.desc_size = 28
> >
> > for_each_efi_memory_desc_in_map() does the following comparison:
> >
> > (md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size);
> >
> > where md = 0, (m)->map_end = 0 and (m)->desc_size = 28 but when we subtract
> > something from a NULL pointer wrap around happens and we end up returning
> > invalid pointer.
> >
> > Fixes: 78ce248faa3c ("efi: Iterate over efi.memmap in for_each_efi_memory_desc()")
> > Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
> > ---
> > Âinclude/linux/efi.h | 3 ++-
> > Â1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/efi.h b/include/linux/efi.h
> > index c2db3ca..229ccb5 100644
> > --- a/include/linux/efi.h
> > +++ b/include/linux/efi.h
> > @@ -1005,7 +1005,8 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm,
> > Â/* Iterate through an efi_memory_map */
> > Â#define for_each_efi_memory_desc_in_map(m, md) ÂÂÂ\
> > Â for ((md) = (m)->map; ÂÂÂ\
> > - ÂÂÂÂÂ(md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
> > + ÂÂÂÂÂ(efi_memory_desc_t *)((md) + (m)->desc_size) <= ÂÂÂ\
> > + ÂÂÂÂÂ(efi_memory_desc_t *)(m)->map_end; ÂÂÂ\
> > Â ÂÂÂÂÂ(md) = (void *)(md) + (m)->desc_size)
> Curse C type casting!
>
> You're adding m->desc_size to a (efi_memory_desc_t *) which is 40
> bytes. You need the below to avoid breaking regular EFI boot.
>
> But yeah, this looks like a fine fix. Mark, any concerns?

No concerns. It looks like the right thing to do.

>
> ---
>
> diff --git a/include/linux/efi.h b/include/linux/efi.h
> index d8ab480b1089..3a3f8d8bd9be 100644
> --- a/include/linux/efi.h
> +++ b/include/linux/efi.h
> @@ -1026,7 +1026,7 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm,
> Â/* Iterate through an efi_memory_map */
> Â#define for_each_efi_memory_desc_in_map(m, md) ÂÂÂ\
> Â for ((md) = (m)->map; ÂÂÂ\
> - ÂÂÂÂÂ(md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
> + ÂÂÂÂÂ((void *)(md) + (m)->desc_size) <= (m)->map_end; ÂÂÂ\
> Â ÂÂÂÂÂ(md) = (void *)(md) + (m)->desc_size)
> Â
> Â/**