* [PATCH-resend] backports: Fix double fetch in hlist_for_each_entry*_rcu
@ 2014-11-15 19:07 Sven Eckelmann
2014-11-16 16:48 ` Hauke Mehrtens
0 siblings, 1 reply; 2+ messages in thread
From: Sven Eckelmann @ 2014-11-15 19:07 UTC (permalink / raw)
To: backports; +Cc: Sven Eckelmann
The backported (<3.9) version of hlist_for_each_entry_rcu and
hlist_for_each_entry_safe uses the new macro hlist_entry_safe. It is called
with an ACCESS_ONCE parameter for the first parameter ptr. This disallows
merging of the two loads which the current version of the macro uses.
This is problematic because this macro must only generate one load. Otherwise
with two contexts (or CPUs) following could happen:
1. context 1 fetches the ptr to the last entry in hlist_entry_safe() and
accepts this non-NULL ptr
2. context 2 deletes the last entry and terminates the list with NULL
3. context 1 re-fetches the pointer, doesn't check for zero, calculates the
entry based on a NULL pointer
4. context 1 crashes because it tries to load/write data from/to the invalid
address
Instead use a single load to a temporary variable and do the NULL-check and
calculation based on that one. This is also the approach used in the current
Linux versions and was introduced by Paul E. McKenney.
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
backport/backport-include/linux/list.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/backport/backport-include/linux/list.h b/backport/backport-include/linux/list.h
index 9042830..5ac2615 100644
--- a/backport/backport-include/linux/list.h
+++ b/backport/backport-include/linux/list.h
@@ -17,7 +17,9 @@
#undef hlist_entry_safe
#define hlist_entry_safe(ptr, type, member) \
- (ptr) ? hlist_entry(ptr, type, member) : NULL
+ ({ typeof(ptr) ____ptr = (ptr); \
+ ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
+ })
#define hlist_for_each_entry4(tpos, pos, head, member) \
for (pos = (head)->first; \
--
2.1.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH-resend] backports: Fix double fetch in hlist_for_each_entry*_rcu
2014-11-15 19:07 [PATCH-resend] backports: Fix double fetch in hlist_for_each_entry*_rcu Sven Eckelmann
@ 2014-11-16 16:48 ` Hauke Mehrtens
0 siblings, 0 replies; 2+ messages in thread
From: Hauke Mehrtens @ 2014-11-16 16:48 UTC (permalink / raw)
To: Sven Eckelmann, backports
On 11/15/2014 08:07 PM, Sven Eckelmann wrote:
> The backported (<3.9) version of hlist_for_each_entry_rcu and
> hlist_for_each_entry_safe uses the new macro hlist_entry_safe. It is called
> with an ACCESS_ONCE parameter for the first parameter ptr. This disallows
> merging of the two loads which the current version of the macro uses.
>
> This is problematic because this macro must only generate one load. Otherwise
> with two contexts (or CPUs) following could happen:
>
> 1. context 1 fetches the ptr to the last entry in hlist_entry_safe() and
> accepts this non-NULL ptr
>
> 2. context 2 deletes the last entry and terminates the list with NULL
>
> 3. context 1 re-fetches the pointer, doesn't check for zero, calculates the
> entry based on a NULL pointer
>
> 4. context 1 crashes because it tries to load/write data from/to the invalid
> address
>
> Instead use a single load to a temporary variable and do the NULL-check and
> calculation based on that one. This is also the approach used in the current
> Linux versions and was introduced by Paul E. McKenney.
>
> Signed-off-by: Sven Eckelmann <sven@narfation.org>
> ---
> backport/backport-include/linux/list.h | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
Thank you for your patch, it was pushed upstream.
Hauke
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2014-11-16 16:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-15 19:07 [PATCH-resend] backports: Fix double fetch in hlist_for_each_entry*_rcu Sven Eckelmann
2014-11-16 16:48 ` Hauke Mehrtens
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox