Skip to contentSkip to conversation

net: skb extension follow-ups

PATCH Superseded Paolo Abeni 3 participants 10 messages Dec 20, 2018 – Dec 21, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Paolo Abeni

author maintainer
#0 lore raw

cover net: skb extension follow-ups

This series includes some follow-up for the recently added skb extension. The first patch addresses an unlikely race while adding skb extensions, and the following two are just minor code clean-up.

Paolo Abeni (3):
net: fix possible user-after-free in skb_ext_add()
net: drop the unused helper skb_ext_get()
net: minor cleanup in skb_ext_add()

 include/linux/skbuff.h | 11 -----------
 net/core/skbuff.c      | 11 ++++-------
 2 files changed, 4 insertions(+), 18 deletions(-)
Signature · 1 line
2.19.2
author maintainer
#1 lore raw

PATCH 1/3 net: fix possible user-after-free in skb_ext_add()

On cow we can free the old extension: we must avoid dereferencing such extension after skb_ext_maybe_cow(). Since 'new' contents are always equal to 'old' after the copy, we can fix the above accessing the relevant data using 'new'.

Fixes df5042f4c5b9 ("sk_buff: add skb extension infrastructure")
---
 net/core/skbuff.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/skbuff.c +2 -2
index cb0bf4215745..e1d88762f659 100644
@@ -5666,13 +5666,13 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
 		if (!new)
 			return NULL;
 
-		if (__skb_ext_exist(old, id)) {
+		if (__skb_ext_exist(new, id)) {
 			if (old != new)
 				skb->extensions = new;
 			goto set_active;
 		}
 
-		newoff = old->chunks;
+		newoff = new->chunks;
 	} else {
 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
 
Signature · 1 line
2.19.2
author maintainer
#3 lore raw

PATCH 2/3 net: drop the unused helper skb_ext_get()

In reply to Paolo Abeni

Such helper is currently unused, and skb extension users are better off using skb_ext_add()/skb_ext_del(). So let's drop it.

---
 include/linux/skbuff.h | 11 -----------
 1 file changed, 11 deletions(-)
include/linux/skbuff.h +0 -11
index 3f741b04e55d..2a57a365c711 100644
@@ -3938,16 +3938,6 @@ static inline void skb_ext_put(struct sk_buff *skb)
 		__skb_ext_put(skb->extensions);
 }
 
-static inline void skb_ext_get(struct sk_buff *skb)
-{
-	if (skb->active_extensions) {
-		struct skb_ext *ext = skb->extensions;
-
-		if (ext)
-			refcount_inc(&ext->refcnt);
-	}
-}
-
 static inline void __skb_ext_copy(struct sk_buff *dst,
 				  const struct sk_buff *src)
 {
@@ -3995,7 +3985,6 @@ static inline void *skb_ext_find(const struct sk_buff *skb, enum skb_ext_id id)
 }
 #else
 static inline void skb_ext_put(struct sk_buff *skb) {}
-static inline void skb_ext_get(struct sk_buff *skb) {}
 static inline void skb_ext_del(struct sk_buff *skb, int unused) {}
 static inline void __skb_ext_copy(struct sk_buff *d, const struct sk_buff *s) {}
 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {}
Signature · 1 line
2.19.2
author maintainer
#5 lore raw

PATCH 3/3 net: minor cleanup in skb_ext_add()

In reply to Paolo Abeni

When the extension to be added is already present, the only skb field we may need to update is 'extensions': we can reorder the code and avoid a branch.

---
 net/core/skbuff.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
net/core/skbuff.c +3 -6
index e1d88762f659..38afa3ff2b44 100644
@@ -5666,11 +5666,8 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
 		if (!new)
 			return NULL;
 
-		if (__skb_ext_exist(new, id)) {
-			if (old != new)
-				skb->extensions = new;
+		if (__skb_ext_exist(new, id))
 			goto set_active;
-		}
 
 		newoff = new->chunks;
 	} else {
@@ -5684,9 +5681,9 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
 	newlen = newoff + skb_ext_type_len[id];
 	new->chunks = newlen;
 	new->offset[id] = newoff;
-	skb->extensions = new;
-set_active:
 	skb->active_extensions |= 1 << id;
+set_active:
+	skb->extensions = new;
 	return skb_ext_get_ptr(new, id);
 }
 EXPORT_SYMBOL(skb_ext_add);
Signature · 1 line
2.19.2
#6 lore raw

Re: [PATCH net-next 3/3] net: minor cleanup in skb_ext_add()

Quoted 14 lines · Paolo Abeni

When the extension to be added is already present, the only skb field we may need to update is 'extensions': we can reorder the code and avoid a branch.

---
 net/core/skbuff.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
net/core/skbuff.c +0 -0
index e1d88762f659..38afa3ff2b44 100644

net/core/skbuff.c void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) · line 5671

Quoted 6 lines
net/core/skbuff.c +0 -3
@@ -5666,11 +5666,8 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
 		if (!new)
 			return NULL;
 
-		if (__skb_ext_exist(new, id)) {
-			if (old != new)
-				skb->extensions = new;

Yes, the old != new check can be avoided, we would replace "extensions" with the same content.

Quoted 7 lines
net/core/skbuff.c +1 -1
+		if (__skb_ext_exist(new, id))
 			goto set_active;
-		}
 
 		newoff = new->chunks;
 	} else {

net/core/skbuff.c void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) · line 5686

Quoted 8 lines
net/core/skbuff.c +2 -2
@@ -5684,9 +5681,9 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
 	newlen = newoff + skb_ext_type_len[id];
 	new->chunks = newlen;
 	new->offset[id] = newoff;
-	skb->extensions = new;
-set_active:
 	skb->active_extensions |= 1 << id;
+set_active:
+	skb->extensions = new;

Why are you moving the label?
I don't think thats correct.

We must make sure the id is flagged as active in
active_extensions.

Acked-by on v1 2/3
#4 lore raw

Re: [PATCH net-next 2/3] net: drop the unused helper skb_ext_get()

In reply to Paolo Abeni
Quoted 3 lines · Paolo Abeni

Such helper is currently unused, and skb extension users are better off using skb_ext_add()/skb_ext_del(). So let's drop it.

Right, I had a look at one of my older branches and its not used there either.

Acked-by on v1 1/3
#2 lore raw

Re: [PATCH net-next 1/3] net: fix possible user-after-free in skb_ext_add()

In reply to Paolo Abeni
Quoted 4 lines · Paolo Abeni

On cow we can free the old extension: we must avoid dereferencing such extension after skb_ext_maybe_cow(). Since 'new' contents are always equal to 'old' after the copy, we can fix the above accessing the relevant data using 'new'.

Yep, this is indeed possible when two clones of same skb have skb_ext_add() invoked at the same time from two different context.

Thanks for spotting this.

maintainer
#7 lore raw
In reply to Paolo Abeni

From: Paolo Abeni <[email protected]>
Date: Thu, 20 Dec 2018 19:38:15 +0100

Quoted 3 lines

This series includes some follow-up for the recently added skb extension. The first patch addresses an unlikely race while adding skb extensions, and the following two are just minor code clean-up.

Please respin after addressing Florian's feedback on patch #3.

Thanks!

author maintainer
#8 lore raw
Quoted 8 lines · David Miller

From: Paolo Abeni <[email protected]>
Date: Thu, 20 Dec 2018 19:38:15 +0100

Quoted 3 lines

This series includes some follow-up for the recently added skb extension. The first patch addresses an unlikely race while adding skb extensions, and the following two are just minor code clean-up.

Please respin after addressing Florian's feedback on patch #3.

I'm sorry for the not so prompt reply.

I will do/I'm doing that, but this is net-next material, so I guess it has to wait, right?

Thank you,

Paolo

maintainer
#9 lore raw

From: Paolo Abeni <[email protected]>
Date: Fri, 21 Dec 2018 13:09:34 +0100

Quoted 8 lines · David Miller
Quoted 7 lines

From: Paolo Abeni <[email protected]>
Date: Thu, 20 Dec 2018 19:38:15 +0100

Quoted 3 lines

This series includes some follow-up for the recently added skb extension. The first patch addresses an unlikely race while adding skb extensions, and the following two are just minor code clean-up.

Quoted 6 lines
Quoted 1 line

Please respin after addressing Florian's feedback on patch #3.

I'm sorry for the not so prompt reply.

I will do/I'm doing that, but this is net-next material, so I guess it has to wait, right?

Please send it to me.