xen/list: some fixes in list.h
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.
There are some additional cleanups possible in list.h, which I can do for 4.21 when wanted:
- Removal of list_prepare_entry(), which seems to be unused since some time now (and it seems to be thought of as a list.h internal macro only).
- More questionable: removal of unused iterators, like e.g. list_for_each_entry_continue() or list_for_each_entry_from(). The main idea to keep list.h in sync with the Linux version would be violated by this removal, though. OTOH with patch 1 they are out of sync anyway now, but I'm planning to submit a Linux kernel patch fixing the UB in the Linux variant, too.
Juergen Gross (2):
xen/list: avoid UB in list iterators
xen/list: fix comments in include/xen/list.h
xen/include/xen/list.h | 144 +++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 55 deletions(-)
Signature · 1 line
2.43.0
PATCH 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 then 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.
---
No proper Fixes: tag, as this bug predates Xen's git and mercurial history.
--- xen/include/xen/list.h | 110 +++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 38 deletions(-)
xen/include/xen/list.h +72 -38
@@ -291,6 +291,17 @@ static inline void list_move_tail(struct list_head *list,list_add_tail (list, head); } +/** +* list_is_first - tests whether @list is the first entry in list @head +* @list: the entry to test +* @head: the head of the list +*/ +static inline int list_is_first (const struct list_head *list, +const struct list_head *head) +{ +return list->prev == head; +} +/** * list_is_last - tests whether @list is the last entry in list @head * @list: the entry to test
@@ -440,7 +451,19 @@ static inline void list_splice_init(struct list_head *list,*/ #define list_next_entry (pos, member) \ list_entry((pos)->member.next, typeof(*(pos)), member) - + +/** +* list_next_entry_or_null - get the next element in list +* @pos: the type * to cursor +* @member: the name of the struct list_head within the struct. +* +* Note that if the end of the list is reached, it returns NULL. +*/ +#define list_next_entry_or_null (head, pos, member) \ + list_is_last(&(pos)->member, head) \ + ? NULL \ + : list_entry((pos)->member.next, typeof(*(pos)), member) +/** * list_prev_entry - get the prev element in list * @pos: the type * to cursor
@@ -449,6 +472,18 @@ static inline void list_splice_init(struct list_head *list,#define list_prev_entry (pos, member) \ list_entry((pos)->member.prev, typeof(*(pos)), member) +/** +* list_prev_entry_or_null - get the prev element in list +* @pos: the type * to cursor +* @member: the name of the struct list_head within the struct. +* +* Note that if the start of the list is reached, it returns NULL. +*/ +#define list_prev_entry_or_null (head, pos, member) \ + list_is_first(&(pos)->member, head) \ + ? NULL \ + : list_entry((pos)->member.prev, typeof(*(pos)), member) +/** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor.
@@ -492,10 +527,10 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + pos; \ + (pos) = list_next_entry_or_null(head, pos, member) )/** * list_for_each_entry_reverse - iterate backwards over list of given type.
@@ -503,10 +538,10 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_reverse (pos, head, member) \ - for ((pos) = list_entry((head)->prev, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = list_entry((pos)->member.prev, typeof(*(pos)), member)) +#define list_for_each_entry_reverse (pos, head, member) \ + for ( (pos) = list_last_entry_or_null(head, typeof(*(pos)), member); \ + pos; \ + (pos) = list_prev_entry_or_null(head, pos, member) )/** * list_prepare_entry - prepare a pos entry for use in
@@ -530,10 +565,10 @@ static inline void list_splice_init(struct list_head *list,* Continue to iterate over list of given type, continuing after * the current position. */ -#define list_for_each_entry_continue (pos, head, member) \ - for ((pos) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_continue (pos, head, member) \ + for ( (pos) = list_next_entry_or_null(head, pos, member); \ + pos; \ + (pos) = list_next_entry_or_null(head, pos, member) )/** * list_for_each_entry_from - iterate over list of given type from the
@@ -544,9 +579,8 @@ static inline void list_splice_init(struct list_head *list,* * Iterate over list of given type, continuing from current position. */ -#define list_for_each_entry_from (pos, head, member) \ - for (; &(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_from (pos, head, member) \ + for ( ; pos; (pos) = list_next_entry_or_null(head, pos, member) )/** * list_for_each_entry_safe - iterate over list of given type safe
@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \ + pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )/** * list_for_each_entry_safe_continue
@@ -572,11 +606,11 @@ static inline void list_splice_init(struct list_head *list,* Iterate over list of given type, continuing after current point, * safe against removal of list entry. */ -#define list_for_each_entry_safe_continue (pos, n, head, member) \ - for ((pos) = list_entry((pos)->member.next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe_continue (pos, n, head, member) \ + for ( (pos) = list_next_entry_or_null(head, pos, member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \ + pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )/** * list_for_each_entry_safe_from
@@ -589,9 +623,9 @@ static inline void list_splice_init(struct list_head *list,* removal of list entry. */ #define list_for_each_entry_safe_from (pos, n, head, member) \ - for ((n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) + for ( (n) = list_next_entry_or_null(head, pos, member); \ + pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )/** * list_for_each_entry_safe_reverse
@@ -603,11 +637,11 @@ static inline void list_splice_init(struct list_head *list,* Iterate backwards over list of given type, safe against removal * of list entry. */ -#define list_for_each_entry_safe_reverse (pos, n, head, member) \ - for ((pos) = list_entry((head)->prev, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.prev, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.prev, typeof(*(n)), member)) +#define list_for_each_entry_safe_reverse (pos, n, head, member) \ + for ( (pos) = list_last_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_prev_entry_or_null(head, pos, member) : NULL; \ + pos; \ + (pos) = (n), (n) = list_prev_entry_or_null(head, n, member) )/** * list_for_each_rcu - iterate over an rcu-protected list
@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )/** * list_for_each_continue_rcu
Signature · 1 line
2.43.0
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
Quoted 7 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 then the list head.
Nit: s/then/than/
xen/include/xen/list.h static inline void list_move_tail(struct list_head *list, · line 299
Quoted 12 lines
xen/include/xen/list.h +6
-0@@ -291,6 +291,17 @@ static inline void list_move_tail(struct list_head *list,list_add_tail (list, head); } +/** +* list_is_first - tests whether @list is the first entry in list @head +* @list: the entry to test +* @head: the head of the list +*/ +static inline int list_is_first(const struct list_head *list,
bool?
xen/include/xen/list.h static inline void list_move_tail(struct list_head *list, · line 303
Quoted 4 lines
xen/include/xen/list.h +4
-0+ const struct list_head *head) +{ +return list->prev == head; +}
"list" is ambiguous, as it could also mean the start of the list. Maybe better "entry"? (I understand that'll be inconsistent with the subsequent list_is_last(), but what do you do.)
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 458
Quoted 10 lines
xen/include/xen/list.h +5
-1@@ -440,7 +451,19 @@ static inline void list_splice_init(struct list_head *list,*/ #define list_next_entry (pos, member) \ list_entry((pos)->member.next, typeof(*(pos)), member) - + +/** +* list_next_entry_or_null - get the next element in list +* @pos: the type * to cursor +* @member: the name of the struct list_head within the struct.
Nit: Stray 2nd blank before "within"
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 532
Quoted 11 lines
xen/include/xen/list.h +3
-4@@ -492,10 +527,10 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + pos; \
I suspect Misra would demand pos to be parenthesized here (and in similar places elsewhere), too.
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 595
Quoted 12 lines
xen/include/xen/list.h +3
-5@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \
n can end up being NULL here even if pos isn't. Then ...
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 597
Quoted 2 lines
xen/include/xen/list.h +2
-0+pos ; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )
... you can't use list_next_entry_or_null() on it.
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 695
Quoted 12 lines
xen/include/xen/list.h +4
-4@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )
Don't you need a list_next_entry_or_null_rcu() flavor here, using rcu_dereference() on the passed in pos for the (pos)->member.next deref?
Question on the patch as a whole: Since I have a vague recollection that we may have a use or two of one of these iterator macros which actually make assumptions on the state of pos upon loop exit, did you audit all use sites?
Jan
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
Quoted 10 lines · Jan Beulich
Quoted 7 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 then the list head.
Nit: s/then/than/
Oh, of course.
Quoted 7 lines
Quoted 7 lines
xen/include/xen/list.h +0
-0@@ -291,6 +291,17 @@ static inline void list_move_tail(struct list_head *list,list_add_tail (list, head); }
Quoted 8 lines
xen/include/xen/list.h static inline void list_move_tail(struct list_head *list, · line 299
Quoted 6 lines
xen/include/xen/list.h +6
-0+/** +* list_is_first - tests whether @list is the first entry in list @head +* @list: the entry to test +* @head: the head of the list +*/ +static inline int list_is_first(const struct list_head *list,bool?
Fine with me, guessing that you'd accept the deviation from list_is_last().
Quoted 9 lines
xen/include/xen/list.h static inline void list_move_tail(struct list_head *list, · line 303
Quoted 5 lines
xen/include/xen/list.h +4
-0+ const struct list_head *head) +{ +return list->prev == head; +}"list" is ambiguous, as it could also mean the start of the list. Maybe better "entry"? (I understand that'll be inconsistent with the subsequent
list_is_last(), but what do you do.)
Okay.
Quoted 13 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 458
Quoted 11 lines
xen/include/xen/list.h +5
-1@@ -440,7 +451,19 @@ static inline void list_splice_init(struct list_head *list,*/ #define list_next_entry (pos, member) \ list_entry((pos)->member.next, typeof(*(pos)), member) - + +/** +* list_next_entry_or_null - get the next element in list +* @pos: the type * to cursor +* @member: the name of the struct list_head within the struct. Nit: Stray 2nd blank before "within"
Thanks for noticing.
Quoted 15 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 532
Quoted 12 lines
xen/include/xen/list.h +3
-4@@ -492,10 +527,10 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + pos; \I suspect Misra would demand pos to be parenthesized here (and in similar places elsewhere), too.
I don't mind.
Quoted 16 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 595
Quoted 13 lines
xen/include/xen/list.h +3
-5@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \n can end up being NULL here even if pos isn't. Then ...
Quoted 4 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 597
Quoted 2 lines
xen/include/xen/list.h +2
-0+pos ; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )... you can't use
list_next_entry_or_null()on it.
Ah, indeed.
What would you prefer? Handling that in the *_safe() iterator macros, or allowing the *_entry_or_null() macros to be called with a NULL parameter?
Quoted 16 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 695
Quoted 13 lines
xen/include/xen/list.h +4
-4@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \ - (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )Don't you need a
list_next_entry_or_null_rcu()flavor here, usingrcu_dereference()on the passed in pos for the (pos)->member.next deref?
Isn't the "rcu_dereference(pos);" all what is needed for the current iteration? Otherwise today's implementation would suffer from the same problem IMHO.
Quoted 3 lines
Question on the patch as a whole: Since I have a vague recollection that we may have a use or two of one of these iterator macros which actually make assumptions on the state of pos upon loop exit, did you audit all use sites?
No, I didn't. I'm doing it right now.
Found 1 case up to now.
Juergen
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
Quoted 20 lines · Juergen Gross
Quoted 19 lines · Jan Beulich
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 595
Quoted 12 lines · Juergen Gross
xen/include/xen/list.h +3
-5@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \n can end up being NULL here even if pos isn't. Then ...
Quoted 2 lines
(quoted diff) +2
-0+ pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )
Quoted 6 lines
Quoted 1 line
... you can't use
list_next_entry_or_null()on it.Ah, indeed.
What would you prefer? Handling that in the
*_safe()iterator macros, or allowing the*_entry_or_null()macros to be called with a NULL parameter?
I'd prefer the former, but I probably wouldn't mind the latter.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/include/xen/list.h +0
-3@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \Quoted 8 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 695
Quoted 5 lines
xen/include/xen/list.h +4
-1- (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )Don't you need a
list_next_entry_or_null_rcu()flavor here, usingrcu_dereference()on the passed in pos for the (pos)->member.next deref?
Quoted 1 line
Isn't the "
rcu_dereference(pos);" all what is needed for the current iteration?
Reading Linux'es rcu_dereference.rst, my understanding is that one of them would suffice if then we used its result rather than the original pointer. Then again RCU has been somewhat opaque to me for all the years ...
Quoted 1 line
Otherwise today's implementation would suffer from the same problem IMHO.
That's the impression I'm (now) having.
Jan
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
Quoted 48 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
Quoted 19 lines · Jan Beulich
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 595
Quoted 12 lines · Juergen Gross
xen/include/xen/list.h +3
-5@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \n can end up being NULL here even if pos isn't. Then ...
Quoted 2 lines
(quoted diff) +2
-0+ pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )Quoted 6 lines
Quoted 1 line
... you can't use
list_next_entry_or_null()on it.Ah, indeed.
What would you prefer? Handling that in the
*_safe()iterator macros, or allowing the*_entry_or_null()macros to be called with a NULL parameter?I'd prefer the former, but I probably wouldn't mind the latter.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/include/xen/list.h +0
-3@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \Quoted 8 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 695
Quoted 5 lines
xen/include/xen/list.h +4
-1- (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )Don't you need a
list_next_entry_or_null_rcu()flavor here, usingrcu_dereference()on the passed in pos for the (pos)->member.next deref?Quoted 1 line
Isn't the "
rcu_dereference(pos);" all what is needed for the current iteration?
Quoted 7 lines
Reading Linux'es
rcu_dereference.rst, my understanding is that one of them would suffice if then we used its result rather than the original pointer. Then again RCU has been somewhat opaque to me for all the years ...Quoted 1 line
Otherwise today's implementation would suffer from the same problem IMHO.
That's the impression I'm (now) having.
The rcu_dereference() is basically just for documentation purposes. If needed by an arch, it can add barriers, too, but according to the comments those would be needed on alpha only. The returned value is always the original pointer value. See the comment block in xen/include/xen/rcupdate.h
Juergen
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
Quoted 53 lines · Jürgen Groß
Quoted 52 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
Quoted 19 lines · Jan Beulich
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 595
Quoted 12 lines · Juergen Gross
xen/include/xen/list.h +3
-5@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \n can end up being NULL here even if pos isn't. Then ...
Quoted 2 lines
(quoted diff) +2
-0+ pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )Quoted 6 lines
Quoted 1 line
... you can't use
list_next_entry_or_null()on it.Ah, indeed.
What would you prefer? Handling that in the
*_safe()iterator macros, or allowing the*_entry_or_null()macros to be called with a NULL parameter?I'd prefer the former, but I probably wouldn't mind the latter.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/include/xen/list.h +0
-3@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \Quoted 8 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 695
Quoted 5 lines
xen/include/xen/list.h +4
-1- (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )Don't you need a
list_next_entry_or_null_rcu()flavor here, usingrcu_dereference()on the passed in pos for the (pos)->member.next deref?Quoted 1 line
Isn't the "
rcu_dereference(pos);" all what is needed for the current iteration?Reading Linux'es
rcu_dereference.rst, my understanding is that one of them would suffice if then we used its result rather than the original pointer. Then again RCU has been somewhat opaque to me for all the years ...
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Otherwise today's implementation would suffer from the same problem IMHO.
That's the impression I'm (now) having.
The
rcu_dereference()is basically just for documentation purposes. If needed by an arch, it can add barriers, too, but according to the comments those would be needed on alpha only. The returned value is always the original pointer value. See the comment block inxen/include/xen/rcupdate.h
Note the "This pointer may later be safely dereferenced" in there. As said, we'd be fine if we used the return value instead of the original "pos". Yet we don't. We effectively assume that the macro expands to just the passed in pointer, with no barriers or anything else.
Anyway, since - as you validly say - this is a pre-existing issue, let's put it on the side right here.
Jan
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
Quoted 63 lines · Jan Beulich
Quoted 53 lines · Jürgen Groß
Quoted 52 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
Quoted 19 lines · Jan Beulich
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 595
Quoted 12 lines · Juergen Gross
xen/include/xen/list.h +3
-5@@ -556,11 +590,11 @@ static inline void list_splice_init(struct list_head *list,* @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe (pos, n, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member), \ - (n) = list_entry((pos)->member.next, typeof(*(pos)), member); \ - &(pos)->member != (head); \ - (pos) = (n), (n) = list_entry((n)->member.next, typeof(*(n)), member)) +#define list_for_each_entry_safe (pos, n, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member), \ + (n) = (pos) ? list_next_entry_or_null(head, pos, member) : NULL; \n can end up being NULL here even if pos isn't. Then ...
Quoted 2 lines
(quoted diff) +2
-0+ pos; \ + (pos) = (n), (n) = list_next_entry_or_null(head, n, member) )Quoted 6 lines
Quoted 1 line
... you can't use
list_next_entry_or_null()on it.Ah, indeed.
What would you prefer? Handling that in the
*_safe()iterator macros, or allowing the*_entry_or_null()macros to be called with a NULL parameter?I'd prefer the former, but I probably wouldn't mind the latter.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/include/xen/list.h +0
-3@@ -655,10 +689,10 @@ static inline void list_splice_init(struct list_head *list,* the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu (pos, head, member) \ - for ((pos) = list_entry((head)->next, typeof(*(pos)), member); \ - &rcu_dereference(pos)->member != (head); \Quoted 8 lines
xen/include/xen/list.h static inline void list_splice_init(struct list_head *list, · line 695
Quoted 5 lines
xen/include/xen/list.h +4
-1- (pos) = list_entry((pos)->member.next, typeof(*(pos)), member)) +#define list_for_each_entry_rcu (pos, head, member) \ + for ( (pos) = list_first_entry_or_null(head, typeof(*(pos)), member); \ + rcu_dereference(pos); \ + (pos) = list_next_entry_or_null(head, pos, member) )Don't you need a
list_next_entry_or_null_rcu()flavor here, usingrcu_dereference()on the passed in pos for the (pos)->member.next deref?Quoted 1 line
Isn't the "
rcu_dereference(pos);" all what is needed for the current iteration?Reading Linux'es
rcu_dereference.rst, my understanding is that one of them would suffice if then we used its result rather than the original pointer. Then again RCU has been somewhat opaque to me for all the years ...Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Otherwise today's implementation would suffer from the same problem IMHO.
That's the impression I'm (now) having.
The
rcu_dereference()is basically just for documentation purposes. If needed by an arch, it can add barriers, too, but according to the comments those would be needed on alpha only. The returned value is always the original pointer value. See the comment block inxen/include/xen/rcupdate.h
Quoted 7 lines
Note the "This pointer may later be safely dereferenced" in there. As said, we'd be fine if we used the return value instead of the original "pos". Yet we don't. We effectively assume that the macro expands to just the passed in pointer, with no barriers or anything else.
Anyway, since - as you validly say - this is a pre-existing issue, let's put it on the side right here.
I'll setup a patch for 4.21 fixing all the rcu list iterators.
Juergen
Re: [PATCH 1/2] xen/list: avoid UB in list iterators
In reply to Juergen GrossQuoted 8 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 then the list head.
Quoted 8 lines
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.
Reported-by Andrew Cooper <[email protected]>Signed-off-by Juergen Gross <[email protected]>
I have to admit that my gut feeling is that this is vastly overcomplicated.
It also further diverges from Linux. I couldn't find an obvious example of this kind of UBSAN failure in Linux which suggests to me that one of the differences might be relevant.
I did start experimenting in this direction, but haven't finished.
~Andrew
PATCH 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
Re: [PATCH 2/2] xen/list: fix comments in include/xen/list.h
Quoted 4 lines · Juergen Gross
There are several places in
list.hwhere "list_struct" is used instead of "structlist_head". Fix that.Signed-off-by Juergen Gross <[email protected]>
Quoted 21 lines · Juergen Gross
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.hI 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.There are some additional cleanups possible in
list.h, which I can do for 4.21 when wanted:- Removal of
list_prepare_entry(), which seems to be unused since some time now (and it seems to be thought of as alist.hinternal macro only).- More questionable: removal of unused iterators, like e.g.
list_for_each_entry_continue()orlist_for_each_entry_from(). The main idea to keeplist.hin sync with the Linux version would be violated by this removal, though. OTOH with patch 1 they are out of sync anyway now, but I'm planning to submit a Linux kernel patch fixing the UB in the Linux variant, too.
Quoted 3 lines
Juergen Gross (2):
xen/list: avoid UB in list iterators
xen/list: fix comments ininclude/xen/list.h
With getting of proper Acks we can have both patches in 4.20:
Thanks.
~ Oleksii
Quoted 4 lines
xen/include/xen/list.h | 144 +++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 55 deletions(-)
Quoted 10 lines · Juergen Gross
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.hI 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.There are some additional cleanups possible in
list.h, which I can do for 4.21 when wanted:
Quoted 8 lines
- Removal of
list_prepare_entry(), which seems to be unused since some time now (and it seems to be thought of as alist.hinternal macro only).- More questionable: removal of unused iterators, like e.g.
list_for_each_entry_continue()orlist_for_each_entry_from(). The main idea to keeplist.hin sync with the Linux version would be violated by this removal, though.
That's true for the unused list_prepare_entry(), too, isn't it? Which in turn is coupled with list_for_each_entry_continue().
Quoted 3 lines
OTOH with patch 1 they are out of sync anyway
now, but I'm planning to submit a Linux kernel patch fixing the UB in the Linux variant, too.
I'd go with whatever the Linux side is going accept.
Jan