netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] nfp: fix use-after-free in area_cache_get()
@ 2022-08-10  7:30 Jialiang Wang
  2022-08-11  4:10 ` Yinjun Zhang
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Jialiang Wang @ 2022-08-10  7:30 UTC (permalink / raw)
  To: simon.horman, kuba, davem, edumazet, pabeni, niejianglei2021,
	wangjialiang0806
  Cc: oss-drivers, netdev, linux-kernel

area_cache_get() is used to distribute cache->area and set cache->id,
 and if cache->id is not 0 and cache->area->kref refcount is 0, it will
 release the cache->area by nfp_cpp_area_release(). area_cache_get()
 set cache->id before cpp->op->area_init() and nfp_cpp_area_acquire().

But if area_init() or nfp_cpp_area_acquire() fails, the cache->id is
 is already set but the refcount is not increased as expected. At this
 time, calling the nfp_cpp_area_release() will cause use-after-free.

To avoid the use-after-free, set cache->id after area_init() and
 nfp_cpp_area_acquire() complete successfully.

Note: This vulnerability is triggerable by providing emulated device
 equipped with specified configuration.

 BUG: KASAN: use-after-free in nfp6000_area_init (/home/user/Kernel/v5.19
/x86_64/src/drivers/net/ethernet/netronome/nfp/nfpcore/nfp6000_pcie.c:760)
  Write of size 4 at addr ffff888005b7f4a0 by task swapper/0/1

 Call Trace:
  <TASK>
 nfp6000_area_init (/home/user/Kernel/v5.19/x86_64/src/drivers/net
/ethernet/netronome/nfp/nfpcore/nfp6000_pcie.c:760)
 area_cache_get.constprop.8 (/home/user/Kernel/v5.19/x86_64/src/drivers
/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c:884)

 Allocated by task 1:
 nfp_cpp_area_alloc_with_name (/home/user/Kernel/v5.19/x86_64/src/drivers
/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c:303)
 nfp_cpp_area_cache_add (/home/user/Kernel/v5.19/x86_64/src/drivers/net
/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c:802)
 nfp6000_init (/home/user/Kernel/v5.19/x86_64/src/drivers/net/ethernet
/netronome/nfp/nfpcore/nfp6000_pcie.c:1230)
 nfp_cpp_from_operations (/home/user/Kernel/v5.19/x86_64/src/drivers/net
/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c:1215)
 nfp_pci_probe (/home/user/Kernel/v5.19/x86_64/src/drivers/net/ethernet
/netronome/nfp/nfp_main.c:744)

 Freed by task 1:
 kfree (/home/user/Kernel/v5.19/x86_64/src/mm/slub.c:4562)
 area_cache_get.constprop.8 (/home/user/Kernel/v5.19/x86_64/src/drivers
/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c:873)
 nfp_cpp_read (/home/user/Kernel/v5.19/x86_64/src/drivers/net/ethernet
/netronome/nfp/nfpcore/nfp_cppcore.c:924 /home/user/Kernel/v5.19/x86_64
/src/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c:973)
 nfp_cpp_readl (/home/user/Kernel/v5.19/x86_64/src/drivers/net/ethernet
/netronome/nfp/nfpcore/nfp_cpplib.c:48)

Signed-off-by: Jialiang Wang <wangjialiang0806@163.com>
---
 drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c
index 34c0d2ddf9ef..a8286d0032d1 100644
--- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c
+++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cppcore.c
@@ -874,7 +874,6 @@ area_cache_get(struct nfp_cpp *cpp, u32 id,
 	}
 
 	/* Adjust the start address to be cache size aligned */
-	cache->id = id;
 	cache->addr = addr & ~(u64)(cache->size - 1);
 
 	/* Re-init to the new ID and address */
@@ -894,6 +893,8 @@ area_cache_get(struct nfp_cpp *cpp, u32 id,
 		return NULL;
 	}
 
+	cache->id = id;
+
 exit:
 	/* Adjust offset */
 	*offset = addr - cache->addr;
-- 
2.17.1


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

end of thread, other threads:[~2022-12-15  9:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10  7:30 [PATCH v3] nfp: fix use-after-free in area_cache_get() Jialiang Wang
2022-08-11  4:10 ` Yinjun Zhang
2022-08-11  8:34   ` Simon Horman
2022-08-11 16:10 ` patchwork-bot+netdevbpf
2022-12-07 12:21 ` Lee Jones
2022-12-08 14:09   ` Simon Horman
2022-12-14 10:20     ` Lee Jones
2022-12-15  9:25     ` Lee Jones
2022-12-15  9:45       ` Greg KH
2022-12-15  9:51         ` Lee Jones

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).