net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
authorYifei Chu <Chuyf26@linux.alibaba.com>
Mon, 24 Aug 2026 02:27:19 +0000 (10:27 +0800)
committerPaolo Abeni <pabeni@redhat.com>
Thu, 27 Aug 2026 08:54:38 +0000 (10:54 +0200)
IPPROTO_SMC sockets create an internal TCP sock ("clcsock") from the
proto->init hook. When socket creation fails after proto->init has
run - e.g. a cgroup BPF program attached to BPF_CGROUP_INET_SOCK_CREATE
denies the socket - sk_common_release() only invokes sk_prot->destroy
if it is set, but neither smc_inet_prot nor smc_inet6_prot defines it,
and smc_destruct() returns early unless sk_state is SMC_CLOSED. As a
result, every failing socket(AF_INET, SOCK_STREAM, IPPROTO_SMC) call
leaks one tcp_sock, so an unprivileged task able to attach a deny-all
BPF_CGROUP_INET_SOCK_CREATE program to its own cgroup can grow kernel
memory unboundedly.

Add a .destroy hook to both protos that releases the clcsock via
smc_clcsock_release(). smc_sk_init() hashes the sock into the smc
hashinfo before the clcsock is created, and smc_diag dumps walk that
hash dereferencing smc->clcsock without taking clcsock_release_lock,
while sk_common_release() calls .destroy before .unhash. Unhash the
sock before releasing the clcsock, as __smc_release() does, so a
concurrent dump cannot observe the release; the second unhash in
sk_common_release() is a no-op.

Fixes: d25a92ccae6b ("net/smc: Introduce IPPROTO_SMC")
Reported-by: Abaci <abaci@linux.alibaba.com>
Assisted-by: abaci:qwen3.8-max
Signed-off-by: Yifei Chu <Chuyf26@linux.alibaba.com>
Reviewed-by: Dust Li <dust.li@linux.alibaba.com>
Link: https://patch.msgid.link/178753843966.342810.566471390946765094@linux.alibaba.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
net/smc/smc_inet.c

index a94084b..520b666 100644 (file)
 
 #include "smc_inet.h"
 #include "smc.h"
+#include "smc_close.h"
 
 static int smc_inet_init_sock(struct sock *sk);
+static void smc_inet_destroy_sock(struct sock *sk);
 
 static struct proto smc_inet_prot = {
        .name           = "INET_SMC",
        .owner          = THIS_MODULE,
        .init           = smc_inet_init_sock,
+       .destroy        = smc_inet_destroy_sock,
        .hash           = smc_hash_sk,
        .unhash         = smc_unhash_sk,
        .release_cb     = smc_release_cb,
@@ -68,6 +71,7 @@ static struct proto smc_inet6_prot = {
        .name           = "INET6_SMC",
        .owner          = THIS_MODULE,
        .init           = smc_inet_init_sock,
+       .destroy        = smc_inet_destroy_sock,
        .hash           = smc_hash_sk,
        .unhash         = smc_unhash_sk,
        .release_cb     = smc_release_cb,
@@ -116,6 +120,18 @@ static int smc_inet_init_sock(struct sock *sk)
        return smc_create_clcsk(net, sk, sk->sk_family);
 }
 
+static void smc_inet_destroy_sock(struct sock *sk)
+{
+       /* The sock is hashed and smc_diag dumps dereference smc->clcsock
+        * without clcsock_release_lock, while sk_common_release() calls
+        * .destroy before .unhash. Unhash first, as __smc_release() does,
+        * so no dump can observe the clcsock being released; the second
+        * unhash is a no-op.
+        */
+       sk->sk_prot->unhash(sk);
+       smc_clcsock_release(smc_sk(sk));
+}
+
 int __init smc_inet_init(void)
 {
        int rc;