Netdev List
 help / color / mirror / Atom feed
From: Guikun Yang <yangguikun19@qq.com>
To: Jeremy Kerr <jk@codeconstruct.com.au>,
	Matt Johnston <matt@codeconstruct.com.au>
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [BUG net] mctp: sk_buff leak in mctp_dst_input() when mctp_key_add() fails
Date: Sat, 24 Oct 2026 18:32:20 +0800	[thread overview]
Message-ID: <tencent_43A65D51143E18518EA968DF9323694EFD08@qq.com> (raw)

Hi Jeremy, Matt,

In mctp_dst_input() (net/mctp/route.c), the SOM (start-of-message)
branch allocates a reassembly key, queues the packet into it, and only
afterwards publishes the key:

		if (!key) {
			key = mctp_key_alloc(msk, netid, mh->dest, mh->src,
					     tag, GFP_ATOMIC);
			if (!key) {
				rc = -ENOMEM;
				goto out_unlock;
			}

			/* we can queue without the key lock here, as the
			 * key isn't observable yet
			 */
			mctp_frag_queue(key, skb);
			skb = NULL;

			rc = mctp_key_add(key, msk);
			if (!rc)
				trace_mctp_key_acquire(key);

			mctp_key_unref(key);
			key = NULL;

If mctp_key_add() fails, the packet has already been consumed by
mctp_frag_queue(): for the first packet of a message it takes ownership
of the skb via skb_unshare() and stores it in key->reasm_head, and the
local variable is set to NULL.  The failure path then drops the only
key reference (mctp_key_unref() -> refcount 0 -> kfree(key)) without
freeing key->reasm_head, so the sk_buff is leaked: neither the caller's
out: kfree_skb(skb) (skb is NULL) nor anything else ever releases it.

mctp_key_add() can fail in two ways:

  1. -EEXIST: a concurrent packet with the same (net, src, dst, tag)
     won the race in mctp_key_add()'s duplicate check.  The frag_queue()
     call happens outside net->mctp.keys_lock, so two CPUs processing
     two identical SOM packets can each allocate a key; the loser fails
     here and leaks its skb.

  2. -EINVAL: the socket went away (SOCK_DEAD) between the bind lookup
     and key_add -- e.g. the fd is closed while the packet is in flight.
     This window is much wider than the double-SOM race and does not
     need any attacker-side timing.

Either way, each failure leaks one sk_buff (the reassembly head,
including whatever frags it has).  Repeated SOM packets racing a close,
or hammering the double-SOM window on an SMP system, leak memory until
exhaustion.  The packets only need to reach a bound AF_MCTP socket on
the local machine, so anyone on the MCTP bus can trigger this against
an mctp-i2c/serial/netdev interface.

The same shape is handled correctly elsewhere: the !SOM duplicate path
uses __mctp_key_done_in(), which takes key->reasm_head and kfree_skb()s
it.  The fix is to do the same on the key_add failure path -- free
key->reasm_head (and tail fragments, of which the first packet has
none) before dropping the reference, e.g.:

			rc = mctp_key_add(key, msk);
			if (!rc)
				trace_mctp_key_acquire(key);
			else {
				kfree_skb(key->reasm_head);
				key->reasm_head = NULL;
			}

			mctp_key_unref(key);
			key = NULL;

Checked against linux-next (2026-10-24, net/mctp/route.c): the failure
path is unchanged.  syzbot has no MCTP report matching this; marc shows
no prior report (the usblib series is unrelated).

Found by code inspection.

Best,
Guikun Yang


                 reply	other threads:[~2026-10-24 10:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_43A65D51143E18518EA968DF9323694EFD08@qq.com \
    --to=yangguikun19@qq.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jk@codeconstruct.com.au \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matt@codeconstruct.com.au \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox