net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
Subsystems (5)
PATCH 1/1 net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
IPPROTO_SMC sockets wrap an internal TCP sock ("clcsock"), which is created by smc_inet_init_sock() via smc_create_clcsk() from the proto->init hook of inet_create()/inet6_create(). When socket creation fails after proto->init has succeeded - for example when a cgroup BPF program attached to BPF_CGROUP_INET_SOCK_CREATE denies the socket - inet_create() calls sk_common_release(), which only invokes sk_prot->destroy if it is set. Neither smc_inet_prot nor smc_inet6_prot defines .destroy, and the sock destructor smc_destruct() returns early unless sk_state is SMC_CLOSED (it is SMC_INIT here), so the internal TCP sock is never released.
As a result, every failing socket(AF_INET, SOCK_STREAM, IPPROTO_SMC) call leaks one tcp_sock. Any unprivileged task able to attach a deny-all BPF_CGROUP_INET_SOCK_CREATE program to its own cgroup (or a task confined by an LSM policy) can grow kernel memory unboundedly.
Reproduced on v7.2-rc7: with a deny-all BPF_CGROUP_INET_SOCK_CREATE program attached, a loop of socket(AF_INET, SOCK_STREAM, IPPROTO_SMC) calls fails with EPERM and kmemleak reports one unreferenced tcp_sock object per call.
Add a .destroy hook to both IPPROTO_SMC protos that releases the clcsock via smc_clcsock_release(). That helper is safe here: it takes the clcsock_release_lock initialized by smc_sk_init() and skips a NULL clcsock, which is what smc_create_clcsk() leaves behind when creating the TCP sock fails. Also initialize clcsock to NULL at the start of smc_inet_init_sock(): the smc_sock slab is SLAB_TYPESAFE_BY_RCU, so recycled objects are not zeroed and stale memory must not be handed to sock_release().
The same behaviour is visible without any debugging option: on a plain kernel the slabinfo "TCP" active_objs count grows by one per failing socket() call and never shrinks.
With the fix, the same reproducer leaves no unreferenced objects in kmemleak and the "TCP" slabinfo count stays flat, and regular IPPROTO_SMC socket create/close cycles are unaffected (sk_common_release() from inet_release() also routes through the new .destroy, where the already-NULL clcsock is a no-op).
--- net/smc/smc_inet.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
net/smc/smc_inet.c +23 -0
@@ -15,13 +15,16 @@#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,
@@ -109,6 +113,14 @@ static struct inet_protosw smc_inet6_protosw = {static int smc_inet_init_sock (struct sock *sk) {struct net *net =sock_net (sk); +struct smc_sock *smc =smc_sk (sk); + +/* +* The smc_sock slab is SLAB_TYPESAFE_BY_RCU and recycled objects +* are not zeroed. .destroy may run even if .init never completed, +* so make sure smc_clcsock_release() sees a valid clcsock. +*/ + smc->clcsock =NULL ;/* init common smc sock */ smc_sk_init (net, sk,IPPROTO_SMC );
@@ -116,6 +128,17 @@ 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) +{ +/* +* If inet_create()/inet6_create() fail after .init has created the +* internal TCP sock (e.g. rejected by a cgroup BPF program), +* sk_common_release() ends up here. Release the TCP sock, otherwise +* it leaks on every failed IPPROTO_SMC socket() call. +*/ +smc_clcsock_release (smc_sk (sk)); +} +int __init smc_inet_init (void ) {int rc;
Signature · 204 lines
2.43.5
Reproducer (no clang/bpftool needed, hand-assembled BPF via bpf(2));
run as root on a cgroup-v2 system, needs CAP_BPF and CONFIG_CGROUP_BPF:
$ gcc -O2 -o smc_leak smc_leak.c
$ ./smc_leak 20000
It attaches a deny-all BPF_CGROUP_INET_SOCK_CREATE program to a fresh
cgroup, enters it and loops socket(AF_INET, SOCK_STREAM, IPPROTO_SMC).
Before this patch every call fails with EPERM and the slabinfo "TCP"
active_objs count grows by ~1 per call (20004 for 20000 iterations,
never shrinking); with this patch the count stays flat.
/* --- smc_leak.c --- */
/*
* IPPROTO_SMC socket-creation leak reproducer (no clang/bpftool needed).
*
* 1. creates a cgroupv2 subgroup, loads a hand-assembled deny-all
* BPF_CGROUP_INET_SOCK_CREATE prog via bpf(2), attaches it;
* 2. moves itself into that cgroup;
* 3. loops socket(AF_INET, SOCK_STREAM, IPPROTO_SMC);
* 4. prints slabinfo "TCP" delta and kmemleak report.
*
* Run as root on a cgroup-v2 system:
* ./smc_leak 20000
* Expected on buggy kernel: every socket() fails with EPERM and leaks one
* TCP sock per call (slabinfo "TCP" active_objs grows by ~1 per iteration;
* with CONFIG_DEBUG_KMEMLEAK, unreferenced tcp_sock objects are reported).
* On a fixed kernel: no growth.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <linux/bpf.h>
#include <stdint.h>
#ifndef IPPROTO_SMC
#define IPPROTO_SMC 256
#endif
#define BPF_RAW(CODE, DST, SRC, OFF, IMM) ((struct bpf_insn){CODE, DST, SRC, OFF, IMM})
static int sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
static int load_deny_prog(void)
{
/* r0 = 0 (deny); exit */
struct bpf_insn insns[] = {
BPF_RAW(BPF_ALU64 | BPF_MOV | BPF_K, BPF_REG_0, 0, 0, 0),
BPF_RAW(BPF_JMP | BPF_EXIT, 0, 0, 0, 0),
};
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
attr.insns = (uint64_t)(unsigned long)insns;
attr.insn_cnt = 2;
attr.license = (uint64_t)(unsigned long)"GPL";
int fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
if (fd < 0)
perror("BPF_PROG_LOAD");
return fd;
}
static int attach_prog(int prog_fd, const char *cgpath)
{
int cg_fd = open(cgpath, O_RDONLY | O_DIRECTORY);
if (cg_fd < 0) {
perror("open cgroup");
return -1;
}
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.link_create.prog_fd = prog_fd;
attr.link_create.target_fd = cg_fd;
attr.link_create.attach_type = BPF_CGROUP_INET_SOCK_CREATE;
int fd = sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
if (fd < 0) {
perror("BPF_LINK_CREATE");
close(cg_fd);
return -1;
}
return fd;
}
static long tcp_active_objs(void)
{
FILE *f = fopen("/proc/slabinfo", "r");
char line[256];
long objs = -1;
if (!f)
return -1;
while (fgets(line, sizeof(line), f)) {
char name[64];
long active;
if (sscanf(line, "%63s %ld", name, &active) == 2 &&
!strcmp(name, "TCP")) {
objs = active;
break;
}
}
fclose(f);
return objs;
}
static void kmemleak_scan(void)
{
mkdir("/sys/kernel/debug", 0755);
mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL);
FILE *f = fopen("/sys/kernel/debug/kmemleak", "w");
if (f) {
fputs("scan", f);
fclose(f);
sleep(3);
system("grep -c 'unreferenced object' /sys/kernel/debug/kmemleak 2>/dev/null | sed 's/^/kmemleak unreferenced objects: /'");
system("head -60 /sys/kernel/debug/kmemleak 2>/dev/null");
} else {
printf("(kmemleak unavailable: %s)\n", strerror(errno));
}
}
int main(int argc, char **argv)
{
const char *name = argc > 1 ? argv[1] : "smctest";
long iters = argc > 2 ? atol(argv[2]) : 20000;
char cgpath[256], path[280];
int prog_fd, link_fd;
if (access("/sys/fs/cgroup/cgroup.controllers", F_OK)) {
fprintf(stderr, "cgroup v2 required\n");
return 1;
}
snprintf(cgpath, sizeof(cgpath), "/sys/fs/cgroup/%s", name);
mkdir(cgpath, 0755);
prog_fd = load_deny_prog();
if (prog_fd < 0)
return 1;
link_fd = attach_prog(prog_fd, cgpath);
if (link_fd == -1)
return 1;
snprintf(path, sizeof(path), "%s/cgroup.procs", cgpath);
FILE *f = fopen(path, "w");
if (!f) {
perror("open cgroup.procs");
return 1;
}
fprintf(f, "%d", getpid());
fclose(f);
long before = tcp_active_objs();
printf("TCP slab objs before: %ld\n", before);
long denied = 0, ok = 0, other = 0;
for (long i = 0; i < iters; i++) {
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SMC);
if (fd >= 0) {
close(fd);
ok++;
} else if (errno == EPERM) {
denied++;
} else {
if (!other)
printf("socket error: %s\n", strerror(errno));
other++;
break;
}
}
long after = tcp_active_objs();
printf("iterations=%ld denied(EPERM)=%ld ok=%ld other=%ld\n",
iters, denied, ok, other);
printf("TCP slab objs after: %ld (delta %+ld)\n", after, after - before);
if (denied && after - before > denied / 2)
printf("=> LEAK CONFIRMED: ~%ld TCP socks leaked on error path\n",
after - before);
kmemleak_scan();
/* leave cgroup so it can be removed */
f = fopen("/sys/fs/cgroup/cgroup.procs", "w");
if (f) {
fprintf(f, "%d", getpid());
fclose(f);
}
if (link_fd >= 0)
close(link_fd);
rmdir(cgpath);
return 0;
}
/* --- end smc_leak.c --- */[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
IPPROTO_SMCsockets wrap an internal TCP sock ("clcsock"), which is created bysmc_inet_init_sock()viasmc_create_clcsk()from theproto->inithook of inet_create()/inet6_create(). When socket creation fails afterproto->inithas succeeded - for example when a cgroup BPF program attached toBPF_CGROUP_INET_SOCK_CREATEdenies the socket -inet_create()callssk_common_release(), which only invokessk_prot->destroyif it is set. Neithersmc_inet_protnorsmc_inet6_protdefines .destroy, and the sock destructorsmc_destruct()returns early unlesssk_stateisSMC_CLOSED(it isSMC_INIThere), so the internal TCP sock is never released.As a result, every failing
socket(AF_INET, SOCK_STREAM, IPPROTO_SMC)call leaks onetcp_sock. Any unprivileged task able to attach a deny-allBPF_CGROUP_INET_SOCK_CREATEprogram to its own cgroup (or a task confined by an LSM policy) can grow kernel memory unboundedly. [ ... ]
Quoted 4 lines
Fixes d25a92ccae6b ("net/smc: Introduce IPPROTO_SMC")Reported-by Abaci <[email protected]>Assisted-by abaci:qwen3.8-maxSigned-off-by Chuyf26 <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260812071538.o7mdtMhZ1eOzDgQnKZduQfWlq0FhL7OCAPLjt9AA11w@z?part=1
A message that is not in the archive (sent off-list or not yet received): [email protected]
Sidraya Jayagond
maintainerQuoted 34 lines · On 12/08/26 12:45 pm, Chuyf26 wrote:
IPPROTO_SMCsockets wrap an internal TCP sock ("clcsock"), which is created bysmc_inet_init_sock()viasmc_create_clcsk()from theproto->inithook of inet_create()/inet6_create(). When socket creation fails afterproto->inithas succeeded - for example when a cgroup BPF program attached toBPF_CGROUP_INET_SOCK_CREATEdenies the socket -inet_create()callssk_common_release(), which only invokessk_prot->destroyif it is set. Neithersmc_inet_protnorsmc_inet6_protdefines .destroy, and the sock destructorsmc_destruct()returns early unlesssk_stateisSMC_CLOSED(it isSMC_INIThere), so the internal TCP sock is never released.As a result, every failing
socket(AF_INET, SOCK_STREAM, IPPROTO_SMC)call leaks onetcp_sock. Any unprivileged task able to attach a deny-allBPF_CGROUP_INET_SOCK_CREATEprogram to its own cgroup (or a task confined by an LSM policy) can grow kernel memory unboundedly.Reproduced on v7.2-rc7: with a deny-all
BPF_CGROUP_INET_SOCK_CREATEprogram attached, a loop ofsocket(AF_INET, SOCK_STREAM, IPPROTO_SMC)calls fails with EPERM and kmemleak reports one unreferencedtcp_sockobject per call.Add a .destroy hook to both
IPPROTO_SMCprotos that releases the clcsock viasmc_clcsock_release(). That helper is safe here: it takes theclcsock_release_lockinitialized bysmc_sk_init()and skips a NULL clcsock, which is whatsmc_create_clcsk()leaves behind when creating the TCP sock fails. Also initialize clcsock to NULL at the start ofsmc_inet_init_sock(): thesmc_sockslab isSLAB_TYPESAFE_BY_RCU, so recycled objects are not zeroed and stale memory must not be handed tosock_release().The same behaviour is visible without any debugging option: on a plain kernel the slabinfo "TCP"
active_objscount grows by one per failingsocket()call and never shrinks.
Quoted 6 lines
With the fix, the same reproducer leaves no unreferenced objects in kmemleak and the "TCP" slabinfo count stays flat, and regular
IPPROTO_SMCsocket create/close cycles are unaffected (sk_common_release()frominet_release()also routes through the new .destroy, where the already-NULL clcsock is a no-op).
The fix looks good, but I think the commit message is longer than needed and spends too much space on reproducer details and internal call path narration.
I would trim the detailed reproducer/results text and most of the internal call path explanation, and keep it focused on the leak, the failure path, and why adding .destroy plus clcsock = NULL fixes the issue. If you want to keep the reproducer and validation details, please move those below ... instead of keeping them in the main commit message body.
Thank you,
Sidraya
Quoted 70 lines
Fixes d25a92ccae6b ("net/smc: Introduce IPPROTO_SMC")Reported-by Abaci <[email protected]>Assisted-by abaci:qwen3.8-maxSigned-off-by Chuyf26 <[email protected]>--- net/smc/smc_inet.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)net/smc/smc_inet.c +23
-0@@ -15,13 +15,16 @@#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,@@ -109,6 +113,14 @@ static struct inet_protosw smc_inet6_protosw = {static int smc_inet_init_sock (struct sock *sk) {struct net *net =sock_net (sk); +struct smc_sock *smc =smc_sk (sk); + +/* +* The smc_sock slab is SLAB_TYPESAFE_BY_RCU and recycled objects +* are not zeroed. .destroy may run even if .init never completed, +* so make sure smc_clcsock_release() sees a valid clcsock. +*/ + smc->clcsock =NULL ;/* init common smc sock */ smc_sk_init (net, sk,IPPROTO_SMC );@@ -116,6 +128,17 @@ 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) +{ +/* +* If inet_create()/inet6_create() fail after .init has created the +* internal TCP sock (e.g. rejected by a cgroup BPF program), +* sk_common_release() ends up here. Release the TCP sock, otherwise +* it leaks on every failed IPPROTO_SMC socket() call. +*/ +smc_clcsock_release (smc_sk (sk)); +} +int __init smc_inet_init (void ) {int rc;
PATCH v2 1/1 net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
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(), which is safe here because it skips a NULL clcsock under clcsock_release_lock. Also initialize clcsock to NULL when setting the sock up: the smc_sock slab is SLAB_TYPESAFE_BY_RCU, so recycled objects are not zeroed.
--- net/smc/smc_inet.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
net/smc/smc_inet.c +23 -0
@@ -15,13 +15,16 @@#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,
@@ -109,6 +113,14 @@ static struct inet_protosw smc_inet6_protosw = {static int smc_inet_init_sock (struct sock *sk) {struct net *net =sock_net (sk); +struct smc_sock *smc =smc_sk (sk); + +/* +* The smc_sock slab is SLAB_TYPESAFE_BY_RCU and recycled objects +* are not zeroed. .destroy may run even if .init never completed, +* so make sure smc_clcsock_release() sees a valid clcsock. +*/ + smc->clcsock =NULL ;/* init common smc sock */ smc_sk_init (net, sk,IPPROTO_SMC );
@@ -116,6 +128,17 @@ 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) +{ +/* +* If inet_create()/inet6_create() fail after .init has created the +* internal TCP sock (e.g. rejected by a cgroup BPF program), +* sk_common_release() ends up here. Release the TCP sock, otherwise +* it leaks on every failed IPPROTO_SMC socket() call. +*/ +smc_clcsock_release (smc_sk (sk)); +} +int __init smc_inet_init (void ) {int rc;
Signature · 210 lines
2.43.5
Thanks for the review. Changes since v1:
- Trimmed the commit message as suggested; the code is unchanged.
- Kept the reproducer and measurements below this separator, in
case anyone wants to give the leak a spin or double-check the fix.
Reproducer (no clang/bpftool needed, hand-assembled BPF via bpf(2));
run as root on a cgroup-v2 system, needs CAP_BPF and CONFIG_CGROUP_BPF:
$ gcc -O2 -o smc_leak smc_leak.c
$ ./smc_leak 20000
It attaches a deny-all BPF_CGROUP_INET_SOCK_CREATE program to a fresh
cgroup, enters it and loops socket(AF_INET, SOCK_STREAM, IPPROTO_SMC).
Before this patch every call fails with EPERM and the slabinfo "TCP"
active_objs count grows by ~1 per call (20004 for 20000 iterations,
never shrinking); with this patch the count stays flat.
/* --- smc_leak.c --- */
/*
* IPPROTO_SMC socket-creation leak reproducer (no clang/bpftool needed).
*
* 1. creates a cgroupv2 subgroup, loads a hand-assembled deny-all
* BPF_CGROUP_INET_SOCK_CREATE prog via bpf(2), attaches it;
* 2. moves itself into that cgroup;
* 3. loops socket(AF_INET, SOCK_STREAM, IPPROTO_SMC);
* 4. prints slabinfo "TCP" delta and kmemleak report.
*
* Run as root on a cgroup-v2 system:
* ./smc_leak 20000
* Expected on buggy kernel: every socket() fails with EPERM and leaks one
* TCP sock per call (slabinfo "TCP" active_objs grows by ~1 per iteration;
* with CONFIG_DEBUG_KMEMLEAK, unreferenced tcp_sock objects are reported).
* On a fixed kernel: no growth.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <linux/bpf.h>
#include <stdint.h>
#ifndef IPPROTO_SMC
#define IPPROTO_SMC 256
#endif
#define BPF_RAW(CODE, DST, SRC, OFF, IMM) ((struct bpf_insn){CODE, DST, SRC, OFF, IMM})
static int sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
static int load_deny_prog(void)
{
/* r0 = 0 (deny); exit */
struct bpf_insn insns[] = {
BPF_RAW(BPF_ALU64 | BPF_MOV | BPF_K, BPF_REG_0, 0, 0, 0),
BPF_RAW(BPF_JMP | BPF_EXIT, 0, 0, 0, 0),
};
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
attr.insns = (uint64_t)(unsigned long)insns;
attr.insn_cnt = 2;
attr.license = (uint64_t)(unsigned long)"GPL";
int fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
if (fd < 0)
perror("BPF_PROG_LOAD");
return fd;
}
static int attach_prog(int prog_fd, const char *cgpath)
{
int cg_fd = open(cgpath, O_RDONLY | O_DIRECTORY);
if (cg_fd < 0) {
perror("open cgroup");
return -1;
}
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.link_create.prog_fd = prog_fd;
attr.link_create.target_fd = cg_fd;
attr.link_create.attach_type = BPF_CGROUP_INET_SOCK_CREATE;
int fd = sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
if (fd < 0) {
perror("BPF_LINK_CREATE");
close(cg_fd);
return -1;
}
return fd;
}
static long tcp_active_objs(void)
{
FILE *f = fopen("/proc/slabinfo", "r");
char line[256];
long objs = -1;
if (!f)
return -1;
while (fgets(line, sizeof(line), f)) {
char name[64];
long active;
if (sscanf(line, "%63s %ld", name, &active) == 2 &&
!strcmp(name, "TCP")) {
objs = active;
break;
}
}
fclose(f);
return objs;
}
static void kmemleak_scan(void)
{
mkdir("/sys/kernel/debug", 0755);
mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL);
FILE *f = fopen("/sys/kernel/debug/kmemleak", "w");
if (f) {
fputs("scan", f);
fclose(f);
sleep(3);
system("grep -c 'unreferenced object' /sys/kernel/debug/kmemleak 2>/dev/null | sed 's/^/kmemleak unreferenced objects: /'");
system("head -60 /sys/kernel/debug/kmemleak 2>/dev/null");
} else {
printf("(kmemleak unavailable: %s)\n", strerror(errno));
}
}
int main(int argc, char **argv)
{
const char *name = argc > 1 ? argv[1] : "smctest";
long iters = argc > 2 ? atol(argv[2]) : 20000;
char cgpath[256], path[280];
int prog_fd, link_fd;
if (access("/sys/fs/cgroup/cgroup.controllers", F_OK)) {
fprintf(stderr, "cgroup v2 required\n");
return 1;
}
snprintf(cgpath, sizeof(cgpath), "/sys/fs/cgroup/%s", name);
mkdir(cgpath, 0755);
prog_fd = load_deny_prog();
if (prog_fd < 0)
return 1;
link_fd = attach_prog(prog_fd, cgpath);
if (link_fd == -1)
return 1;
snprintf(path, sizeof(path), "%s/cgroup.procs", cgpath);
FILE *f = fopen(path, "w");
if (!f) {
perror("open cgroup.procs");
return 1;
}
fprintf(f, "%d", getpid());
fclose(f);
long before = tcp_active_objs();
printf("TCP slab objs before: %ld\n", before);
long denied = 0, ok = 0, other = 0;
for (long i = 0; i < iters; i++) {
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SMC);
if (fd >= 0) {
close(fd);
ok++;
} else if (errno == EPERM) {
denied++;
} else {
if (!other)
printf("socket error: %s\n", strerror(errno));
other++;
break;
}
}
long after = tcp_active_objs();
printf("iterations=%ld denied(EPERM)=%ld ok=%ld other=%ld\n",
iters, denied, ok, other);
printf("TCP slab objs after: %ld (delta %+ld)\n", after, after - before);
if (denied && after - before > denied / 2)
printf("=> LEAK CONFIRMED: ~%ld TCP socks leaked on error path\n",
after - before);
kmemleak_scan();
/* leave cgroup so it can be removed */
f = fopen("/sys/fs/cgroup/cgroup.procs", "w");
if (f) {
fprintf(f, "%d", getpid());
fclose(f);
}
if (link_fd >= 0)
close(link_fd);
rmdir(cgpath);
return 0;
}
/* --- end smc_leak.c --- */[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
IPPROTO_SMCsockets create an internal TCP sock ("clcsock") from theproto->inithook. When socket creation fails afterproto->inithas run - e.g. a cgroup BPF program attached toBPF_CGROUP_INET_SOCK_CREATEdenies the socket -sk_common_release()only invokessk_prot->destroyif it is set, but neithersmc_inet_protnorsmc_inet6_protdefines it, andsmc_destruct()returns early unlesssk_stateisSMC_CLOSED. As a result, every failingsocket(AF_INET, SOCK_STREAM, IPPROTO_SMC)call leaks onetcp_sock, so an unprivileged task able to attach a deny-allBPF_CGROUP_INET_SOCK_CREATEprogram to its own cgroup can grow kernel memory unboundedly.Add a .destroy hook to both protos that releases the clcsock via
smc_clcsock_release(), which is safe here because it skips a NULL clcsock underclcsock_release_lock. Also initialize clcsock to NULL when setting the sock up: thesmc_sockslab isSLAB_TYPESAFE_BY_RCU, so recycled objects are not zeroed.
Quoted 4 lines
Fixes d25a92ccae6b ("net/smc: Introduce IPPROTO_SMC")Reported-by Abaci <[email protected]>Assisted-by abaci:qwen3.8-maxSigned-off-by Chuyf26 <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260813060456.b48HqF9m6NTfFXvq_PL9w_Ih493OogqAqxfAHAfU74A@z?part=1
Paolo Abeni
maintainerThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
This adds a .destroy hook to smc_inet_prot and smc_inet6_prot that calls smc_clcsock_release(), so the internal TCP sock created by smc_inet_init_sock() is freed when socket creation fails after proto->init has run. It also stores NULL into smc->clcsock at the top of smc_inet_init_sock().
A couple of questions below.
Quoted 21 lines
net/smc/smc_inet.c +3
-0@@ -15,13 +15,16 @@#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,
[ ... ]
net/smc/smc_inet.c static struct inet_protosw smc_inet6_protosw = { · line 123
Quoted 12 lines
net/smc/smc_inet.c +8
-0@@ -109,6 +113,14 @@ static struct inet_protosw smc_inet6_protosw = {static int smc_inet_init_sock (struct sock *sk) {struct net *net =sock_net (sk); +struct smc_sock *smc =smc_sk (sk); + +/* +* The smc_sock slab is SLAB_TYPESAFE_BY_RCU and recycled objects +* are not zeroed. .destroy may run even if .init never completed, +* so make sure smc_clcsock_release() sees a valid clcsock. +*/ + smc->clcsock =NULL ;
[Severity: Low]
Is the premise of this comment (and of the matching changelog paragraph, "Also initialize clcsock to NULL when setting the sock up: the smc_sock slab is SLAB_TYPESAFE_BY_RCU, so recycled objects are not zeroed") correct?
sk_alloc() always adds __GFP_ZERO, and sk_prot_alloc() then clears the whole protocol object:
net/core/sock.c:sk_alloc() {
sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
...
}net/core/sock.c:sk_prot_alloc() {
sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
if (!sk)
return sk;
if (want_init_on_alloc(priority))
sk_prot_clear_nulls(sk, prot->obj_size);
...
}sk_prot_clear_nulls() memsets everything except sk_node.next, and smc_sock.clcsock sits well past sk_node, so isn't clcsock already NULL on entry to .init even for a recycled SLAB_TYPESAFE_BY_RCU object, making this store dead code?
The second sentence of the comment also seems hard to satisfy: the mutex that smc_clcsock_release() takes is only initialized later, inside smc_sk_init():
net/smc/af_smc.c:smc_sk_init() {
...
mutex_init(&smc->clcsock_release_lock);
...
}So if .destroy really could run before .init completed, wouldn't smc_clcsock_release() lock an uninitialized mutex regardless of this NULL store? Could the comment and changelog paragraph be dropped or reworded to match what the code actually guarantees?
Quoted 7 lines
net/smc/smc_inet.c +0
-0/* init common smc sock */ smc_sk_init (net, sk,IPPROTO_SMC );@@ -116,6 +128,17 @@ static int smc_inet_init_sock(struct sock *sk)return smc_create_clcsk (net, sk, sk->sk_family); }
net/smc/smc_inet.c static int smc_inet_init_sock(struct sock *sk) · line 140
Quoted 10 lines
net/smc/smc_inet.c +10
-0+static void smc_inet_destroy_sock (struct sock *sk) +{ +/* +* If inet_create()/inet6_create() fail after .init has created the +* internal TCP sock (e.g. rejected by a cgroup BPF program), +* sk_common_release() ends up here. Release the TCP sock, otherwise +* it leaks on every failed IPPROTO_SMC socket() call. +*/ +smc_clcsock_release (smc_sk (sk)); +}
[Severity: High]
Can this free the internal TCP socket while the SMC sock is still on the SMC diag hash list?
sk_common_release() runs .destroy before .unhash:
net/core/sock.c:sk_common_release() {
if (sk->sk_prot->destroy)
sk->sk_prot->destroy(sk);
...
sk->sk_prot->unhash(sk);
...
}and the sk was already inserted into smc_v4_hashinfo/smc_v6_hashinfo by smc_sk_init() -> sk->sk_prot->hash(sk) == smc_hash_sk() before smc_create_clcsk() ran, so when smc_inet_destroy_sock() calls smc_clcsock_release() the sock is still visible to the diag dumper.
The reader side walks that same hashinfo holding only the hashinfo rwlock, and does an unlocked check-then-dereference of smc->clcsock:
net/smc/smc_diag.c:smc_diag_dump_proto() {
read_lock(&prot->h.smc_hash->lock);
head = &prot->h.smc_hash->ht;
...
sk_for_each(sk, head) {
...
}net/smc/smc_diag.c:smc_diag_msg_common_fill() {
...
if (!smc->clcsock)
return;
r->id.idiag_sport = htons(smc->clcsock->sk->sk_num);
r->id.idiag_dport = smc->clcsock->sk->sk_dport;
...
}while the writer holds only clcsock_release_lock:
net/smc/smc_close.c:smc_clcsock_release() {
mutex_lock(&smc->clcsock_release_lock);
if (smc->clcsock) {
tcp = smc->clcsock;
smc->clcsock = NULL;
sock_release(tcp);
}
...
}With an empty lockset intersection, can a concurrent AF_SMC diag dump (for example "ss --smc", which has no capability check in smc_diag_handler_dump()) re-read smc->clcsock as NULL after the NULL check and oops, or dereference the just-released struct socket / tcp_sock and copy freed memory into the netlink reply?
The existing teardown path unhashes first and only then drops the clcsock:
net/smc/af_smc.c:__smc_release() {
sk->sk_prot->unhash(sk);
if (sk->sk_state == SMC_CLOSED) {
if (smc->clcsock) {
release_sock(sk);
smc_clcsock_release(smc);
...
}Would it be better to keep that ordering here, for instance by unhashing before releasing the clcsock in the destroy path, or by taking clcsock_release_lock in the diag reader?
The lockless clcsock read in smc_diag is itself older than this patch (see also smc_close_active_abort() and smc_close_passive_work()), but on the socket-creation-failure path nothing was freed while the sk was hashed before this change, since the clcsock was simply leaked.
Quoted 4 lines
net/smc/smc_inet.c +0
-0int __init smc_inet_init (void ) {int rc;
Signature · 1 line
This is an AI-generated review.
PATCH v3 1/1 net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
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.
---
Changes since v2 (answering the review):
- dropped the smc->clcsock = NULL store: sk_alloc() zeroes the sock (__GFP_ZERO / sk_prot_clear_nulls()), so it is already NULL. The mutex concern does not apply either: .destroy only runs after smc_sk_init() initialized clcsock_release_lock.
- unhash the sock before releasing the clcsock in .destroy so a concurrent smc_diag dump cannot observe the release (sk_common_release() calls .destroy before .unhash); the second unhash is a no-op. Tested on a KASAN+SMC_DIAG kernel: the v2 version hits a KASAN report in __smc_diag_dump() when a diag dump races with failing IPPROTO_SMC socket creation, v3 stays clean under the same stress.
net/smc/smc_inet.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
net/smc/smc_inet.c +16 -0
@@ -15,13 +15,16 @@#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;
Signature · 1 line
2.43.5
Jakub Kicinski
maintainerQuoted 1 line · On Fri, 21 Aug 2026 15:38:23 +0800 Chuyf26 wrote:
Signed-off-by Chuyf26 <[email protected]>
We need something more official-name like for the author and the sign off. The numbers do not look right/
It'd also be great if you paid attention to the automated emails you're receiving telling to not post new patches in reply to the old ones
Signature · 1 line
pw-bot: cr
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] A preexisting race condition in smc_close_active_abort and smc_close_passive_work can cause a Use-After-Free or NULL pointer dereference when racing with a concurrent smc_diag dump. --
--- Patch [1]: [PATCH v3] net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure --- commit e59fabfdc9a39ca0071e05ed30e01dc2a6a78c65
Author: Chuyf26 <[email protected]>
net/smc: release the internal TCP sock on IPPROTO_SMC socket creation failure
This commit adds a .destroy hook to SMC protocols to release the internal TCP clcsock when socket creation fails. It specifically unhashes the socket before releasing the clcsock to prevent concurrent smc_diag dumps from dereferencing a freed socket.
Quoted 4 lines
net/smc/smc_inet.c +0
-0
[ ... ]
Quoted 6 lines
net/smc/smc_inet.c +2
-0@@ -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) +{
net/smc/smc_inet.c static int smc_inet_init_sock(struct sock *sk) · line 134
Quoted 10 lines
net/smc/smc_inet.c +10
-0+/* 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)); +} +
[Severity: High]
This is a pre-existing issue, but since the comments mention that smc_diag dumps dereference smc->clcsock without locks, could this same race occur in smc_close_active_abort() and smc_close_passive_work()?
In net/smc/smc_close.c:smc_close_active_abort(), clcsock is released:
if (release_clcsock) {
release_sock(sk);
smc_clcsock_release(smc);
lock_sock(sk);
}And similarly in net/smc/smc_close.c:smc_close_passive_work():
release_sock(sk);
if (release_clcsock)
smc_clcsock_release(smc);
sock_put(sk);In these paths, smc_clcsock_release() is called, but it appears the socket is still in the hash table as unhashing typically happens later.
Concurrently, a dump in net/smc/smc_diag.c:smc_diag_msg_common_fill() does:
if (!smc->clcsock)
return;
r->id.idiag_sport = htons(smc->clcsock->sk->sk_num);Could a concurrent smc_diag dump hit a use-after-free or NULL pointer dereference here if a link group failure or peer disconnect triggers socket closure while the dump iterates the hash table?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 77 lines · On 13/08/26 11:34 am, Chuyf26 wrote:
IPPROTO_SMCsockets create an internal TCP sock ("clcsock") from theproto->inithook. When socket creation fails afterproto->inithas run - e.g. a cgroup BPF program attached toBPF_CGROUP_INET_SOCK_CREATEdenies the socket -sk_common_release()only invokessk_prot->destroyif it is set, but neithersmc_inet_protnorsmc_inet6_protdefines it, andsmc_destruct()returns early unlesssk_stateisSMC_CLOSED. As a result, every failingsocket(AF_INET, SOCK_STREAM, IPPROTO_SMC)call leaks onetcp_sock, so an unprivileged task able to attach a deny-allBPF_CGROUP_INET_SOCK_CREATEprogram to its own cgroup can grow kernel memory unboundedly.Add a .destroy hook to both protos that releases the clcsock via
smc_clcsock_release(), which is safe here because it skips a NULL clcsock underclcsock_release_lock. Also initialize clcsock to NULL when setting the sock up: thesmc_sockslab isSLAB_TYPESAFE_BY_RCU, so recycled objects are not zeroed.Fixes d25a92ccae6b ("net/smc: Introduce IPPROTO_SMC")Reported-by Abaci <[email protected]>Assisted-by abaci:qwen3.8-maxSigned-off-by Chuyf26 <[email protected]>--- net/smc/smc_inet.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)net/smc/smc_inet.c +16
-0@@ -15,13 +15,16 @@#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,@@ -109,6 +113,14 @@ static struct inet_protosw smc_inet6_protosw = {static int smc_inet_init_sock (struct sock *sk) {struct net *net =sock_net (sk); +struct smc_sock *smc =smc_sk (sk); + +/* +* The smc_sock slab is SLAB_TYPESAFE_BY_RCU and recycled objects +* are not zeroed. .destroy may run even if .init never completed, +* so make sure smc_clcsock_release() sees a valid clcsock. +*/ + smc->clcsock =NULL ;/* init common smc sock */ smc_sk_init (net, sk,IPPROTO_SMC );@@ -116,6 +128,17 @@ 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) +{ +/* +* If inet_create()/inet6_create() fail after .init has created the
net/smc/smc_inet.c static int smc_inet_init_sock(struct sock *sk) · line 144
Quoted 10 lines
net/smc/smc_inet.c +7
-0+* internal TCP sock (e.g. rejected by a cgroup BPF program), +* sk_common_release() ends up here. Release the TCP sock, otherwise +* it leaks on every failed IPPROTO_SMC socket() call. +*/ +smc_clcsock_release (smc_sk (sk)); +} +int __init smc_inet_init (void ) {int rc;
Thank you for fixing this.