Skip to contentSkip to conversation

bpf: Reject sleepable struct_ops programs in atomic context

PATCH In review Sechang Lim 3 participants 7 messages Nov 24 – Nov 25

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

cover bpf: Reject sleepable struct_ops programs in atomic context

Any struct_ops program can be loaded as sleepable, and it is up to each struct_ops to reject that in check_member for members that run in atomic context. tcp_congestion_ops, Qdisc_ops and smc_hs_ctrl have no check_member, although the kernel calls their members in softirq, under a spinlock or under rcu_read_lock(). A sleepable program attached there is entered through __bpf_prog_enter_sleepable(), and lockdep reports "BUG: Invalid wait context" at its might_fault().

This series adds a check_member that rejects sleepable programs to each of them, like bpf_tcp_ops does.

Sechang Lim (3):
bpf: Reject sleepable programs in bpf-tcp-cc
bpf: net_sched: Reject sleepable programs in bpf qdisc net/smc: bpf: Reject sleepable programs in smc_hs_ctrl

 net/ipv4/bpf_tcp_ca.c | 11 +++++++++++
 net/sched/bpf_qdisc.c | 11 +++++++++++
 net/smc/smc_hs_bpf.c  | 11 +++++++++++
 3 files changed, 33 insertions(+)
Signature · 1 line
2.56.1
#1 lore raw

PATCH 1/3 bpf: Reject sleepable programs in bpf-tcp-cc

A bpf-tcp-cc program can be loaded with BPF_F_SLEEPABLE, but the TCP stack calls the congestion control ops in atomic context. For example, tcp_set_ca_state() is called from tcp_v4_syn_recv_sock() in softirq with the socket spinlock held.

bpf_tcp_congestion_ops has no check_member, so nothing rejects such a program. Add one that rejects sleepable programs.

Fixes 1e12d3ef47d2 ("bpf: Allow BPF_PROG_TYPE_STRUCT_OPS programs to be sleepable")
---
 net/ipv4/bpf_tcp_ca.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/ipv4/bpf_tcp_ca.c +11 -0
index 2a71dc81562f..acd5e84a3dd9 100644
@@ -208,6 +208,16 @@ static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
 };
 
+static int bpf_tcp_ca_check_member(const struct btf_type *t,
+				   const struct btf_member *member,
+				   const struct bpf_prog *prog)
+{
+	if (prog->sleepable)
+		return -EINVAL;
+
+	return 0;
+}
+
 static int bpf_tcp_ca_init_member(const struct btf_type *t,
 				  const struct btf_member *member,
 				  void *kdata, const void *udata)
@@ -335,6 +345,7 @@ static struct bpf_struct_ops bpf_tcp_congestion_ops = {
 	.reg = bpf_tcp_ca_reg,
 	.unreg = bpf_tcp_ca_unreg,
 	.update = bpf_tcp_ca_update,
+	.check_member = bpf_tcp_ca_check_member,
 	.init_member = bpf_tcp_ca_init_member,
 	.init = bpf_tcp_ca_init,
 	.validate = bpf_tcp_ca_validate,
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/3 bpf: net_sched: Reject sleepable programs in bpf qdisc

In reply to Sechang Lim

A bpf qdisc program can be loaded with BPF_F_SLEEPABLE, but enqueue and dequeue run with the qdisc root lock held and BH disabled.

bpf_Qdisc_ops has no check_member, so nothing rejects such a program. Add one that rejects sleepable programs.

Fixes c8240344956e ("bpf: net_sched: Support implementation of Qdisc_ops in bpf")
---
 net/sched/bpf_qdisc.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/sched/bpf_qdisc.c +11 -0
index 5691c13781a8..3dbe52a04fa2 100644
@@ -356,6 +356,16 @@ static const struct bpf_verifier_ops bpf_qdisc_verifier_ops = {
 	.gen_epilogue		= bpf_qdisc_gen_epilogue,
 };
 
+static int bpf_qdisc_check_member(const struct btf_type *t,
+				  const struct btf_member *member,
+				  const struct bpf_prog *prog)
+{
+	if (prog->sleepable)
+		return -EINVAL;
+
+	return 0;
+}
+
 static int bpf_qdisc_init_member(const struct btf_type *t,
 				 const struct btf_member *member,
 				 void *kdata, const void *udata)
@@ -446,6 +456,7 @@ static struct bpf_struct_ops bpf_Qdisc_ops = {
 	.reg = bpf_qdisc_reg,
 	.unreg = bpf_qdisc_unreg,
 	.validate = bpf_qdisc_validate,
+	.check_member = bpf_qdisc_check_member,
 	.init_member = bpf_qdisc_init_member,
 	.init = bpf_qdisc_init,
 	.name = "Qdisc_ops",
Signature · 1 line
2.56.1
#3 lore raw

PATCH 3/3 net/smc: bpf: Reject sleepable programs in smc_hs_ctrl

In reply to Sechang Lim

An smc_hs_ctrl program can be loaded with BPF_F_SLEEPABLE, but smc_call_hsbpf() calls syn_option and synack_option under rcu_read_lock().

bpf_smc_hs_ctrl_ops has no check_member, so nothing rejects such a program. Add one that rejects sleepable programs.

Fixes 15f295f55656 ("net/smc: bpf: Introduce generic hook for handshake flow")
---
 net/smc/smc_hs_bpf.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/smc/smc_hs_bpf.c +11 -0
index 063d23d85850..66cf5143109a 100644
@@ -82,6 +82,16 @@ static void smc_bpf_hs_ctrl_unreg(void *kdata, struct bpf_link *link)
 	smc_hs_ctrl_unreg(kdata);
 }
 
+static int smc_bpf_hs_ctrl_check_member(const struct btf_type *t,
+					const struct btf_member *member,
+					const struct bpf_prog *prog)
+{
+	if (prog->sleepable)
+		return -EINVAL;
+
+	return 0;
+}
+
 static int smc_bpf_hs_ctrl_init_member(const struct btf_type *t,
 				       const struct btf_member *member,
 				       void *kdata, const void *udata)
@@ -130,6 +140,7 @@ static struct bpf_struct_ops bpf_smc_hs_ctrl_ops = {
 	.unreg		= smc_bpf_hs_ctrl_unreg,
 	.cfi_stubs	= &__smc_bpf_hs_ctrl,
 	.verifier_ops	= &smc_bpf_verifier_ops,
+	.check_member	= smc_bpf_hs_ctrl_check_member,
 	.init_member	= smc_bpf_hs_ctrl_init_member,
 	.owner		= THIS_MODULE,
 };
Signature · 1 line
2.56.1
#5 lore raw
In reply to Sechang Lim
Quoted 3 lines · Sechang Lim

atomic context. tcp_congestion_ops, Qdisc_ops and smc_hs_ctrl have no check_member, although the kernel calls their members in softirq, under a spinlock or under rcu_read_lock(). A sleepable program attached there

Not all of them. Qdisc_ops init and destroy run under rtnl and can sleep:

qdisc_create_dflt()
qdisc_alloc() kzalloc_node() with GFP_KERNEL
ops->init(sch, NULL, extack) no lock taken in between
__qdisc_destroy()

    ops->destroy(qdisc)           fq_codel_destroy() takes a mutex here

A sleepable init is valid today. bpf_arena_alloc_pages() uses GFP_KERNEL there instead of alloc_pages_nolock(). Patch 2 rejects such progs.

Quoted 2 lines

This series adds a check_member that rejects sleepable programs to each of them, like bpf_tcp_ops does.

That makes four copies of one function. The default is the bug. bpf_tcp_ops v2 and blk-iocost v1 missed the same check:
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/bpf/[email protected]/

Fix it once in check_struct_ops_btf_id(). Patches 1 and 3 go away. sched_ext, hid, io_uring, binfmt_misc and bpf_dummy_ops have check_member already, so they keep their sleepable progs.
A struct_ops with check_member still has to test prog->sleepable itself.

In place of the series.

include/linux/bpf.h +2 -1
index 1dd9db1..f7c7380 100644
@@ -2209,7 +2209,8 @@ struct btf_member;
  * @check_member: When defined, a callback invoked by the verifier to allow
  *		  the subsystem to determine if an entry in the struct_ops map
  *		  is valid. A nonzero return value means that the map is
- *		  invalid and should be rejected by the verifier.
+ *		  invalid and should be rejected by the verifier. When not
+ *		  defined, the verifier rejects sleepable progs.
  * @init_member: A callback that is invoked for each member of the struct_ops
  *		 map to allow the subsystem to initialize the member. A nonzero
  *		 value means the member could not be initialized. This callback
kernel/bpf/verifier.c +4 -0
index 01a74e1..42a90c7 100644
@@ -22244,6 +22244,10 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 				mname, st_ops->name);
 			return err;
 		}
+	} else if (prog->sleepable) {
+		verbose(env, "member %s of struct %s cannot be sleepable\n",
+			mname, st_ops->name);
+		return -EINVAL;
 	}
 
 	if (prog->aux->priv_stack_requested && !bpf_jit_supports_private_stack()) {
net/ipv4/bpf_tcp_ops.c +0 -11
index 2ba73dd..4e54165 100644
@@ -301,16 +301,6 @@ static int bpf_tcp_ops_init_member(const struct btf_type *t,
 	return 0;
 }
 
-static int bpf_tcp_ops_check_member(const struct btf_type *t,
-				    const struct btf_member *member,
-				    const struct bpf_prog *prog)
-{
-	if (prog->sleepable)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int bpf_tcp_ops_init(struct btf *btf)
 {
 	return 0;
@@ -329,7 +319,6 @@ static const struct bpf_verifier_ops bpf_tcp_ops_verifier = {
 static struct bpf_struct_ops bpf_tcp_ops = {
 	.verifier_ops = &bpf_tcp_ops_verifier,
 	.init_member = bpf_tcp_ops_init_member,
-	.check_member = bpf_tcp_ops_check_member,
 	.init = bpf_tcp_ops_init,
 	.validate = bpf_tcp_ops_validate,
 	.name = "bpf_tcp_ops",
net/sched/bpf_qdisc.c +19 -0
index 5691c13..61a1971 100644
@@ -356,6 +356,24 @@ static const struct bpf_verifier_ops bpf_qdisc_verifier_ops = {
 	.gen_epilogue		= bpf_qdisc_gen_epilogue,
 };
 
+static int bpf_qdisc_check_member(const struct btf_type *t,
+				  const struct btf_member *member,
+				  const struct bpf_prog *prog)
+{
+	u32 moff = __btf_member_bit_offset(t, member) / 8;
+
+	switch (moff) {
+	case offsetof(struct Qdisc_ops, init):
+	case offsetof(struct Qdisc_ops, destroy):
+		break;
+	default:
+		if (prog->sleepable)
+			return -EINVAL;
+	}
+
+	return 0;
+}
+
 static int bpf_qdisc_init_member(const struct btf_type *t,
 				 const struct btf_member *member,
 				 void *kdata, const void *udata)
@@ -446,6 +464,7 @@ static struct bpf_struct_ops bpf_Qdisc_ops = {
 	.reg = bpf_qdisc_reg,
 	.unreg = bpf_qdisc_unreg,
 	.validate = bpf_qdisc_validate,
+	.check_member = bpf_qdisc_check_member,
 	.init_member = bpf_qdisc_init_member,
 	.init = bpf_qdisc_init,
 	.name = "Qdisc_ops",

Pls target bpf-next.

pw-bot: cr

#6 lore raw
Quoted 127 lines · Alexei Starovoitov
Quoted 3 lines · Sechang Lim

atomic context. tcp_congestion_ops, Qdisc_ops and smc_hs_ctrl have no check_member, although the kernel calls their members in softirq, under a spinlock or under rcu_read_lock(). A sleepable program attached there

Not all of them. Qdisc_ops init and destroy run under rtnl and can sleep:

qdisc_create_dflt()
qdisc_alloc() kzalloc_node() with GFP_KERNEL
ops->init(sch, NULL, extack) no lock taken in between
__qdisc_destroy()
ops->destroy(qdisc) fq_codel_destroy() takes a mutex here

A sleepable init is valid today. bpf_arena_alloc_pages() uses GFP_KERNEL there instead of alloc_pages_nolock(). Patch 2 rejects such progs.

Quoted 2 lines

This series adds a check_member that rejects sleepable programs to each of them, like bpf_tcp_ops does.

That makes four copies of one function. The default is the bug. bpf_tcp_ops v2 and blk-iocost v1 missed the same check:
https://lore.kernel.org/bpf/[email protected]/
https://lore.kernel.org/bpf/[email protected]/

Fix it once in check_struct_ops_btf_id(). Patches 1 and 3 go away. sched_ext, hid, io_uring, binfmt_misc and bpf_dummy_ops have check_member already, so they keep their sleepable progs.
A struct_ops with check_member still has to test prog->sleepable itself.

In place of the series.

include/linux/bpf.h +2 -1
index 1dd9db1..f7c7380 100644
@@ -2209,7 +2209,8 @@ struct btf_member;
 * @check_member: When defined, a callback invoked by the verifier to allow
 *		  the subsystem to determine if an entry in the struct_ops map
 *		  is valid. A nonzero return value means that the map is
- *		  invalid and should be rejected by the verifier.
+ *		  invalid and should be rejected by the verifier. When not
+ *		  defined, the verifier rejects sleepable progs.
 * @init_member: A callback that is invoked for each member of the struct_ops
 *		 map to allow the subsystem to initialize the member. A nonzero
 *		 value means the member could not be initialized. This callback
kernel/bpf/verifier.c +4 -0
index 01a74e1..42a90c7 100644
@@ -22244,6 +22244,10 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 				mname, st_ops->name);
 			return err;
 		}
+	} else if (prog->sleepable) {
+		verbose(env, "member %s of struct %s cannot be sleepable\n",
+			mname, st_ops->name);
+		return -EINVAL;
 	}
 
 	if (prog->aux->priv_stack_requested && !bpf_jit_supports_private_stack()) {
net/ipv4/bpf_tcp_ops.c +0 -11
index 2ba73dd..4e54165 100644
@@ -301,16 +301,6 @@ static int bpf_tcp_ops_init_member(const struct btf_type *t,
 	return 0;
 }
 
-static int bpf_tcp_ops_check_member(const struct btf_type *t,
-				    const struct btf_member *member,
-				    const struct bpf_prog *prog)
-{
-	if (prog->sleepable)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int bpf_tcp_ops_init(struct btf *btf)
 {
 	return 0;
@@ -329,7 +319,6 @@ static const struct bpf_verifier_ops bpf_tcp_ops_verifier = {
 static struct bpf_struct_ops bpf_tcp_ops = {
 	.verifier_ops = &bpf_tcp_ops_verifier,
 	.init_member = bpf_tcp_ops_init_member,
-	.check_member = bpf_tcp_ops_check_member,
 	.init = bpf_tcp_ops_init,
 	.validate = bpf_tcp_ops_validate,
 	.name = "bpf_tcp_ops",
net/sched/bpf_qdisc.c +19 -0
index 5691c13..61a1971 100644
@@ -356,6 +356,24 @@ static const struct bpf_verifier_ops bpf_qdisc_verifier_ops = {
 	.gen_epilogue		= bpf_qdisc_gen_epilogue,
 };
 
+static int bpf_qdisc_check_member(const struct btf_type *t,
+				  const struct btf_member *member,
+				  const struct bpf_prog *prog)
+{
+	u32 moff = __btf_member_bit_offset(t, member) / 8;
+
+	switch (moff) {
+	case offsetof(struct Qdisc_ops, init):
+	case offsetof(struct Qdisc_ops, destroy):
+		break;
+	default:
+		if (prog->sleepable)
+			return -EINVAL;
+	}
+
+	return 0;
+}
+
 static int bpf_qdisc_init_member(const struct btf_type *t,
 				 const struct btf_member *member,
 				 void *kdata, const void *udata)
@@ -446,6 +464,7 @@ static struct bpf_struct_ops bpf_Qdisc_ops = {
 	.reg = bpf_qdisc_reg,
 	.unreg = bpf_qdisc_unreg,
 	.validate = bpf_qdisc_validate,
+	.check_member = bpf_qdisc_check_member,
 	.init_member = bpf_qdisc_init_member,
 	.init = bpf_qdisc_init,
 	.name = "Qdisc_ops",
Quoted 2 lines

Pls target bpf-next.

Thanks, that's cleaner. I'll move the check into check_struct_ops_btf_id() and send v2 against bpf-next tree.

Thanks,
sechang