Skip to contentSkip to conversation

net: skbuff: propagate shared-frag marker through pskb_copy()

PATCH Superseded Hyunwoo Kim 2 participants 5 messages May 13

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: skbuff: propagate shared-frag marker through pskb_copy()

__pskb_copy_fclone() shallow-copies the source's frag descriptors and bumps each page's refcount via skb_frag_ref(), then defers the rest of the shinfo metadata to skb_copy_header(). That helper only carries over gso_{size,segs,type} and never touches skb_shinfo()->flags, so the destination skb keeps a reference to the same externally-owned or page-cache-backed pages while reporting skb_has_shared_frag() as false.

The mismatch is harmful in any in-place writer that uses skb_has_shared_frag() to decide whether shared pages must be detoured through skb_cow_data(). ESP input is one such writer (esp4.c, esp6.c), and a single nft 'dup to <local>' rule -- or any other nf_dup_ipv4() / xt_TEE caller -- is enough to land a pskb_copy()'d skb in esp_input() with the marker stripped, letting an unprivileged user write into the page cache of a root-owned read-only file via authencesn-ESN stray writes.

Set SKBFL_SHARED_FRAG on the destination whenever frag descriptors were actually moved from the source. skb_copy() and skb_copy_expand() share skb_copy_header() too but linearize all paged data into freshly allocated head storage and emerge with nr_frags == 0, so skb_has_shared_frag() returns false on its own; they need no change.

Fixes cef401de7be8 ("net: fix possible wrong checksum generation")
Fixes f4c50a4034e6 ("xfrm: esp: avoid in-place decrypt on shared skb frags")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +3 -0
index 7dad68e3b518..15bdec53e8d9 100644
@@ -2248,6 +2248,7 @@ struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
 			skb_frag_ref(skb, i);
 		}
 		skb_shinfo(n)->nr_frags = i;
+		skb_shinfo(n)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
 	}
 
 	if (skb_has_frag_list(skb)) {
@@ -6200,6 +6201,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	       from_shinfo->frags,
 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
 	to_shinfo->nr_frags += from_shinfo->nr_frags;
+	if (from_shinfo->nr_frags)
+		to_shinfo->flags |= from_shinfo->flags & SKBFL_SHARED_FRAG;
 
 	if (!skb_cloned(from))
 		from_shinfo->nr_frags = 0;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 17 lines · Hyunwoo Kim

__pskb_copy_fclone() shallow-copies the source's frag descriptors and bumps each page's refcount via skb_frag_ref(), then defers the rest of the shinfo metadata to skb_copy_header(). That helper only carries over gso_{size,segs,type} and never touches skb_shinfo()->flags, so the destination skb keeps a reference to the same externally-owned or page-cache-backed pages while reporting skb_has_shared_frag() as false.

The mismatch is harmful in any in-place writer that uses skb_has_shared_frag() to decide whether shared pages must be detoured through skb_cow_data(). ESP input is one such writer (esp4.c, esp6.c), and a single nft 'dup to <local>' rule -- or any other nf_dup_ipv4() / xt_TEE caller -- is enough to land a pskb_copy()'d skb in esp_input() with the marker stripped, letting an unprivileged user write into the page cache of a root-owned read-only file via authencesn-ESN stray writes.

Quoted 5 lines

Set SKBFL_SHARED_FRAG on the destination whenever frag descriptors were actually moved from the source. skb_copy() and skb_copy_expand() share skb_copy_header() too but linearize all paged data into freshly allocated head storage and emerge with nr_frags == 0, so skb_has_shared_frag() returns false on its own; they need no change.

What about skb_shift()? It seems like that should also propagate this flag. But I could be missing some reason why it's not necessary.

Ben.

Quoted 31 lines
Fixes cef401de7be8 ("net: fix possible wrong checksum generation")
Fixes f4c50a4034e6 ("xfrm: esp: avoid in-place decrypt on shared skb frags")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +3 -0
index 7dad68e3b518..15bdec53e8d9 100644
@@ -2248,6 +2248,7 @@ struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
 			skb_frag_ref(skb, i);
 		}
 		skb_shinfo(n)->nr_frags = i;
+		skb_shinfo(n)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
 	}
 
 	if (skb_has_frag_list(skb)) {
@@ -6200,6 +6201,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	       from_shinfo->frags,
 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
 	to_shinfo->nr_frags += from_shinfo->nr_frags;
+	if (from_shinfo->nr_frags)
+		to_shinfo->flags |= from_shinfo->flags & SKBFL_SHARED_FRAG;
 
 	if (!skb_cloned(from))
 		from_shinfo->nr_frags = 0;
Signature · 2 lines
Ben Hutchings
Tomorrow will be cancelled due to lack of interest.
#2 lore raw
Quoted 18 lines · Ben Hutchings
Quoted 17 lines · Hyunwoo Kim

__pskb_copy_fclone() shallow-copies the source's frag descriptors and bumps each page's refcount via skb_frag_ref(), then defers the rest of the shinfo metadata to skb_copy_header(). That helper only carries over gso_{size,segs,type} and never touches skb_shinfo()->flags, so the destination skb keeps a reference to the same externally-owned or page-cache-backed pages while reporting skb_has_shared_frag() as false.

The mismatch is harmful in any in-place writer that uses skb_has_shared_frag() to decide whether shared pages must be detoured through skb_cow_data(). ESP input is one such writer (esp4.c, esp6.c), and a single nft 'dup to <local>' rule -- or any other nf_dup_ipv4() / xt_TEE caller -- is enough to land a pskb_copy()'d skb in esp_input() with the marker stripped, letting an unprivileged user write into the page cache of a root-owned read-only file via authencesn-ESN stray writes.

Quoted 8 lines
Quoted 5 lines

Set SKBFL_SHARED_FRAG on the destination whenever frag descriptors were actually moved from the source. skb_copy() and skb_copy_expand() share skb_copy_header() too but linearize all paged data into freshly allocated head storage and emerge with nr_frags == 0, so skb_has_shared_frag() returns false on its own; they need no change.

What about skb_shift()? It seems like that should also propagate this flag. But I could be missing some reason why it's not necessary.

That is one of the things I am testing.

Best regards,
Hyunwoo Kim

Quoted 38 lines

Ben.

Quoted 31 lines
Fixes cef401de7be8 ("net: fix possible wrong checksum generation")
Fixes f4c50a4034e6 ("xfrm: esp: avoid in-place decrypt on shared skb frags")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +3 -0
index 7dad68e3b518..15bdec53e8d9 100644
@@ -2248,6 +2248,7 @@ struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
 			skb_frag_ref(skb, i);
 		}
 		skb_shinfo(n)->nr_frags = i;
+		skb_shinfo(n)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
 	}
 
 	if (skb_has_frag_list(skb)) {
@@ -6200,6 +6201,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	       from_shinfo->frags,
 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
 	to_shinfo->nr_frags += from_shinfo->nr_frags;
+	if (from_shinfo->nr_frags)
+		to_shinfo->flags |= from_shinfo->flags & SKBFL_SHARED_FRAG;
 
 	if (!skb_cloned(from))
 		from_shinfo->nr_frags = 0;
Signature · 2 lines
Ben Hutchings
Tomorrow will be cancelled due to lack of interest.
#3 lore raw
In reply to Ben Hutchings
Quoted 18 lines · Ben Hutchings
Quoted 17 lines · Hyunwoo Kim

__pskb_copy_fclone() shallow-copies the source's frag descriptors and bumps each page's refcount via skb_frag_ref(), then defers the rest of the shinfo metadata to skb_copy_header(). That helper only carries over gso_{size,segs,type} and never touches skb_shinfo()->flags, so the destination skb keeps a reference to the same externally-owned or page-cache-backed pages while reporting skb_has_shared_frag() as false.

The mismatch is harmful in any in-place writer that uses skb_has_shared_frag() to decide whether shared pages must be detoured through skb_cow_data(). ESP input is one such writer (esp4.c, esp6.c), and a single nft 'dup to <local>' rule -- or any other nf_dup_ipv4() / xt_TEE caller -- is enough to land a pskb_copy()'d skb in esp_input() with the marker stripped, letting an unprivileged user write into the page cache of a root-owned read-only file via authencesn-ESN stray writes.

Quoted 8 lines
Quoted 5 lines

Set SKBFL_SHARED_FRAG on the destination whenever frag descriptors were actually moved from the source. skb_copy() and skb_copy_expand() share skb_copy_header() too but linearize all paged data into freshly allocated head storage and emerge with nr_frags == 0, so skb_has_shared_frag() returns false on its own; they need no change.

What about skb_shift()? It seems like that should also propagate this flag. But I could be missing some reason why it's not necessary.

Yes, since skb_shift() is also a function that moves frag descriptors, I think SHARED_FRAG should be propagated as well. The actual trigger conditions are tricky (not deterministic) due to TCP write-queue skb merging, but I believe the fix is the right thing to do.

I'm planning to submit a v2 patch. What do you think?

Best regards,
Hyunwoo Kim

#4 lore raw
Quoted 28 lines · Hyunwoo Kim
Quoted 18 lines · Ben Hutchings
Quoted 17 lines · Hyunwoo Kim

__pskb_copy_fclone() shallow-copies the source's frag descriptors and bumps each page's refcount via skb_frag_ref(), then defers the rest of the shinfo metadata to skb_copy_header(). That helper only carries over gso_{size,segs,type} and never touches skb_shinfo()->flags, so the destination skb keeps a reference to the same externally-owned or page-cache-backed pages while reporting skb_has_shared_frag() as false.

The mismatch is harmful in any in-place writer that uses skb_has_shared_frag() to decide whether shared pages must be detoured through skb_cow_data(). ESP input is one such writer (esp4.c, esp6.c), and a single nft 'dup to <local>' rule -- or any other nf_dup_ipv4() / xt_TEE caller -- is enough to land a pskb_copy()'d skb in esp_input() with the marker stripped, letting an unprivileged user write into the page cache of a root-owned read-only file via authencesn-ESN stray writes.

Quoted 8 lines
Quoted 5 lines

Set SKBFL_SHARED_FRAG on the destination whenever frag descriptors were actually moved from the source. skb_copy() and skb_copy_expand() share skb_copy_header() too but linearize all paged data into freshly allocated head storage and emerge with nr_frags == 0, so skb_has_shared_frag() returns false on its own; they need no change.

What about skb_shift()? It seems like that should also propagate this flag. But I could be missing some reason why it's not necessary.

Quoted 6 lines

Yes, since skb_shift() is also a function that moves frag descriptors, I think SHARED_FRAG should be propagated as well. The actual trigger conditions are tricky (not deterministic) due to TCP write-queue skb merging, but I believe the fix is the right thing to do.

I'm planning to submit a v2 patch. What do you think?

And skb_gro_receive() also appears to need work. Further testing is in progress...

Quoted 4 lines

Best regards,
Hyunwoo Kim