Skip to contentSkip to conversation

xdp: avoid calling kfree twice

PATCH Superseded Zhu Yanjun 4 participants 5 messages Dec 8, 2020 – Dec 10, 2020

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zhu Yanjun

author
#0 lore raw

PATCH v2 1/1 xdp: avoid calling kfree twice

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

---
 net/xdp/xdp_umem.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
net/xdp/xdp_umem.c +5 -12
index 56a28a686988..01b31c56cead 100644
@@ -97,7 +97,6 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
 {
 	unsigned int gup_flags = FOLL_WRITE;
 	long npgs;
-	int err;
 
 	umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs),
 			    GFP_KERNEL | __GFP_NOWARN);
@@ -112,20 +111,14 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
 	if (npgs != umem->npgs) {
 		if (npgs >= 0) {
 			umem->npgs = npgs;
-			err = -ENOMEM;
-			goto out_pin;
+			xdp_umem_unpin_pages(umem);
+			return -ENOMEM;
 		}
-		err = npgs;
-		goto out_pgs;
+		kfree(umem->pgs);
+		umem->pgs = NULL;
+		return (int)npgs;
 	}
 	return 0;
-
-out_pin:
-	xdp_umem_unpin_pages(umem);
-out_pgs:
-	kfree(umem->pgs);
-	umem->pgs = NULL;
-	return err;
 }
 
 static int xdp_umem_account_pages(struct xdp_umem *umem)
Signature · 1 line
2.18.4
#1 lore raw
Quoted 8 lines · Zhu Yanjun

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

Please also fix up the commit log according to Bjorn's prior feedback [0]. If it's just a cleanup, it should state so, the commit message right now makes it sound like an actual double free bug.

[0] https://lore.kernel.org/netdev/[email protected]/

#2 lore raw
Quoted 8 lines · Daniel Borkmann
Quoted 6 lines · Zhu Yanjun

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

Quoted 6 lines
Quoted 2 lines

Please also fix up the commit log according to Bjorn's prior feedback [0]. If it's just a cleanup, it should state so, the commit message right now makes it sound like an actual double free bug.

The umem->pgs is actually freed twice. Since umem->pgs is set to NULL after the first kfree,
the second kfree would not trigger call trace.
IMO, the commit log is very clear about this.

Zhu Yanjun

Quoted 2 lines
   [0] https://lore.kernel.org/netdev/[email protected]/
#3 lore raw
Quoted 12 lines · Zhu Yanjun
Quoted 11 lines · Daniel Borkmann
Quoted 8 lines · Zhu Yanjun

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

Quoted 8 lines
Quoted 3 lines

Please also fix up the commit log according to Bjorn's prior feedback [0]. If it's just a cleanup, it should state so, the commit message right now makes it sound like an actual double free bug.

The umem->pgs is actually freed twice. Since umem->pgs is set to NULL after the first kfree,
the second kfree would not trigger call trace.
IMO, the commit log is very clear about this.

Yes, it is very clear; and also wrong. As someone already pointed out, passing a NULL pointer to kfree() doesn't actually lead to a double free:

https://elixir.bootlin.com/linux/latest/source/mm/slub.c#L4106

-Toke

#4 lore raw
Quoted 19 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Zhu Yanjun
Quoted 14 lines · Daniel Borkmann
Quoted 8 lines · Zhu Yanjun

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

Please also fix up the commit log according to Bjorn's prior feedback [0]. If it's just a cleanup, it should state so, the commit message right now makes it sound like an actual double free bug.

Quoted 8 lines
Quoted 4 lines

The umem->pgs is actually freed twice. Since umem->pgs is set to NULL after the first kfree,
the second kfree would not trigger call trace.
IMO, the commit log is very clear about this.

Yes, it is very clear; and also wrong. As someone already pointed out, passing a NULL pointer to kfree() doesn't actually lead to a double free:

In your commit, does "double free" mean the call trace bug? If so, I will correct it in my commit log.
In my commit log, I just mean that kfree is called twice. And the second kfree is meaningless. And since NULL
is passed to it, this will not trigger "double free" CallTrace.

I will send the latest version soon.

Zhu Yanjun

Quoted 5 lines

https://elixir.bootlin.com/linux/latest/source/mm/slub.c#L4106

-Toke