Skip to contentSkip to conversation

net: mctp: don't hold a zero-refcount socket in mctp_key_alloc()

PATCH Superseded Andrew Stellman 2 participants 2 messages Dec 2 – Dec 4

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

Fixes de8a6b15d965 ("net: mctp: add an explicit reference from a mctp_sk_key to sock")

---
Tested on net-next 19c484e12ed2 (7.4.0-rc4) in arm64 QEMU with a private KUnit case that set a zero sk_refcnt on a bound socket (I didn't reproduce the close race itself): without the fix it hit the "addition on 0" and "underflow" refcount warnings, and with the fix it passed with no warning.
Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude Opus 5.5 and Claude Fable 5.2; the fix was drafted with OpenAI GPT-6 (Codex). A reproducer is available on request.

 net/mctp/route.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/mctp/route.c +4 -1
index 96fd75ea791b..5bc3a8e091d8 100644
@@ -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
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Andrew,

Quoted 10 lines

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.

Looks good, thanks.

Quoted 2 lines

Tested on net-next 19c484e12ed2 (7.4.0-rc4) in arm64 QEMU with a private KUnit case

Can we have that too?

Cheers,

Jeremy