* [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen
@ 2016-08-09 10:16 Jan Beulich
[not found] ` <57A9C9A60200007800104268-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
0 siblings, 1 reply; 7+ messages in thread
From: Jan Beulich @ 2016-08-09 10:16 UTC (permalink / raw)
To: matt-mF/unelCI9GS6iBeEJttW/XRex20P6io
Cc: Vitaly Kuznetsov, Jiri Slaby, linux-efi-u79uwXL29TY76Z2rM5mHXA
While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map()
for empty memmaps") made an attempt to deal with empty memory maps, it
didn't address the case where the desc_size field never gets set, as is
apparently the case when running under Xen.
Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org>
Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org>
Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org>
Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org>
---
include/linux/efi.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
--- 4.8-rc1/include/linux/efi.h
+++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h
@@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions
/* Iterate through an efi_memory_map */
#define for_each_efi_memory_desc_in_map(m, md) \
for ((md) = (m)->map; \
- ((void *)(md) + (m)->desc_size) <= (m)->map_end; \
+ ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \
(md) = (void *)(md) + (m)->desc_size)
/**
^ permalink raw reply [flat|nested] 7+ messages in thread[parent not found: <57A9C9A60200007800104268-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>]
* Re: [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen [not found] ` <57A9C9A60200007800104268-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org> @ 2016-08-09 13:03 ` Jiri Slaby [not found] ` <c5dc2cae-1189-f2dd-6c6b-2c310a9d829f-AlSwsSmVLrQ@public.gmane.org> 0 siblings, 1 reply; 7+ messages in thread From: Jiri Slaby @ 2016-08-09 13:03 UTC (permalink / raw) To: Jan Beulich, matt-mF/unelCI9GS6iBeEJttW/XRex20P6io Cc: Vitaly Kuznetsov, linux-efi-u79uwXL29TY76Z2rM5mHXA On 08/09/2016, 12:16 PM, Jan Beulich wrote: > While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map() > for empty memmaps") made an attempt to deal with empty memory maps, it > didn't address the case where the desc_size field never gets set, as is > apparently the case when running under Xen. > > Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> > Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> > Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org> > Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org> > Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> > --- > include/linux/efi.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > --- 4.8-rc1/include/linux/efi.h > +++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h > @@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions > /* Iterate through an efi_memory_map */ > #define for_each_efi_memory_desc_in_map(m, md) \ > for ((md) = (m)->map; \ > - ((void *)(md) + (m)->desc_size) <= (m)->map_end; \ > + ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \ Is there any specific reason you change both the size and the comparator? IMO, either (readable) ((void *)(md) + (m)->desc_size) < (m)->map_end; or (mindfuck version) ((void *)(md) + (m)->desc_size - 1) <= (m)->map_end; is correct, not their mix. thanks, -- js suse labs ^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <c5dc2cae-1189-f2dd-6c6b-2c310a9d829f-AlSwsSmVLrQ@public.gmane.org>]
* Re: [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen [not found] ` <c5dc2cae-1189-f2dd-6c6b-2c310a9d829f-AlSwsSmVLrQ@public.gmane.org> @ 2016-08-09 13:39 ` Jan Beulich [not found] ` <57A9F94E0200007800104477-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org> 0 siblings, 1 reply; 7+ messages in thread From: Jan Beulich @ 2016-08-09 13:39 UTC (permalink / raw) To: Jiri Slaby Cc: matt-mF/unelCI9GS6iBeEJttW/XRex20P6io, Vitaly Kuznetsov, linux-efi-u79uwXL29TY76Z2rM5mHXA >>> On 09.08.16 at 15:03, <jslaby-AlSwsSmVLrQ@public.gmane.org> wrote: > On 08/09/2016, 12:16 PM, Jan Beulich wrote: >> While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map() >> for empty memmaps") made an attempt to deal with empty memory maps, it >> didn't address the case where the desc_size field never gets set, as is >> apparently the case when running under Xen. >> >> Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >> Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> >> Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org> >> Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org> >> Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >> --- >> include/linux/efi.h | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> --- 4.8-rc1/include/linux/efi.h >> +++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h >> @@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions >> /* Iterate through an efi_memory_map */ >> #define for_each_efi_memory_desc_in_map(m, md) \ >> for ((md) = (m)->map; \ >> - ((void *)(md) + (m)->desc_size) <= (m)->map_end; \ >> + ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \ > > Is there any specific reason you change both the size and the comparator? > > IMO, either (readable) > ((void *)(md) + (m)->desc_size) < (m)->map_end; > or (mindfuck version) > ((void *)(md) + (m)->desc_size - 1) <= (m)->map_end; > is correct, not their mix. We're not talking about an off-by-one getting fixed here: map_end points past the valid range. The adjustment leverages overflow (or underflow, to be precise) to produce correct behavior when (m)->desc_size is zero. Jan ^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <57A9F94E0200007800104477-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>]
* Re: [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen [not found] ` <57A9F94E0200007800104477-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org> @ 2016-08-09 13:49 ` Mark Rutland 2016-08-09 14:07 ` Jan Beulich 2016-08-09 13:51 ` Jiri Slaby 1 sibling, 1 reply; 7+ messages in thread From: Mark Rutland @ 2016-08-09 13:49 UTC (permalink / raw) To: Jan Beulich Cc: Jiri Slaby, matt-mF/unelCI9GS6iBeEJttW/XRex20P6io, Vitaly Kuznetsov, linux-efi-u79uwXL29TY76Z2rM5mHXA On Tue, Aug 09, 2016 at 07:39:58AM -0600, Jan Beulich wrote: > >>> On 09.08.16 at 15:03, <jslaby-AlSwsSmVLrQ@public.gmane.org> wrote: > > On 08/09/2016, 12:16 PM, Jan Beulich wrote: > >> While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map() > >> for empty memmaps") made an attempt to deal with empty memory maps, it > >> didn't address the case where the desc_size field never gets set, as is > >> apparently the case when running under Xen. > >> > >> Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> > >> Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> > >> Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org> > >> Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org> > >> Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> > >> --- > >> include/linux/efi.h | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> --- 4.8-rc1/include/linux/efi.h > >> +++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h > >> @@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions > >> /* Iterate through an efi_memory_map */ > >> #define for_each_efi_memory_desc_in_map(m, md) \ > >> for ((md) = (m)->map; \ > >> - ((void *)(md) + (m)->desc_size) <= (m)->map_end; \ > >> + ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \ > > > > Is there any specific reason you change both the size and the comparator? > > > > IMO, either (readable) > > ((void *)(md) + (m)->desc_size) < (m)->map_end; > > or (mindfuck version) > > ((void *)(md) + (m)->desc_size - 1) <= (m)->map_end; > > is correct, not their mix. > > We're not talking about an off-by-one getting fixed here: map_end > points past the valid range. The adjustment leverages overflow (or > underflow, to be precise) to produce correct behavior when > (m)->desc_size is zero. That deserves a comment in the code, given the Xen case is not particularly obvious, and it's caught out at least one reviewer. What is map_end initialised to in the Xen case? Is that Xen with an empty map, of Xen generally? Thanks, Mark. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen 2016-08-09 13:49 ` Mark Rutland @ 2016-08-09 14:07 ` Jan Beulich 0 siblings, 0 replies; 7+ messages in thread From: Jan Beulich @ 2016-08-09 14:07 UTC (permalink / raw) To: Mark Rutland Cc: matt-mF/unelCI9GS6iBeEJttW/XRex20P6io, Vitaly Kuznetsov, Jiri Slaby, linux-efi-u79uwXL29TY76Z2rM5mHXA >>> On 09.08.16 at 15:49, <mark.rutland-5wv7dgnIgG8@public.gmane.org> wrote: > On Tue, Aug 09, 2016 at 07:39:58AM -0600, Jan Beulich wrote: >> >>> On 09.08.16 at 15:03, <jslaby-AlSwsSmVLrQ@public.gmane.org> wrote: >> > On 08/09/2016, 12:16 PM, Jan Beulich wrote: >> >> While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map() >> >> for empty memmaps") made an attempt to deal with empty memory maps, it >> >> didn't address the case where the desc_size field never gets set, as is >> >> apparently the case when running under Xen. >> >> >> >> Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >> >> Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> >> >> Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org> >> >> Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org> >> >> Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >> >> --- >> >> include/linux/efi.h | 2 +- >> >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> >> >> --- 4.8-rc1/include/linux/efi.h >> >> +++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h >> >> @@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions >> >> /* Iterate through an efi_memory_map */ >> >> #define for_each_efi_memory_desc_in_map(m, md) \ >> >> for ((md) = (m)->map; \ >> >> - ((void *)(md) + (m)->desc_size) <= (m)->map_end; \ >> >> + ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \ >> > >> > Is there any specific reason you change both the size and the comparator? >> > >> > IMO, either (readable) >> > ((void *)(md) + (m)->desc_size) < (m)->map_end; >> > or (mindfuck version) >> > ((void *)(md) + (m)->desc_size - 1) <= (m)->map_end; >> > is correct, not their mix. >> >> We're not talking about an off-by-one getting fixed here: map_end >> points past the valid range. The adjustment leverages overflow (or >> underflow, to be precise) to produce correct behavior when >> (m)->desc_size is zero. > > That deserves a comment in the code, given the Xen case is not > particularly obvious, and it's caught out at least one reviewer. > > What is map_end initialised to in the Xen case? Is that Xen with an > empty map, of Xen generally? All fields are zero under Xen, as the raw EFI memory map doesn't get exposed to the Dom0 kernel. Jan ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen [not found] ` <57A9F94E0200007800104477-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org> 2016-08-09 13:49 ` Mark Rutland @ 2016-08-09 13:51 ` Jiri Slaby [not found] ` <97e96c16-09dc-773f-3beb-4fefc589921a-AlSwsSmVLrQ@public.gmane.org> 1 sibling, 1 reply; 7+ messages in thread From: Jiri Slaby @ 2016-08-09 13:51 UTC (permalink / raw) To: Jan Beulich Cc: matt-mF/unelCI9GS6iBeEJttW/XRex20P6io, Vitaly Kuznetsov, linux-efi-u79uwXL29TY76Z2rM5mHXA On 08/09/2016, 03:39 PM, Jan Beulich wrote: >>>> On 09.08.16 at 15:03, <jslaby-AlSwsSmVLrQ@public.gmane.org> wrote: >> On 08/09/2016, 12:16 PM, Jan Beulich wrote: >>> While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map() >>> for empty memmaps") made an attempt to deal with empty memory maps, it >>> didn't address the case where the desc_size field never gets set, as is >>> apparently the case when running under Xen. >>> >>> Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >>> Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> >>> Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org> >>> Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org> >>> Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >>> --- >>> include/linux/efi.h | 2 +- >>> 1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> --- 4.8-rc1/include/linux/efi.h >>> +++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h >>> @@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions >>> /* Iterate through an efi_memory_map */ >>> #define for_each_efi_memory_desc_in_map(m, md) \ >>> for ((md) = (m)->map; \ >>> - ((void *)(md) + (m)->desc_size) <= (m)->map_end; \ >>> + ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \ >> >> Is there any specific reason you change both the size and the comparator? >> >> IMO, either (readable) >> ((void *)(md) + (m)->desc_size) < (m)->map_end; >> or (mindfuck version) >> ((void *)(md) + (m)->desc_size - 1) <= (m)->map_end; >> is correct, not their mix. > > We're not talking about an off-by-one getting fixed here: map_end > points past the valid range. The adjustment leverages overflow (or > underflow, to be precise) to produce correct behavior when > (m)->desc_size is zero. And what would be map_end in case desc_size is zero so that the test fails? Anyway, if I understand correctly, you subtract one from the pointer. Note that behaviour of this is undefined according to the C standard and may result in unpredictable results. Pointer can only point inside an object (or array) or one element *past* the end, not before. thanks, -- js suse labs ^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <97e96c16-09dc-773f-3beb-4fefc589921a-AlSwsSmVLrQ@public.gmane.org>]
* Re: [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen [not found] ` <97e96c16-09dc-773f-3beb-4fefc589921a-AlSwsSmVLrQ@public.gmane.org> @ 2016-08-09 14:09 ` Jan Beulich 0 siblings, 0 replies; 7+ messages in thread From: Jan Beulich @ 2016-08-09 14:09 UTC (permalink / raw) To: Jiri Slaby Cc: matt-mF/unelCI9GS6iBeEJttW/XRex20P6io, Vitaly Kuznetsov, linux-efi-u79uwXL29TY76Z2rM5mHXA >>> On 09.08.16 at 15:51, <jslaby-AlSwsSmVLrQ@public.gmane.org> wrote: > On 08/09/2016, 03:39 PM, Jan Beulich wrote: >>>>> On 09.08.16 at 15:03, <jslaby-AlSwsSmVLrQ@public.gmane.org> wrote: >>> On 08/09/2016, 12:16 PM, Jan Beulich wrote: >>>> While commit 55f1ea15216 ("efi: Fix for_each_efi_memory_desc_in_map() >>>> for empty memmaps") made an attempt to deal with empty memory maps, it >>>> didn't address the case where the desc_size field never gets set, as is >>>> apparently the case when running under Xen. >>>> >>>> Reported-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >>>> Cc: Vitaly Kuznetsov <vkuznets-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> >>>> Cc: Jiri Slaby <jslaby-AlSwsSmVLrQ@public.gmane.org> >>>> Signed-off-by: Jan Beulich <jbeulich-IBi9RG/b67k@public.gmane.org> >>>> Tested-by: <lists-Ilq5uHa1fWNWk0Htik3J/w@public.gmane.org> >>>> --- >>>> include/linux/efi.h | 2 +- >>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>> >>>> --- 4.8-rc1/include/linux/efi.h >>>> +++ 4.8-rc1-EFI-memdesc-iterator-Xen/include/linux/efi.h >>>> @@ -946,7 +946,7 @@ extern int efi_memattr_apply_permissions >>>> /* Iterate through an efi_memory_map */ >>>> #define for_each_efi_memory_desc_in_map(m, md) \ >>>> for ((md) = (m)->map; \ >>>> - ((void *)(md) + (m)->desc_size) <= (m)->map_end; \ >>>> + ((void *)(md) + (m)->desc_size - 1) < (m)->map_end; \ >>> >>> Is there any specific reason you change both the size and the comparator? >>> >>> IMO, either (readable) >>> ((void *)(md) + (m)->desc_size) < (m)->map_end; >>> or (mindfuck version) >>> ((void *)(md) + (m)->desc_size - 1) <= (m)->map_end; >>> is correct, not their mix. >> >> We're not talking about an off-by-one getting fixed here: map_end >> points past the valid range. The adjustment leverages overflow (or >> underflow, to be precise) to produce correct behavior when >> (m)->desc_size is zero. > > And what would be map_end in case desc_size is zero so that the test fails? Zero. > Anyway, if I understand correctly, you subtract one from the pointer. > Note that behaviour of this is undefined according to the C standard and > may result in unpredictable results. Pointer can only point inside an > object (or array) or one element *past* the end, not before. Considering an object spanning the entire address space, I'm not sure there's anything really undefined here. But of course the whole arithmetic could instead be done with unsigned longs (using respective casts). Jan ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2016-08-09 14:09 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-09 10:16 [PATCH] EFI: make for_each_efi_memory_desc_in_map() cope with running on Xen Jan Beulich
[not found] ` <57A9C9A60200007800104268-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
2016-08-09 13:03 ` Jiri Slaby
[not found] ` <c5dc2cae-1189-f2dd-6c6b-2c310a9d829f-AlSwsSmVLrQ@public.gmane.org>
2016-08-09 13:39 ` Jan Beulich
[not found] ` <57A9F94E0200007800104477-rw/UEucdPrvD8XXLLHKrIiOjQekVJEpY@public.gmane.org>
2016-08-09 13:49 ` Mark Rutland
2016-08-09 14:07 ` Jan Beulich
2016-08-09 13:51 ` Jiri Slaby
[not found] ` <97e96c16-09dc-773f-3beb-4fefc589921a-AlSwsSmVLrQ@public.gmane.org>
2016-08-09 14:09 ` Jan Beulich
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox