* [PATCH net v2 1/2] net: mctp: don't hold a zero-refcount socket in mctp_key_alloc()
2026-12-04 18:42 [PATCH net v2 0/2] net: mctp: avoid a zero-refcount socket hold Andrew Stellman
@ 2026-12-04 18:42 ` Andrew Stellman
2026-12-04 18:42 ` [PATCH net v2 2/2] net: mctp: test route input with a zero-refcount socket Andrew Stellman
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Stellman @ 2026-12-04 18:42 UTC (permalink / raw)
To: Jeremy Kerr, Matt Johnston
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, netdev, linux-kernel
From: Andrew Stellman <astellman@stellman-greene.com>
mctp_dst_input() finds the bound socket with mctp_lookup_bind() under
RCU only, without holding a reference. If a concurrent close drops
the last reference after that lookup, mctp_key_alloc() calls
sock_hold() on a zero refcount, which saturates it and warns, and
mctp_key_unref() later underflows it.
Take the reference with refcount_inc_not_zero() and fail the key
allocation if the socket is already at zero.
The issue was found by Claude Opus 5.5 and Claude Fable 5.2
running Quality Playbook, an LLM-driven code review tool:
https://github.com/andrewstellman/quality-playbook
The fix and this changelog were drafted with OpenAI GPT-6 (Codex)
and reviewed by me.
Fixes: de8a6b15d965 ("net: mctp: add an explicit reference from a mctp_sk_key to sock")
Reviewed-by: Jeremy Kerr <jk@codeconstruct.com.au>
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
v2: no code change; add Jeremy Kerr's Reviewed-by.
v1: https://lore.kernel.org/netdev/20261202164958.49945-1-bugs@stellman-greene.com/
Tested on net 3c8881ee9806 (7.4.0-rc5) in arm64 QEMU with KASAN, using
the KUnit case in 2/2, which sets a zero sk_refcnt on a bound socket
(the close race itself wasn't reproduced): it hit the "addition on 0"
and "underflow" refcount warnings without this fix and passed with it.
net/mctp/route.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/net/mctp/route.c b/net/mctp/route.c
index 96fd75ea7..5bc3a8e09 100644
--- a/net/mctp/route.c
+++ b/net/mctp/route.c
@@ -239,7 +239,10 @@ static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk,
key->valid = true;
spin_lock_init(&key->lock);
refcount_set(&key->refs, 1);
- sock_hold(key->sk);
+ if (!refcount_inc_not_zero(&msk->sk.sk_refcnt)) {
+ kfree(key);
+ return NULL;
+ }
return key;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH net v2 2/2] net: mctp: test route input with a zero-refcount socket
2026-12-04 18:42 [PATCH net v2 0/2] net: mctp: avoid a zero-refcount socket hold Andrew Stellman
2026-12-04 18:42 ` [PATCH net v2 1/2] net: mctp: don't hold a zero-refcount socket in mctp_key_alloc() Andrew Stellman
@ 2026-12-04 18:42 ` Andrew Stellman
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Stellman @ 2026-12-04 18:42 UTC (permalink / raw)
To: Jeremy Kerr, Matt Johnston
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, netdev, linux-kernel
From: Andrew Stellman <astellman@stellman-greene.com>
Add a KUnit case for the zero-refcount socket path in
mctp_key_alloc(). Send a start-of-message packet without EOM
while the bound socket is RCU-visible with no references, and check
that its count stays zero.
The issue was found by Claude Opus 5.5 and Claude Fable 5.2
running Quality Playbook, an LLM-driven code review tool:
https://github.com/andrewstellman/quality-playbook
The test was drafted with OpenAI GPT-6 (Codex), this changelog with
OpenAI GPT-6 (Codex) and Claude Opus 5.5, and both were reviewed by me.
Suggested-by: Jeremy Kerr <jk@codeconstruct.com.au>
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
Tested on net 3c8881ee9806 (7.4.0-rc5) in arm64 QEMU with KASAN: this
case failed without 1/2 and passed with it, and both MCTP KUnit suites
passed with the series.
net/mctp/test/route-test.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/net/mctp/test/route-test.c b/net/mctp/test/route-test.c
index f05f75bf9..6864dc732 100644
--- a/net/mctp/test/route-test.c
+++ b/net/mctp/test/route-test.c
@@ -1717,7 +1717,37 @@ static void mctp_test_route_output_extaddr_no_eids(struct kunit *test)
mctp_test_destroy_dev(dev);
}
+static void mctp_test_route_input_sk_zero_ref(struct kunit *test)
+{
+ const struct mctp_hdr hdr = RX_HDR(1, 10, 8, FL_S | FL_TO);
+ struct mctp_test_dev *dev;
+ struct mctp_dst dst;
+ struct socket *sock;
+ struct sk_buff *skb;
+ struct sock *sk;
+ int refs, rc;
+
+ __mctp_route_test_init(test, &dev, &dst, &sock, MCTP_NET_ANY);
+ sk = sock->sk;
+ refs = refcount_read(&sk->sk_refcnt);
+ skb = mctp_test_create_skb(&hdr, 10);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
+ mctp_test_skb_set_dev(skb, dev);
+
+ /* Model a socket still visible to RCU lookup after its last put. */
+ sock_set_flag(sk, SOCK_DEAD);
+ refcount_set(&sk->sk_refcnt, 0);
+ rc = mctp_dst_input(&dst, skb);
+ KUNIT_EXPECT_NE(test, rc, 0);
+ KUNIT_EXPECT_EQ(test, refcount_read(&sk->sk_refcnt), 0);
+ refcount_set(&sk->sk_refcnt, refs + (rc == 0));
+ sock_reset_flag(sk, SOCK_DEAD);
+
+ __mctp_route_test_fini(test, dev, &dst, sock);
+}
+
static struct kunit_case mctp_test_cases[] = {
+ KUNIT_CASE(mctp_test_route_input_sk_zero_ref),
KUNIT_CASE_PARAM(mctp_test_fragment, mctp_frag_gen_params),
KUNIT_CASE_PARAM(mctp_test_rx_input, mctp_rx_input_gen_params),
KUNIT_CASE_PARAM(mctp_test_route_input_sk, mctp_route_input_sk_gen_params),
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread