Skip to contentSkip to conversation

smsc95xx: Use skb_cow_head to deal with cloned skbs

PATCH Superseded James Hughes 3 participants 7 messages Apr 18, 2017 – Apr 19, 2017

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 smsc95xx: Use skb_cow_head to deal with cloned skbs

The driver was failing to check that the SKB wasn't cloned before adding checksum data.
Replace existing handling to extend/copy the header buffer with skb_cow_head.

---
Changes in v2
- Changed skb_cow to skb_cow_head as suggested by netdev list

 drivers/net/usb/smsc95xx.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/net/usb/smsc95xx.c +3 -7
index df60c98..094f0ee 100644
@@ -2067,13 +2067,9 @@ static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
 	/* We do not advertise SG, so skbs should be already linearized */
 	BUG_ON(skb_shinfo(skb)->nr_frags);
 
-	if (skb_headroom(skb) < overhead) {
-		struct sk_buff *skb2 = skb_copy_expand(skb,
-			overhead, 0, flags);
-		dev_kfree_skb_any(skb);
-		skb = skb2;
-		if (!skb)
-			return NULL;
+	/* Make writable and expand header space by overhead if required */
+	if (skb_cow_head(skb, overhead)) {
+		return NULL;
 	}
 
 	if (csum) {
Signature · 1 line
2.9.3
#1 lore raw
Quoted 21 lines · James Hughes

The driver was failing to check that the SKB wasn't cloned before adding checksum data.
Replace existing handling to extend/copy the header buffer with skb_cow_head.

---
Changes in v2
- Changed skb_cow to skb_cow_head as suggested by netdev list

 drivers/net/usb/smsc95xx.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/net/usb/smsc95xx.c +0 -0
index df60c98..094f0ee 100644
@@ -2067,13 +2067,9 @@ static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
 	/* We do not advertise SG, so skbs should be already linearized */
 	BUG_ON(skb_shinfo(skb)->nr_frags);

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, · line 2071

Quoted 10 lines
drivers/net/usb/smsc95xx.c +2 -7
 
-	if (skb_headroom(skb) < overhead) {
-		struct sk_buff *skb2 = skb_copy_expand(skb,
-			overhead, 0, flags);
-		dev_kfree_skb_any(skb);
-		skb = skb2;
-		if (!skb)
-			return NULL;
+	/* Make writable and expand header space by overhead if required */
+	if (skb_cow_head(skb, overhead)) {

I believe you still need to

		dev_kfree_skb_any(skb);
Quoted 4 lines
drivers/net/usb/smsc95xx.c +1 -0
+		return NULL;
 	}
 
 	if (csum) {
#2 lore raw
Quoted 14 lines

Review static struct sk_buff *smsc95xx_tx_fixup(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -2067,13 +2067,9 @@ static struct sk_buff *smsc95xx_tx_fixup(struct

usbnet *dev,

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,

Quoted 11 lines
drivers/net/usb/smsc95xx.c +1 -7
 	/* We do not advertise SG, so skbs should be already linearized */
 	BUG_ON(skb_shinfo(skb)->nr_frags);
 
-	if (skb_headroom(skb) < overhead) {
-		struct sk_buff *skb2 = skb_copy_expand(skb,
-			overhead, 0, flags);
-		dev_kfree_skb_any(skb);
-		skb = skb2;
-		if (!skb)
-			return NULL;
+	/* Make writable and expand header space by overhead if required

*/

Quoted 5 lines

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, · line 2071

Quoted 1 line
drivers/net/usb/smsc95xx.c +1 -0
+	if (skb_cow_head(skb, overhead)) {

I believe you still need to

		dev_kfree_skb_any(skb);

I think caller of usbnet_start_xmit() takes care of free when return NULL.

- Woojung

#3 lore raw
Quoted 14 lines · On Tue, 2017-04-18 at 22:09 +0000, [email protected] wrote:
Quoted 14 lines

Review static struct sk_buff *smsc95xx_tx_fixup(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -2067,13 +2067,9 @@ static struct sk_buff *smsc95xx_tx_fixup(struct

usbnet *dev,

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,

Quoted 11 lines
drivers/net/usb/smsc95xx.c +1 -7
 	/* We do not advertise SG, so skbs should be already linearized */
 	BUG_ON(skb_shinfo(skb)->nr_frags);
 
-	if (skb_headroom(skb) < overhead) {
-		struct sk_buff *skb2 = skb_copy_expand(skb,
-			overhead, 0, flags);
-		dev_kfree_skb_any(skb);
-		skb = skb2;
-		if (!skb)
-			return NULL;
+	/* Make writable and expand header space by overhead if required

*/

Quoted 6 lines
Quoted 5 lines

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, · line 2071

Quoted 1 line
drivers/net/usb/smsc95xx.c +1 -0
+	if (skb_cow_head(skb, overhead)) {

I believe you still need to

		dev_kfree_skb_any(skb);

I think caller of usbnet_start_xmit() takes care of free when return NULL.

I do not think so. Here is the code in usbnet_start_xmit() :

if (info->tx_fixup) {
skb = info->tx_fixup (dev, skb, GFP_ATOMIC);

if (!skb) { // Note that skb is NULL now

       if (info->flags & FLAG_MULTI_PACKET)
             goto not_drop;
        netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
        goto drop;

If you really think about it (even before double checking the code in usbnet_start_xmit()), that would have been a very serious bug.

Calling dev_kfree_skb_any(skb) in this fixup code, then later from usbnet_start_xmit() would have been a double free, or use after free.

#4 lore raw
Quoted 4 lines

if (info->tx_fixup) {
skb = info->tx_fixup (dev, skb, GFP_ATOMIC);

if (!skb) { // Note that skb is NULL now

You are right. skb is return value of tx_fixup().

- Woojung

#5 lore raw
In reply to Eric Dumazet
Quoted 35 lines · Eric Dumazet
Quoted 13 lines · On Tue, 2017-04-18 at 22:09 +0000, [email protected] wrote:
Quoted 13 lines

Review static struct sk_buff *smsc95xx_tx_fixup(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -2067,13 +2067,9 @@ static struct sk_buff *smsc95xx_tx_fixup(struct

usbnet *dev,

Quoted 11 lines
drivers/net/usb/smsc95xx.c +1 -7
  /* We do not advertise SG, so skbs should be already linearized */
  BUG_ON(skb_shinfo(skb)->nr_frags);
 
- if (skb_headroom(skb) < overhead) {
-         struct sk_buff *skb2 = skb_copy_expand(skb,
-                 overhead, 0, flags);
-         dev_kfree_skb_any(skb);
-         skb = skb2;
-         if (!skb)
-                 return NULL;
+ /* Make writable and expand header space by overhead if required
Quoted 7 lines
Quoted 6 lines

*/

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, · line 2071

Quoted 1 line
drivers/net/usb/smsc95xx.c +1 -0
+ if (skb_cow_head(skb, overhead)) {

I believe you still need to

            dev_kfree_skb_any(skb);

I think caller of usbnet_start_xmit() takes care of free when return NULL.

I do not think so. Here is the code in usbnet_start_xmit() :

if (info->tx_fixup) {
   skb = info->tx_fixup (dev, skb, GFP_ATOMIC);

   if (!skb) { // Note that skb is NULL now

       if (info->flags & FLAG_MULTI_PACKET)
             goto not_drop;
        netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
        goto drop;
Quoted 7 lines

If you really think about it (even before double checking the code in usbnet_start_xmit()), that would have been a very serious bug.

Calling dev_kfree_skb_any(skb) in this fixup code, then later from usbnet_start_xmit() would have been a double free, or use after free.

So, I still need to return NULL (as per the code this is replacing) to indicate failure, but need to free the skb prior to return, as per fragment below.

/* Make writable and expand header space by overhead if required */ if (skb_cow_head(skb, overhead)) {
dev_kfree_skb_any(skb);
return NULL;
}

Once confirmed, I'll do a another patch.

#6 lore raw
Quoted 48 lines · James Hughes
Quoted 35 lines · Eric Dumazet
Quoted 13 lines · On Tue, 2017-04-18 at 22:09 +0000, [email protected] wrote:
Quoted 13 lines

Review static struct sk_buff *smsc95xx_tx_fixup(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -2067,13 +2067,9 @@ static struct sk_buff *smsc95xx_tx_fixup(struct

usbnet *dev,

Quoted 11 lines
drivers/net/usb/smsc95xx.c +1 -7
  /* We do not advertise SG, so skbs should be already linearized */
  BUG_ON(skb_shinfo(skb)->nr_frags);
 
- if (skb_headroom(skb) < overhead) {
-         struct sk_buff *skb2 = skb_copy_expand(skb,
-                 overhead, 0, flags);
-         dev_kfree_skb_any(skb);
-         skb = skb2;
-         if (!skb)
-                 return NULL;
+ /* Make writable and expand header space by overhead if required
Quoted 7 lines
Quoted 6 lines

*/

drivers/net/usb/smsc95xx.c static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, · line 2071

Quoted 1 line
drivers/net/usb/smsc95xx.c +1 -0
+ if (skb_cow_head(skb, overhead)) {

I believe you still need to

            dev_kfree_skb_any(skb);

I think caller of usbnet_start_xmit() takes care of free when return NULL.

I do not think so. Here is the code in usbnet_start_xmit() :

if (info->tx_fixup) {
   skb = info->tx_fixup (dev, skb, GFP_ATOMIC);

   if (!skb) { // Note that skb is NULL now

       if (info->flags & FLAG_MULTI_PACKET)
             goto not_drop;
        netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
        goto drop;
Quoted 7 lines

If you really think about it (even before double checking the code in usbnet_start_xmit()), that would have been a very serious bug.

Calling dev_kfree_skb_any(skb) in this fixup code, then later from usbnet_start_xmit() would have been a double free, or use after free.

So, I still need to return NULL (as per the code this is replacing) to indicate failure, but need to free the skb prior to return, as per fragment below.

Quoted 7 lines

/* Make writable and expand header space by overhead if required */

if (skb_cow_head(skb, overhead)) {
   dev_kfree_skb_any(skb);
   return NULL;
}

Once confirmed, I'll do a another patch.

Actually, having checked the code path, doing the free here does look like the correct operation. Returning NULL indicates to the caller that the call has failed, but also means the skb is not deallocated later on. It's seems rather a clunky mechanism for the driver to have to deallocate, but I guess that's 'just the way it is'.

I'll do another patch.