Fix reference state management for synchronous callbacks
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainercover Fix reference state management for synchronous callbacks
This is patch 1, 2 + their individual tests split into a separate series from the RFC, so that these can be taken in, while we continue working towards a fix for handling stack access inside the callback.
Changelog:
----------
RFC v1 -> v1:
RFC v1: https://lore.kernel.org/bpf/[email protected]
* Fix up commit log to add more explanation (Alexei)
* Split reference state fix out into a separate series
Kumar Kartikeya Dwivedi (3):
bpf: Move bpf_loop and bpf_for_each_map_elem under CAP_BPF
bpf: Fix reference state management for synchronous callbacks selftests/bpf: Add tests for reference state fixes for callbacks
include/linux/bpf_verifier.h | 11 ++ kernel/bpf/helpers.c | 8 +- kernel/bpf/verifier.c | 42 +++++-- .../selftests/bpf/prog_tests/cb_refs.c | 48 +++++++ tools/testing/selftests/bpf/progs/cb_refs.c | 118 ++++++++++++++++++ 5 files changed, 214 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/cb_refs.c create mode 100644 tools/testing/selftests/bpf/progs/cb_refs.c
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH 1/3 bpf: Move bpf_loop and bpf_for_each_map_elem under CAP_BPF
They would require func_info which needs prog BTF anyway. Loading BTF and setting the prog btf_fd while loading the prog indirectly requires CAP_BPF, so just to reduce confusion, move both these helpers taking callback under bpf_capable() protection as well, since they cannot be used without CAP_BPF.
--- kernel/bpf/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +4 -4
@@ -1633,10 +1633,6 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_ringbuf_submit_dynptr_proto;case BPF_FUNC_ringbuf_discard_dynptr:return &bpf_ringbuf_discard_dynptr_proto; -case BPF_FUNC_for_each_map_elem: -return &bpf_for_each_map_elem_proto; -case BPF_FUNC_loop: -return &bpf_loop_proto;case BPF_FUNC_strncmp:return &bpf_strncmp_proto;case BPF_FUNC_dynptr_from_mem:
@@ -1675,6 +1671,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_timer_cancel_proto;case BPF_FUNC_kptr_xchg:return &bpf_kptr_xchg_proto; +case BPF_FUNC_for_each_map_elem: +return &bpf_for_each_map_elem_proto; +case BPF_FUNC_loop: +return &bpf_loop_proto;default :break ; }
Signature · 1 line
2.34.1
John Fastabend
reviewerRE: [PATCH bpf v1 1/3] bpf: Move bpf_loop and bpf_for_each_map_elem under CAP_BPF
Quoted 8 lines · Kumar Kartikeya Dwivedi
They would require
func_infowhich needs prog BTF anyway. Loading BTF and setting the progbtf_fdwhile loading the prog indirectly requiresCAP_BPF, so just to reduce confusion, move both these helpers taking callback underbpf_capable()protection as well, since they cannot be used withoutCAP_BPF.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
This should have a fixes tag IMO. You'll get unexpected results if we don't have get it backported to the right places.
Quoted 32 lines
kernel/bpf/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)kernel/bpf/helpers.c +4
-4@@ -1633,10 +1633,6 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_ringbuf_submit_dynptr_proto;case BPF_FUNC_ringbuf_discard_dynptr:return &bpf_ringbuf_discard_dynptr_proto; -case BPF_FUNC_for_each_map_elem: -return &bpf_for_each_map_elem_proto; -case BPF_FUNC_loop: -return &bpf_loop_proto;case BPF_FUNC_strncmp:return &bpf_strncmp_proto;case BPF_FUNC_dynptr_from_mem:@@ -1675,6 +1671,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_timer_cancel_proto;case BPF_FUNC_kptr_xchg:return &bpf_kptr_xchg_proto; +case BPF_FUNC_for_each_map_elem: +return &bpf_for_each_map_elem_proto; +case BPF_FUNC_loop: +return &bpf_loop_proto;default :break ; }Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf v1 1/3] bpf: Move bpf_loop and bpf_for_each_map_elem under CAP_BPF
Quoted 8 lines · John Fastabend
Quoted 6 lines · Kumar Kartikeya Dwivedi
They would require
func_infowhich needs prog BTF anyway. Loading BTF and setting the progbtf_fdwhile loading the prog indirectly requiresCAP_BPF, so just to reduce confusion, move both these helpers taking callback underbpf_capable()protection as well, since they cannot be used withoutCAP_BPF.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
This should have a fixes tag IMO. You'll get unexpected results if we don't have get it backported to the right places.
Hm, I was unsure if this requires a Fixes tag. It's technically not a fix, it's a minor reorg in my opinion (could have gone through bpf-next as well) which has no real resulting change for users loading programs, and makes things less confusing. The actual fix in patch 2 is independent of this change.
Quoted 34 lines
Quoted 32 lines
kernel/bpf/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)kernel/bpf/helpers.c +4
-5@@ -1633,10 +1633,6 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_ringbuf_submit_dynptr_proto;case BPF_FUNC_ringbuf_discard_dynptr:return &bpf_ringbuf_discard_dynptr_proto; -case BPF_FUNC_for_each_map_elem: -return &bpf_for_each_map_elem_proto; -case BPF_FUNC_loop: -return &bpf_loop_proto;case BPF_FUNC_strncmp:return &bpf_strncmp_proto;case BPF_FUNC_dynptr_from_mem:@@ -1675,6 +1671,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_timer_cancel_proto;case BPF_FUNC_kptr_xchg:return &bpf_kptr_xchg_proto; +case BPF_FUNC_for_each_map_elem: +return &bpf_for_each_map_elem_proto; +case BPF_FUNC_loop: +return &bpf_loop_proto;default :break ; } --2.34.1
Alexei Starovoitov
AppliedRe: [PATCH bpf v1 1/3] bpf: Move bpf_loop and bpf_for_each_map_elem under CAP_BPF
Quoted 17 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · John Fastabend
Quoted 6 lines · Kumar Kartikeya Dwivedi
They would require
func_infowhich needs prog BTF anyway. Loading BTF and setting the progbtf_fdwhile loading the prog indirectly requiresCAP_BPF, so just to reduce confusion, move both these helpers taking callback underbpf_capable()protection as well, since they cannot be used withoutCAP_BPF.Quoted 6 lines
Quoted 2 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
This should have a fixes tag IMO. You'll get unexpected results if we don't have get it backported to the right places.
Quoted 5 lines
Hm, I was unsure if this requires a Fixes tag. It's technically not a fix, it's a minor reorg in my opinion (could have gone through bpf-next as well) which has no real resulting change for users loading programs, and makes things less confusing. The actual fix in patch 2 is independent of this change.
Pushed to bpf-next.
Such corner case fixes are too risky to go directly into the bpf tree.
Kumar Kartikeya Dwivedi
maintainerPATCH 2/3 bpf: Fix reference state management for synchronous callbacks
In reply to Kumar Kartikeya DwivediCurrently, verifier verifies callback functions (sync and async) as if they will be executed once, (i.e. it explores execution state as if the function was being called once). The next insn to explore is set to start of subprog and the exit from nested frame is handled using curframe > 0 and prepare_func_exit. In case of async callback it uses a customized variant of push_stack simulating a kind of branch to set up custom state and execution context for the async callback.
While this approach is simple and works when callback really will be executed only once, it is unsafe for all of our current helpers which are for_each style, i.e. they execute the callback multiple times.
A callback releasing acquired references of the caller may do so multiple times, but currently verifier sees it as one call inside the frame, which then returns to caller. Hence, it thinks it released some reference that the cb e.g. got access through callback_ctx (register filled inside cb from spilled typed register on stack).
Similarly, it may see that an acquire call is unpaired inside the callback, so the caller will copy the reference state of callback and then will have to release the register with new ref_obj_ids. But again, the callback may execute multiple times, but the verifier will only account for acquired references for a single symbolic execution of the callback, which will cause leaks.
Note that for async callback case, things are different. While currently we have bpf_timer_set_callback which only executes it once, even for multiple executions it would be safe, as reference state is NULL and check_reference_leak would force program to release state before BPF_EXIT. The state is also unaffected by analysis for the caller frame. Hence async callback is safe.
Since we want the reference state to be accessible, e.g. for pointers loaded from stack through callback_ctx's PTR_TO_STACK, we still have to copy caller's reference_state to callback's bpf_func_state, but we enforce that whatever references it adds to that reference_state has been released before it hits BPF_EXIT. This requires introducing a new callback_ref member in the reference state to distinguish between caller vs callee references. Hence, check_reference_leak now errors out if it sees we are in callback_fn and we have not released callback_ref refs. Since there can be multiple nested callbacks, like frame 0 -> cb1 -> cb2 etc. we need to also distinguish between whether this particular ref belongs to this callback frame or parent, and only error for our own, so we store state->frameno (which is always non-zero for callbacks).
In short, callbacks can read parent reference_state, but cannot mutate it, to be able to use pointers acquired by the caller. They must only undo their changes (by releasing their own acquired_refs before BPF_EXIT) on top of caller reference_state before returning (at which point the caller and callback state will match anyway, so no need to copy it back to caller).
--- include/linux/bpf_verifier.h | 11 ++++++++++ kernel/bpf/verifier.c | 42 ++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 9 deletions(-)
include/linux/bpf_verifier.h +11 -0
@@ -212,6 +212,17 @@ struct bpf_reference_state {* is used purely to inform the user of a reference leak. */ int insn_idx; +/* There can be a case like: +* main (frame 0) +* cb (frame 1) +* func (frame 3) +* cb (frame 4) +* Hence for frame 4, if callback_ref just stored boolean, it would be +* impossible to distinguish nested callback refs. Hence store the +* frameno and compare that to callback_ref in check_reference_leak when +* exiting a callback function. +*/ +int callback_ref; };/* state of the program:
kernel/bpf/verifier.c +33 -9
84 lines not on this page · raw message
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH 3/3 selftests/bpf: Add tests for reference state fixes for callbacks
In reply to Kumar Kartikeya DwivediThese are regression tests to ensure we don't end up in invalid runtime state for helpers that execute callbacks multiple times. It exercises the fixes to verifier callback handling for reference state in previous patches.
--- .../selftests/bpf/prog_tests/cb_refs.c | 48 +++++++ tools/testing/selftests/bpf/progs/cb_refs.c | 118 ++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cb_refs.c create mode 100644 tools/testing/selftests/bpf/progs/cb_refs.c
tools/testing/selftests/bpf/prog_tests/cb_refs.c new file +48 -0
@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "bpf/libbpf.h" +#include <test_progs.h> +#include <network_helpers.h> + +#include "cb_refs.skel.h" + +static char log_buf[1024 *1024 ]; + +struct { +const char *prog_name; +const char *err_msg; +} cb_refs_tests[] = { + {"underflow_prog" ,"reference has not been acquired before" }, + {"leak_prog" ,"Unreleased reference" }, + {"nested_cb" ,"Unreleased reference id=4 alloc_insn=25" }, + {"non_cb_transfer_ref" ,"Unreleased reference id=4 alloc_insn=12" }, +}; + +void test_cb_refs (void ) +{ + LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf, + .kernel_log_size = sizeof(log_buf), + .kernel_log_level = 1); +struct bpf_program *prog; +struct cb_refs *skel; +int i; + +for (i =0 ; i <ARRAY_SIZE (cb_refs_tests); i++) { +LIBBPF_OPTS (bpf_test_run_opts, run_opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + skel =cb_refs__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"cb_refs__open_and_load" )) +return ; + prog =bpf_object__find_program_by_name (skel->obj, cb_refs_tests[i].prog_name); +bpf_program__set_autoload (prog, true); +if (!ASSERT_ERR (cb_refs__load (skel),"cb_refs__load" )) +bpf_prog_test_run_opts (bpf_program__fd (prog), &run_opts); +if (!ASSERT_OK_PTR (strstr (log_buf, cb_refs_tests[i].err_msg),"expected error message" )) { +fprintf (stderr,"Expected: %s\n" , cb_refs_tests[i].err_msg); +fprintf (stderr,"Verifier: %s\n" , log_buf); + } +cb_refs__destroy (skel); + } +}
tools/testing/selftests/bpf/progs/cb_refs.c new file +118 -0
118 lines not on this page · raw message
Signature · 1 line
2.34.1