Commit bf17063
bpf: Fix sleepable context for async callbacks
Fix the BPF verifier to correctly determine the sleepable context of
async callbacks based on the async primitive type rather than the arming
program's context.
The bug is in in_sleepable() which uses OR logic to check if the current
execution context is sleepable. When a sleepable program arms a timer
callback, the callback's state correctly has in_sleepable=false, but
in_sleepable() would still return true due to env->prog->sleepable being
true. This incorrectly allows sleepable helpers like
bpf_copy_from_user() inside timer callbacks when armed from sleepable
programs, even though timer callbacks always execute in non-sleepable
context.
Fix in_sleepable() to rely solely on env->cur_state->in_sleepable, and
initialize state->in_sleepable to env->prog->sleepable in
do_check_common() for the main program entry. This ensures the sleepable
context is properly tracked per verification state rather than being
overridden by the program's sleepability.
The env->cur_state NULL check in in_sleepable() was only needed for
do_misc_fixups() which runs after verification when env->cur_state is
set to NULL. Update do_misc_fixups() to use env->prog->sleepable
directly for the storage_get_function check, and remove the redundant
NULL check from in_sleepable().
Introduce is_async_cb_sleepable() helper to explicitly determine async
callback sleepability based on the primitive type:
- bpf_timer callbacks are never sleepable
- bpf_wq and bpf_task_work callbacks are always sleepable
Add verifier_bug() check to catch unhandled async callback types,
ensuring future additions cannot be silently mishandled. Move the
is_task_work_add_kfunc() forward declaration to the top alongside other
callback-related helpers.
Finally, update push_async_cb() to adjust to the new changes.
Fixes: 81f1d7a ("bpf: wq: add bpf_wq_set_callback_impl")
Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>1 parent 2a180ed commit bf17063
1 file changed
+20
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
515 | 515 | | |
516 | 516 | | |
517 | 517 | | |
| 518 | + | |
518 | 519 | | |
519 | 520 | | |
520 | 521 | | |
| |||
547 | 548 | | |
548 | 549 | | |
549 | 550 | | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
550 | 566 | | |
551 | 567 | | |
552 | 568 | | |
| |||
5826 | 5842 | | |
5827 | 5843 | | |
5828 | 5844 | | |
5829 | | - | |
5830 | | - | |
| 5845 | + | |
5831 | 5846 | | |
5832 | 5847 | | |
5833 | 5848 | | |
| |||
10366 | 10381 | | |
10367 | 10382 | | |
10368 | 10383 | | |
10369 | | - | |
10370 | | - | |
10371 | 10384 | | |
10372 | 10385 | | |
10373 | 10386 | | |
| |||
10586 | 10599 | | |
10587 | 10600 | | |
10588 | 10601 | | |
10589 | | - | |
10590 | | - | |
| 10602 | + | |
10591 | 10603 | | |
10592 | 10604 | | |
10593 | 10605 | | |
| |||
22483 | 22495 | | |
22484 | 22496 | | |
22485 | 22497 | | |
22486 | | - | |
| 22498 | + | |
22487 | 22499 | | |
22488 | 22500 | | |
22489 | 22501 | | |
| |||
23154 | 23166 | | |
23155 | 23167 | | |
23156 | 23168 | | |
| 23169 | + | |
23157 | 23170 | | |
23158 | 23171 | | |
23159 | 23172 | | |
| |||
0 commit comments