net: mctp: avoid a zero-refcount socket hold
v2 cover net: mctp: avoid a zero-refcount socket hold
From: Andrew Stellman <[email protected]>
This v2 adds the KUnit case Jeremy Kerr requested. The code in 1/2 is unchanged from v1 and has his Reviewed-by.
---
v1: https://lore.kernel.org/netdev/[email protected]/
Andrew Stellman (2):
net: mctp: don't hold a zero-refcount socket in mctp_key_alloc()
net: mctp: test route input with a zero-refcount socket
net/mctp/route.c | 5 ++++- net/mctp/test/route-test.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 net: mctp: don't hold a zero-refcount socket in mctp_key_alloc()
From: Andrew Stellman <[email protected]>
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.
---
v2: no code change; add Jeremy Kerr's Reviewed-by.
v1: https://lore.kernel.org/netdev/[email protected]/
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(-)
net/mctp/route.c +4 -1
@@ -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; }
Signature · 1 line
2.53.0
PATCH v2 2/2 net: mctp: test route input with a zero-refcount socket
In reply to Andrew StellmanFrom: Andrew Stellman <[email protected]>
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.
---
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(+)
net/mctp/test/route-test.c +30 -0
@@ -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),
Signature · 1 line
2.53.0