xen/list: some fixes in list.h
v2 cover xen/list: some fixes in list.h
Patch 1 is a fix for an undefined behavior reported by Andrew. I think this patch should be considered for 4.20.
Patch 2 is fixing wrong comments in list.h I stumbled over when doing patch 1. As it is absolutely no risk involved with this patch, I think it should be 4.20 material, too.
Changes in V2:
- comments addressed
Juergen Gross (2):
xen/list: avoid UB in list iterators
xen/list: fix comments in include/xen/list.h
xen/drivers/passthrough/x86/hvm.c | 3 +- xen/include/xen/list.h | 144 ++++++++++++++++++------------ 2 files changed, 90 insertions(+), 57 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 xen/list: avoid UB in list iterators
The list_for_each_entry*() iterators are testing for having reached the end of the list in a way which relies on undefined behavior: the iterator (being a pointer to the struct of a list element) is advanced and only then tested to have reached not the next element, but the list head. This results in the list head being addressed via a list element pointer, which is undefined, in case the list elements have a higher alignment than the list head.
Avoid that by testing for the end of the list before advancing the iterator. In case of having reached the end of the list, set the iterator to NULL and use that for stopping the loop. This has the additional advantage of not leaking the iterator pointing to something which isn't a list element past the loop.
There is one case in the Xen code where the iterator is used after the loop: it is tested to be non-NULL to indicate the loop has run until reaching the end of the list. This case is modified to use the iterator being NULL for indicating the end of the list has been reached.
---
No proper Fixes: tag, as this bug predates Xen's git and mercurial history.
V2:
- fix one use case (Jan Beulich)
- let list_is_first() return bool, rename parameter (Jan Beulich)
- paranthesize iterator when used as non-NULL test (Jan Beulich)
- avoid dereferencing NULL in the safe iterators (Jan Beulich)
--- xen/drivers/passthrough/x86/hvm.c | 3 +- xen/include/xen/list.h | 110 +++++++++++++++++++----------- 2 files changed, 73 insertions(+), 40 deletions(-)
xen/drivers/passthrough/x86/hvm.c +1 -2
@@ -639,12 +639,11 @@ int pt_irq_destroy_bind( {list_del (&girq->list);xfree (girq); - girq =NULL ;break ; } } -if ( girq ) +if ( !girq ) {write_unlock (&d->event_lock);return -EINVAL ;
xen/include/xen/list.h +72 -38
182 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] xen/list: avoid UB in list iterators
Quoted 20 lines · Juergen Gross
The list_for_each_entry*() iterators are testing for having reached the end of the list in a way which relies on undefined behavior: the iterator (being a pointer to the struct of a list element) is advanced and only then tested to have reached not the next element, but the list head. This results in the list head being addressed via a list element pointer, which is undefined, in case the list elements have a higher alignment than the list head.
Avoid that by testing for the end of the list before advancing the iterator. In case of having reached the end of the list, set the iterator to NULL and use that for stopping the loop. This has the additional advantage of not leaking the iterator pointing to something which isn't a list element past the loop.
There is one case in the Xen code where the iterator is used after the loop: it is tested to be non-NULL to indicate the loop has run until reaching the end of the list. This case is modified to use the iterator being NULL for indicating the end of the list has been reached.
Quoted 3 lines
Reported-by Andrew Cooper <[email protected]>Signed-off-by Juergen Gross <[email protected]>Release-Acked-by Oleksii Kurochko <[email protected]>
I agree there's an issue here, but as said before, I do not agree with this patch.
For starters, bloat-o-meter on a random top-of-tree build says
add/remove: 8/1 grow/shrink: 112/68 up/down: 4314/-2855 (1459)
which is a horrible overhead for a case where the sequence of instructions are correct (only the C level types are a problem) and ...
Quoted 10 lines
---
No proper Fixes: tag, as this bug predates Xen's git and mercurial history.
V2:
- fix one use case (Jan Beulich)
- letlist_is_first()return bool, rename parameter (Jan Beulich)
- paranthesize iterator when used as non-NULL test (Jan Beulich)
- avoid dereferencing NULL in the safe iterators (Jan Beulich)--- xen/drivers/passthrough/x86/hvm.c | 3 +-
... the need for this adjustment being discovered after-the-fact means it's a very risky change at this juncture in the release.
~Andrew
Re: [PATCH v2 1/2] xen/list: avoid UB in list iterators
Quoted 46 lines · Andrew Cooper
Quoted 20 lines · Juergen Gross
The list_for_each_entry*() iterators are testing for having reached the end of the list in a way which relies on undefined behavior: the iterator (being a pointer to the struct of a list element) is advanced and only then tested to have reached not the next element, but the list head. This results in the list head being addressed via a list element pointer, which is undefined, in case the list elements have a higher alignment than the list head.
Avoid that by testing for the end of the list before advancing the iterator. In case of having reached the end of the list, set the iterator to NULL and use that for stopping the loop. This has the additional advantage of not leaking the iterator pointing to something which isn't a list element past the loop.
There is one case in the Xen code where the iterator is used after the loop: it is tested to be non-NULL to indicate the loop has run until reaching the end of the list. This case is modified to use the iterator being NULL for indicating the end of the list has been reached.
Quoted 3 lines
Reported-by Andrew Cooper <[email protected]>Signed-off-by Juergen Gross <[email protected]>Release-Acked-by Oleksii Kurochko <[email protected]>I agree there's an issue here, but as said before, I do not agree with this patch.
For starters, bloat-o-meter on a random top-of-tree build says
add/remove: 8/1 grow/shrink: 112/68 up/down: 4314/-2855 (1459)
which is a horrible overhead for a case where the sequence of instructions are correct (only the C level types are a problem) and ...
Quoted 10 lines
---
No proper Fixes: tag, as this bug predates Xen's git and mercurial history.
V2:
- fix one use case (Jan Beulich)
- letlist_is_first()return bool, rename parameter (Jan Beulich)
- paranthesize iterator when used as non-NULL test (Jan Beulich)
- avoid dereferencing NULL in the safe iterators (Jan Beulich)--- xen/drivers/passthrough/x86/hvm.c | 3 +-
Quoted 2 lines
... the need for this adjustment being discovered after-the-fact means it's a very risky change at this juncture in the release.
I have not reviewed the patch in enough detail to form an opinion on the approach yet. However, I want to note that I also don't think that this series should be committed at this stage of the release process. It would be better to wait for the 4.21 release cycle.
Re: [PATCH v2 1/2] xen/list: avoid UB in list iterators
Quoted 45 lines · Stefano Stabellini
Quoted 44 lines · Andrew Cooper
Quoted 20 lines · Juergen Gross
The list_for_each_entry*() iterators are testing for having reached the end of the list in a way which relies on undefined behavior: the iterator (being a pointer to the struct of a list element) is advanced and only then tested to have reached not the next element, but the list head. This results in the list head being addressed via a list element pointer, which is undefined, in case the list elements have a higher alignment than the list head.
Avoid that by testing for the end of the list before advancing the iterator. In case of having reached the end of the list, set the iterator to NULL and use that for stopping the loop. This has the additional advantage of not leaking the iterator pointing to something which isn't a list element past the loop.
There is one case in the Xen code where the iterator is used after the loop: it is tested to be non-NULL to indicate the loop has run until reaching the end of the list. This case is modified to use the iterator being NULL for indicating the end of the list has been reached.
Quoted 3 lines
Reported-by Andrew Cooper<[email protected]>Signed-off-by Juergen Gross<[email protected]>Release-Acked-by Oleksii Kurochko<[email protected]>I agree there's an issue here, but as said before, I do not agree with this patch.
For starters, bloat-o-meter on a random top-of-tree build says
add/remove: 8/1 grow/shrink: 112/68 up/down: 4314/-2855 (1459)
which is a horrible overhead for a case where the sequence of instructions are correct (only the C level types are a problem) and ...
Quoted 10 lines
---
No proper Fixes: tag, as this bug predates Xen's git and mercurial history.
V2:
- fix one use case (Jan Beulich)
- letlist_is_first()return bool, rename parameter (Jan Beulich)
- paranthesize iterator when used as non-NULL test (Jan Beulich)
- avoid dereferencing NULL in the safe iterators (Jan Beulich)--- xen/drivers/passthrough/x86/hvm.c | 3 +-
Quoted 6 lines
Quoted 2 lines
... the need for this adjustment being discovered after-the-fact means it's a very risky change at this juncture in the release.
I have not reviewed the patch in enough detail to form an opinion on the approach yet. However, I want to note that I also don't think that this series should be committed at this stage of the release process. It would be better to wait for the 4.21 release cycle.
Based on the comments above lets consider then this patch to be merged to 4.21.
Thanks.
~ Oleksii
PATCH v2 2/2 xen/list: fix comments in include/xen/list.h
In reply to Juergen GrossThere are several places in list.h where "list_struct" is used instead of "struct list_head". Fix that.
--- xen/include/xen/list.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
xen/include/xen/list.h +17 -17
@@ -395,7 +395,7 @@ static inline void list_splice_init(struct list_head *list,* list_entry - get the struct for this entry * @ptr: the &struct list_head pointer. * @type: the type of the struct this is embedded in. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. */ #define list_entry (ptr, type, member) \ container_of(ptr, type, member)
@@ -404,7 +404,7 @@ static inline void list_splice_init(struct list_head *list,* list_first_entry - get the first element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Note, that list is expected to be not empty. */
@@ -415,7 +415,7 @@ static inline void list_splice_init(struct list_head *list,* list_last_entry - get the last element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Note, that list is expected to be not empty. */
@@ -426,7 +426,7 @@ static inline void list_splice_init(struct list_head *list,* list_first_entry_or_null - get the first element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Note that if the list is empty, it returns NULL. */
@@ -437,7 +437,7 @@ static inline void list_splice_init(struct list_head *list,* list_last_entry_or_null - get the last element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Note that if the list is empty, it returns NULL. */
@@ -447,7 +447,7 @@ static inline void list_splice_init(struct list_head *list,/** * list_next_entry - get the next element in list * @pos: the type * to cursor -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. */ #define list_next_entry (pos, member) \ list_entry((pos)->member.next, typeof(*(pos)), member)
@@ -467,7 +467,7 @@ static inline void list_splice_init(struct list_head *list,/** * list_prev_entry - get the prev element in list * @pos: the type * to cursor -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. */ #define list_prev_entry (pos, member) \ list_entry((pos)->member.prev, typeof(*(pos)), member)
@@ -525,7 +525,7 @@ static inline void list_splice_init(struct list_head *list,* list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. */ #define list_for_each_entry (pos, head, member) \ for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \
@@ -536,7 +536,7 @@ static inline void list_splice_init(struct list_head *list,* list_for_each_entry_reverse - iterate backwards over list of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. */ #define list_for_each_entry_reverse (pos, head, member) \ for ( (pos) = list_last_entry_or_null(head, typeof(*(pos)), member); \
@@ -548,7 +548,7 @@ static inline void list_splice_init(struct list_head *list,* list_for_each_entry_continue * @pos: the type * to use as a start point * @head: the head of the list -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Prepares a pos entry for use as a start point in * list_for_each_entry_continue.
@@ -560,7 +560,7 @@ static inline void list_splice_init(struct list_head *list,* list_for_each_entry_continue - continue iteration over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Continue to iterate over list of given type, continuing after * the current position.
@@ -575,7 +575,7 @@ static inline void list_splice_init(struct list_head *list,* current point * @pos: the type * to use as a loop cursor. * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Iterate over list of given type, continuing from current position. */
@@ -588,7 +588,7 @@ static inline void list_splice_init(struct list_head *list,* @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. */ #define list_for_each_entry_safe (pos, n, head, member) \ for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \
@@ -601,7 +601,7 @@ static inline void list_splice_init(struct list_head *list,* @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Iterate over list of given type, continuing after current point, * safe against removal of list entry.
@@ -617,7 +617,7 @@ static inline void list_splice_init(struct list_head *list,* @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Iterate over list of given type from current point, safe against * removal of list entry.
@@ -632,7 +632,7 @@ static inline void list_splice_init(struct list_head *list,* @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * Iterate backwards over list of given type, safe against removal * of list entry.
@@ -683,7 +683,7 @@ static inline void list_splice_init(struct list_head *list,* list_for_each_entry_rcu - iterate over rcu list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. -* @member: the name of the list_struct within the struct. +* @member: the name of the struct list_head within the struct. * * This list-traversal primitive may safely run concurrently with * the _rcu list-mutation primitives such as list_add_rcu()
Signature · 1 line
2.43.0