Skip to contentSkip to conversation

net/smc: fix out-of-bounds read when sk_user_data holds a sk_psock

PATCH In review Sechang Lim 4 participants 5 messages Jun 29 – Jul 2

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

PATCH v3 1/1 net/smc: fix out-of-bounds read when sk_user_data holds a sk_psock

A passive-open child inherits the listener's smc_clcsock_data_ready(). sk_clone_lock() clears its sk_user_data to NULL because the listener tagged it SK_USER_DATA_NOCOPY. Until accept restores the callback, a BPF sock_ops program can add the established child to a sockmap, and sk_psock_init() installs a sk_psock into the NULL sk_user_data. The inherited callback then reads it back through smc_clcsock_user_data(), which strips only NOCOPY, takes the sk_psock for an smc_sock, and dereferences a clcsk_* field past its end:

  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
   [...]
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   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
   [...]

Resolve the conflict on the write path. Reserve the child's sk_user_data with a NULL pointer tagged SK_USER_DATA_NOCOPY so sk_psock_init() returns -EBUSY, and release it at accept. smc_clcsock_user_data() still strips the tag to NULL, so the inherited callback stays a no-op.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")

---
v3:
- reserve sk_user_data on the write path instead of the read-side check (D. Wythe)

v2:
- https://lore.kernel.org/netdev/[email protected]/

v1:
- https://lore.kernel.org/netdev/[email protected]/

 net/smc/af_smc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/smc/af_smc.c +9 -1
index b5db69073e20..78f162344fe3 100644
@@ -154,7 +154,11 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 					       own_req, opt_child_init);
 	/* child must not inherit smc or its ops */
 	if (child) {
-		rcu_assign_sk_user_data(child, NULL);
+		/* reserve sk_user_data so sockmap cannot claim the slot */
+		write_lock_bh(&child->sk_callback_lock);
+		__rcu_assign_sk_user_data_with_flags(child, NULL,
+						     SK_USER_DATA_NOCOPY);
+		write_unlock_bh(&child->sk_callback_lock);
 
 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
@@ -1773,6 +1777,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 	/* new clcsock has inherited the smc listen-specific sk_data_ready
 	 * function; switch it back to the original sk_data_ready function
 	 */
+	write_lock_bh(&new_clcsock->sk->sk_callback_lock);
 	new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
 
 	/* if new clcsock has also inherited the fallback-specific callback
@@ -1786,6 +1791,9 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (lsmc->clcsk_error_report)
 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
 	}
+	/* release the slot reserved in smc_tcp_syn_recv_sock() */
+	rcu_assign_sk_user_data(new_clcsock->sk, NULL);
+	write_unlock_bh(&new_clcsock->sk->sk_callback_lock);
 
 	(*new_smc)->clcsock = new_clcsock;
 out:
Signature · 1 line
2.43.0
#1 lore raw
Thank 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:
- [Critical] Unconditional overwrite of sk_user_data during SMC fallback causes sk_psock memory leak and critical type confusion (control flow hijack). --

commit c092e3d84654761aba7753030c182ca2840fc519
Author: Sechang Lim <[email protected]>

net/smc: fix out-of-bounds read when sk_user_data holds a sk_psock

A passive-open child inherits the listener's smc_clcsock_data_ready callback. This commit reserves the child's sk_user_data with a NULL pointer tagged SK_USER_DATA_NOCOPY to prevent sockmap from claiming the slot, and releases it at accept.

Quoted 4 lines
net/smc/af_smc.c +0 -0
index b5db69073e20f..78f162344fe3a 100644

[ ... ]

net/smc/af_smc.c static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) · line 1795

Quoted 6 lines
net/smc/af_smc.c +2 -0
@@ -1786,6 +1791,9 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (lsmc->clcsk_error_report)
 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
 	}
+	/* release the slot reserved in smc_tcp_syn_recv_sock() */
+	rcu_assign_sk_user_data(new_clcsock->sk, NULL);

[Severity: Critical]
This is a pre-existing issue, but does clearing this reservation to NULL explicitly re-open the window for a BPF sockmap program to attach to the socket during the subsequent SMC handshake?

If a BPF SOCK_OPS program attaches the established TCP socket to a sockmap, sk_psock_init() can set sk_user_data to a new sk_psock and hijack the socket callbacks. Should the SMC handshake fail and fall back to TCP, the fallback path appears to blindly overwrite sk_user_data:

net/smc/af_smc.c:smc_fback_replace_callbacks() {
        ...
        write_lock_bh(&clcsk->sk_callback_lock);
        __rcu_assign_sk_user_data_with_flags(clcsk, smc, SK_USER_DATA_NOCOPY);
        ...
}

Can this lead to a type confusion if the hijacked sockmap callbacks are left active? When TCP data arrives, smc_fback_data_ready would forward to the sockmap callback which dereferences sk_user_data expecting a struct sk_psock, but receives a struct smc_sock instead.

Quoted 4 lines
net/smc/af_smc.c +1 -0
+	write_unlock_bh(&new_clcsock->sk->sk_callback_lock);
 
 	(*new_smc)->clcsock = new_clcsock;
 out:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Sechang Lim
Quoted 74 lines · Sechang Lim

A passive-open child inherits the listener's smc_clcsock_data_ready(). sk_clone_lock() clears its sk_user_data to NULL because the listener tagged it SK_USER_DATA_NOCOPY. Until accept restores the callback, a BPF sock_ops program can add the established child to a sockmap, and sk_psock_init() installs a sk_psock into the NULL sk_user_data. The inherited callback then reads it back through smc_clcsock_user_data(), which strips only NOCOPY, takes the sk_psock for an smc_sock, and dereferences a clcsk_* field past its end:

  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
   [...]
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   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
   [...]

Resolve the conflict on the write path. Reserve the child's sk_user_data with a NULL pointer tagged SK_USER_DATA_NOCOPY so sk_psock_init() returns -EBUSY, and release it at accept. smc_clcsock_user_data() still strips the tag to NULL, so the inherited callback stays a no-op.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")

---
v3:
- reserve sk_user_data on the write path instead of the read-side check (D. Wythe)

v2:
- https://lore.kernel.org/netdev/[email protected]/

v1:
- https://lore.kernel.org/netdev/[email protected]/

 net/smc/af_smc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/smc/af_smc.c +6 -1
index b5db69073e20..78f162344fe3 100644
@@ -154,7 +154,11 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 					       own_req, opt_child_init);
 	/* child must not inherit smc or its ops */
 	if (child) {
-		rcu_assign_sk_user_data(child, NULL);
+		/* reserve sk_user_data so sockmap cannot claim the slot */
+		write_lock_bh(&child->sk_callback_lock);
+		__rcu_assign_sk_user_data_with_flags(child, NULL,
+						     SK_USER_DATA_NOCOPY);
+		write_unlock_bh(&child->sk_callback_lock);
 
 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
@@ -1773,6 +1777,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 	/* new clcsock has inherited the smc listen-specific sk_data_ready
 	 * function; switch it back to the original sk_data_ready function
 	 */
+	write_lock_bh(&new_clcsock->sk->sk_callback_lock);
 	new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
 
 	/* if new clcsock has also inherited the fallback-specific callback

net/smc/af_smc.c static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) · line 1796

Quoted 6 lines
net/smc/af_smc.c +3 -0
@@ -1786,6 +1791,9 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (lsmc->clcsk_error_report)
 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
 	}
+	/* release the slot reserved in smc_tcp_syn_recv_sock() */
+	rcu_assign_sk_user_data(new_clcsock->sk, NULL);
+	write_unlock_bh(&new_clcsock->sk->sk_callback_lock);

Sashiko reports that this still cause problem on fallback.

@Wythe, I understand from previous discussion that you would prefer to address such issues separately (and thus you are fine with the patch in the current form). Could you please confirm?

/P

Quoted 3 lines
net/smc/af_smc.c +0 -0
 	(*new_smc)->clcsock = new_clcsock;
 out:
maintainer
#3 lore raw
Quoted 84 lines · Paolo Abeni
Quoted 74 lines · Sechang Lim

A passive-open child inherits the listener's smc_clcsock_data_ready(). sk_clone_lock() clears its sk_user_data to NULL because the listener tagged it SK_USER_DATA_NOCOPY. Until accept restores the callback, a BPF sock_ops program can add the established child to a sockmap, and sk_psock_init() installs a sk_psock into the NULL sk_user_data. The inherited callback then reads it back through smc_clcsock_user_data(), which strips only NOCOPY, takes the sk_psock for an smc_sock, and dereferences a clcsk_* field past its end:

  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
   [...]
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   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
   [...]

Resolve the conflict on the write path. Reserve the child's sk_user_data with a NULL pointer tagged SK_USER_DATA_NOCOPY so sk_psock_init() returns -EBUSY, and release it at accept. smc_clcsock_user_data() still strips the tag to NULL, so the inherited callback stays a no-op.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")

---
v3:
- reserve sk_user_data on the write path instead of the read-side check (D. Wythe)

v2:
- https://lore.kernel.org/netdev/[email protected]/

v1:
- https://lore.kernel.org/netdev/[email protected]/

 net/smc/af_smc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/smc/af_smc.c +6 -1
index b5db69073e20..78f162344fe3 100644
@@ -154,7 +154,11 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 					       own_req, opt_child_init);
 	/* child must not inherit smc or its ops */
 	if (child) {
-		rcu_assign_sk_user_data(child, NULL);
+		/* reserve sk_user_data so sockmap cannot claim the slot */
+		write_lock_bh(&child->sk_callback_lock);
+		__rcu_assign_sk_user_data_with_flags(child, NULL,
+						     SK_USER_DATA_NOCOPY);
+		write_unlock_bh(&child->sk_callback_lock);
 
 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
@@ -1773,6 +1777,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 	/* new clcsock has inherited the smc listen-specific sk_data_ready
 	 * function; switch it back to the original sk_data_ready function
 	 */
+	write_lock_bh(&new_clcsock->sk->sk_callback_lock);
 	new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
 
 	/* if new clcsock has also inherited the fallback-specific callback

net/smc/af_smc.c static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) · line 1796

Quoted 6 lines
net/smc/af_smc.c +3 -0
@@ -1786,6 +1791,9 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (lsmc->clcsk_error_report)
 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
 	}
+	/* release the slot reserved in smc_tcp_syn_recv_sock() */
+	rcu_assign_sk_user_data(new_clcsock->sk, NULL);
+	write_unlock_bh(&new_clcsock->sk->sk_callback_lock);

Sashiko reports that this still cause problem on fallback.

Quoted 3 lines

@Wythe, I understand from previous discussion that you would prefer to address such issues separately (and thus you are fine with the patch in the current form). Could you please confirm?

No, I do not agree with this patch in any form. As I replied to it, I do not think this is the right direction.

D. Wythe

Quoted 5 lines

/P

Quoted 3 lines
net/smc/af_smc.c +0 -0
 	(*new_smc)->clcsock = new_clcsock;
 out:
maintainer
#4 lore raw
In reply to Sechang Lim
Quoted 75 lines · Sechang Lim

A passive-open child inherits the listener's smc_clcsock_data_ready(). sk_clone_lock() clears its sk_user_data to NULL because the listener tagged it SK_USER_DATA_NOCOPY. Until accept restores the callback, a BPF sock_ops program can add the established child to a sockmap, and sk_psock_init() installs a sk_psock into the NULL sk_user_data. The inherited callback then reads it back through smc_clcsock_user_data(), which strips only NOCOPY, takes the sk_psock for an smc_sock, and dereferences a clcsk_* field past its end:

  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
   [...]
   </IRQ>

  Allocated by task 67930:
   sk_psock_init+0x142/0x740 net/core/skmsg.c:766
   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
   [...]

Resolve the conflict on the write path. Reserve the child's sk_user_data with a NULL pointer tagged SK_USER_DATA_NOCOPY so sk_psock_init() returns -EBUSY, and release it at accept. smc_clcsock_user_data() still strips the tag to NULL, so the inherited callback stays a no-op.

Fixes a60a2b1e0af1 ("net/smc: reduce active tcp_listen workers")

---
v3:
- reserve sk_user_data on the write path instead of the read-side check (D. Wythe)

v2:
- https://lore.kernel.org/netdev/[email protected]/

v1:
- https://lore.kernel.org/netdev/[email protected]/

 net/smc/af_smc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/smc/af_smc.c +6 -1
index b5db69073e20..78f162344fe3 100644
@@ -154,7 +154,11 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
 					       own_req, opt_child_init);
 	/* child must not inherit smc or its ops */
 	if (child) {
-		rcu_assign_sk_user_data(child, NULL);
+		/* reserve sk_user_data so sockmap cannot claim the slot */
+		write_lock_bh(&child->sk_callback_lock);
+		__rcu_assign_sk_user_data_with_flags(child, NULL,
+						     SK_USER_DATA_NOCOPY);
+		write_unlock_bh(&child->sk_callback_lock);
 
 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
@@ -1773,6 +1777,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 	/* new clcsock has inherited the smc listen-specific sk_data_ready
 	 * function; switch it back to the original sk_data_ready function
 	 */
+	write_lock_bh(&new_clcsock->sk->sk_callback_lock);
 	new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
 
 	/* if new clcsock has also inherited the fallback-specific callback

net/smc/af_smc.c static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) · line 1799

Quoted 9 lines
net/smc/af_smc.c +3 -0
@@ -1786,6 +1791,9 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (lsmc->clcsk_error_report)
 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
 	}
+	/* release the slot reserved in smc_tcp_syn_recv_sock() */
+	rcu_assign_sk_user_data(new_clcsock->sk, NULL);
+	write_unlock_bh(&new_clcsock->sk->sk_callback_lock);
 
 	(*new_smc)->clcsock = new_clcsock;
 out:

I do not think this is the right direction.

You have now sent three versions of essentially the same patch, but I still do not see a clear explanation of why this approach is supposed to solve the problem you described. At this point, it looks like you are repeatedly posting changes without demonstrating that you fully understand the ownership and lifetime rules involved here.

This is not the approach I suggested. My preference is to keep the sk_user_data reserved from the time the clcsock is created, by not setting NOCOPY, so that it remains inherited by the passive socket. That avoids the release-then-reacquire pattern entirely. The problem is more complicated than it may appear, and if you choose this direction, you also need to account for how SMC fallback continues to work with sockmap.

If you do not think you can fully address that approach, then feel free to pursue other options that you are able to handle, but I will not ack any workaround version. You may seek ack from other maintainers.

D. Wythe

Quoted 1 line

2.43.0