netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash()
@ 2024-08-22 18:11 Jeongjun Park
  2024-08-27 11:19 ` Paolo Abeni
  2024-08-29  0:20 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 6+ messages in thread
From: Jeongjun Park @ 2024-08-22 18:11 UTC (permalink / raw)
  To: wei.liu, paul
  Cc: davem, edumazet, kuba, pabeni, madhuparnabhowmik04, xen-devel,
	netdev, linux-kernel, Jeongjun Park

During the list_for_each_entry_rcu iteration call of xenvif_flush_hash, 
kfree_rcu does not exist inside the rcu read critical section, so if 
kfree_rcu is called when the rcu grace period ends during the iteration, 
UAF occurs when accessing head->next after the entry becomes free.

Therefore, to solve this, you need to change it to list_for_each_entry_safe.

Fixes: f3265971ded9 ("net: xen-netback: hash.c: Use built-in RCU list checking")
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
---
 drivers/net/xen-netback/hash.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/net/xen-netback/hash.c b/drivers/net/xen-netback/hash.c
index ff96f22648ef..45ddce35f6d2 100644
--- a/drivers/net/xen-netback/hash.c
+++ b/drivers/net/xen-netback/hash.c
@@ -95,7 +95,7 @@ static u32 xenvif_new_hash(struct xenvif *vif, const u8 *data,
 
 static void xenvif_flush_hash(struct xenvif *vif)
 {
-	struct xenvif_hash_cache_entry *entry;
+	struct xenvif_hash_cache_entry *entry, *n;
 	unsigned long flags;
 
 	if (xenvif_hash_cache_size == 0)
@@ -103,8 +103,7 @@ static void xenvif_flush_hash(struct xenvif *vif)
 
 	spin_lock_irqsave(&vif->hash.cache.lock, flags);
 
-	list_for_each_entry_rcu(entry, &vif->hash.cache.list, link,
-				lockdep_is_held(&vif->hash.cache.lock)) {
+	list_for_each_entry_safe(entry, n, &vif->hash.cache.list, link) {
 		list_del_rcu(&entry->link);
 		vif->hash.cache.count--;
 		kfree_rcu(entry, rcu);
--

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash()
  2024-08-22 18:11 [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash() Jeongjun Park
@ 2024-08-27 11:19 ` Paolo Abeni
  2024-08-27 14:14   ` Jakub Kicinski
  2024-08-28 12:52   ` Jeongjun Park
  2024-08-29  0:20 ` patchwork-bot+netdevbpf
  1 sibling, 2 replies; 6+ messages in thread
From: Paolo Abeni @ 2024-08-27 11:19 UTC (permalink / raw)
  To: Jeongjun Park, wei.liu, paul
  Cc: davem, edumazet, kuba, madhuparnabhowmik04, xen-devel, netdev,
	linux-kernel

On 8/22/24 20:11, Jeongjun Park wrote:
> During the list_for_each_entry_rcu iteration call of xenvif_flush_hash,
> kfree_rcu does not exist inside the rcu read critical section, so if

The above wording is confusing, do you mean "kfree_rcu does not exit 
from "...?

> kfree_rcu is called when the rcu grace period ends during the iteration,
> UAF occurs when accessing head->next after the entry becomes free.

The loop runs with irq disabled, the RCU critical section extends over 
it, uninterrupted.

Do you have a splat for the reported UAF?

This does not look the correct solution.

Thanks,

Paolo


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash()
  2024-08-27 11:19 ` Paolo Abeni
@ 2024-08-27 14:14   ` Jakub Kicinski
  2024-08-28 12:52   ` Jeongjun Park
  1 sibling, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2024-08-27 14:14 UTC (permalink / raw)
  To: Paolo Abeni
  Cc: Jeongjun Park, wei.liu, paul, davem, edumazet,
	madhuparnabhowmik04, xen-devel, netdev, linux-kernel

On Tue, 27 Aug 2024 13:19:59 +0200 Paolo Abeni wrote:
> On 8/22/24 20:11, Jeongjun Park wrote:
> > During the list_for_each_entry_rcu iteration call of xenvif_flush_hash,
> > kfree_rcu does not exist inside the rcu read critical section, so if  
> 
> The above wording is confusing, do you mean "kfree_rcu does not exit 
> from "...?

I think they mean that kfree_rcu() is called without holding RCU read
lock..

> > kfree_rcu is called when the rcu grace period ends during the iteration,
> > UAF occurs when accessing head->next after the entry becomes free.  

.. so it can run immediately. Therefore the loop fetching head->next
may cause a UAF.

> The loop runs with irq disabled, the RCU critical section extends over 
> it, uninterrupted.

Is this an official RCU rule? I remember Paul told us it's the case for
softirq, but IDK if it is also for local IRQ disable.

> Do you have a splat for the reported UAF?
> 
> This does not look the correct solution.

The problem may not exist, but FWIW the change makes sense to me :)
We hold the write lock, and modify the list. for_each_entry_safe()
seems like a better fit than for_each_entry_rcu()

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash()
  2024-08-27 11:19 ` Paolo Abeni
  2024-08-27 14:14   ` Jakub Kicinski
@ 2024-08-28 12:52   ` Jeongjun Park
  2024-08-29  0:06     ` Jakub Kicinski
  1 sibling, 1 reply; 6+ messages in thread
From: Jeongjun Park @ 2024-08-28 12:52 UTC (permalink / raw)
  To: Paolo Abeni
  Cc: wei.liu, paul, davem, edumazet, kuba, madhuparnabhowmik04,
	xen-devel, netdev, linux-kernel

On Tue, 27 Aug 2024 13:19:59 +0200 Paolo Abeni wrote:
> On 8/22/24 20:11, Jeongjun Park wrote:
> > During the list_for_each_entry_rcu iteration call of xenvif_flush_hash,
> > kfree_rcu does not exist inside the rcu read critical section, so if
>
> The above wording is confusing, do you mean "kfree_rcu does not exit
> from "...?
>
> > kfree_rcu is called when the rcu grace period ends during the iteration,
> > UAF occurs when accessing head->next after the entry becomes free.
>
> The loop runs with irq disabled, the RCU critical section extends over
> it, uninterrupted.

Basically, list_for_each_entry_rcu is specified to be used under the protection
of rcu_read_lock(), but this is not the case with xenvif_new_hash(). If it is
used without the protection of rcu_read_lock(), kfree is called immediately
after the grace period ends after the call to kfree_rcu() inside
list_for_each_entry_rcu, so the entry is released, and a UAF occurs when
fetching with ->next thereafter.

Regards,
Jeongjun Park

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash()
  2024-08-28 12:52   ` Jeongjun Park
@ 2024-08-29  0:06     ` Jakub Kicinski
  0 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2024-08-29  0:06 UTC (permalink / raw)
  To: Jeongjun Park
  Cc: Paolo Abeni, wei.liu, paul, davem, edumazet, madhuparnabhowmik04,
	xen-devel, netdev, linux-kernel

On Wed, 28 Aug 2024 21:52:12 +0900 Jeongjun Park wrote:
> > The loop runs with irq disabled, the RCU critical section extends over
> > it, uninterrupted.  
> 
> Basically, list_for_each_entry_rcu is specified to be used under the protection
> of rcu_read_lock(), but this is not the case with xenvif_new_hash(). If it is
> used without the protection of rcu_read_lock(), kfree is called immediately
> after the grace period ends after the call to kfree_rcu() inside
> list_for_each_entry_rcu, so the entry is released, and a UAF occurs when
> fetching with ->next thereafter.

You cut off and didn't answer Paolo's question whether you have a splat
/ saw this actually cause a crash or a KASAN warning.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash()
  2024-08-22 18:11 [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash() Jeongjun Park
  2024-08-27 11:19 ` Paolo Abeni
@ 2024-08-29  0:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-08-29  0:20 UTC (permalink / raw)
  To: Jeongjun Park
  Cc: wei.liu, paul, davem, edumazet, kuba, pabeni, madhuparnabhowmik04,
	xen-devel, netdev, linux-kernel

Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Fri, 23 Aug 2024 03:11:09 +0900 you wrote:
> During the list_for_each_entry_rcu iteration call of xenvif_flush_hash,
> kfree_rcu does not exist inside the rcu read critical section, so if
> kfree_rcu is called when the rcu grace period ends during the iteration,
> UAF occurs when accessing head->next after the entry becomes free.
> 
> Therefore, to solve this, you need to change it to list_for_each_entry_safe.
> 
> [...]

Here is the summary with links:
  - [net] net/xen-netback: prevent UAF in xenvif_flush_hash()
    https://git.kernel.org/netdev/net-next/c/0fa5e94a1811

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-08-29  0:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-22 18:11 [PATCH net] net/xen-netback: prevent UAF in xenvif_flush_hash() Jeongjun Park
2024-08-27 11:19 ` Paolo Abeni
2024-08-27 14:14   ` Jakub Kicinski
2024-08-28 12:52   ` Jeongjun Park
2024-08-29  0:06     ` Jakub Kicinski
2024-08-29  0:20 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).