🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Xin Long Fri, 27 Nov 2026 14:26
[PATCH net v3] sctp: unshare GSO skb frag_list members instead of linearizing
#23578
Author: Xin Long
Date: Fri, 27 Nov 2026 14:26
118 lines
14714 bytes
When the iptables TEE target copies an SCTP GSO packet with pskb_copy(),
the frag_list skbs are shared by reference via skb_clone_fraglist().
The SCTP receive path then races on shared sk_buff headers in
sctp_inq_pop(), and can hit a BUG_ON in __pskb_pull_tail().

Commit fd60d8a08619 ("sctp: linearize cloned gso packets in sctp_rcv")
tried to fix this by checking skb_cloned() on the outer skb, but
pskb_copy() gives the outer skb a private head without marking it
cloned, so that check never fires for the TEE case. The frag_list
members stay shared regardless. Even when linearization does trigger
(e.g. for AF_PACKET clones), it destroys per-segment boundaries,
breaking AUTH: the HMAC is computed per segment on transmit, but after
linearization the receive side hashes across all concatenated segments
and the digest never matches.

Fix by avoiding linearization for GSO packets entirely. Add
sctp_gso_unshare() which walks the frag_list and ensures exclusive
ownership of each member:

 - skb_unclone() on the outer skb to privatize the head when cloned.

 - For shared members (users > 1): replace with a pskb_copy() to get
   a private sk_buff and data buffer.

 - For non-shared members: skb_unclone() to handle the case where
   users == 1 but the data buffer is still shared (dataref > 1). This
   state arises when a cloned GSO skb is trimmed via ___pskb_trim() or
   __pskb_pull_tail(), e.g. an AF_PACKET socket with a BPF snaplen on
   loopback trims a clone from dev_queue_xmit_nit(), leaving the
   original's frag_list members with privately-owned sk_buff headers
   but shared data.

Data exclusivity matters because sctp_sf_authenticate() zeroes the
digest and writes the computed HMAC in place; shared data would corrupt
AF_PACKET readers or let a crafted packet pass AUTH on a second copy.

Fixes: 90017accff61 ("sctp: Add GSO support")
Reported-by: syzbot+f15a6eff7a53f6eb7dc3@syzkaller.appspotmail.com
Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
v2:
  - Use a proper Fixes tag and refine the commit message.
  - Use pskb_copy() instead of skb_clone() for shared skbs in frag_list
    (noted by Sashiko AI review).
v3:
  - Call skb_unclone() on the skbs in frag_list that are not shared in
    sctp_gso_unshare() (noted by Sashiko AI review).
---
 net/sctp/input.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)

diff --git a/net/sctp/input.c b/net/sctp/input.c
index 9494cfa51106..89599fe4c2f1 100644
--- a/net/sctp/input.c
+++ b/net/sctp/input.c
@@ -82,6 +82,42 @@ static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
 	return 0;
 }
 
+/* Ensure exclusive ownership of head and frag_list data for GSO skbs.
+ * pskb_copy() shares frag_list via skb_clone_fraglist(): copy shared
+ * members and unclone shared data so sctp_sf_authenticate() can safely
+ * zero and rewrite the HMAC digest in place.
+ */
+static int sctp_gso_unshare(struct sk_buff *skb)
+{
+	struct sk_buff **pp, *p;
+
+	if (skb_unclone(skb, GFP_ATOMIC))
+		return -ENOMEM;
+
+	if (!skb_has_frag_list(skb))
+		return 0;
+
+	pp = &skb_shinfo(skb)->frag_list;
+	skb_walk_frags(skb, p) {
+		if (skb_shared(p)) {
+			struct sk_buff *copy;
+
+			copy = pskb_copy(p, GFP_ATOMIC);
+			if (!copy)
+				return -ENOMEM;
+			copy->next = p->next;
+			consume_skb(p);
+			p = copy;
+			*pp = p;
+		} else if (skb_unclone(p, GFP_ATOMIC)) {
+			return -ENOMEM;
+		}
+		pp = &p->next;
+	}
+
+	return 0;
+}
+
 /*
  * This is the routine which IP calls when receiving an SCTP packet.
  */
@@ -117,8 +153,14 @@ int sctp_rcv(struct sk_buff *skb)
 	 * it's better to just linearize it otherwise crc computing
 	 * takes longer.
 	 */
-	if (((!is_gso || skb_cloned(skb)) && skb_linearize(skb)) ||
-	    !pskb_may_pull(skb, sizeof(struct sctphdr)))
+	if (!is_gso) {
+		if (skb_linearize(skb))
+			goto discard_it;
+	} else if (sctp_gso_unshare(skb)) {
+		goto discard_it;
+	}
+
+	if (!pskb_may_pull(skb, sizeof(struct sctphdr)))
 		goto discard_it;
 
 	/* Pull up the IP header. */
-- 
2.47.1
Re: [PATCH net v3] sctp: unshare GSO skb frag_list members instead of linearizing
#23918
Author: netdev-bot+sashiko
Date: Sat, 28 Nov 2026 15:11
154 lines
14134 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The patch makes data buffers exclusive only for GSO skbs, but
  the commit message's reason for doing so applies just as much to non-…
- [Medium] sctp: short GSO frag_list segment read in sctp_inq_pop() after
  unshare

--

sctp: unshare GSO skb frag_list members instead of linearizing

The patch stops linearizing SCTP GSO packets in sctp_rcv(). It adds
sctp_gso_unshare(), which unclones the outer skb and takes exclusive
ownership of each frag_list member, using pskb_copy() for shared members
and skb_unclone() for the others. The stated goal is to keep data
exclusive, so that sctp_sf_authenticate() can rewrite the HMAC digest in
place without corrupting other readers.

> diff --git a/net/sctp/input.c b/net/sctp/input.c
> index 9494cfa51106c..89599fe4c2f1a 100644
> --- a/net/sctp/input.c
> +++ b/net/sctp/input.c

[ ... ]

> @@ -117,8 +153,14 @@ int sctp_rcv(struct sk_buff *skb)
>  	 * it's better to just linearize it otherwise crc computing
>  	 * takes longer.
>  	 */
> -	if (((!is_gso || skb_cloned(skb)) && skb_linearize(skb)) ||
> -	    !pskb_may_pull(skb, sizeof(struct sctphdr)))
> +	if (!is_gso) {
> +		if (skb_linearize(skb))
> +			goto discard_it;
> +	} else if (sctp_gso_unshare(skb)) {
> +		goto discard_it;
> +	}

[Severity: Medium]
Can a trimmed trailing frag_list member end up being parsed as a
chunk header with fewer than four valid bytes behind it?

Both length checks in sctp_rcv() look only at the aggregate skb->len.
One runs before the linearize/unshare step and the other after
sk_filter(). Suppose ___pskb_trim() cuts a multi-segment GSO skb so
that the last frag_list member keeps only 1 to 3 bytes. The total
length still passes both checks, but that member cannot hold a struct
sctp_chunkhdr.

Before this patch, a cloned GSO skb in that state was linearized.
sctp_inq_pop() only treats the remaining bytes as another chunk when
chunk->chunk_end + sizeof(*ch) <= skb_tail_pointer(chunk->skb), so the
short tail was ignored.

Now the frag_list is kept. If the member was trimmed before
sctp_gso_unshare() runs, pskb_copy() and skb_unclone() (through
pskb_expand_head()) both allocate a new head and copy only up to the
tail, which is p->len bytes. The bytes after that, up to
sizeof(struct sctp_chunkhdr), are uninitialized.

sctp_inq_pop() then moves on to that member with
chunk->skb = chunk->skb->next and jumps to new_skb, which does:

	ch = (struct sctp_chunkhdr *)chunk->skb->data;
	...
	chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
	skb_pull(chunk->skb, sizeof(*ch));

Nothing checks chunk->skb->len against sizeof(*ch) first, so a few
things follow:

- ch->length is read from the uninitialized part of the new buffer,
  which KMSAN would report.

- skb_pull() returns NULL without advancing data.

- If the chunk is then rejected for its length,
  sctp_make_abort_violation() copies chunk->chunk_hdr->length into the
  ABORT it sends. Two bytes of uninitialized heap go out on the wire,
  and the association is torn down.

Trimming a shared or cloned GSO skb before sctp_rcv() needs
CAP_NET_ADMIN in some namespace, for example tc, netfilter, or TEE
combined with AF_PACKET.

A socket filter attached with SO_ATTACH_FILTER can produce the same
short member from sk_filter(). That happens after sctp_gso_unshare(),
so the bytes past the tail are stale rather than uninitialized. The
header read past the member's len is still there in that case.

Should sctp_gso_unshare() drop a GSO skb whose frag_list members are
shorter than sizeof(struct sctp_chunkhdr)? Or should sctp_inq_pop()
check chunk->skb->len at new_skb and discard that segment? The second
option would also cover a trim done by sk_filter() after the unshare.

[Severity: Medium]
Can the non-GSO path still write into a shared data buffer? The commit
message gives this reason for the change:

  "Data exclusivity matters because sctp_sf_authenticate() zeroes the
  digest and writes the computed HMAC in place; shared data would corrupt
  AF_PACKET readers or let a crafted packet pass AUTH on a second copy."

On the !is_gso branch, only skb_linearize() runs. It returns 0 right away
when !skb_is_nonlinear(skb). A cloned linear skb (dataref > 1) therefore
reaches the AUTH handling still sharing its data with the other clone.

Later, in sctp_sf_authenticate() in net/sctp/sm_statefuns.c:

	memset(digest, 0, sig_len);

	if (sctp_auth_calculate_hmac(asoc, chunk->skb,
				     (struct sctp_auth_chunk *)chunk->chunk_hdr,
				     sh_key, GFP_ATOMIC)) {

sctp_auth_calculate_hmac() in net/sctp/auth.c then writes the computed
HMAC to digest = (u8 *)(&auth->auth_hdr + 1). Both writes go into the
shared data area.

This state looks common on ingress. With an AF_PACKET tap active:

__netif_receive_skb_core()->deliver_skb()  (refcount_inc(&skb->users))
packet_rcv()  (skb_shared(skb), so it queues skb_clone(skb))
ip_rcv()->...->sctp_rcv()  (original arrives with cloned=1)

A tc mirred mirror produces the same state through skb_clone().

In that case, the AF_PACKET reader would see the digest field zeroed or
replaced by the HMAC the receiver computed. If two clones of a forged
packet reach the same association, the first fails AUTH but leaves a valid
HMAC in the shared data, and the second one could pass.

The non-GSO behaviour existed before this patch. Since the commit message
gives this exact hazard as its reason, should the non-GSO branch also call
skb_unclone(), or something equivalent, before AUTH can write in place?

> +
> +	if (!pskb_may_pull(skb, sizeof(struct sctphdr)))
>  		goto discard_it;
>  
>  	/* Pull up the IP header. */

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/112afb6053d880d08bc768ecc72853357240ce29.1795789564.git.lucien.xin%40gmail.com
Re: [PATCH net v3] sctp: unshare GSO skb frag_list members instead of linearizing
#24909
Author: Xin Long
Date: Tue, 01 Dec 2026 02:41
172 lines
19043 bytes
On Sat, Nov 28, 2026 at 10:11 AM <netdev-bot+sashiko@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
>
> Critical: 0 · High: 0 · Medium: 2 · Low: 0
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
> - [Medium] The patch makes data buffers exclusive only for GSO skbs, but
>   the commit message's reason for doing so applies just as much to non-…
> - [Medium] sctp: short GSO frag_list segment read in sctp_inq_pop() after
>   unshare
>
> --
>
> sctp: unshare GSO skb frag_list members instead of linearizing
>
> The patch stops linearizing SCTP GSO packets in sctp_rcv(). It adds
> sctp_gso_unshare(), which unclones the outer skb and takes exclusive
> ownership of each frag_list member, using pskb_copy() for shared members
> and skb_unclone() for the others. The stated goal is to keep data
> exclusive, so that sctp_sf_authenticate() can rewrite the HMAC digest in
> place without corrupting other readers.
>
> > diff --git a/net/sctp/input.c b/net/sctp/input.c
> > index 9494cfa51106c..89599fe4c2f1a 100644
> > --- a/net/sctp/input.c
> > +++ b/net/sctp/input.c
>
> [ ... ]
>
> > @@ -117,8 +153,14 @@ int sctp_rcv(struct sk_buff *skb)
> >        * it's better to just linearize it otherwise crc computing
> >        * takes longer.
> >        */
> > -     if (((!is_gso || skb_cloned(skb)) && skb_linearize(skb)) ||
> > -         !pskb_may_pull(skb, sizeof(struct sctphdr)))
> > +     if (!is_gso) {
> > +             if (skb_linearize(skb))
> > +                     goto discard_it;
> > +     } else if (sctp_gso_unshare(skb)) {
> > +             goto discard_it;
> > +     }
>
> [Severity: Medium]
> Can a trimmed trailing frag_list member end up being parsed as a
> chunk header with fewer than four valid bytes behind it?
>
> Both length checks in sctp_rcv() look only at the aggregate skb->len.
> One runs before the linearize/unshare step and the other after
> sk_filter(). Suppose ___pskb_trim() cuts a multi-segment GSO skb so
> that the last frag_list member keeps only 1 to 3 bytes. The total
> length still passes both checks, but that member cannot hold a struct
> sctp_chunkhdr.
>
> Before this patch, a cloned GSO skb in that state was linearized.
> sctp_inq_pop() only treats the remaining bytes as another chunk when
> chunk->chunk_end + sizeof(*ch) <= skb_tail_pointer(chunk->skb), so the
> short tail was ignored.
>
> Now the frag_list is kept. If the member was trimmed before
> sctp_gso_unshare() runs, pskb_copy() and skb_unclone() (through
> pskb_expand_head()) both allocate a new head and copy only up to the
> tail, which is p->len bytes. The bytes after that, up to
> sizeof(struct sctp_chunkhdr), are uninitialized.
>
> sctp_inq_pop() then moves on to that member with
> chunk->skb = chunk->skb->next and jumps to new_skb, which does:
>
>         ch = (struct sctp_chunkhdr *)chunk->skb->data;
>         ...
>         chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
>         skb_pull(chunk->skb, sizeof(*ch));
>
> Nothing checks chunk->skb->len against sizeof(*ch) first, so a few
> things follow:
>
> - ch->length is read from the uninitialized part of the new buffer,
>   which KMSAN would report.
>
> - skb_pull() returns NULL without advancing data.
>
> - If the chunk is then rejected for its length,
>   sctp_make_abort_violation() copies chunk->chunk_hdr->length into the
>   ABORT it sends. Two bytes of uninitialized heap go out on the wire,
>   and the association is torn down.
>
> Trimming a shared or cloned GSO skb before sctp_rcv() needs
> CAP_NET_ADMIN in some namespace, for example tc, netfilter, or TEE
> combined with AF_PACKET.
>
> A socket filter attached with SO_ATTACH_FILTER can produce the same
> short member from sk_filter(). That happens after sctp_gso_unshare(),
> so the bytes past the tail are stale rather than uninitialized. The
> header read past the member's len is still there in that case.
>
> Should sctp_gso_unshare() drop a GSO skb whose frag_list members are
> shorter than sizeof(struct sctp_chunkhdr)? Or should sctp_inq_pop()
> check chunk->skb->len at new_skb and discard that segment? The second
> option would also cover a trim done by sk_filter() after the unshare.
>
I didn't expect BPF to change the SCTP GSO skb structure.

The similar issue is also reported on:

https://lore.kernel.org/netdev/20261130022102.31794-1-zhaoxinyuan24@mails.ucas.ac.cn/

I may drop a SCTP GSO skb if it ever pulled some data from the frag_list.

> [Severity: Medium]
> Can the non-GSO path still write into a shared data buffer? The commit
> message gives this reason for the change:
>
>   "Data exclusivity matters because sctp_sf_authenticate() zeroes the
>   digest and writes the computed HMAC in place; shared data would corrupt
>   AF_PACKET readers or let a crafted packet pass AUTH on a second copy."
>
> On the !is_gso branch, only skb_linearize() runs. It returns 0 right away
> when !skb_is_nonlinear(skb). A cloned linear skb (dataref > 1) therefore
> reaches the AUTH handling still sharing its data with the other clone.
>
> Later, in sctp_sf_authenticate() in net/sctp/sm_statefuns.c:
>
>         memset(digest, 0, sig_len);
>
>         if (sctp_auth_calculate_hmac(asoc, chunk->skb,
>                                      (struct sctp_auth_chunk *)chunk->chunk_hdr,
>                                      sh_key, GFP_ATOMIC)) {
>
> sctp_auth_calculate_hmac() in net/sctp/auth.c then writes the computed
> HMAC to digest = (u8 *)(&auth->auth_hdr + 1). Both writes go into the
> shared data area.
>
> This state looks common on ingress. With an AF_PACKET tap active:
>
> __netif_receive_skb_core()->deliver_skb()  (refcount_inc(&skb->users))
> packet_rcv()  (skb_shared(skb), so it queues skb_clone(skb))
> ip_rcv()->...->sctp_rcv()  (original arrives with cloned=1)
>
> A tc mirred mirror produces the same state through skb_clone().
>
> In that case, the AF_PACKET reader would see the digest field zeroed or
> replaced by the HMAC the receiver computed. If two clones of a forged
> packet reach the same association, the first fails AUTH but leaves a valid
> HMAC in the shared data, and the second one could pass.
>
> The non-GSO behaviour existed before this patch. Since the commit message
> gives this exact hazard as its reason, should the non-GSO branch also call
> skb_unclone(), or something equivalent, before AUTH can write in place?
>
A legit one, although it's a pre-existing issue for non-GSO packets.

I will address it together in the next post by moving skb_unclone() to
sctp_rcv().

Thanks.

pw-bot: cr
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads