bpf: Reject sleepable struct_ops programs in atomic context
Subsystems (8)
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
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.
--- net/ipv4/bpf_tcp_ca.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/ipv4/bpf_tcp_ca.c +11 -0
@@ -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
PATCH 2/3 bpf: net_sched: Reject sleepable programs in bpf qdisc
In reply to Sechang LimA 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.
--- net/sched/bpf_qdisc.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/sched/bpf_qdisc.c +11 -0
@@ -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
PATCH 3/3 net/smc: bpf: Reject sleepable programs in smc_hs_ctrl
In reply to Sechang LimAn 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.
--- net/smc/smc_hs_bpf.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/smc/smc_hs_bpf.c +11 -0
@@ -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
[email protected]
AI reviewSashiko has reviewed this 3-patch series and found no issues.
Sashiko has reviewed this 3-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Quoted 3 lines · Sechang Lim
atomic context.
tcp_congestion_ops,Qdisc_opsandsmc_hs_ctrlhave nocheck_member, although the kernel calls their members in softirq, under a spinlock or underrcu_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_memberthat rejects sleepable programs to each of them, likebpf_tcp_opsdoes.
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
@@ -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
@@ -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
@@ -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
@@ -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
Quoted 127 lines · Alexei Starovoitov
Quoted 3 lines · Sechang Lim
atomic context.
tcp_congestion_ops,Qdisc_opsandsmc_hs_ctrlhave nocheck_member, although the kernel calls their members in softirq, under a spinlock or underrcu_read_lock(). A sleepable program attached thereNot all of them.
Qdisc_opsinit and destroy run under rtnl and can sleep:
qdisc_create_dflt()
qdisc_alloc()kzalloc_node()withGFP_KERNEL
ops->init(sch, NULL, extack)no lock taken in between
__qdisc_destroy()
ops->destroy(qdisc)fq_codel_destroy()takes a mutex hereA sleepable init is valid today.
bpf_arena_alloc_pages()usesGFP_KERNELthere instead ofalloc_pages_nolock(). Patch 2 rejects such progs.Quoted 2 lines
This series adds a
check_memberthat rejects sleepable programs to each of them, likebpf_tcp_opsdoes.That makes four copies of one function. The default is the bug.
bpf_tcp_opsv2 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_miscandbpf_dummy_opshavecheck_memberalready, so they keep their sleepable progs.
Astruct_opswithcheck_memberstill has to testprog->sleepableitself.In place of the series.
include/linux/bpf.h +2
-1@@ -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@@ -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@@ -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@@ -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