Skip to contentSkip to conversation

net: sock_reuseport: check program type on the migration path

PATCH In review Shihuang Liu 5 participants 5 messages Nov 18 – Nov 19

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

PATCH v2 1/1 net: sock_reuseport: check program type on the migration path

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group. Closing a listener that still has queued children then runs it with a struct sk_reuseport_kern context even though it was verified against struct sk_buff, which KASAN reports as a stack-out-of-bounds in ___bpf_prog_run(). Reaching this without CAP_BPF requires kernel.unprivileged_bpf_disabled=0; with CONFIG_BPF_UNPRIV_DEFAULT_OFF=y, the Kconfig default, the sysctl starts at 2.

reuseport_migrate_sock() decides from prog->expected_attach_type alone, a field user space sets that does not identify the program type. Require BPF_PROG_TYPE_SK_REUSEPORT there and in reuseport_stop_listen_sock(); socket filters still work as selectors, so the selection path is unchanged.

Fixes d5e4ddaeb6ab ("bpf: Support socket migration by eBPF.")

---
Changes in v2:
- Fix the author identity in From: and Signed-off-by:.
- Mention the kernel.unprivileged_bpf_disabled dependency.

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

 net/core/sock_reuseport.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
net/core/sock_reuseport.c +14 -2
index fccbdd0430b059..9ab76697a794fc 100644
@@ -460,6 +460,18 @@ void reuseport_detach_sock(struct sock *sk)
 }
 EXPORT_SYMBOL(reuseport_detach_sock);
 
+/*
+ * Only a BPF_PROG_TYPE_SK_REUSEPORT program is verified against
+ * struct sk_reuseport_kern, which is the context bpf_run_sk_reuseport()
+ * passes to it.  expected_attach_type is set by user space and does not
+ * identify the program type.
+ */
+static bool reuseport_is_migration_prog(const struct bpf_prog *prog)
+{
+	return prog && prog->type == BPF_PROG_TYPE_SK_REUSEPORT &&
+	       prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE;
+}
+
 void reuseport_stop_listen_sock(struct sock *sk)
 {
 	if (sk->sk_protocol == IPPROTO_TCP) {
@@ -474,7 +486,7 @@ void reuseport_stop_listen_sock(struct sock *sk)
 						 lockdep_is_held(&reuseport_lock));
 
 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req) ||
-		    (prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
+		    reuseport_is_migration_prog(prog)) {
 			/* Migration capable, move sk from the listening section
 			 * to the closed section.
 			 */
@@ -644,7 +656,7 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 
 	hash = migrating_sk->sk_hash;
 	prog = rcu_dereference(reuse->prog);
-	if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
+	if (!reuseport_is_migration_prog(prog)) {
 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req))
 			goto select_by_hash;
 		goto failure;
Signature · 1 line
2.43.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Shihuang Liu
Quoted 62 lines · Shihuang Liu

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group. Closing a listener that still has queued children then runs it with a struct sk_reuseport_kern context even though it was verified against struct sk_buff, which KASAN reports as a stack-out-of-bounds in ___bpf_prog_run(). Reaching this without CAP_BPF requires kernel.unprivileged_bpf_disabled=0; with CONFIG_BPF_UNPRIV_DEFAULT_OFF=y, the Kconfig default, the sysctl starts at 2.

reuseport_migrate_sock() decides from prog->expected_attach_type alone, a field user space sets that does not identify the program type. Require BPF_PROG_TYPE_SK_REUSEPORT there and in reuseport_stop_listen_sock(); socket filters still work as selectors, so the selection path is unchanged.

Fixes d5e4ddaeb6ab ("bpf: Support socket migration by eBPF.")

---
Changes in v2:
- Fix the author identity in From: and Signed-off-by:.
- Mention the kernel.unprivileged_bpf_disabled dependency.

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

 net/core/sock_reuseport.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
net/core/sock_reuseport.c +13 -1
index fccbdd0430b059..9ab76697a794fc 100644
@@ -460,6 +460,18 @@ void reuseport_detach_sock(struct sock *sk)
 }
 EXPORT_SYMBOL(reuseport_detach_sock);
 
+/*
+ * Only a BPF_PROG_TYPE_SK_REUSEPORT program is verified against
+ * struct sk_reuseport_kern, which is the context bpf_run_sk_reuseport()
+ * passes to it.  expected_attach_type is set by user space and does not
+ * identify the program type.
+ */
+static bool reuseport_is_migration_prog(const struct bpf_prog *prog)
+{
+	return prog && prog->type == BPF_PROG_TYPE_SK_REUSEPORT &&
+	       prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE;
+}
+
 void reuseport_stop_listen_sock(struct sock *sk)
 {
 	if (sk->sk_protocol == IPPROTO_TCP) {
@@ -474,7 +486,7 @@ void reuseport_stop_listen_sock(struct sock *sk)
 						 lockdep_is_held(&reuseport_lock));
 
 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req) ||
-		    (prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
+		    reuseport_is_migration_prog(prog)) {
 			/* Migration capable, move sk from the listening section
 			 * to the closed section.
 			 */

net/core/sock_reuseport.c struct sock *reuseport_migrate_sock(struct sock *sk, · line 659

Quoted 5 lines
net/core/sock_reuseport.c +1 -1
@@ -644,7 +656,7 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 
 	hash = migrating_sk->sk_hash;
 	prog = rcu_dereference(reuse->prog);
-	if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
+	if (!reuseport_is_migration_prog(prog)) {

Looks fine.
Which tree should it go to?

netdev folks, opinion?
I'm fine whichever way.

maintainer Reviewed-by on v2 1/1
#4 lore raw
In reply to Shihuang Liu
Quoted 11 lines · Shihuang Liu

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group. Closing a listener that still has queued children then runs it with a struct sk_reuseport_kern context even though it was verified against struct sk_buff, which KASAN reports as a stack-out-of-bounds in ___bpf_prog_run(). Reaching this without CAP_BPF requires kernel.unprivileged_bpf_disabled=0; with CONFIG_BPF_UNPRIV_DEFAULT_OFF=y, the Kconfig default, the sysctl starts at 2.

Quoted 8 lines

reuseport_migrate_sock() decides from prog->expected_attach_type alone, a field user space sets that does not identify the program type. Require BPF_PROG_TYPE_SK_REUSEPORT there and in reuseport_stop_listen_sock(); socket filters still work as selectors, so the selection path is unchanged.

Fixes d5e4ddaeb6ab ("bpf: Support socket migration by eBPF.")

Thanks