Skip to contentSkip to conversation

net: dsa: skb_put_padto() already frees nskb

PATCH In review Florian Fainelli 2 participants 2 messages Aug 21, 2017 – Aug 22, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: dsa: skb_put_padto() already frees nskb

skb_put_padto() already frees the passed sk_buff reference upon error, so calling kfree_skb() on it again is not necessary.

Detected by CoverityScan, CID#1416687 ("USE_AFTER_FREE")

Fixes e71cb9e00922 ("net: dsa: ksz: fix skb freeing")
---
 net/dsa/tag_ksz.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
net/dsa/tag_ksz.c +1 -3
index de66ca8e6201..107172c82107 100644
@@ -60,10 +60,8 @@ static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev)
 					 skb_transport_header(skb) - skb->head);
 		skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
 
-		if (skb_put_padto(nskb, nskb->len + padlen)) {
-			kfree_skb(nskb);
+		if (skb_put_padto(nskb, nskb->len + padlen))
 			return NULL;
-		}
 
 		kfree_skb(skb);
 	}
Signature · 1 line
2.9.3
maintainer
#1 lore raw

From: Florian Fainelli <[email protected]>
Date: Mon, 21 Aug 2017 12:41:31 -0700

Quoted 7 lines

skb_put_padto() already frees the passed sk_buff reference upon error, so calling kfree_skb() on it again is not necessary.

Detected by CoverityScan, CID#1416687 ("USE_AFTER_FREE")

Fixes e71cb9e00922 ("net: dsa: ksz: fix skb freeing")

I know it seems like a lot of work, but with appropriate wrappers we can control the freeing that skb_pad() does at the deepest part of this call chain.

int __skb_pad(struct sk_buff *skb, int pad, bool free_skb_on_err); static inline int skb_pad(struct sk_buff *skb, int pad)

{
	return __skb_pad(skb, pad, true);
}

static inline int __skb_put_padto(struct sk_buff *skb, unsigned int len,
bool free_skb_on_err)

{
	unsigned int size = skb->len;

	if (unlikely(size < len)) {
		len -= size;
		if (__skb_pad(skb, len, free_skb_on_err))
			return -ENOMEM;
		__skb_put(skb, len);
	}
	return 0;
}

static inline int skb_put_padto(struct sk_buff *skb, unsigned int len)

{
	return __skb_put_padto(skb, len, true);
}

And then here in the ksz_xmit() code, invoke __skb_put_padto() with the boolean set appropriately.