Redesign Verification Errors
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainerv4 cover Redesign Verification Errors
TL;DR: This set reworks verifier error messages to include source and instruction annotations, together with more causal context, making failures easier to understand and more actionable when debugging and repairing BPF programs.
Changelog:
----------
v3 -> v4
v3: https://lore.kernel.org/bpf/[email protected]
* Introduce helpers with their first callers and add printf annotations. (Eduard, Sashiko)
* Remove "report" from diagnostic function names. (Sashiko)
* Reuse bpf_linfo_source and seq_buf, simplify internal names, and use shared formatting storage. (Eduard)
* Use compact common event fields and record branches at successor entry. (Eduard)
* Bound event storage at 1 MiB, use kvrealloc(), and drop events non-fatally. (Eduard, Sashiko)
* Restore diagnostic history only for activated queued states, preserving the active failure trace during cleanup. (Eduard, Sashiko)
* Record register changes through begin/end and scrub helpers, deriving targets and origins without caller-saved snapshots. (Eduard)
* Store lineage marks on events and rewind shared formatting storage after rendering each event. (Eduard)
* Record iterator return values before snapshotting alternate paths. (Sashiko)
* Use the current verifier instruction for global-subprogram dynptr errors. (Sashiko)
* Use the supplied call name for nullable global-subprogram arguments. (Sashiko)
* Describe global calls under locks as a verifier restriction rather than a sleepability failure. (Sashiko)
* Keep diagnostic strings unsplit and put long call openings on their own line. (Eduard)
* Keep kfunc metadata zeroed before early fetch and allowability failures. (Sashiko)
* Drop the Verifier Internal Error report patch. (Eduard)
* Distinguish never-initialized registers from invalidated registers. (local review)
* Preserve the legacy different-lock verifier message. (local review)
* Preserve nullable type qualifiers and stable mismatch formatting. (local review)
* Mark truncated call chains with an ellipsis. (local review)
v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]
* Address various comments from Eduard and Sashiko.
* Move instruction context from a separate gutter into a new section following source context, since surrounding source lines and BPF instructions do not map one-to-one.
* Fix active-path branch reconstruction when switching to queued states, and expand register histories to follow value lineage across spills, fills, stack reads, helper/kfunc clobbers, and dynptr invalidation.
* Misc improvements and refinements.
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Reworked diagnostic history from per-verifier-state log to active path log with positions saved and reset when verifier search backtracks. (Eduard)
* Moved reusable diagnostic formatting storage into struct bpf_diag under struct bpf_verifier_env, and removed large per-report scratch buffers from verifier stack frames. (Eduard)
* Added stack-slot events so diagnostics follow ordinary stack spill/fill value flow and invalidations in register-scoped histories. (Eduard)
* Reused existing source and BTF formatting helpers for diagnostics, including bpf_get_linfo_file_line() and btf_type_snprintf_show_name(). (Eduard)
* Fixed diagnostic edge cases around signed offset text, BPF_MAX_VAR_OFF reporting, negative-offset clamping, poisoned stack reads, and borrowed-reference invalidations. (Eduard)
* Fixed various miscellaneous diagnostic bugs. (Sashiko)
* Misc improvements and refinements.
--
Motivation
~~~~~~~~~~
The verifier log is the primary interface through which the verifier communicates to the user its verdict on whether a program was accepted or rejected.
To aid the debugging of rejection decisions, the verifier also reports the symbolic state of the program at each instruction, across every explored path of the BPF program. Such detailed information is critical to introspect the correctness of verification decisions, and provide insight into why a given program may have failed to load in the kernel.
A constant pain point in the BPF ecosystem throughout the years has been the difficulty of debugging verification errors. The human-readable error messages produced in response to a failure in satisfying safety-related constraints are often terse, context-dependent, or insufficient for understanding why a given error may have happened. Users must fall back to the verbose instruction-by-instruction breakdown of how the symbolic state evolved to surface the root cause. For programs with a huge log volume due to high verification complexity, such logs quickly become inscrutable.
All of this has made life difficult for users lacking an understanding of how the verifier works, and the various heuristics and idiosyncrasies used by it. In some cases, even seasoned BPF experts spend significant time reverse engineering why a program may have failed, and have to reach into the verifier's source code to form a complete picture of the verification process.
Such a steep learning curve and cognitive burden also hurts the speed of BPF development, as the verifier sits right in the middle of the user's iteration loop while they make use of BPF to solve any given problem. Expertise in debugging verifier errors does not scale in terms of teams deploying these programs in production across a diverse set of kernels.
Overall, this leads to a poorer developer experience, causes visible user dissatisfaction, and remains a drag on wider BPF adoption. With some of the more recent developments where users increasingly leverage AI tooling [0] to author their code, this bottleneck becomes even more critical to address, since it throttles the much faster iteration loop of AI agents.
[0]: https://lwn.net/Articles/1075067
Approach
~~~~~~~~
This series starts moving selected failures from terse terminal messages toward diagnostics that carry the relevant context for a verification failure. The existing verbose log remains the low-level trace. For selected failures, the new report is emitted after this trace and answers the immediate debugging questions:
- what verifier rule failed,
- why the current state does not satisfy it,
- where the failing instruction maps to source,
- which earlier branch or state event made this path fail,
- what kind of source change would satisfy the verifier.
The series adds a text-only diagnostics framework under kernel/bpf and uses it to augment selected verifier errors. Existing verbose(env, ...) messages are kept, so current selftest expectations and existing log consumers continue to see the legacy text. The new report has a uniform outer shape:
Verification failed: <category>: <problem>
Reason:
exact reason for the verification failure, with details
At:
source and instruction annotation
Causal path:
compressed branch and verifier-state events relevant for debugging
Suggestion:
speculation on potential fixes to repair the program
The outer shape is shared, but report construction is category-specific.
The categories are intentionally broad and reviewable. This revision covers representative cases in Register Type Safety, Memory Safety, Resource Lifetime Safety, Call Type Safety, Execution Context Safety, Program Structure, Policy, and Verifier Limit errors.
It does not attempt to convert every verbose(env, ...) site for now. Additional verbose-only errors can be moved into the same framework incrementally.
The following excerpts are copied from this current run on this branch:
./test_progs -j1 \
-a cpumask/test_populate_invalid_destination,\
cpumask/test_alloc_no_release,\
verifier_helper_value_access/via_variable_no_max_check_1,\
verifier_sock/invalidate_pkt_pointers_from_global_func \
-vvThey show the old terminal error and the exact new diagnostic report, including the source and instruction annotations.
Call Type Safety, cpumask/test_populate_invalid_destination:
Legacy:
R1 type=scalar expected=fp
Diagnostic:
Verification failed: Call Type Safety: Invalid call argument
Reason:
The first argument (R1) to bpf_cpumask_populate does not satisfy the verifier contract: the kfunc
expects 24 bytes of memory for (struct bpf_cpumask), but it is an integer scalar and not
verifier-known memory.
At:
test_populate_invalid_destination @ cpumask_failure.c:234:8
Source context:
232 | ...
233 | ...
>>> 234 | ret = bpf_cpumask_populate(invalid, &bits, sizeof(bits));
| ^-- error: invalid first argument (R1) for bpf_cpumask_populate
235 | if (!ret)
236 | err = 2;
Instruction context:
2 | (b7) r1 = 1193046
3 | (b7) r3 = 8
>>> 4 | (85) call bpf_cpumask_populate#62860
5 | (56) if w0 != 0x0 goto pc+4
6 | (18) r1 = 0xffffc9000028e000
Causal path:
test_populate_invalid_destination @ cpumask_failure.c:234:8
Source context:
232 | ...
233 | ...
>>> 234 | ret = bpf_cpumask_populate(invalid, &bits, sizeof(bits));
| ^-- update: R1 changed from context pointer at offset 0 to integer scalar value
| 1193046
235 | if (!ret)
236 | err = 2;
Instruction context:
0 | (bf) r2 = r10
1 | (07) r2 += -8
>>> 2 | (b7) r1 = 1193046
3 | (b7) r3 = 8
4 | (85) call bpf_cpumask_populate#62860
Suggestion:
Pass stack, map, context, or other verifier-known memory of the expected type and size, not an
integer cast to a pointer.Register Type Safety, verifier_sock/invalidate_pkt_pointers_from_global_func:
Legacy:
R7 invalid mem access 'scalar'
Diagnostic:
Verification failed: Register Type Safety: Invalid dereference
Reason:
R7 is an integer scalar here, not a pointer to memory.
At:
invalidate_pkt_pointers_from_global_func @ verifier_sock.c:1067:5
Source context:
1065 | ...
1066 | skb_pull_data1(sk, 0);
>>> 1067 | *p = 42; /* this is unsafe */
| ^-- error: invalid dereference of R7 (an integer scalar)
1068 | ...
1069 | }
Instruction context:
8 | (85) call pc+4
9 | (b4) w1 = 42
>>> 10 | (63) *(u32 *)(r7 +0) = r1
11 | (bc) w0 = w6
12 | (95) exit
Causal path:
invalidate_pkt_pointers_from_global_func @ verifier_sock.c:1062:29
Source context:
1060 | int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk)
1061 | ...
>>> 1062 | int *p = (void *)(long)sk->data;
| ^-- update: R7 changed from uninitialized value to pkt at offset 0
1063 | ...
1064 | if ((void *)(p + 1) > (void *)(long)sk->data_end)
Instruction context:
0 | (b4) w6 = 2
1 | (61) r2 = *(u32 *)(r1 +80)
>>> 2 | (61) r7 = *(u32 *)(r1 +76)
3 | (bf) r3 = r7
4 | (07) r3 += 4
invalidate_pkt_pointers_from_global_func @ verifier_sock.c:1064:22
Source context:
1062 | int *p = (void *)(long)sk->data;
1063 | ...
>>> 1064 | if ((void *)(p + 1) > (void *)(long)sk->data_end)
| ^-- branch: took the false branch of this conditional, goto not followed
1065 | ...
1066 | skb_pull_data1(sk, 0);
Instruction context:
3 | (bf) r3 = r7
4 | (07) r3 += 4
>>> 5 | (2d) if r3 > r2 goto pc+5
6 | (b4) w6 = 0
7 | (b4) w2 = 0
invalidate_pkt_pointers_from_global_func @ verifier_sock.c:1066:2
Source context:
1064 | if ((void *)(p + 1) > (void *)(long)sk->data_end)
1065 | ...
>>> 1066 | skb_pull_data1(sk, 0);
| ^-- invalidated: R7: packet data may have moved; previous value was pkt at
| offset 0
1067 | *p = 42; /* this is unsafe */
1068 | ...
Instruction context:
6 | (b4) w6 = 0
7 | (b4) w2 = 0
>>> 8 | (85) call pc+4
9 | (b4) w1 = 42
10 | (63) *(u32 *)(r7 +0) = r1
Suggestion:
Preserve a pointer-valued register where needed, or reload and revalidate the pointer after scalar
arithmetic, helper calls, or other operations that can invalidate it.Memory Safety, verifier_helper_value_access/via_variable_no_max_check_1:
Legacy:
R1 unbounded memory access, make sure to bounds check any such access
Diagnostic:
Verification failed: Memory Safety: Access outside bounds
Reason:
The verifier cannot prove offset + access_size <= object_size. Here, the maximal bound for a
memory access is 4294967295 and exceeds maximum allowed offset of 536870912. R1 is map_value;
offset is variable: known bits 0x0, unknown mask 0xffffffff; signed range [0, 4294967295],
unsigned range [0, 4294967295]; access_size is 1; object_size is 48.
At:
via_variable_no_max_check_1 @ verifier_helper_value_access.c:627:2
Source context:
625 | ...
626 | ...
>>> 627 | asm volatile (" \
| ^-- error: access may be outside object bounds
628 | ...
629 | ...
Instruction context:
11 | (b7) r2 = 1
12 | (b7) r3 = 0
>>> 13 | (85) call bpf_probe_read_kernel#113
14 | (95) exit
Causal path:
via_variable_no_max_check_1 @ verifier_helper_value_access.c:627:2
Source context:
625 | ...
626 | ...
>>> 627 | asm volatile (" \
| ^-- update: R0 changed from uninitialized value to nullable map value from
| map_hash_48b at offset 0
628 | ...
629 | ...
Instruction context:
4 | (18) r1 = 0xffff88810a3ea000
>>> 6 | (85) call bpf_map_lookup_elem#1
7 | (15) if r0 == 0x0 goto pc+6
8 | (bf) r1 = r0
via_variable_no_max_check_1 @ verifier_helper_value_access.c:627:2
Source context:
625 | ...
626 | ...
>>> 627 | asm volatile (" \
| ^-- branch: took the false branch of this conditional, goto not followed
628 | ...
629 | ...
Instruction context:
6 | (85) call bpf_map_lookup_elem#1
>>> 7 | (15) if r0 == 0x0 goto pc+6
8 | (bf) r1 = r0
9 | (61) r3 = *(u32 *)(r0 +0)
via_variable_no_max_check_1 @ verifier_helper_value_access.c:627:2
Source context:
625 | ...
626 | ...
>>> 627 | asm volatile (" \
| ^-- update: R1 changed from uninitialized value to map value from map_hash_48b
| at offset 0
628 | ...
629 | ...
Instruction context:
6 | (85) call bpf_map_lookup_elem#1
7 | (15) if r0 == 0x0 goto pc+6
>>> 8 | (bf) r1 = r0
9 | (61) r3 = *(u32 *)(r0 +0)
10 | (0f) r1 += r3
via_variable_no_max_check_1 @ verifier_helper_value_access.c:627:2
Source context:
625 | ...
626 | ...
>>> 627 | asm volatile (" \
| ^-- update: R1 changed from map value from map_hash_48b at offset 0 to map value
| from map_hash_48b with variable offset: known bits 0x0, unknown mask
| 0xffffffff, signed range [0, 4294967295], unsigned range [0, 4294967295]
628 | ...
629 | ...
Instruction context:
8 | (bf) r1 = r0
9 | (61) r3 = *(u32 *)(r0 +0)
>>> 10 | (0f) r1 += r3
11 | (b7) r2 = 1
12 | (b7) r3 = 0
Suggestion:
Add or adjust a bounds check that proves offset + access_size stays within the object.Resource Lifetime Safety, cpumask/test_alloc_no_release:
Legacy:
Unreleased reference id=2 alloc_insn=0
BPF_EXIT instruction in main prog would lead to reference leak
Diagnostic:
Verification failed: Resource Lifetime Safety: Unreleased resource
Reason:
Owned resource (id=2) was acquired at instruction 0 and still needs to be released before this
exit path.
At:
test_alloc_no_release @ cpumask_failure.c:36:5
Source context:
34 | ...
35 | ...
>>> 36 | int BPF_PROG(test_alloc_no_release, struct task_struct *task, u64 clone_flags)
| ^-- error: owned resource (id=2) still needs release
37 | ...
38 | ...
Instruction context:
19 | (7b) *(u64 *)(r10 -8) = r6
20 | (b4) w0 = 0
>>> 21 | (95) exit
Causal path:
test_alloc_no_release @ cpumask_common.h:78:12
Source context:
76 | ...
77 | ...
>>> 78 | cpumask = bpf_cpumask_create();
| ^-- acquired: owned resource (id=2)
79 | if (!cpumask) {
80 | err = 1;
Instruction context:
>>> 0 | (85) call bpf_cpumask_create#62851
1 | (bf) r6 = r0
2 | (55) if r6 != 0x0 goto pc+5
test_alloc_no_release @ cpumask_common.h:79:6
Source context:
77 | ...
78 | cpumask = bpf_cpumask_create();
>>> 79 | if (!cpumask) {
| ^-- branch: took the true branch of this conditional, goto followed
80 | err = 1;
81 | ...
Instruction context:
0 | (85) call bpf_cpumask_create#62851
1 | (bf) r6 = r0
>>> 2 | (55) if r6 != 0x0 goto pc+5
3 | (18) r1 = 0xffffc90000252000
test_alloc_no_release @ cpumask_common.h:84:6
Source context:
82 | ...
83 | ...
>>> 84 | if (!bpf_cpumask_empty(cast(cpumask))) {
| ^-- branch: took the true branch of this conditional, goto followed
85 | err = 2;
86 | bpf_cpumask_release(cpumask);
Instruction context:
9 | (85) call bpf_cpumask_empty#62852
10 | (54) w0 &= 1
>>> 11 | (56) if w0 != 0x0 goto pc+7
12 | (18) r1 = 0xffffc90000252000
Suggestion:
Release or transfer ownership of the acquired resource on every path before the program exits.Patch layout:
- Patches 1-2 add the common infrastructure: report sections, diagnostic categories, source-line lookup, and separate source and instruction context blocks.
- Patches 3-7 add growable environment-owned diagnostic history. The history follows the active verifier path and is pruned when backtracking; it records branch outcomes, material register changes, reference lifetime events, and execution-context events so reports can explain the path and causal state transitions that led to the failure.
- Patches 8-15 add the first category-specific reports. These patches hook selected verifier failure sites and choose the evidence that is useful for that error class.
- Patch 16 gates diagnostic collection and rendering on verifier log level, so stats-only loads do not collect the extra path history. The overhead is limited to verbose log mode through this change.
Evaluation
~~~~~~~~~~
To quantitatively assess diagnostic quality beyond subjective human feedback, we use AI models (called over APIs) and veristat metrics to compare results.
Models are used as a way to measure repair utility of the extra diagnostics over a fixed test set. Each prompt contains only a sanitized source snippet and either the legacy verifier log or the new diagnostic log. To avoid leaking the answer through the test itself, comments, annotations, and other source hints that describe the intended failure were removed. The model is not given internet access, repository access, test execution, verifier access, or the expected fix. The expected causes and intended repairs are kept outside the prompt. Under those constraints, correctness, exact repair rate, output size, reasoning tokens, cost, and wall time provide a proxy for whether the additional verifier context makes the failure easier to understand and turn into a source-level fix.
Verifier cost is assessed by forcing the collection of diagnostics information during normal verification. By default, this information is collected and processed only when verbose logs are enabled, but forcing it even without a verbose log helps us measure the CPU time and memory cost of the extra data.
Both evaluations are covered in the sections below.
Repair Quality
--------------
Repair quality is measured by asking API-only models to propose source fixes from a sanitized source snippet and verifier log. The criterion is score >= 3 on a 0-4 local grading scale, where 3 means a likely fix with incomplete detail and 4 means an actionable source-level fix. Score 4 is reported separately as the exact repair rate. The reported model set contains 596 completed API responses: 298 diagnostic and 298 legacy.
Main results (details available in Appendix):
Metric Diagnostic Legacy Delta ---------------------------------- ----------- ----------- -------- Answers 298 298 Success rate 97.0% 97.3% -0.3 pp Exact repair rate 82.2% 72.1% +10.1 pp Mean score 3.79 3.69 +0.10 Solver cost $8.93 $10.37 -13.8% Mean output tokens per answer 1662 1975 -15.8% Mean reasoning tokens per answer 951 1080 -11.9% Mean wall time per answer 37.3s 44.1s -15.4%
Diagnostic prompts carry more input context. The resulting answers are still shorter and cheaper. In this run, diagnostics do not materially change the coarse success rate, but they increase exact repairs by 10.1 percentage points while reducing cost, output tokens, reasoning tokens, and wall time.
Verifier cost
-------------
Verifier cost is measured with veristat over the BPF selftest programs selected by tools/testing/selftests/bpf/veristat.cfg, with five repetitions per configuration. With diagnostics gated by log level, wall time and verifier duration stay close to baseline. Forcing diagnostics on for every verifier run adds modest overhead on this workload.
memory.peak is measured with cgroup v2 memory accounting for each program load. The table reports the mean wall time, the mean summed verifier duration, and the mean of the per-repetition maximum memory.peak values.
Configuration Wall time mean Verifier duration memory.peak ---------------------------- -------------- ----------------- ----------- bpf-next baseline 25.78s 9.86s 142 MiB diagnostics, gated 26.64s 10.16s 144 MiB diagnostics, forced on 28.01s 11.00s 148 MiB
TODO
~~~~
Known follow-up work:
- Convert more verbose-only verifier errors into category-specific reports.
- Integrate loop-convergence failure summarization from Eduard.
- Report candidate kfuncs/helpers for releasing owned resources.
- Explore association of source variables with verifier registers where debug info permits it.
- Refine suggestions per category and, where useful, link diagnostics to maintained documentation.
- Bring verifier warnings into the same reporting framework.
Appendix: AI repair details
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 20 verifier-failing selftest cases are:
Case Diff Category Selftest selector ------- ------ -------------------------- --------------------------------------------- case-001 easy Call Type Safety cpumask/test_populate_invalid_destination case-002 easy Resource Lifetime Safety cpumask/test_alloc_no_release case-003 easy Register Type Safety verifier_spill_fill/check_corrupted_spill_fill case-004 easy Register Type Safety test_global_funcs/global_func12 case-005 easy Execution Context Safety preempt_lock/preempt_sleepable_helper case-006 easy Policy verifier_helper_restricted/in_bpf_prog_type_kprobe_1 case-007 medium Memory Safety dynptr/dynptr_slice_var_len1 case-008 medium Call Type Safety dynptr/test_dynptr_skb_small_buff case-009 medium Call Type Safety task_kfunc/task_kfunc_acquire_untrusted case-010 medium Register Type Safety test_global_funcs/global_func6 case-011 medium Resource Lifetime Safety dynptr/ringbuf_missing_release2 case-012 medium Execution Context Safety irq/irq_sleepable_helper_global_subprog case-013 medium Verifier Limit test_global_funcs/global_func1 case-014 hard Memory Safety verifier_helper_value_access/via_variable_no_max_check_1 case-015 hard Register Type Safety verifier_sock/invalidate_pkt_pointers_from_global_func case-016 hard Resource Lifetime Safety verifier_ref_tracking/check_free_in_one_subbranch case-017 hard Resource Lifetime Safety irq/irq_restore_ooo case-018 hard Resource Lifetime Safety res_spin_lock_failure/res_spin_lock_ooo_unlock case-019 hard Program Structure verifier_loops1/bounded_recursion case-020 hard Verifier Limit verifier_liveness_exp/liveness_exponential_complexity
The grading scale is:
- 4: identifies the verifier cause and gives an actionable source-level fix.
- 3: gives a likely fix, but with incomplete explanation or detail.
- 2: identifies part of the issue, but not enough to fix confidently.
- 1: gives only a broad verifier-area answer, or a wrong/insufficient fix.
- 0: does not identify the intended verifier failure.
Detailed effort metrics for the model set:
Metric Variant Mean Median P99 ----------------------- ---------- -------- -------- -------- Cost per answer diagnostic $0.030 $0.019 $0.203 Cost per answer legacy $0.035 $0.018 $0.223 Input tokens diagnostic 1391 1220 4048 Input tokens legacy 1052 805 3655 Output tokens diagnostic 1662 954 8680 Output tokens legacy 1975 1034 9912 Reasoning tokens diagnostic 951 208 8108 Reasoning tokens legacy 1080 228 6322 Wall time diagnostic 37.3s 18.3s 222.7s Wall time legacy 44.1s 19.8s 255.5s
Per-model results for diagnostic prompts:
Model profile Ans Succ Exact Mean Cost OutK ReasK Wall ----------------------------------------- --- ----- ----- ---- ------- ---- ----- ----- anthropic-haiku-4.5-default 20 90.0 80.0 3.70 $0.087 11.4 0.0 5.0s anthropic-opus-4.8-high 20 100.0 90.0 3.90 $0.819 25.5 0.0 15.5s anthropic-opus-4.8-medium 20 95.0 90.0 3.85 $0.870 27.5 0.0 12.7s anthropic-sonnet-4.6-high 20 95.0 80.0 3.75 $0.824 48.9 0.0 21.6s anthropic-sonnet-4.6-medium 20 100.0 65.0 3.65 $0.278 12.4 0.0 6.6s openai-gpt-5.3-codex-high 20 100.0 80.0 3.80 $0.601 39.8 33.9 25.0s openai-gpt-5.3-codex-medium 20 95.0 85.0 3.80 $0.287 17.5 11.4 13.5s openai-gpt-5.5-high 20 100.0 90.0 3.90 $2.356 74.4 65.2 56.8s openai-gpt-5.5-low 20 100.0 90.0 3.90 $0.686 18.7 8.5 21.3s openai-gpt-5.5-medium 19 100.0 84.2 3.84 $1.353 41.1 31.8 37.4s openai-gpt-5.5-none 20 95.0 90.0 3.85 $0.457 11.1 0.0 10.4s openrouter-deepseek-r1-0528 20 100.0 75.0 3.75 $0.145 61.5 53.8 98.3s openrouter-deepseek-v3.2 19 100.0 78.9 3.79 $0.028 64.2 58.1 87.3s openrouter-glm-5.1-high 20 95.0 80.0 3.75 $0.113 28.8 20.7 19.3s openrouter-qwen3-coder 20 90.0 75.0 3.65 $0.028 12.4 0.0 7.1s
Per-model results for legacy prompts:
Model profile Ans Succ Exact Mean Cost OutK ReasK Wall ----------------------------------------- --- ----- ----- ---- ------- ---- ----- ----- anthropic-haiku-4.5-default 20 90.0 45.0 3.35 $0.081 11.6 0.0 5.0s anthropic-opus-4.8-high 20 90.0 70.0 3.60 $1.192 42.2 0.0 17.5s anthropic-opus-4.8-medium 20 95.0 85.0 3.80 $1.001 34.5 0.0 13.4s anthropic-sonnet-4.6-high 20 100.0 75.0 3.75 $1.181 74.1 0.0 24.4s anthropic-sonnet-4.6-medium 20 95.0 65.0 3.60 $0.420 23.4 0.0 12.3s openai-gpt-5.3-codex-high 20 100.0 85.0 3.85 $0.562 37.8 31.6 27.1s openai-gpt-5.3-codex-medium 20 100.0 75.0 3.75 $0.318 20.3 13.7 13.6s openai-gpt-5.5-high 19 100.0 78.9 3.79 $2.613 84.0 75.4 98.1s openai-gpt-5.5-low 20 100.0 75.0 3.75 $0.664 19.0 9.7 21.7s openai-gpt-5.5-medium 20 100.0 75.0 3.75 $1.602 50.2 41.0 56.1s openai-gpt-5.5-none 20 95.0 85.0 3.80 $0.416 10.7 0.0 10.9s openrouter-deepseek-r1-0528 20 95.0 70.0 3.65 $0.149 64.6 57.5 92.5s openrouter-deepseek-v3.2 20 100.0 60.0 3.60 $0.030 74.3 67.8 98.3s openrouter-glm-5.1-high 19 100.0 63.2 3.63 $0.115 32.1 24.9 30.4s openrouter-qwen3-coder 20 100.0 75.0 3.75 $0.022 9.5 0.0 5.4s
Kumar Kartikeya Dwivedi (16):
bpf: Add verifier diagnostics report helpers
bpf: Add source and instruction diagnostic context
bpf: Add verifier diagnostic event log
bpf: Prune verifier diagnostics when switching paths
bpf: Track verifier register diagnostic events
bpf: Track verifier reference diagnostic events
bpf: Track verifier context diagnostic events
bpf: Report Register Type Safety errors
bpf: Report Memory Safety bounds errors
bpf: Report Resource Lifetime reference leaks
bpf: Report Call Type Safety argument errors
bpf: Report Execution Context Safety errors
bpf: Report Program Structure CFG errors
bpf: Report Policy helper and kfunc errors
bpf: Report Verifier Limit errors
bpf: Gate verifier diagnostics on log level
include/linux/bpf.h | 13 +- include/linux/bpf_verifier.h | 16 + include/linux/btf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/btf.c | 10 + kernel/bpf/cfg.c | 35 + kernel/bpf/core.c | 35 +- kernel/bpf/diagnostics.c | 2319 +++++++++++++++++ kernel/bpf/diagnostics.h | 177 ++ kernel/bpf/liveness.c | 6 + kernel/bpf/log.c | 11 - kernel/bpf/verifier.c | 1106 +++++++- .../bpf/progs/test_global_func_deep_stack.c | 1 + .../selftests/bpf/progs/verifier_map_in_map.c | 1 + .../selftests/bpf/progs/verifier_uninit.c | 1 + 15 files changed, 3582 insertions(+), 152 deletions(-) create mode 100644 kernel/bpf/diagnostics.c create mode 100644 kernel/bpf/diagnostics.h
base-commit: 3a59f11e0f989bdd637c87151992605a6559a7cb
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 1/16 bpf: Add verifier diagnostics report helpers
Add a small diagnostics renderer for verifier reports and wire it into the BPF build. The initial helpers emit the common text structure: a failure header plus reusable report sections.
Wrap report prose at 100 columns so Reason and Suggestion text stays readable without changing source or instruction gutters.
Gate the helpers on normal verifier log output from the start, so BPF_LOG_STATS-only loads do not collect or render diagnostics.
--- kernel/bpf/Makefile | 2 +- kernel/bpf/diagnostics.c | 55 ++++++++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 16 ++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/diagnostics.c create mode 100644 kernel/bpf/diagnostics.h
kernel/bpf/Makefile +1 -1
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o
kernel/bpf/diagnostics.c new file +55 -0
55 lines not on this page · raw message
kernel/bpf/diagnostics.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#ifndef __BPF_DIAGNOSTICS_H +#define __BPF_DIAGNOSTICS_H + +#include <linux/compiler_attributes.h> +#include <linux/types.h> + +struct bpf_verifier_env ; + +bool bpf_diag_enabled (const struct bpf_verifier_env *env); +void bpf_diag_header (struct bpf_verifier_env *env,const char *category, +const char *problem); + +#endif /* __BPF_DIAGNOSTICS_H */
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 01/16] bpf: Add verifier diagnostics report helpers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The patch completely fails to implement the prose wrapping and reusable report sections promised…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The patch completely fails to implement the prose wrapping and reusable report sections promised in the commit message.
- [Low] Diagnostic category string macros are defined in the .c file instead of the header, making them inaccessible to callers. --
--- Patch [1]: [PATCH bpf-next v4 01/16] bpf: Add verifier diagnostics report helpers --- commit 2d39aa0d2b938aedc24539fb306c5c0aabb08c25
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Add verifier diagnostics report helpers
This patch adds a small diagnostics renderer for verifier reports and wires it into the BPF build. It includes helpers to emit a failure header and aims to provide reusable report sections.
Quoted 2 lines
Wrap report prose at 100 columns so Reason and Suggestion text stays readable without changing source or instruction gutters.
[Severity: Medium]
Is this description accurate for the current implementation? Looking at the code for bpf_diag_header() and diag_write() added in this patch, there does not appear to be any text wrapping logic or implementation for reusable report sections like Reason and Suggestion. Was this logic accidentally omitted from this patch?
Quoted 12 lines
kernel/bpf/diagnostics.c new file +7
-0@@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-only +// Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + +#include <linux/bpf_verifier.h> +#include <linux/ctype.h> +#include <linux/stdarg.h> +
kernel/bpf/diagnostics.c line 17
Quoted 10 lines
kernel/bpf/diagnostics.c new file +10
-0+#include "diagnostics.h" + +#define MEMORY_SAFETY "Memory Safety" +#define REGISTER_TYPE_SAFETY "Register Type Safety" +#define CALL_TYPE_SAFETY "Call Type Safety" +#define RESOURCE_LIFETIME_SAFETY "Resource Lifetime Safety" +#define EXECUTION_CONTEXT_SAFETY "Execution Context Safety" +#define PROGRAM_STRUCTURE "Program Structure" +#define POLICY "Policy" +#define VERIFIER_LIMIT "Verifier Limit"
[Severity: Low]
Should these category string macros be defined in diagnostics.h instead of here in the source file? Since they are not used within diagnostics.c and are not exported in the header file, other files in the verifier will not be able to use them as standardized arguments when calling bpf_diag_header().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 01/16] bpf: Add verifier diagnostics report helpers
In reply to Kumar Kartikeya DwivediOn Thu, 2026-08-13 at 01:33 +0200, Kumar Kartikeya Dwivedi wrote:
...
Quoted 5 lines
kernel/bpf/diagnostics.c new file +0
-0
...
Quoted 1 line
+void
bpf_diag_header(struct bpf_verifier_env *env, const char *category, const char *problem)
Nit: no external users for this function.
Quoted 17 lines
kernel/bpf/diagnostics.c new file +17
-0+{ +char first; + +if (!bpf_diag_enabled (env)) +return ; + + category = category ?:"Verifier Error" ; + problem = problem ?:"" ; + +if (!problem[0 ]) { +diag_write (env,"\nVerification failed: %s\n" , category); +return ; + } + + first =toupper (problem[0 ]); +diag_write (env,"\nVerification failed: %s: %c%s\n" , category, first, problem +1 ); +}
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 2/16 bpf: Add source and instruction diagnostic context
In reply to Kumar Kartikeya DwivediTeach verifier diagnostics to annotate an instruction with BTF source line information and nearby BPF instructions. The renderer keeps source text in a fixed-width lane and prints instructions in a stable right-hand gutter.
Wrap annotation text under the source line so long error labels remain readable while the source and instruction lanes keep their fixed layout.
Keeping source and instruction context in one commit preserves the visual layout contract that later diagnostic reports rely on.
--- include/linux/bpf.h | 13 +- include/linux/bpf_verifier.h | 4 + include/linux/btf.h | 1 + kernel/bpf/btf.c | 10 + kernel/bpf/core.c | 35 ++- kernel/bpf/diagnostics.c | 511 +++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 8 + kernel/bpf/verifier.c | 32 ++- 8 files changed, 581 insertions(+), 33 deletions(-)
include/linux/bpf.h +11 -2
@@ -4147,8 +4147,17 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) }const struct bpf_line_info *bpf_find_linfo (const struct bpf_prog *prog,u32 insn_off); -void bpf_get_linfo_file_line (struct btf *btf,const struct bpf_line_info *linfo, -const char **filep,const char **linep,int *nump); +#define BPF_LINFO_LINE_TRIM 1 +struct bpf_linfo_source { +const char *file; +const char *line; +u32 file_name_off; +int line_num; +int line_col; +}; + +void bpf_get_linfo_source (struct btf *btf,const struct bpf_line_info *linfo, +struct bpf_linfo_source *src,u32 flags);int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep,int *nump);struct bpf_prog *bpf_prog_find_from_stack (void );
include/linux/bpf_verifier.h +4 -0
@@ -830,6 +830,7 @@ static inline u16 bpf_in_stack_arg_cnt(const struct bpf_subprog_info *sub)return 0 ; } +struct bpf_diag ;struct bpf_verifier_env ;struct backtrack_state {
@@ -946,6 +947,7 @@ struct bpf_verifier_env {struct bpf_insn_aux_data *insn_aux_data;/* array of per-insn state */ const struct bpf_line_info *prev_linfo;struct bpf_verifier_log log; +struct bpf_diag *diag;struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS +2 ];/* max + 2 for the fake and exception subprogs */ /* subprog indices sorted in topological order: leaves first, callers last */ int subprog_topo_order[BPF_MAX_SUBPROGS +2 ];
@@ -1429,8 +1431,10 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifievoid print_insn_state (struct bpf_verifier_env *env,const struct bpf_verifier_state *vstate,u32 frameno);u32 bpf_vlog_alignment (u32 pos); +const char *bpf_disasm_kfunc_name (void *data,const struct bpf_insn *insn);struct bpf_subprog_info *bpf_find_containing_subprog (struct bpf_verifier_env *env,int off); +const char *bpf_subprog_name (const struct bpf_verifier_env *env,int subprog);int bpf_jmp_offset (struct bpf_insn *insn);struct bpf_iarray *bpf_insn_successors (struct bpf_verifier_env *env,u32 idx);void bpf_fmt_stack_mask (char *buf,ssize_t buf_sz,u64 stack_mask);
include/linux/btf.h +1 -0
@@ -214,6 +214,7 @@ int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,*/ int btf_type_snprintf_show (const struct btf *btf,u32 type_id,void *obj,char *buf,int len,u64 flags); +int btf_type_snprintf_show_name (const struct btf *btf,u32 type_id,char *buf,int len);int btf_get_fd_by_id (u32 id);u32 btf_obj_id (const struct btf *btf);
kernel/bpf/btf.c +10 -0
@@ -8317,6 +8317,16 @@ int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,return ssnprintf.len; } +int btf_type_snprintf_show_name (const struct btf *btf,u32 type_id,char *buf,int len) +{ +struct btf_show show = { + .btf = btf, + .state.type_id = type_id, + }; + +return snprintf (buf, len,"%s" ,btf_show_name (&show)); +} +#ifdef CONFIG_PROC_FS static void bpf_btf_show_fdinfo (struct seq_file *m,struct file *filp) {
kernel/bpf/core.c +17 -18
@@ -3477,24 +3477,16 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);#ifdef CONFIG_BPF_SYSCALL -void bpf_get_linfo_file_line (struct btf *btf,const struct bpf_line_info *linfo, -const char **filep,const char **linep,int *nump) +void bpf_get_linfo_source (struct btf *btf,const struct bpf_line_info *linfo, +struct bpf_linfo_source *src,u32 flags) { -/* Get base component of the file path. */ -if (filep) { - *filep =btf_name_by_offset (btf, linfo->file_name_off); - *filep =kbasename (*filep); - } - -/* Obtain the source line, and strip whitespace in prefix. */ -if (linep) { - *linep =btf_name_by_offset (btf, linfo->line_off); -while (isspace (**linep)) - *linep +=1 ; - } - -if (nump) - *nump =BPF_LINE_INFO_LINE_NUM (linfo->line_col); + src->file =kbasename (btf_name_by_offset (btf, linfo->file_name_off)); + src->line =btf_name_by_offset (btf, linfo->line_off); +while ((flags &BPF_LINFO_LINE_TRIM ) &&isspace (*src->line)) + src->line++; + src->file_name_off = linfo->file_name_off; + src->line_num =BPF_LINE_INFO_LINE_NUM (linfo->line_col); + src->line_col =BPF_LINE_INFO_LINE_COL (linfo->line_col); }const struct bpf_line_info *bpf_find_linfo (const struct bpf_prog *prog,u32 insn_off)
@@ -3537,6 +3529,7 @@ const struct bpf_line_info *bpf_find_linfo(const struct bpf_prog *prog, u32 insnint bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep,int *nump) { +struct bpf_linfo_source src;int idx =-1 , insn_start, insn_end, len;struct bpf_line_info *linfo;void **jited_linfo;
@@ -3568,7 +3561,13 @@ int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char *if (idx ==-1 )return -ENOENT ; -bpf_get_linfo_file_line (btf, &linfo[idx], filep, linep, nump); +bpf_get_linfo_source (btf, &linfo[idx], &src,BPF_LINFO_LINE_TRIM ); +if (filep) + *filep = src.file; +if (linep) + *linep = src.line; +if (nump) + *nump = src.line_num;return 0 ; }
kernel/bpf/diagnostics.c +511 -0
542 lines not on this page · raw message
kernel/bpf/diagnostics.h +8 -0
@@ -10,7 +10,15 @@struct bpf_verifier_env ;bool bpf_diag_enabled (const struct bpf_verifier_env *env); +int bpf_diag_init (struct bpf_verifier_env *env); +char *bpf_diag_fmt_buf (struct bpf_verifier_env *env,size_t size); +const char *bpf_diag_vfmt (struct bpf_verifier_env *env,const char *fmt,va_list args) +__printf (2 ,0 ); +const char *bpf_diag_fmt (struct bpf_verifier_env *env,const char *fmt, ...)__printf (2 ,3 ); +void bpf_diag_free (struct bpf_verifier_env *env);void bpf_diag_header (struct bpf_verifier_env *env,const char *category,const char *problem); +void bpf_diag_source(struct bpf_verifier_env *env, u32 insn_idx, const char *label, + const char *fmt, ...) __printf(4, 5);#endif /* __BPF_DIAGNOSTICS_H */
kernel/bpf/verifier.c +19 -13
@@ -34,6 +34,7 @@#include <linux/trace_events.h> #include <linux/kallsyms.h> +#include "diagnostics.h" #include "disasm.h" static const struct bpf_verifier_ops *const bpf_verifier_ops[] = {
@@ -405,7 +406,7 @@ static bool subprog_returns_void(struct bpf_verifier_env *env, int subprog)return btf_type_is_void (type); } -static const char *subprog_name (const struct bpf_verifier_env *env,int subprog) +const char *bpf_subprog_name (const struct bpf_verifier_env *env,int subprog) {struct bpf_func_info *info;
@@ -3032,8 +3033,8 @@ static int sort_subprogs_topo(struct bpf_verifier_env *env)if (bpf_pseudo_func (&insn[idx]))continue ;verbose (env,"recursive call from %s() to %s()\n" , -subprog_name (env, cur), -subprog_name (env, callee)); +bpf_subprog_name (env, cur), +bpf_subprog_name (env, callee)); ret = -EINVAL ; goto out; }
@@ -3053,8 +3054,8 @@ static int sort_subprogs_topo(struct bpf_verifier_env *env)if (env->log.level &BPF_LOG_LEVEL2 )for (i =0 ; i < cnt; i++) -verbose (env,"topo_order[%d] = %s\n" , - i,subprog_name (env, env->subprog_topo_order[i])); +verbose (env,"topo_order[%d] = %s\n" , i, +bpf_subprog_name (env, env->subprog_topo_order[i])); out:kvfree (dfs_stack);kvfree (color);
@@ -3198,7 +3199,7 @@ static void linked_regs_unpack(u64 val, struct linked_regs *s) } } -static const char *disasm_kfunc_name (void *data,const struct bpf_insn *insn) +const char *bpf_disasm_kfunc_name (void *data,const struct bpf_insn *insn) {const struct btf_type *func;struct btf *desc_btf;
@@ -3217,9 +3218,9 @@ static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn)void bpf_verbose_insn (struct bpf_verifier_env *env,struct bpf_insn *insn) {const struct bpf_insn_cbs cbs = { - .cb_call = disasm_kfunc_name, - .cb_print = verbose, - .private_data = env, + .cb_call = bpf_disasm_kfunc_name, + .cb_print = verbose, + .private_data = env, };print_bpf_insn (&cbs, insn, env->allow_ptr_leaks);
@@ -9391,7 +9392,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (err == -EFAULT )return err;if (bpf_subprog_is_global (env, subprog)) { -const char *sub_name =subprog_name (env, subprog); +const char *sub_name =bpf_subprog_name (env, subprog);if (env->cur_state->active_locks) { verbose(env, "global function calls are not allowed while holding a lock,\n"
@@ -18433,7 +18434,7 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) regs = state->frame[state->curframe]->regs;if (subprog || env->prog->type ==BPF_PROG_TYPE_EXT ) { -const char *sub_name =subprog_name (env, subprog); +const char *sub_name =bpf_subprog_name (env, subprog);struct bpf_subprog_arg_info *arg;struct bpf_reg_state *reg;
@@ -18603,8 +18604,9 @@ static int do_check_subprogs(struct bpf_verifier_env *env)if (ret) {return ret; }else if (env->log.level &BPF_LOG_LEVEL ) { -verbose (env,"Func#%d ('%s') is safe for any args that match its prototype\n" , - i,subprog_name (env, i)); +verbose (env, +"Func#%d ('%s') is safe for any args that match its prototype\n" , i, +bpf_subprog_name (env, i)); }/* We verified new global subprog, it might have called some
@@ -20134,6 +20136,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_free_env; + ret =bpf_diag_init (env); +if (ret) + goto err_prep;if (env->signature) { ret =bpf_prog_calc_tag (env->prog);if (ret <0 )
@@ -20424,6 +20429,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,kvfree (env->scc_info);kvfree (env->succ);kvfree (env->gotox_tmp_buf); +bpf_diag_free (env);kvfree (env);return ret; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 02/16] bpf: Add source and instruction diagnostic context
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf_disasm_kfunc_name() will trigger a NULL pointer dereference or mutate verifier state if…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf_disasm_kfunc_name() will trigger a NULL pointer dereference or mutate verifier state if diagnostic formatting is invoked before the add_kfuncs() pass completes. --
--- Patch [2]: [PATCH bpf-next v4 02/16] bpf: Add source and instruction diagnostic context --- commit ef91626322111071ba03bd2ef6640700c748dd5f
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Add source and instruction diagnostic context
Teach verifier diagnostics to annotate an instruction with BTF source line information and nearby BPF instructions. The renderer keeps source text in a fixed-width lane and prints instructions in a stable right-hand gutter.
Quoted 3 lines
kernel/bpf/diagnostics.c +0
-0
[ ... ]
Quoted 1 line
kernel/bpf/diagnostics.c +0
-0@@ -35,6 +245,182 @@ static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...)
[ ... ]
Quoted 7 lines
kernel/bpf/diagnostics.c +7
-0+static const char *disasm_kfunc_name (void *private_data,const struct bpf_insn *insn) +{ +struct disasm_ctx *ctx = private_data; + +return bpf_disasm_kfunc_name (ctx->env, insn); +} +
kernel/bpf/diagnostics.c static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) · line 338
Quoted 10 lines
kernel/bpf/diagnostics.c +10
-0+static void format_disasm_line (struct bpf_verifier_env *env,int insn_idx, +struct disasm_line *line) +{ +struct disasm_ctx ctx = { .env = env }; +struct bpf_insn *insn; +const struct bpf_insn_cbs cbs = { + .cb_call = disasm_kfunc_name, + .cb_print = disasm_print, + .private_data = &ctx, + };
[Severity: High]
Can this sequence trigger a NULL pointer dereference or unsafe state mutation?
If bpf_diag_source() handles an error during early structural validation (before add_kfuncs() runs), it calls format_disasm_line() to print nearby context instructions.
If any instruction in this window is a module kfunc call, it invokes the bpf_disasm_kfunc_name() callback shown above, which eventually cascades into __find_kfunc_desc_btf() in kernel/bpf/verifier.c.
__find_kfunc_desc_btf() then executes:
tab = env->prog->aux->kfunc_btf_tab;
b = bsearch(&kf_btf, tab->descs, tab->nr_descs, ...);
If the kfunc BTF table hasn't been allocated yet, wouldn't dereferencing tab cause a panic?
Additionally, if the table is only partially initialized, wouldn't __find_kfunc_desc_btf() mutate the table by appending and sorting new entries during what is expected to be a read-only logging operation?
Quoted 4 lines
kernel/bpf/diagnostics.c +4
-0+ + line->idx = insn_idx; + line->valid = false; +seq_buf_init (&ctx.seq, line->text,sizeof (line->text));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf-next v4 02/16] bpf: Add source and instruction diagnostic context
In reply to Kumar Kartikeya DwivediOn Thu, 2026-08-13 at 01:33 +0200, Kumar Kartikeya Dwivedi wrote:
...
include/linux/bpf.h static inline bool bpf_is_subprog(const struct bpf_prog *prog) · line 4150
Quoted 11 lines
include/linux/bpf.h +0
-0@@ -4147,8 +4147,17 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) }
const structbpf_line_info*bpf_find_linfo(const structbpf_prog*prog, u32insn_off);include/linux/bpf.h +1
-2-void bpf_get_linfo_file_line (struct btf *btf,const struct bpf_line_info *linfo, - const char **filep, const char **linep, int *nump); +#define BPF_LINFO_LINE_TRIM 1
Nit: a single callsite requires trim, might as well trim there and avoid the flag.
Quoted 10 lines
include/linux/bpf.h +10
-0+struct bpf_linfo_source { +const char *file; +const char *line; +u32 file_name_off; +int line_num; +int line_col; +}; + +void bpf_get_linfo_source (struct btf *btf,const struct bpf_line_info *linfo, + struct bpf_linfo_source *src, u32 flags);
...
include/linux/btf.h int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, · line 217
Quoted 7 lines
include/linux/btf.h +1
-0@@ -214,6 +214,7 @@ int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,*/ int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, char *buf, int len, u64 flags); +int btf_type_snprintf_show_name (const struct btf *btf,u32 type_id,char *buf,int len);
Nit: the name of the function suggests that it does a printf.
...
Quoted 4 lines
kernel/bpf/diagnostics.c +0
-0
...
Quoted 5 lines
kernel/bpf/diagnostics.c +0
-0@@ -16,6 +24,50 @@ #define POLICY "Policy" #define VERIFIER_LIMIT "Verifier Limit"
+#defineBPF_DIAG_MSG_LEN512
...
Quoted 1 line
+#define
BPF_DIAG_REG_DESC_LEN512
...
Quoted 1 line
+#define
BPF_DIAG_REG_TMP_LEN192
dead constants
...
kernel/bpf/diagnostics.c bool bpf_diag_enabled(const struct bpf_verifier_env *env) · line 81
Quoted 4 lines
kernel/bpf/diagnostics.c +4
-0+static struct bpf_diag *diag_env (struct bpf_verifier_env *env) +{ +return env->diag; +}
this wrapper is unnecessary
Quoted 9 lines
kernel/bpf/diagnostics.c +9
-0+static char *diag_fmt_alloc (struct bpf_verifier_env *env,size_t size) +{ +struct bpf_diag *diag =diag_env (env); +struct diag_fmt_chunk *chunk; +size_t capacity, available; +char *buf; + +if (!diag || !size || size >INT_MAX ) +return NULL ;
kernel/bpf/diagnostics.c bool bpf_diag_enabled(const struct bpf_verifier_env *env) · line 121
Quoted 10 lines
kernel/bpf/diagnostics.c +10
-0+ +if (!list_empty (&diag->fmt_chunks)) { + chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node); + available =seq_buf_get_buf (&chunk->seq, &buf); +if (available >= size) + goto commit; + } + + capacity =max_t (size_t,BPF_DIAG_FMT_CHUNK_SIZE , size); + chunk =kmalloc (struct_size (chunk, data, capacity),GFP_KERNEL_ACCOUNT );
Q: would it be cheaper to allocate one page per chunk?
Quoted 13 lines
kernel/bpf/diagnostics.c +13
-0+if (!chunk) +return NULL ; + +seq_buf_init (&chunk->seq, chunk->data, capacity); +list_add_tail (&chunk->node, &diag->fmt_chunks); + available =seq_buf_get_buf (&chunk->seq, &buf); +if (WARN_ON_ONCE (available < size)) +return NULL ; + +commit: +seq_buf_commit (&chunk->seq, size); +return buf; +}
...
Quoted 13 lines
kernel/bpf/diagnostics.c +13
-0+static void diag_fmt_restore (struct bpf_verifier_env *env,struct diag_fmt_mark mark) +{ +struct bpf_diag *diag =diag_env (env); +struct diag_fmt_chunk *chunk; + +if (!diag) +return ; + +while (!list_empty (&diag->fmt_chunks)) { + chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node); +if (chunk == mark.chunk) +break ; +list_del (&chunk->node);
kernel/bpf/diagnostics.c bool bpf_diag_enabled(const struct bpf_verifier_env *env) · line 210
Quoted 10 lines
kernel/bpf/diagnostics.c +10
-0+kfree (chunk); + } + +if (mark.chunk) { + mark.chunk->seq.len = mark.len; +seq_buf_str (&mark.chunk->seq); + } +} + +static void diag_fmt_free (struct bpf_verifier_env *env)
Nit: single user, might as well inline. Also, can it be:
diag_fmt_restore(env, (struct diag_fmt_mark mark){})
?
Quoted 25 lines
kernel/bpf/diagnostics.c +25
-0+{ +struct bpf_diag *diag =diag_env (env); +struct diag_fmt_chunk *chunk, *tmp; + +if (!diag) +return ; + +list_for_each_entry_safe (chunk, tmp, &diag->fmt_chunks, node) { +list_del (&chunk->node); +kfree (chunk); + } +} + +void bpf_diag_free (struct bpf_verifier_env *env) +{ +struct bpf_diag *diag = env->diag; + +if (!diag) +return ; + +diag_fmt_free (env); +kfree (diag); + env->diag =NULL ; +} +
...
kernel/bpf/diagnostics.c static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) · line 417
Quoted 12 lines
kernel/bpf/diagnostics.c +12
-0+static void diag_format_source_lane (char *buf,size_t size,const char *source_prefix, +int source_line_width,int line_num,const char *line) +{ +int len, text_width; + +if (line_num <=0 ) { + buf[0 ] ='\0' ; +return ; + } + + len =scnprintf (buf, size,"%s%*d | " , source_prefix, source_line_width, line_num); +if (len >= (int )size)
Can this condition ever be true? scnprintf returns a value in range [0..size].
Quoted 5 lines
kernel/bpf/diagnostics.c +5
-0+return ; + + text_width =BPF_DIAG_SOURCE_LANE_WIDTH - len; +diag_format_source_text (buf + len, size - len, line, text_width); +}
...
Quoted 3 lines
kernel/bpf/diagnostics.c +3
-0+void bpf_diag_source (struct bpf_verifier_env *env,u32 insn_idx,const char *label, + const char *fmt, ...) +{
...
kernel/bpf/diagnostics.c void bpf_diag_header(struct bpf_verifier_env *env, const char *category, const c · line 499
Quoted 10 lines
kernel/bpf/diagnostics.c +10
-0+ linfo =bpf_find_linfo (env->prog, insn_idx); +if (!btf || !linfo) { +diag_write (env," insn %u\n" , insn_idx); +diag_print_source_annotation (env,0 ,0 , label, msg); + goto out_restore; + } +bpf_get_linfo_source (btf, linfo, &src,0 ); +if (!src.file || !*src.file || !src.line || !*src.line) { +diag_write (env," insn %u\n" , insn_idx); +diag_print_source_annotation (env,0 ,0 , label, msg);
Should this case still print instruction context?
(and the one above it).
Quoted 3 lines
kernel/bpf/diagnostics.c +3
-0+ goto out_restore; + } +
...
Quoted 2 lines
kernel/bpf/diagnostics.h +0
-0
...
kernel/bpf/diagnostics.h line 22
Quoted 2 lines
kernel/bpf/diagnostics.h +2
-0+void bpf_diag_source(struct bpf_verifier_env *env, u32 insn_idx, const char *label, + const char *fmt, ...) __printf(4, 5);
Nit: no external users.
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 3/16 bpf: Add verifier diagnostic event log
In reply to Kumar Kartikeya DwivediAdd an environment-owned diagnostic history for verifier reports. Event payloads keep the user-facing branch history shape, while storage lives in bpf_verifier_env and follows the active verifier path.
Grow the event array as entries are appended and keep saved positions as array indices. Later patches can truncate back to those positions when verifier search backtracks. Add the branch event renderer and branch recording.
--- kernel/bpf/diagnostics.c | 108 +++++++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 3 ++ kernel/bpf/verifier.c | 21 ++++++++ 3 files changed, 132 insertions(+)
kernel/bpf/diagnostics.c +108 -0
@@ -34,8 +34,24 @@#define BPF_DIAG_REG_TMP_LEN 192#define BPF_DIAG_FMT_CHUNK_SIZE 1024#define BPF_DIAG_FMT_BUF_SIZE 256 +#define BPF_DIAG_EVENT_LOG_MAX_SIZE (1U << 20)#define DISASM_LINE_LEN 160 +enum bpf_diag_history_kind { +BPF_DIAG_HISTORY_BRANCH , +}; + +struct bpf_diag_history_event { +u32 insn_idx :24 ; +u32 kind :8 ; +u8 in_lineage :1 ; +union { +struct { +bool cond_true; + } branch; + }; +}; +struct disasm_line {char text[DISASM_LINE_LEN ];int idx;
@@ -58,12 +74,21 @@ struct diag_fmt_mark {size_t len; }; +struct bpf_diag_log { +struct bpf_diag_history_event *events; +u32 cnt; +u32 cap; +u32 dropped; +bool capped; +}; +struct bpf_diag_scratch {struct bpf_linfo_source source_lines[BPF_DIAG_CONTEXT_CNT ];struct disasm_line disasm_lines[BPF_DIAG_CONTEXT_CNT ]; };struct bpf_diag { +struct bpf_diag_log log;struct bpf_diag_scratch scratch;struct list_head fmt_chunks; };
@@ -229,6 +254,7 @@ void bpf_diag_free(struct bpf_verifier_env *env)return ;diag_fmt_free (env); +kvfree (diag->log.events);kfree (diag); env->diag =NULL ; }
@@ -245,6 +271,75 @@ static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...)va_end (args); } +static struct bpf_diag_log *diag_event_log (struct bpf_verifier_env *env) +{ +struct bpf_diag *diag =diag_env (env); + +return diag ? &diag->log :NULL ; +} + +u32 bpf_diag_event_log_pos (struct bpf_verifier_env *env) +{ +struct bpf_diag *diag =diag_env (env); + +if (!diag) +return 0 ; +return diag->log.cnt; +} + +void bpf_diag_event_log_reset (struct bpf_verifier_env *env,u32 pos) +{ +struct bpf_diag *diag = env->diag; +struct bpf_diag_log *log; +u32 end; + +if (!diag) +return ; + + log = &diag->log; + end = log->cnt; +if (WARN_ON_ONCE (pos > end)) + pos = end; + + log->cnt = pos; +} + +static void diag_append_history (struct bpf_verifier_env *env, +const struct bpf_diag_history_event *event) +{ +struct bpf_diag_history_event *events; +struct bpf_diag_log *log; +u32 cap, max_events; + + log =diag_event_log (env); +if (!log) +return ; + +if (log->cnt < log->cap) { + log->events[log->cnt++] = *event; +return ; + } + + max_events =BPF_DIAG_EVENT_LOG_MAX_SIZE /sizeof (*events); +if (log->capped || log->cap == max_events) { +if (log->dropped !=U32_MAX ) + log->dropped++; +return ; + } + + cap =min_t (u32, log->cap ? log->cap *2 :64 , max_events); + events =kvrealloc (log->events,array_size (cap,sizeof (*events)),GFP_KERNEL_ACCOUNT ); +if (!events) { + log->capped = true; +if (log->dropped !=U32_MAX ) + log->dropped++; +return ; + } + log->events = events; + log->cap = cap; + log->events[log->cnt++] = *event; +} +static void diag_print_wrapped_prefixed (struct bpf_verifier_env *env,const char *first_prefix,const char *next_prefix,const char *text) {
@@ -564,3 +659,16 @@ void bpf_diag_source(struct bpf_verifier_env *env, u32 insn_idx, const char *lab out_restore:diag_fmt_restore (env, mark); } + +void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true) +{ +struct bpf_diag_history_event event = { + .insn_idx = insn_idx, + .kind =BPF_DIAG_HISTORY_BRANCH , + .branch = { + .cond_true = cond_true, + }, + }; + +diag_append_history (env, &event); +}
kernel/bpf/diagnostics.h +3 -0
@@ -15,10 +15,13 @@ char *bpf_diag_fmt_buf(struct bpf_verifier_env *env, size_t size);const char *bpf_diag_vfmt (struct bpf_verifier_env *env,const char *fmt,va_list args)__printf (2 ,0 );const char *bpf_diag_fmt (struct bpf_verifier_env *env,const char *fmt, ...)__printf (2 ,3 ); +u32 bpf_diag_event_log_pos (struct bpf_verifier_env *env); +void bpf_diag_event_log_reset (struct bpf_verifier_env *env,u32 pos);void bpf_diag_free (struct bpf_verifier_env *env);void bpf_diag_header (struct bpf_verifier_env *env,const char *category,const char *problem); void bpf_diag_source(struct bpf_verifier_env *env, u32 insn_idx, const char *label, const char *fmt, ...) __printf(4, 5); +void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);#endif /* __BPF_DIAGNOSTICS_H */
kernel/bpf/verifier.c +21 -0
@@ -17369,6 +17369,27 @@ static int do_check(struct bpf_verifier_env *env) state->last_insn_idx = env->prev_insn_idx; state->insn_idx = env->insn_idx; +/* +* Record the incoming edge so active and queued paths use the same +* branch-recording path. A zero-offset conditional has identical +* successors, so its outcome cannot be reconstructed from the edge. +*/ +if (!state->speculative && prev_insn_idx >=0 && prev_insn_idx < insn_cnt) { +struct bpf_insn *prev_insn = &insns[prev_insn_idx]; +int fallthrough_idx = prev_insn_idx +1 ; +int branch_idx = prev_insn_idx +bpf_jmp_offset (prev_insn) +1 ; +u8 class =BPF_CLASS (prev_insn->code); +u8 opcode =BPF_OP (prev_insn->code); + +if ((class ==BPF_JMP || class ==BPF_JMP32 ) && + opcode !=BPF_JA && opcode !=BPF_CALL && opcode !=BPF_EXIT && + opcode <=BPF_JCOND && branch_idx != fallthrough_idx) { +if (env->insn_idx == branch_idx) +bpf_diag_record_branch (env, prev_insn_idx, true); +else if (env->insn_idx == fallthrough_idx) +bpf_diag_record_branch (env, prev_insn_idx, false); + } + }if (bpf_is_prune_point (env, env->insn_idx)) { err =bpf_is_state_visited (env, env->insn_idx);
Signature · 1 line
2.53.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 03/16] bpf: Add verifier diagnostic event log
On Thu, 2026-08-13 at 01:33 +0200, Kumar Kartikeya Dwivedi wrote:
...
Quoted 10 lines
kernel/bpf/diagnostics.c | 108 +++++++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 3 ++ kernel/bpf/verifier.c | 21 ++++++++ 3 files changed, 132 insertions(+)kernel/bpf/diagnostics.c +0
-0
kernel/bpf/diagnostics.c line 37
Quoted 4 lines
kernel/bpf/diagnostics.c +1
-0@@ -34,8 +34,24 @@#define BPF_DIAG_REG_TMP_LEN 192#define BPF_DIAG_FMT_CHUNK_SIZE 1024#define BPF_DIAG_FMT_BUF_SIZE 256 +#define BPF_DIAG_EVENT_LOG_MAX_SIZE (1U << 20)
Q: did you observe the drops in practice?
I'm curious what would be a practical limit here. As it stands the events buffer is 7K entries, seem to be low.
...
Quoted 5 lines
kernel/bpf/diagnostics.c +5
-0+void bpf_diag_event_log_reset (struct bpf_verifier_env *env,u32 pos) +{ +struct bpf_diag *diag = env->diag; +struct bpf_diag_log *log; +u32 end;
kernel/bpf/diagnostics.c static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) · line 304
Quoted 10 lines
kernel/bpf/diagnostics.c +10
-0+ +if (!diag) +return ; + + log = &diag->log; + end = log->cnt; +if (WARN_ON_ONCE (pos > end)) + pos = end; + + log->cnt = pos;
Should this reset 'dropped' as well? 'capped'?
If not, do we need an exact count for 'dropped'?
Quoted 2 lines
kernel/bpf/diagnostics.c +2
-0+} +
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 4/16 bpf: Prune verifier diagnostics when switching paths
In reply to Kumar Kartikeya DwivediSave the diagnostic event-log position with each verifier stack entry and reset the environment-owned stream together with the normal verifier log when a queued state is popped. Also reset the diagnostic stream after successful subprogram verification even when level-2 logging preserves the normal verifier log.
--- kernel/bpf/verifier.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +9 -2
@@ -194,6 +194,7 @@ struct bpf_verifier_stack_elem {struct bpf_verifier_stack_elem *next;/* length of verifier log at the time this state was pushed on stack */ u32 log_pos; +u32 diag_log_pos; };#define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192
@@ -1700,6 +1701,7 @@ static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, err =bpf_copy_verifier_state (cur, &head->st);if (err)return err; +bpf_diag_event_log_reset (env, head->diag_log_pos); }if (pop_log)bpf_vlog_reset (&env->log, head->log_pos);
@@ -1743,6 +1745,7 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, elem->prev_insn_idx = prev_insn_idx; elem->next = env->head; elem->log_pos = env->log.end_pos; + elem->diag_log_pos =bpf_diag_event_log_pos (env); env->head = elem; env->stack_size++; err =bpf_copy_verifier_state (&elem->st, cur);
@@ -2264,6 +2267,7 @@ static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, elem->prev_insn_idx = prev_insn_idx; elem->next = env->head; elem->log_pos = env->log.end_pos; + elem->diag_log_pos =bpf_diag_event_log_pos (env); env->head = elem; env->stack_size++;if (env->stack_size >BPF_COMPLEXITY_LIMIT_JMP_SEQ ) {
@@ -18566,8 +18570,11 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) ret =do_check (env); out: -if (!ret && pop_log) -bpf_vlog_reset (&env->log,0 ); +if (!ret) { +if (pop_log) +bpf_vlog_reset (&env->log,0 ); +bpf_diag_event_log_reset (env,0 ); + }free_states (env);return ret; }
Signature · 1 line
2.53.0
Re: [PATCH bpf-next v4 04/16] bpf: Prune verifier diagnostics when switching paths
Quoted 8 lines · Kumar Kartikeya Dwivedi
Save the diagnostic event-log position with each verifier stack entry and reset the environment-owned stream together with the normal verifier log when a queued state is popped. Also reset the diagnostic stream after successful subprogram verification even when level-2 logging preserves the normal verifier log.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 5/16 bpf: Track verifier register diagnostic events
In reply to Kumar Kartikeya DwivediRecord material register and outgoing stack argument changes so diagnostics can explain how a value reached its current type, bounds, or unreadable state.
Store old and new register types, scalar ranges, tnum value and mask, map and BTF type identity, and basic operand metadata in the environment-owned diagnostic event stream.
Record invalidations when packet data moves, references are released, or borrowed references leave their protected region. Register-scoped history starts at the latest matching modification and then shows later branch outcomes.
Also record fixed stack spills and overwrites, and tag register fills from stack so register-scoped history can follow value flow through spilled stack slots.
The type_is_map_ptr() helper previously lived as a static function in kernel/bpf/log.c since commit 0c95c9fdb696 ("bpf: emit map name in register state if applicable and available"). Move it verbatim to include/linux/bpf_verifier.h as a static inline, next to the other type classifiers, so diagnostics.c can reuse it without duplicating the case list.
--- include/linux/bpf_verifier.h | 12 ++ kernel/bpf/diagnostics.c | 271 ++++++++++++++++++++++++++++++++++- kernel/bpf/diagnostics.h | 80 +++++++++++ kernel/bpf/log.c | 11 -- kernel/bpf/verifier.c | 134 +++++++++++++---- 5 files changed, 468 insertions(+), 40 deletions(-)
include/linux/bpf_verifier.h +12 -0
@@ -1347,6 +1347,18 @@ static inline bool type_is_non_owning_ref(u32 type)return type_is_ptr_alloc_obj (type) &&type_flag (type) &NON_OWN_REF ; } +static inline bool type_is_map_ptr (enum bpf_reg_type type) +{ +switch (base_type (type)) { +case CONST_PTR_TO_MAP : +case PTR_TO_MAP_KEY : +case PTR_TO_MAP_VALUE : +return true; +default : +return false; + } +} +static inline bool type_is_pkt_pointer (enum bpf_reg_type type) { type =base_type (type);
kernel/bpf/diagnostics.c +269 -2
309 lines not on this page · raw message
kernel/bpf/diagnostics.h +80 -0
98 lines not on this page · raw message
kernel/bpf/log.c +0 -11
@@ -615,17 +615,6 @@ static void print_scalar_ranges(struct bpf_verifier_env *env, } } -static bool type_is_map_ptr (enum bpf_reg_type t) { -switch (base_type (t)) { -case CONST_PTR_TO_MAP : -case PTR_TO_MAP_KEY : -case PTR_TO_MAP_VALUE : -return true; -default : -return false; - } -} -/* * _a stands for append, was shortened to avoid multiline statements below. * This macro is used to output a comma separated list of attributes.
kernel/bpf/verifier.c +107 -27
270 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 05/16] bpf: Track verifier register diagnostic events
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing diagnostic event tracking for BPF_LD_IMM64 and BPF_ATOMIC (with BPF_FETCH) operations. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing diagnostic event tracking for BPF_LD_IMM64 and BPF_ATOMIC (with BPF_FETCH) operations. --
--- Patch [5]: [PATCH bpf-next v4 05/16] bpf: Track verifier register diagnostic events --- commit 7db736a554444e8ded5ae93ab09a9f7f17a9c87b
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Track verifier register diagnostic events
This commit introduces verifier register diagnostic events to track how a value reached its current state. It records register modifications, stack spills, reference invalidations, and other events in the diagnostic history.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 12 lines
kernel/bpf/verifier.c +4
-2@@ -6391,15 +6421,19 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn, src_reg_type = regs[insn->src_reg].type; -/* Check if (src_reg + off) is readable. The state of dst_reg will be -* updated by this call. +/* +* check_stack_read_fixed_off() may refine the modification's origin to +* the source stack slot. */ +bpf_diag_mod_begin (env, ®s[insn->dst_reg],NULL ,BPF_DIAG_MOD_WRITE ); err = check_mem_access(env, env->insn_idx, regs + insn->src_reg, argno_from_reg(insn->src_reg), insn->off,
kernel/bpf/verifier.c static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 6439
Quoted 10 lines
kernel/bpf/verifier.c +2
-0BPF_SIZE (insn->code),BPF_READ , insn->dst_reg, strict_alignment_once, is_ldsx); err = err ?:save_aux_ptr_type (env, src_reg_type, allow_trust_mismatch); err = err ?:reg_bounds_sanity_check (env, ®s[insn->dst_reg], ctx); +if (!err) +bpf_diag_mod_end (env);return err; }
[Severity: Medium]
Are we missing similar diagnostic event tracking for BPF_LD_IMM64 and BPF_ATOMIC (with BPF_FETCH) operations?
For example, in check_ld_imm(), we directly modify the destination register without calling bpf_diag_mod_begin() or bpf_diag_mod_end(), which prevents the origin of the value from being recorded:
dst_reg = ®s[insn->dst_reg];
if (insn->src_reg == 0) {
u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
dst_reg->type = SCALAR_VALUE;
__mark_reg_known(®s[insn->dst_reg], imm);
return 0;
}Additionally, in check_atomic_rmw(), we modify load_reg but lack diagnostic event tracing. If stack memory is fetched, check_stack_read_fixed_off() calls bpf_diag_mod_begin(), but bpf_diag_mod_end() is never called afterwards.
load_reg = bpf_atomic_load_reg(insn);
if (load_reg >= 0) {
/* check and record load of old value */
err = check_reg_arg(env, load_reg, DST_OP);
if (err)
return err;
}
dst_reg = cur_regs(env) + insn->dst_reg;
/* Check whether we can read the memory, with second call for fetch
* case to simulate the register fill.
*/
err = check_mem_access(env, env->insn_idx, dst_reg, argno_from_reg(insn->dst_reg), insn->off,
BPF_SIZE(insn->code), BPF_READ, -1, true, false);
if (!err && load_reg >= 0)
err = check_mem_access(env, env->insn_idx, dst_reg, argno_from_reg(insn->dst_reg),
insn->off, BPF_SIZE(insn->code),
BPF_READ, load_reg, true, false);Does this leak the active modification state and cause the stack fetch event to be permanently lost? Should these paths also be instrumented to track value origins correctly?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 05/16] bpf: Track verifier register diagnostic events
In reply to Kumar Kartikeya DwivediOn Thu, 2026-08-13 at 01:33 +0200, Kumar Kartikeya Dwivedi wrote:
Claude ensures me that BPF_ATOMIC | BPF_FETCH lacks the mod_end() call for when the stack memory is modified. Seem to be true.
...
Quoted 1 line
kernel/bpf/diagnostics.c +0
-0
...
kernel/bpf/diagnostics.c line 34
Quoted 6 lines
kernel/bpf/diagnostics.c +0
-2@@ -30,15 +30,23 @@#define BPF_DIAG_CONTEXT_CNT (1 + BPF_DIAG_CONTEXT * 2)#define BPF_DIAG_SOURCE_LANE_WIDTH 88#define BPF_DIAG_TAB_WIDTH 8 -#define BPF_DIAG_REG_DESC_LEN 512 -#define BPF_DIAG_REG_TMP_LEN 192
Churn
...
kernel/bpf/diagnostics.c struct bpf_diag_history_event { · line 62
Quoted 7 lines
kernel/bpf/diagnostics.c +3
-0@@ -49,6 +57,13 @@ struct bpf_diag_history_event {struct {bool cond_true; } branch; +struct { +struct bpf_diag_mod_target target; +struct bpf_diag_mod_target origin;
Silly question, should we keep the 'src'/'dst' terminology here? In order to be in line with instruction set.
Quoted 6 lines
kernel/bpf/diagnostics.c +4
-0+struct bpf_diag_reg_snapshot old, new; +u8 reason; +bool origin_valid; + } mod; }; };
...
kernel/bpf/diagnostics.c static void diag_print_wrapped_prefixed(struct bpf_verifier_env *env, const char · line 405
Quoted 6 lines
kernel/bpf/diagnostics.c +2
-0@@ -375,6 +401,34 @@ static void diag_print_wrapped_prefixed(struct bpf_verifier_env *env, const char } } +static void bpf_diag_format_btf_type (char *buf,size_t size,const struct btf *btf,u32 type_id) +{
Nit: single caller, might as well inline.
Quoted 25 lines
kernel/bpf/diagnostics.c +25
-0+size_t len; +int ret; + + buf[0 ] ='\0' ; + ret =btf_type_snprintf_show_name (btf, type_id, buf, size); +if (ret <0 || !buf[0 ]) { +scnprintf (buf, size,"BTF type ID %u" , type_id); +return ; + } + + len =strlen (buf); +if (len && buf[len -1 ] =='{' ) + buf[len -1 ] ='\0' ; +} + +const char *bpf_diag_fmt_btf_type (struct bpf_verifier_env *env,const struct btf *btf,u32 type_id) +{ +char *buf =bpf_diag_fmt_buf (env,BPF_DIAG_FMT_BUF_SIZE ); + +if (!buf) +return "" ; + +bpf_diag_format_btf_type (buf,BPF_DIAG_FMT_BUF_SIZE , btf, type_id); +return buf; +}
...
kernel/bpf/diagnostics.c void bpf_diag_record_branch(struct bpf_verifier_env *env, u32 insn_idx, bool con · line 746
Quoted 3 lines
kernel/bpf/diagnostics.c +3
-0+static bool diag_snapshot_eq (const struct bpf_diag_reg_snapshot *old, +const struct bpf_diag_reg_snapshot *new) +{
Nit: memcmp?
Quoted 5 lines
kernel/bpf/diagnostics.c +5
-0+return old->type == new->type && old->map_ptr == new->map_ptr && old->btf == new->btf && + old->btf_id == new->btf_id && old->var_off.value == new->var_off.value && + old->var_off.mask == new->var_off.mask && old->r64.base == new->r64.base && + old->r64.size == new->r64.size; +}
...
kernel/bpf/diagnostics.c void bpf_diag_record_branch(struct bpf_verifier_env *env, u32 insn_idx, bool con · line 819
Quoted 9 lines
kernel/bpf/diagnostics.c +9
-0+static struct bpf_func_state *diag_func_state (struct bpf_verifier_env *env,u32 frameno) +{ +struct bpf_verifier_state *vstate = env->cur_state; +int frame; + +for (frame =0 ; frame <= vstate->curframe; frame++) { +if (vstate->frame[frame]->frameno == frameno) +return vstate->frame[frame]; + }
Isn't this just return frameno <= vstate->curframe ? vstate->frame[frameno] : NULL?
Quoted 3 lines
kernel/bpf/diagnostics.c +3
-0+return NULL ; +} +
...
kernel/bpf/diagnostics.h
Quoted 1 line
kernel/bpf/diagnostics.h +0
-0
...
--- 8< --------------------------------------
kernel/bpf/diagnostics.h
Quoted 6 lines
kernel/bpf/diagnostics.h +6
-0+enum bpf_diag_mod_target_kind { ... +struct bpf_diag_mod_target { ... +static inline struct bpf_diag_mod_target bpf_diag_reg_target(u32 frameno, u8 regno) ... +static inline struct bpf_diag_mod_target bpf_diag_stack_arg_target(u32 frameno, u8 slot) ... +static inline struct bpf_diag_mod_target bpf_diag_stack_slot_target(u32 frameno, u16 spi) ... +static inline struct bpf_diag_mod_target bpf_diag_stack_range_target(u32 frameno, s16 min_off, ...
-------------------------------------- >8 ---
Move this to diagnostics.c? (and drop prefixes).
...
Quoted 1 line
kernel/bpf/verifier.c +0
-0
...
Quoted 11 lines
kernel/bpf/verifier.c +1
-3@@ -8902,13 +8936,18 @@ static int check_func_proto(const struct bpf_func_proto *fn, struct bpf_call_arg*/ static void clear_all_pkt_pointers (struct bpf_verifier_env *env) { +struct bpf_stack_state *stack;struct bpf_func_state *state;struct bpf_reg_state *reg; - bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ - if (reg_is_pkt_pointer_any(reg) || reg_is_dynptr_slice_pkt(reg)) - mark_reg_invalid(env, reg);
kernel/bpf/verifier.c static int check_func_proto(const struct bpf_func_proto *fn, struct bpf_call_arg · line 8951
Quoted 10 lines
kernel/bpf/verifier.c +8
-1- })); +bpf_for_each_reg_in_vstate_mask ( + env->cur_state, state, reg, stack,1 <<STACK_SPILL , ({ +if (reg_is_pkt_pointer_any (reg) ||reg_is_dynptr_slice_pkt (reg)) { +bpf_diag_record_scrub (env, reg,BPF_DIAG_MOD_PKT_DATA_CHANGE ); +mark_reg_invalid (env, reg); + } + })) + ; }
Nit: bpf_for_each_reg_in_vstate_mask conversion is not necessary.
Kumar Kartikeya Dwivedi
maintainerPATCH v4 6/16 bpf: Track verifier reference diagnostic events
In reply to Kumar Kartikeya DwivediAdd reference acquire and release events to diagnostic history so Resource Lifetime Safety reports can show the lifetime of a specific reference id along the path.
Record acquisitions after the verifier assigns the reference id. Record releases only after release_reference_nomark() succeeds, including the kptr_xchg RCU conversion path and owning-to-non-owning conversion path that consume an owning reference.
--- kernel/bpf/diagnostics.c | 28 ++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 2 ++ kernel/bpf/verifier.c | 32 +++++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 7 deletions(-)
kernel/bpf/diagnostics.c +28 -0
@@ -47,6 +47,8 @@ struct bpf_diag_reg_snapshot {enum bpf_diag_history_kind {BPF_DIAG_HISTORY_BRANCH ,BPF_DIAG_HISTORY_MOD , +BPF_DIAG_HISTORY_REF_ACQUIRE , +BPF_DIAG_HISTORY_REF_RELEASE , };struct bpf_diag_history_event {
@@ -64,6 +66,9 @@ struct bpf_diag_history_event {u8 reason;bool origin_valid; } mod; +struct { +u32 ref_id; + } ref; }; };
@@ -939,3 +944,26 @@ void bpf_diag_record_scrub_stack(struct bpf_verifier_env *env, u32 frameno, s16bpf_diag_stack_range_target (frameno, min_off, max_off), reason, NULL, NULL, NULL); } + +static void diag_record_ref (struct bpf_verifier_env *env,u32 insn_idx,u8 kind,u32 ref_id) +{ +struct bpf_diag_history_event event = { + .insn_idx = insn_idx, + .kind = kind, + .ref = { + .ref_id = ref_id, + }, + }; + +diag_append_history (env, &event); +} + +void bpf_diag_record_ref_acquire (struct bpf_verifier_env *env,u32 insn_idx,u32 ref_id) +{ +diag_record_ref (env, insn_idx,BPF_DIAG_HISTORY_REF_ACQUIRE , ref_id); +} + +void bpf_diag_record_ref_release (struct bpf_verifier_env *env,u32 insn_idx,u32 ref_id) +{ +diag_record_ref (env, insn_idx,BPF_DIAG_HISTORY_REF_RELEASE , ref_id); +}
kernel/bpf/diagnostics.h +2 -0
@@ -103,5 +103,7 @@ void bpf_diag_record_scrub(struct bpf_verifier_env *env, const struct bpf_reg_st enum bpf_diag_mod_reason reason);void bpf_diag_record_scrub_stack (struct bpf_verifier_env *env,u32 frameno,s16 min_off,s16 max_off,enum bpf_diag_mod_reason reason); +void bpf_diag_record_ref_acquire (struct bpf_verifier_env *env,u32 insn_idx,u32 ref_id); +void bpf_diag_record_ref_release (struct bpf_verifier_env *env,u32 insn_idx,u32 ref_id);#endif /* __BPF_DIAGNOSTICS_H */
kernel/bpf/verifier.c +25 -7
@@ -205,7 +205,8 @@ struct bpf_verifier_stack_elem {#define BPF_PRIV_STACK_MIN_SIZE 64static int acquire_reference (struct bpf_verifier_env *env,int insn_idx,int parent_id); -static int release_reference_nomark (struct bpf_verifier_state *state,int id); +static int __release_reference_nomark (struct bpf_verifier_state *state,int id); +static int release_reference_nomark (struct bpf_verifier_env *env,int id);static int release_reference (struct bpf_verifier_env *env,int id);static void invalidate_non_owning_refs (struct bpf_verifier_env *env);static void invalidate_rcu_protected_refs (struct bpf_verifier_env *env);
@@ -1419,6 +1420,7 @@ static int acquire_reference(struct bpf_verifier_env *env, int insn_idx, int par s->type =REF_TYPE_PTR ; s->id = ++env->id_gen; s->parent_id = parent_id; +bpf_diag_record_ref_acquire (env, insn_idx, s->id);return s->id; }
@@ -8976,7 +8978,7 @@ static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range reg->range =AT_PKT_END ; } -static int release_reference_nomark (struct bpf_verifier_state *state,int id) +static int __release_reference_nomark (struct bpf_verifier_state *state,int id) {int i;
@@ -8991,6 +8993,16 @@ static int release_reference_nomark(struct bpf_verifier_state *state, int id)return -EINVAL ; } +static int release_reference_nomark (struct bpf_verifier_env *env,int id) +{ +int err; + + err =__release_reference_nomark (env->cur_state, id); +if (!err) +bpf_diag_record_ref_release (env, env->insn_idx, id); +return err; +} +static int idstack_push (struct bpf_idmap *idmap,u32 id) {int i;
@@ -9033,8 +9045,10 @@ static int release_reference(struct bpf_verifier_env *env, int id)if (err)return err; -if (find_reference_state (vstate, id)) -WARN_ON_ONCE (release_reference_nomark (vstate, id)); +if (find_reference_state (vstate, id)) { + err =release_reference_nomark (env, id); +WARN_ON_ONCE (err); + }while ((id =idstack_pop (idstack))) {/*
@@ -9126,7 +9140,9 @@ static int ref_convert_alloc_rcu_protected(struct bpf_verifier_env *env, u32 id)struct bpf_reg_state *reg;int err; - err =release_reference_nomark (env->cur_state, id); + err =release_reference_nomark (env, id); +if (err) +return err; bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ if (reg->id != id)
@@ -11670,8 +11686,10 @@ static void ref_convert_owning_non_owning(struct bpf_verifier_env *env, u32 id) {struct bpf_func_state *unused;struct bpf_reg_state *reg; +int err; -WARN_ON_ONCE (release_reference_nomark (env->cur_state, id)); + err =release_reference_nomark (env, id); +WARN_ON_ONCE (err); bpf_for_each_reg_in_vstate(env->cur_state, unused, reg, ({ if (reg->id == id) {
@@ -15803,7 +15821,7 @@ static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,* No one could have freed the reference state before * doing the NULL check. */ -WARN_ON_ONCE (release_reference_nomark (vstate, id)); +WARN_ON_ONCE (__release_reference_nomark (vstate, id)); bpf_for_each_reg_in_vstate(vstate, state, reg, ({ mark_ptr_or_null_reg(state, reg, id, is_null);
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 7/16 bpf: Track verifier context diagnostic events
In reply to Kumar Kartikeya DwivediRecord verifier context transitions in the diagnostic history so later reports can anchor causal paths to the critical section that made an operation invalid.
This covers lock, IRQ, RCU, and preempt regions without adding any new verifier error reports. Category-specific commits decide where those recorded events should be rendered.
Use context depth when selecting scoped history so nested regions anchor at the outer active region, and fall back to the earliest retained event when the matching entry was pruned.
--- kernel/bpf/diagnostics.c | 39 +++++++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 12 ++++++++++++ kernel/bpf/verifier.c | 28 +++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 5 deletions(-)
kernel/bpf/diagnostics.c +39 -0
@@ -49,6 +49,7 @@ enum bpf_diag_history_kind {BPF_DIAG_HISTORY_MOD ,BPF_DIAG_HISTORY_REF_ACQUIRE ,BPF_DIAG_HISTORY_REF_RELEASE , + BPF_DIAG_HISTORY_CONTEXT, }; struct bpf_diag_history_event {
@@ -69,6 +70,11 @@ struct bpf_diag_history_event {struct {u32 ref_id; } ref; +struct { +u32 depth; +u8 kind; +bool enter; + } ctx; }; };
@@ -335,6 +341,19 @@ void bpf_diag_event_log_reset(struct bpf_verifier_env *env, u32 pos) log->cnt = pos; } +u32 bpf_diag_irq_depth (const struct bpf_verifier_state *state) +{ +u32 depth =0 ; +int i; + +for (i =0 ; i < state->acquired_refs; i++) { +if (state->refs[i].type ==REF_TYPE_IRQ ) + depth++; + } + +return depth; +} +static void diag_append_history (struct bpf_verifier_env *env,const struct bpf_diag_history_event *event) {
@@ -967,3 +986,23 @@ void bpf_diag_record_ref_release(struct bpf_verifier_env *env, u32 insn_idx, u32 {diag_record_ref (env, insn_idx,BPF_DIAG_HISTORY_REF_RELEASE , ref_id); } + +void bpf_diag_record_context (struct bpf_verifier_env *env,u32 insn_idx, +enum bpf_diag_context_kind ctx_kind,bool enter,u32 depth) +{ +/* +* Keep leave events so context rendering can stop at a depth-zero exit +* and show nested-region depth accurately for the active path. +*/ +struct bpf_diag_history_event event = { + .insn_idx = insn_idx, + .kind =BPF_DIAG_HISTORY_CONTEXT , + .ctx = { + .kind = ctx_kind, + .enter = enter, + .depth = depth, + }, + }; + +diag_append_history (env, &event); +}
kernel/bpf/diagnostics.h +12 -0
@@ -9,6 +9,7 @@struct bpf_reg_state ;struct bpf_verifier_env ; +struct bpf_verifier_state ;struct btf ;enum bpf_diag_mod_reason {
@@ -81,6 +82,14 @@ static inline struct bpf_diag_mod_target bpf_diag_stack_range_target(u32 frameno }; } +enum bpf_diag_context_kind { +BPF_DIAG_CONTEXT_NONE , +BPF_DIAG_CONTEXT_RCU , +BPF_DIAG_CONTEXT_PREEMPT , +BPF_DIAG_CONTEXT_IRQ , +BPF_DIAG_CONTEXT_LOCK , +}; +bool bpf_diag_enabled (const struct bpf_verifier_env *env);int bpf_diag_init (struct bpf_verifier_env *env);char *bpf_diag_fmt_buf (struct bpf_verifier_env *env,size_t size);
@@ -90,6 +99,7 @@ const char *bpf_diag_fmt(struct bpf_verifier_env *env, const char *fmt, ...) __pconst char *bpf_diag_fmt_btf_type (struct bpf_verifier_env *env,const struct btf *btf,u32 type_id);u32 bpf_diag_event_log_pos (struct bpf_verifier_env *env);void bpf_diag_event_log_reset (struct bpf_verifier_env *env,u32 pos); +u32 bpf_diag_irq_depth (const struct bpf_verifier_state *state);void bpf_diag_free (struct bpf_verifier_env *env);void bpf_diag_header (struct bpf_verifier_env *env,const char *category,const char *problem);
@@ -105,5 +115,7 @@ void bpf_diag_record_scrub_stack(struct bpf_verifier_env *env, u32 frameno, s16 s16 max_off, enum bpf_diag_mod_reason reason);void bpf_diag_record_ref_acquire (struct bpf_verifier_env *env,u32 insn_idx,u32 ref_id);void bpf_diag_record_ref_release (struct bpf_verifier_env *env,u32 insn_idx,u32 ref_id); +void bpf_diag_record_context (struct bpf_verifier_env *env,u32 insn_idx, +enum bpf_diag_context_kind ctx_kind,bool enter,u32 depth);#endif /* __BPF_DIAGNOSTICS_H */
kernel/bpf/verifier.c +23 -5
@@ -1046,7 +1046,7 @@ static int is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_s }static int acquire_irq_state (struct bpf_verifier_env *env,int insn_idx); -static int release_irq_state (struct bpf_verifier_state *state,int id); +static int release_irq_state (struct bpf_verifier_env *env,int id); static int mark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
@@ -1105,7 +1105,7 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_rreturn -EINVAL ; } - err =release_irq_state (env->cur_state, st->id); + err =release_irq_state (env, st->id);WARN_ON_ONCE (err && err != -EACCES );if (err) {int insn_idx =0 ;
@@ -1440,6 +1440,8 @@ static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum r state->active_locks++; state->active_lock_id = id; state->active_lock_ptr = ptr; +bpf_diag_record_context (env, insn_idx,BPF_DIAG_CONTEXT_LOCK , true, + state->active_locks);return 0 ; }
@@ -1455,6 +1457,8 @@ static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx) s->id = ++env->id_gen; state->active_irq_id = s->id; +bpf_diag_record_context (env, insn_idx,BPF_DIAG_CONTEXT_IRQ , true, +bpf_diag_irq_depth (state));return s->id; }
@@ -1496,8 +1500,9 @@ static bool reg_is_referenced(struct bpf_verifier_env *env, const struct bpf_regreturn find_reference_state (env->cur_state, reg->id); } -static int release_lock_state (struct bpf_verifier_state *state,int type,int id,void *ptr) +static int release_lock_state (struct bpf_verifier_env *env,int type,int id,void *ptr) { +struct bpf_verifier_state *state = env->cur_state;void *prev_ptr =NULL ;u32 prev_id =0 ;int i;
@@ -1510,6 +1515,8 @@ static int release_lock_state(struct bpf_verifier_state *state, int type, int id/* Reassign active lock (id, ptr). */ state->active_lock_id = prev_id; state->active_lock_ptr = prev_ptr; +bpf_diag_record_context (env, env->insn_idx,BPF_DIAG_CONTEXT_LOCK , + false, state->active_locks);return 0 ; }if (state->refs[i].type &REF_TYPE_LOCK_MASK ) {
@@ -1520,8 +1527,9 @@ static int release_lock_state(struct bpf_verifier_state *state, int type, int idreturn -EINVAL ; } -static int release_irq_state (struct bpf_verifier_state *state,int id) +static int release_irq_state (struct bpf_verifier_env *env,int id) { +struct bpf_verifier_state *state = env->cur_state;u32 prev_id =0 ;int i;
@@ -1534,6 +1542,8 @@ static int release_irq_state(struct bpf_verifier_state *state, int id)if (state->refs[i].id == id) {release_reference_state (state, i); state->active_irq_id = prev_id; +bpf_diag_record_context (env, env->insn_idx,BPF_DIAG_CONTEXT_IRQ , + false,bpf_diag_irq_depth (state));return 0 ; }else { prev_id = state->refs[i].id;
@@ -7142,7 +7152,7 @@ static int process_spin_lock(struct bpf_verifier_env *env, struct bpf_reg_stateverbose (env,"%s_unlock cannot be out of order\n" , lock_str);return -EINVAL ; } -if (release_lock_state (cur, type, reg->id, ptr)) { +if (release_lock_state (env, type, reg->id, ptr)) {verbose (env,"%s_unlock of different lock\n" , lock_str);return -EINVAL ; }
@@ -13155,22 +13165,30 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (rcu_lock) { env->cur_state->active_rcu_locks++; +bpf_diag_record_context (env, insn_idx,BPF_DIAG_CONTEXT_RCU , true, + env->cur_state->active_rcu_locks); }else if (rcu_unlock) {if (env->cur_state->active_rcu_locks ==0 ) {verbose (env,"unmatched rcu read unlock (kernel function %s)\n" , func_name);return -EINVAL ; } env->cur_state->active_rcu_locks--; +bpf_diag_record_context (env, insn_idx,BPF_DIAG_CONTEXT_RCU , false, + env->cur_state->active_rcu_locks);if (!in_rcu_cs (env))invalidate_rcu_protected_refs (env); }else if (preempt_disable) { env->cur_state->active_preempt_locks++; +bpf_diag_record_context (env, insn_idx,BPF_DIAG_CONTEXT_PREEMPT , true, + env->cur_state->active_preempt_locks); }else if (preempt_enable) {if (env->cur_state->active_preempt_locks ==0 ) {verbose (env,"unmatched attempt to enable preemption (kernel function %s)\n" , func_name);return -EINVAL ; } env->cur_state->active_preempt_locks--; +bpf_diag_record_context (env, insn_idx,BPF_DIAG_CONTEXT_PREEMPT , false, + env->cur_state->active_preempt_locks);if (!in_rcu_cs (env))invalidate_rcu_protected_refs (env); }
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 8/16 bpf: Report Register Type Safety errors
In reply to Kumar Kartikeya DwivediAugment selected register-state verifier failures with Register Type Safety reports. The existing verbose verifier messages remain in place; the new reports add reason, source context, causal path, and suggestions.
Cover invalid pointer dereferences, unreadable registers, missing outgoing stack arguments for bpf2bpf and kfunc calls, and rejected pointer arithmetic. Use scoped diagnostic history so reports start from the latest relevant value change and then show later branch outcomes.
--- kernel/bpf/diagnostics.c | 898 ++++++++++++++++++ kernel/bpf/diagnostics.h | 18 + kernel/bpf/verifier.c | 128 ++- .../selftests/bpf/progs/verifier_uninit.c | 1 + 4 files changed, 1030 insertions(+), 15 deletions(-)
kernel/bpf/diagnostics.c +898 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only // Copyright (c) 2026 Meta Platforms, Inc. and affiliates. +#include <linux/bitmap.h> #include <linux/bpf.h> #include <linux/bpf_verifier.h> #include <linux/btf.h>
@@ -25,6 +26,7 @@#define VERIFIER_LIMIT "Verifier Limit"#define BPF_DIAG_TEXT_WIDTH 100 +#define BPF_DIAG_TEXT_INDENT " "#define BPF_DIAG_MSG_LEN 512#define BPF_DIAG_CONTEXT 2#define BPF_DIAG_CONTEXT_CNT (1 + BPF_DIAG_CONTEXT * 2)
@@ -78,6 +80,28 @@ struct bpf_diag_history_event { }; }; +enum bpf_diag_history_scope { +BPF_DIAG_HISTORY_SCOPE_ALL , +BPF_DIAG_HISTORY_SCOPE_REG , +BPF_DIAG_HISTORY_SCOPE_STACK_ARG , +BPF_DIAG_HISTORY_SCOPE_REF , +BPF_DIAG_HISTORY_SCOPE_CONTEXT , +}; + +struct bpf_diag_history_opts { +enum bpf_diag_history_scope scope; +u32 frameno; +int regno; +int stack_arg_slot; +u32 ref_id; +enum bpf_diag_context_kind ctx_kind; +u32 ctx_depth; +}; + +static void diag_print_history (struct bpf_verifier_env *env, +const struct bpf_diag_history_opts *opts); +static bool diag_target_matches (const struct bpf_diag_mod_target *event_target, +const struct bpf_diag_mod_target *target);struct disasm_line {char text[DISASM_LINE_LEN ];int idx;
@@ -390,6 +414,12 @@ static void diag_append_history(struct bpf_verifier_env *env, log->events[log->cnt++] = *event; } +static const struct bpf_diag_history_event *diag_history_event (const struct bpf_diag_log *log, +u32 idx) +{ +return &log->events[idx]; +} +static void diag_print_wrapped_prefixed (struct bpf_verifier_env *env,const char *first_prefix,const char *next_prefix,const char *text) {
@@ -425,6 +455,11 @@ static void diag_print_wrapped_prefixed(struct bpf_verifier_env *env, const char } } +static void diag_print_wrapped_text (struct bpf_verifier_env *env,const char *text) +{ +diag_print_wrapped_prefixed (env,BPF_DIAG_TEXT_INDENT ,BPF_DIAG_TEXT_INDENT , text); +} +static void bpf_diag_format_btf_type (char *buf,size_t size,const struct btf *btf,u32 type_id) {size_t len;
@@ -453,6 +488,26 @@ const char *bpf_diag_fmt_btf_type(struct bpf_verifier_env *env, const struct btfreturn buf; } +static void diag_vprint_indented(struct bpf_verifier_env *env, const char *fmt, va_list args) + __printf(2, 0); + +static void diag_vprint_indented (struct bpf_verifier_env *env,const char *fmt,va_list args) +{ +char *buf; + +if (!bpf_diag_enabled (env)) +return ; + + buf =kvasprintf (GFP_KERNEL_ACCOUNT , fmt, args); +if (!buf) { +diag_write (env,"%s<failed to allocate diagnostic text>\n" ,BPF_DIAG_TEXT_INDENT ); +return ; + } + +diag_print_wrapped_text (env, buf); +kfree (buf); +} +static int diag_line_width (unsigned int line) {int width =1 ;
@@ -613,6 +668,47 @@ void bpf_diag_header(struct bpf_verifier_env *env, const char *category, const cdiag_write (env,"\nVerification failed: %s: %c%s\n" , category, first, problem +1 ); } +static void diag_reason(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3); +static void diag_suggestion(struct bpf_verifier_env *env, const char *fmt, ...) + __printf(2, 3); + +static void diag_section (struct bpf_verifier_env *env,const char *title) +{ +if (!bpf_diag_enabled (env)) +return ; + +diag_write (env,"\n%s:\n" , title); +} + +static void diag_reason (struct bpf_verifier_env *env,const char *fmt, ...) +{ +va_list args; + +if (!bpf_diag_enabled (env)) +return ; + +diag_section (env,"Reason" ); + +va_start (args, fmt); +diag_vprint_indented (env, fmt, args); +va_end (args); +} + +static void diag_suggestion (struct bpf_verifier_env *env,const char *fmt, ...) +{ +va_list args; + +if (!bpf_diag_enabled (env)) +return ; + +diag_section (env,"Suggestion" ); + +va_start (args, fmt); +diag_vprint_indented (env, fmt, args); +va_end (args); +diag_write (env,"\n" ); +} +static void diag_print_source_annotation (struct bpf_verifier_env *env,int line_width,int indent,const char *label,const char *msg) {
@@ -738,6 +834,277 @@ void bpf_diag_source(struct bpf_verifier_env *env, u32 insn_idx, const char *labdiag_fmt_restore (env, mark); } +static u32 diag_current_frameno (const struct bpf_verifier_env *env) +{ +return env->cur_state->frame[env->cur_state->curframe]->frameno; +} + +void bpf_diag_register_type (struct bpf_verifier_env *env,u32 insn_idx,int regno, +const char *problem,const char *reason,const char *suggestion) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_REG , + .frameno =diag_current_frameno (env), + .regno = regno, + }; + +bpf_diag_header (env,REGISTER_TYPE_SAFETY , problem); +diag_reason (env,"%s" , reason); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s" , problem); + +if (regno >=0 ) +diag_print_history (env, &opts); + +diag_suggestion (env,"%s" , suggestion); +} + +const char *bpf_diag_reg_type_plain (struct bpf_verifier_env *env,enum bpf_reg_type type) +{ +switch (base_type (type)) { +case NOT_INIT : +return "an uninitialized value" ; +case SCALAR_VALUE : +return "an integer scalar" ; +case PTR_TO_CTX : +return "a context pointer" ; +case PTR_TO_STACK : +return "a stack pointer" ; +case PTR_TO_MAP_VALUE : +if (type_may_be_null (type)) +return "a nullable map value pointer" ; +return "a map value pointer" ; +case PTR_TO_MEM : +if (type_may_be_null (type)) +return "a nullable memory pointer" ; +return "a memory pointer" ; +case PTR_TO_BTF_ID : +if (type_may_be_null (type)) +return "a nullable kernel object pointer" ; +if (type_is_non_owning_ref (type)) +return "a borrowed allocated object pointer" ; +if (type_is_ptr_alloc_obj (type)) +return "an owned allocated object pointer" ; +if (type_flag (type) &PTR_UNTRUSTED ) +return "an untrusted kernel object pointer" ; +return "a kernel object pointer" ; +default : +return reg_type_str (env, type); + } +} + +static const char *diag_arg_ordinal (int argno) +{ +switch (argno) { +case 1 : +return "first" ; +case 2 : +return "second" ; +case 3 : +return "third" ; +case 4 : +return "fourth" ; +case 5 : +return "fifth" ; +case 6 : +return "sixth" ; +case 7 : +return "seventh" ; +case 8 : +return "eighth" ; +case 9 : +return "ninth" ; +case 10 : +return "tenth" ; +case 11 : +return "eleventh" ; +case 12 : +return "twelfth" ; +default : +return NULL ; + } +} + +void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno, +const char *reg_name,const struct bpf_reg_state *reg, +enum bpf_diag_invalid_deref_kind kind,s64 offset) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_REG , + .frameno =diag_current_frameno (env), + .regno = regno, + }; +const char *type_name =bpf_diag_reg_type_plain (env, reg->type); + +bpf_diag_header (env,REGISTER_TYPE_SAFETY ,"invalid dereference" ); + +switch (kind) { +case BPF_DIAG_DEREF_SCALAR : +diag_reason (env,"%s is an integer scalar here, not a pointer to memory." , + reg_name); +break ; +case BPF_DIAG_DEREF_NULLABLE_PTR : +diag_reason ( + env,"%s may be NULL here (%s). The program could dereference NULL on this path, so the verifier cannot prove this access is safe." , + reg_name, type_name); +break ; +case BPF_DIAG_DEREF_MODIFIED_PTR : +diag_reason ( + env,"%s has offset %lld here, but this pointer type must be dereferenced in its original form." , + reg_name, offset); +break ; +case BPF_DIAG_DEREF_INVALID_PTR : +default : +diag_reason ( + env,"%s has type %s here, which is not valid for this memory access." , + reg_name, type_name); +break ; + } + +diag_section (env,"At" ); +if (kind ==BPF_DIAG_DEREF_MODIFIED_PTR ) +bpf_diag_source (env, insn_idx,"error" , +"dereference requires the original %s pointer" , type_name); +else +bpf_diag_source (env, insn_idx,"error" ,"invalid dereference of %s (%s)" , + reg_name, type_name); + +if (regno >=0 ) +diag_print_history (env, &opts); + +switch (kind) { +case BPF_DIAG_DEREF_NULLABLE_PTR : +diag_suggestion ( + env,"Add a NULL check before the access and dereference the pointer only on the non-NULL path." ); +break ; +case BPF_DIAG_DEREF_MODIFIED_PTR : +diag_suggestion ( + env,"Preserve the original pointer in another register, or use only offsets this pointer type permits before dereferencing it." ); +break ; +case BPF_DIAG_DEREF_SCALAR : +case BPF_DIAG_DEREF_INVALID_PTR : +default : +diag_suggestion ( + env,"Preserve a pointer-valued register where needed, or reload and revalidate the pointer after scalar arithmetic, helper calls, or other operations that can invalidate it." ); +break ; + } +} + +void bpf_diag_unreadable_reg (struct bpf_verifier_env *env,u32 insn_idx,int regno) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_REG , + .frameno =diag_current_frameno (env), + .regno = regno, + }; +const struct bpf_diag_log *log =diag_event_log (env); +struct bpf_diag_mod_target target; +bool invalidated = false; +int i; + + target =bpf_diag_reg_target (opts.frameno, regno); +for (i = log ? log->cnt :0 ; i >0 ; i--) { +const struct bpf_diag_history_event *event =diag_history_event (log, i -1 ); + +if (event->kind !=BPF_DIAG_HISTORY_MOD || + !diag_target_matches (&event->mod.target, &target)) +continue ; + invalidated = event->mod.new.type ==NOT_INIT ; +break ; + } + +bpf_diag_header (env,REGISTER_TYPE_SAFETY ,"unreadable register" ); +if (invalidated) +diag_reason ( + env,"R%d is not readable here. A previous operation invalidated this register, so the verifier cannot use it as an input." , + regno); +else if (log && !log->dropped) +diag_reason (env, +"R%d has never been initialized on this path, so the verifier cannot use it as an input." , + regno); +else +diag_reason ( + env,"R%d is not readable here. It may never have been initialized, or an earlier operation may have invalidated it." , + regno); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"R%d is not readable" , regno); + +if (regno >=0 ) +diag_print_history (env, &opts); + +if (invalidated) +diag_suggestion ( + env,"Avoid using the register after it is invalidated, or initialize it again before this instruction." ); +else if (log && !log->dropped) +diag_suggestion (env,"Initialize R%d on every path before this instruction." , regno); +else +diag_suggestion ( + env,"Initialize the register on every path, or initialize it again after any operation that invalidates it." ); +} + +static int diag_stack_argno (u8 slot) +{ +return MAX_BPF_FUNC_REG_ARGS + slot +1 ; +} + +static void diag_format_stack_arg (char *buf,size_t size,u8 slot,const char *arg_name) +{ +int argno =diag_stack_argno (slot); +const char *ordinal =diag_arg_ordinal (argno); + +if (ordinal && arg_name) +scnprintf (buf, size,"outgoing stack argument %u (%s argument, %s)" , slot +1 , + ordinal, arg_name); +else if (ordinal) +scnprintf (buf, size,"outgoing stack argument %u (%s argument)" , slot +1 , ordinal); +else if (arg_name) +scnprintf (buf, size,"outgoing stack argument %u (%s)" , slot +1 , arg_name); +else +scnprintf (buf, size,"outgoing stack argument %u" , slot +1 ); +} + +void bpf_diag_stack_arg_uninit (struct bpf_verifier_env *env,u32 insn_idx,int nargs, +int stack_arg_slot,const char *callee_name, +const char *arg_name) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_STACK_ARG , + .frameno =diag_current_frameno (env), + .stack_arg_slot = stack_arg_slot, + }; +const char *arg_buf; + + arg_buf =bpf_diag_fmt_buf (env,BPF_DIAG_FMT_BUF_SIZE ); +if (arg_buf) +diag_format_stack_arg ((char *)arg_buf,BPF_DIAG_FMT_BUF_SIZE , stack_arg_slot, + arg_name); +else + arg_buf ="" ; +bpf_diag_header (env,REGISTER_TYPE_SAFETY ,"missing stack argument" ); +if (callee_name && *callee_name) +diag_reason (env, +"Function %s expects %d arguments, but %s is not initialized at " +"this call." , + callee_name, nargs, arg_buf); +else +diag_reason (env, +"The callee expects %d arguments, but %s is not initialized at " +"this call." , + nargs, arg_buf); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s is not initialized" , arg_buf); + +if (stack_arg_slot >=0 ) +diag_print_history (env, &opts); + +diag_suggestion (env,"Write the outgoing stack argument after any operation that " +"may invalidate stored pointer values, and before making this " +"call." ); +} +void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true) {struct bpf_diag_history_event event = {
@@ -1006,3 +1373,534 @@ void bpf_diag_record_context(struct bpf_verifier_env *env, u32 insn_idx,diag_append_history (env, &event); } + +static int diag_history_context_start_idx (const struct bpf_diag_log *log, +const struct bpf_diag_history_opts *opts) +{ +int i; + +if (!opts->ctx_depth) +return 0 ; + +/* Find the most recent outermost entry, or a depth-zero exit. */ +for (i = log->cnt; i >0 ; i--) { +const struct bpf_diag_history_event *event; + + event =diag_history_event (log, i -1 ); + +if (event->kind !=BPF_DIAG_HISTORY_CONTEXT || event->ctx.kind != opts->ctx_kind) +continue ; + +if (event->ctx.enter && event->ctx.depth ==1 ) +return i -1 ; +if (!event->ctx.enter && event->ctx.depth ==0 ) +return 0 ; + } + +return 0 ; +} + +struct bpf_diag_history_filter { +const struct bpf_diag_history_opts *opts; +u32 lineage_start; +bool lineage_valid; +}; + +static bool diag_target_matches (const struct bpf_diag_mod_target *event_target, +const struct bpf_diag_mod_target *target) +{ +int slot_off; + +if (event_target->frameno != target->frameno) +return false; + +if (event_target->kind ==BPF_DIAG_MOD_TARGET_STACK_RANGE && + target->kind ==BPF_DIAG_MOD_TARGET_STACK_SLOT ) { + slot_off = -(target->spi +1 ) *BPF_REG_SIZE ; +return event_target->range.min_off < slot_off +BPF_REG_SIZE && + event_target->range.max_off > slot_off; + } + +if (event_target->kind != target->kind) +return false; + +switch (target->kind) { +case BPF_DIAG_MOD_TARGET_REG : +return event_target->regno == target->regno; +case BPF_DIAG_MOD_TARGET_STACK_ARG : +return event_target->stack_arg == target->stack_arg; +case BPF_DIAG_MOD_TARGET_STACK_SLOT : +return event_target->spi == target->spi; +default : +return false; + } +} + +static bool diag_mod_keeps_lineage (struct bpf_verifier_env *env, +const struct bpf_diag_history_event *event) +{ +const struct bpf_insn *insn; +u8 class; + +if (event->mod.reason !=BPF_DIAG_MOD_WRITE || + event->mod.target.kind !=BPF_DIAG_MOD_TARGET_REG ) +return false; + + insn = &env->prog->insnsi[event->insn_idx]; + class =BPF_CLASS (insn->code); +if (class !=BPF_ALU && class !=BPF_ALU64 ) +return false; + +switch (BPF_OP (insn->code)) { +case BPF_ADD : +case BPF_SUB : +case BPF_MUL : +case BPF_OR : +case BPF_AND : +case BPF_LSH : +case BPF_RSH : +case BPF_ARSH : +case BPF_XOR : +case BPF_NEG : +case BPF_END : +return true; +default : +return false; + } +} + +static void diag_build_lineage (struct bpf_verifier_env *env,struct bpf_diag_log *log, +struct bpf_diag_history_filter *filter) +{ +const struct bpf_diag_history_opts *opts = filter->opts; +struct bpf_diag_mod_target target; +int i; + +for (i =0 ; i < log->cnt; i++) + log->events[i].in_lineage = false; + +if (!opts) +return ; + +if (opts->scope ==BPF_DIAG_HISTORY_SCOPE_REG ) + target =bpf_diag_reg_target (opts->frameno, opts->regno); +else if (opts->scope ==BPF_DIAG_HISTORY_SCOPE_STACK_ARG ) + target =bpf_diag_stack_arg_target (opts->frameno, opts->stack_arg_slot); +else +return ; + +/* +* Find the nearest mutation of the active target. A fill or spill changes +* the target to its origin, so the same walk follows register/stack +* lineage recursively until it reaches the write that created the value. +*/ +for (i = log->cnt; i >0 ; i--) { +struct bpf_diag_history_event *event; + + event = &log->events[i -1 ]; +if (event->kind !=BPF_DIAG_HISTORY_MOD || + !diag_target_matches (&event->mod.target, &target)) +continue ; + + event->in_lineage = true; + filter->lineage_start = i -1 ; + filter->lineage_valid = true; + +if (event->mod.origin_valid) { + target = event->mod.origin; +continue ; + } +if (event->mod.reason !=BPF_DIAG_MOD_WRITE && + event->mod.reason !=BPF_DIAG_MOD_SPILL ) +continue ; +if (diag_mod_keeps_lineage (env, event)) +continue ; +break ; + } + +} + +static int diag_history_start_idx (const struct bpf_diag_log *log, +const struct bpf_diag_history_filter *filter) +{ +const struct bpf_diag_history_opts *opts = filter->opts; +int i; + +if (!opts || opts->scope ==BPF_DIAG_HISTORY_SCOPE_ALL ) +return 0 ; +if (opts->scope ==BPF_DIAG_HISTORY_SCOPE_CONTEXT ) +return diag_history_context_start_idx (log, opts); +if (filter->lineage_valid) +return filter->lineage_start; +if (opts->scope !=BPF_DIAG_HISTORY_SCOPE_REF ) +return 0 ; + +for (i = log->cnt; i >0 ; i--) { +const struct bpf_diag_history_event *event; + + event =diag_history_event (log, i -1 ); +if (event->kind ==BPF_DIAG_HISTORY_REF_ACQUIRE && + event->ref.ref_id == opts->ref_id) +return i -1 ; + } + +return 0 ; +} + +static bool diag_history_event_visible (const struct bpf_diag_history_event *event, +const struct bpf_diag_history_filter *filter) +{ +const struct bpf_diag_history_opts *opts = filter->opts; + +if (!opts || opts->scope ==BPF_DIAG_HISTORY_SCOPE_ALL ) +return event->kind !=BPF_DIAG_HISTORY_CONTEXT ; + +switch (event->kind) { +case BPF_DIAG_HISTORY_BRANCH : +return true; +case BPF_DIAG_HISTORY_MOD : +return filter->lineage_valid && event->in_lineage; +case BPF_DIAG_HISTORY_REF_ACQUIRE : +case BPF_DIAG_HISTORY_REF_RELEASE : +return opts->scope ==BPF_DIAG_HISTORY_SCOPE_REF && + event->ref.ref_id == opts->ref_id; +case BPF_DIAG_HISTORY_CONTEXT : +return opts->scope ==BPF_DIAG_HISTORY_SCOPE_CONTEXT && + event->ctx.kind == opts->ctx_kind; +default : +return false; + } +} + +static const char *diag_s64_bound_name (s64 value) +{ +if (value ==S64_MIN ) +return "S64_MIN" ; +if (value ==S64_MAX ) +return "S64_MAX" ; +return NULL ; +} + +static const char *diag_u64_bound_name (u64 value) +{ +if (value ==U64_MAX ) +return "U64_MAX" ; +return NULL ; +} + +static const char *diag_s64_str (struct bpf_verifier_env *env,s64 value) +{ +return diag_s64_bound_name (value) ?:bpf_diag_fmt (env,"%lld" , value); +} + +static const char *diag_u64_str (struct bpf_verifier_env *env,u64 value) +{ +return diag_u64_bound_name (value) ?:bpf_diag_fmt (env,"%llu" , value); +} + +static bool diag_range_unknown (s64 smin,s64 smax,u64 umin,u64 umax) +{ +return smin ==S64_MIN && smax ==S64_MAX && umin ==0 && umax ==U64_MAX ; +} + +static bool diag_cnum64_unknown (struct cnum64 range) +{ +return diag_range_unknown (cnum64_smin (range),cnum64_smax (range),cnum64_umin (range), +cnum64_umax (range)); +} + +static bool diag_snapshot_unknown (const struct bpf_diag_reg_snapshot *snapshot) +{ +return tnum_is_unknown (snapshot->var_off) &&diag_cnum64_unknown (snapshot->r64); +} + +static const char *diag_scalar_range (struct bpf_verifier_env *env,struct cnum64 range) +{ +return bpf_diag_fmt (env,"signed range [%s, %s], unsigned range [%s, %s]" , +diag_s64_str (env,cnum64_smin (range)), +diag_s64_str (env,cnum64_smax (range)), +diag_u64_str (env,cnum64_umin (range)), +diag_u64_str (env,cnum64_umax (range))); +} + +static const char *diag_var_offset (struct bpf_verifier_env *env, +const struct bpf_diag_reg_snapshot *snapshot) +{ +if (tnum_is_const (snapshot->var_off)) +return bpf_diag_fmt (env,"at offset %lld" , (s64 )snapshot->var_off.value); + +if (diag_snapshot_unknown (snapshot)) +return bpf_diag_fmt (env,"with unknown offset" ); + +return bpf_diag_fmt (env, +"with variable offset: known bits %#llx, unknown mask %#llx, %s" , + snapshot->var_off.value, snapshot->var_off.mask, +diag_scalar_range (env, snapshot->r64)); +} + +static const char *diag_snapshot_btf_type (struct bpf_verifier_env *env, +const struct bpf_diag_reg_snapshot *snapshot) +{ +if (!snapshot->btf || !snapshot->btf_id) +return NULL ; + +return bpf_diag_fmt_btf_type (env, snapshot->btf, snapshot->btf_id); +} + +static const char *diag_reg_map_name (const struct bpf_map *map) +{ +if (!map || !map->name[0 ]) +return NULL ; + +return map->name; +} + +static const char *diag_reg_snapshot (struct bpf_verifier_env *env, +const struct bpf_diag_reg_snapshot *snapshot) +{ +const char *type_name =reg_type_str (env, snapshot->type); +const char *offset =diag_var_offset (env, snapshot); +const char *btf =diag_snapshot_btf_type (env, snapshot); +const char *map_name; + +if (snapshot->type ==SCALAR_VALUE ) { +if (tnum_is_const (snapshot->var_off)) +return bpf_diag_fmt (env,"integer scalar value %lld" , + (s64 )snapshot->var_off.value); +if (diag_snapshot_unknown (snapshot)) +return bpf_diag_fmt (env,"integer scalar with unknown value" ); +if (cnum64_is_const (snapshot->r64)) +return bpf_diag_fmt (env,"integer scalar value %lld" , +cnum64_smin (snapshot->r64)); +return bpf_diag_fmt (env,"integer scalar with %s" , +diag_scalar_range (env, snapshot->r64)); + } + +if (snapshot->type ==NOT_INIT ) +return bpf_diag_fmt (env,"uninitialized value" ); + +if (base_type (snapshot->type) ==PTR_TO_CTX ) +return bpf_diag_fmt (env,"context pointer %s" , offset); + +if (base_type (snapshot->type) ==PTR_TO_STACK ) +return bpf_diag_fmt (env,"stack pointer %s" , offset); + +if (base_type (snapshot->type) ==PTR_TO_MAP_VALUE ) { +const char *kind =type_may_be_null (snapshot->type) ?"nullable map value" : +"map value" ; + + map_name =diag_reg_map_name (snapshot->map_ptr); +if (map_name) +return bpf_diag_fmt (env,"%s from %s %s" , kind, map_name, offset); +return bpf_diag_fmt (env,"%s %s" , kind, offset); + } + +if (base_type (snapshot->type) ==CONST_PTR_TO_MAP ) { + map_name =diag_reg_map_name (snapshot->map_ptr); +if (map_name) +return bpf_diag_fmt (env,"map pointer for map %s" , map_name); +return bpf_diag_fmt (env,"map pointer" ); + } + +if (type_is_non_owning_ref (snapshot->type)) { +if (btf) +return bpf_diag_fmt (env,"borrowed allocated object pointer type=%s" , btf); +return bpf_diag_fmt (env,"borrowed allocated object pointer" ); + } + +if (type_is_ptr_alloc_obj (snapshot->type)) { +if (btf) +return bpf_diag_fmt (env,"owned allocated object pointer type=%s" , btf); +return bpf_diag_fmt (env,"owned allocated object pointer" ); + } + +if (base_type (snapshot->type) ==PTR_TO_BTF_ID && btf) +return bpf_diag_fmt (env,"%s type=%s %s" , type_name, btf, offset); + +return bpf_diag_fmt (env,"%s %s" , type_name, offset); +} + +static const char *diag_mod_target_desc (struct bpf_verifier_env *env, +const struct bpf_diag_mod_target *target) +{ +switch (target->kind) { +case BPF_DIAG_MOD_TARGET_REG : +return bpf_diag_fmt (env,"R%u" , target->regno); +case BPF_DIAG_MOD_TARGET_STACK_ARG : +return bpf_diag_fmt (env,"stack arg%d" ,diag_stack_argno (target->stack_arg)); +case BPF_DIAG_MOD_TARGET_STACK_SLOT : +return bpf_diag_fmt (env,"stack slot fp%d" , -(target->spi +1 ) *BPF_REG_SIZE ); +default : +return "value" ; + } +} + +static void diag_print_mod (struct bpf_verifier_env *env,const struct bpf_diag_history_event *event) +{ +const struct bpf_diag_mod_target *target = &event->mod.target; +const char *target_desc, *reason =NULL , *old, *new; +const char *label ="update" ; + +if (target->kind ==BPF_DIAG_MOD_TARGET_STACK_RANGE ) { +bpf_diag_source ( + env, event->insn_idx,"invalidated" , +"variable-offset stack write may affect bytes fp%d through fp%d" , + target->range.min_off, target->range.max_off -1 ); +return ; + } + + old =diag_reg_snapshot (env, &event->mod.old); + new =diag_reg_snapshot (env, &event->mod.new); + target_desc =diag_mod_target_desc (env, target); + +switch (event->mod.reason) { +case BPF_DIAG_MOD_REF_RELEASE : + reason = target->kind ==BPF_DIAG_MOD_TARGET_REG ?"resource release invalidated " +"this pointer" : +"resource release invalidated " +"this value" ; +break ; +case BPF_DIAG_MOD_PKT_DATA_CHANGE : + reason ="packet data may have moved" ; +break ; +case BPF_DIAG_MOD_NON_OWN_REF : + reason ="leaving the protected region invalidated this borrowed pointer" ; +break ; +case BPF_DIAG_MOD_CALLER_SAVED : + reason ="call invalidated this caller-saved register" ; +break ; +case BPF_DIAG_MOD_WRITE : +if (target->kind ==BPF_DIAG_MOD_TARGET_STACK_SLOT ) + reason ="a later stack write overwrote this spilled value" ; +break ; +case BPF_DIAG_MOD_SPILL : + label ="spilled" ; +break ; +case BPF_DIAG_MOD_VAR_WRITE : +default : +break ; + } + +if (reason) { +bpf_diag_source (env, event->insn_idx,"invalidated" , +"%s: %s; previous value was %s" , target_desc, reason, old); +return ; + } + +bpf_diag_source (env, event->insn_idx, label,"%s changed from %s to %s" , target_desc, + old, new); +} + +static void diag_print_ref_event (struct bpf_verifier_env *env, +const struct bpf_diag_history_event *event) +{ +const char *label; + + label = event->kind ==BPF_DIAG_HISTORY_REF_ACQUIRE ?"acquired" :"released" ; +bpf_diag_source (env, event->insn_idx, label,"owned resource (id=%u)" , + event->ref.ref_id); +} + +static const char *diag_context_name (enum bpf_diag_context_kind kind) +{ +switch (kind) { +case BPF_DIAG_CONTEXT_RCU : +return "RCU read lock region" ; +case BPF_DIAG_CONTEXT_PREEMPT : +return "non-preemptible region" ; +case BPF_DIAG_CONTEXT_IRQ : +return "IRQ-disabled region" ; +case BPF_DIAG_CONTEXT_LOCK : +return "lock region" ; +case BPF_DIAG_CONTEXT_NONE : +default : +return "context" ; + } +} + +static void diag_print_context_event (struct bpf_verifier_env *env, +const struct bpf_diag_history_event *event) +{ +bpf_diag_source (env, event->insn_idx,"context" ,"%s %s; depth is now %u" , + event->ctx.enter ?"entered" :"left" , +diag_context_name (event->ctx.kind), event->ctx.depth); +} + +static void diag_print_history (struct bpf_verifier_env *env, +const struct bpf_diag_history_opts *opts) +{ +const struct bpf_diag_history_event *event; +struct bpf_diag_history_filter filter = { + .opts = opts, + }; +struct bpf_diag_log *log; +struct diag_fmt_mark mark; +bool first = true; +bool visible = false; +int start_idx; +u32 i; + +if (!bpf_diag_enabled (env)) +return ; + +if (!env->diag) +return ; + log = &env->diag->log; + +diag_build_lineage (env, log, &filter); + + start_idx =diag_history_start_idx (log, &filter); +for (i = start_idx; i < log->cnt; i++) { + event =diag_history_event (log, i); +if (diag_history_event_visible (event, &filter)) { + visible = true; +break ; + } + } + +if (!visible && !log->dropped && opts && opts->scope ==BPF_DIAG_HISTORY_SCOPE_STACK_ARG ) +return ; + +diag_section (env,"Causal path" ); + mark =diag_fmt_save (env); +for (i = start_idx; i < log->cnt; i++) { + event =diag_history_event (log, i); +if (!diag_history_event_visible (event, &filter)) +continue ; + +diag_fmt_restore (env, mark); + +if (!first) +diag_write (env,"\n" ); + first = false; + +switch (event->kind) { +case BPF_DIAG_HISTORY_BRANCH : +bpf_diag_source (env, event->insn_idx,"branch" , +"took the %s branch of this conditional, goto %s" , + event->branch.cond_true ?"true" :"false" , + event->branch.cond_true ?"followed" :"not followed" ); +break ; +case BPF_DIAG_HISTORY_MOD : +diag_print_mod (env, event); +break ; +case BPF_DIAG_HISTORY_REF_ACQUIRE : +case BPF_DIAG_HISTORY_REF_RELEASE : +diag_print_ref_event (env, event); +break ; +case BPF_DIAG_HISTORY_CONTEXT : +diag_print_context_event (env, event); +break ; +default : +break ; + } + } + +if (!visible) +diag_write (env," no retained diagnostic events on this path\n" ); +if (log->dropped) +diag_write (env," %u causal-history event%s not retained after diagnostic event " +"storage was exhausted\n" , + log->dropped, log->dropped ==1 ?"" :"s" ); +diag_fmt_restore (env, mark); +}
kernel/bpf/diagnostics.h +18 -0
@@ -4,6 +4,7 @@#ifndef __BPF_DIAGNOSTICS_H#define __BPF_DIAGNOSTICS_H +#include <linux/bpf.h> #include <linux/compiler_attributes.h> #include <linux/types.h>
@@ -90,6 +91,13 @@ enum bpf_diag_context_kind { BPF_DIAG_CONTEXT_LOCK, }; +enum bpf_diag_invalid_deref_kind { +BPF_DIAG_DEREF_SCALAR , +BPF_DIAG_DEREF_NULLABLE_PTR , +BPF_DIAG_DEREF_MODIFIED_PTR , +BPF_DIAG_DEREF_INVALID_PTR , +}; +bool bpf_diag_enabled (const struct bpf_verifier_env *env);int bpf_diag_init (struct bpf_verifier_env *env);char *bpf_diag_fmt_buf (struct bpf_verifier_env *env,size_t size);
@@ -97,6 +105,7 @@ const char *bpf_diag_vfmt(struct bpf_verifier_env *env, const char *fmt, va_list__printf (2 ,0 );const char *bpf_diag_fmt (struct bpf_verifier_env *env,const char *fmt, ...)__printf (2 ,3 );const char *bpf_diag_fmt_btf_type (struct bpf_verifier_env *env,const struct btf *btf,u32 type_id); +const char *bpf_diag_reg_type_plain (struct bpf_verifier_env *env,enum bpf_reg_type type);u32 bpf_diag_event_log_pos (struct bpf_verifier_env *env);void bpf_diag_event_log_reset (struct bpf_verifier_env *env,u32 pos);u32 bpf_diag_irq_depth (const struct bpf_verifier_state *state);
@@ -105,6 +114,15 @@ void bpf_diag_header(struct bpf_verifier_env *env, const char *category, const char *problem); void bpf_diag_source(struct bpf_verifier_env *env, u32 insn_idx, const char *label, const char *fmt, ...) __printf(4, 5); +void bpf_diag_register_type (struct bpf_verifier_env *env,u32 insn_idx,int regno, +const char *problem,const char *reason,const char *suggestion); +void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno, +const char *reg_name,const struct bpf_reg_state *reg, +enum bpf_diag_invalid_deref_kind kind,s64 offset); +void bpf_diag_unreadable_reg (struct bpf_verifier_env *env,u32 insn_idx,int regno); +void bpf_diag_stack_arg_uninit (struct bpf_verifier_env *env,u32 insn_idx,int nargs, +int stack_arg_slot,const char *callee_name, +const char *arg_name);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +113 -15
274 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_uninit.c +1 -0
@@ -9,6 +9,7 @@ SEC("socket") __description("read uninitialized register") __failure __msg("R2 !read_ok") +__msg("R2 has never been initialized on this path") __failure_unpriv__naked void read_uninitialized_register (void ) {
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 9/16 bpf: Report Memory Safety bounds errors
In reply to Kumar Kartikeya DwivediAugment selected memory-range verifier failures with Memory Safety reports while preserving the existing terse verifier messages for compatibility.
Cover stack spill corruption, uninitialized stack reads, variable stack helper accesses, and check_mem_region_access() range-proof failures. The bounds report spells out the required offset + access_size <= object_size proof with concrete values and uses scoped diagnostic history for causal context.
--- kernel/bpf/diagnostics.c | 75 +++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.h | 6 +++ kernel/bpf/verifier.c | 85 +++++++++++++++++++++++++++++++++++----- 3 files changed, 157 insertions(+), 9 deletions(-)
kernel/bpf/diagnostics.c +75 -0
@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/list.h> #include <linux/seq_buf.h> +#include <linux/overflow.h> #include <linux/slab.h> #include <linux/stdarg.h> #include <linux/string.h>
@@ -1105,6 +1106,18 @@ void bpf_diag_stack_arg_uninit(struct bpf_verifier_env *env, u32 insn_idx, int n "call."); } +void bpf_diag_memory (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion) +{ +bpf_diag_header (env,MEMORY_SAFETY , problem); +diag_reason (env,"%s" , reason); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s" , problem); + +diag_suggestion (env,"%s" , suggestion); +} +void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true) {struct bpf_diag_history_event event = {
@@ -1623,6 +1636,68 @@ static const char *diag_scalar_range(struct bpf_verifier_env *env, struct cnum64 diag_u64_str(env, cnum64_umax(range))); } +const char *bpf_diag_fmt_s64_sum (struct bpf_verifier_env *env,s64 value,int addend) +{ +s64 sum; + +if (check_add_overflow (value, (s64 )addend, &sum)) +return bpf_diag_fmt (env,"%lld plus %d (%s)" , value, addend, + addend <0 ?"below S64_MIN" :"above S64_MAX" ); + +return bpf_diag_fmt (env,"%lld" , sum); +} + +static const char *diag_access_offset (struct bpf_verifier_env *env,int off, +const struct bpf_reg_state *reg) +{ +if (tnum_is_const (reg->var_off)) +return bpf_diag_fmt (env,"constant %s" , +bpf_diag_fmt_s64_sum (env, (s64 )reg->var_off.value, off)); + +if (tnum_is_unknown (reg->var_off) &&diag_cnum64_unknown (reg->r64)) +return bpf_diag_fmt (env,"unbounded" ); + +if (off) +return bpf_diag_fmt (env, +"variable: known bits %#llx, unknown mask %#llx, plus fixed offset %d; %s" , + (u64 )reg->var_off.value, reg->var_off.mask, off, +diag_scalar_range (env, reg->r64)); +return bpf_diag_fmt (env,"variable: known bits %#llx, unknown mask %#llx; %s" , + (u64 )reg->var_off.value, reg->var_off.mask, +diag_scalar_range (env, reg->r64)); +} + +void bpf_diag_mem_bounds (struct bpf_verifier_env *env,u32 insn_idx,int regno, +const char *reg_name,const char *type_name,const char *proof, +int off,int size,u32 mem_size,const struct bpf_reg_state *reg) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_REG , + .frameno =diag_current_frameno (env), + .regno = regno, + }; +const char *offset_desc; + +if (!bpf_diag_enabled (env)) +return ; + + offset_desc =diag_access_offset (env, off, reg); + +bpf_diag_header (env,MEMORY_SAFETY ,"access outside bounds" ); +diag_reason ( + env,"The verifier cannot prove offset + access_size <= object_size. Here, %s. %s is %s; offset is %s; access_size is %d; object_size is %u." , + proof, reg_name, type_name, offset_desc, size, mem_size); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"access may be outside object bounds" ); + +if (regno >=0 ) +diag_print_history (env, &opts); + +diag_suggestion ( + env,"Add or adjust a bounds check that proves offset + access_size stays within the object." ); +} +static const char *diag_var_offset (struct bpf_verifier_env *env,const struct bpf_diag_reg_snapshot *snapshot) {
kernel/bpf/diagnostics.h +6 -0
@@ -13,6 +13,7 @@ struct bpf_verifier_env;struct bpf_verifier_state ;struct btf ; +const char *bpf_diag_fmt_s64_sum (struct bpf_verifier_env *env,s64 value,int addend);enum bpf_diag_mod_reason {BPF_DIAG_MOD_WRITE ,BPF_DIAG_MOD_SPILL ,
@@ -123,6 +124,11 @@ void bpf_diag_unreadable_reg(struct bpf_verifier_env *env, u32 insn_idx, int regvoid bpf_diag_stack_arg_uninit (struct bpf_verifier_env *env,u32 insn_idx,int nargs,int stack_arg_slot,const char *callee_name,const char *arg_name); +void bpf_diag_memory (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion); +void bpf_diag_mem_bounds (struct bpf_verifier_env *env,u32 insn_idx,int regno, +const char *reg_name,const char *type_name,const char *proof, +int off,int size,u32 mem_size,const struct bpf_reg_state *reg);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +76 -9
158 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH bpf-next v4 09/16] bpf: Report Memory Safety bounds errors
Quoted 10 lines · Kumar Kartikeya Dwivedi
Augment selected memory-range verifier failures with Memory Safety reports while preserving the existing terse verifier messages for compatibility.
Cover stack spill corruption, uninitialized stack reads, variable stack helper accesses, and
check_mem_region_access()range-proof failures. The bounds report spells out the required offset +access_size<=object_sizeproof with concrete values and uses scoped diagnostic history for causal context.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
The const char *fmt = ... is still ugly, though.
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 10/16 bpf: Report Resource Lifetime reference leaks
In reply to Kumar Kartikeya DwivediAugment selected Resource Lifetime Safety failures with structured diagnostics while preserving the existing verifier messages.
Report unreleased references from check_reference_leak() using reference-scoped diagnostic history, and add state reports for dynptr, iterator, lock, and IRQ-flag lifetime misuse.
IRQ restore mismatch and out-of-order diagnostics use IRQ context-scoped history when an IRQ-disabled region is active, so retained save/restore context is still visible after per-state history removal.
--- kernel/bpf/diagnostics.c | 90 +++++++++++++++++++++ kernel/bpf/diagnostics.h | 9 +++ kernel/bpf/verifier.c | 170 +++++++++++++++++++++++++++++++++------ 3 files changed, 245 insertions(+), 24 deletions(-)
kernel/bpf/diagnostics.c +90 -0
@@ -1698,6 +1698,96 @@ void bpf_diag_mem_bounds(struct bpf_verifier_env *env, u32 insn_idx, int regno, env, "Add or adjust a bounds check that proves offset + access_size stays within the object."); } +static const char *diag_lock_name (const struct bpf_reference_state *lock) +{ +switch (lock->type) { +case REF_TYPE_LOCK : +return "bpf_spin_lock" ; +case REF_TYPE_RES_LOCK : +return "resource spin lock" ; +case REF_TYPE_RES_LOCK_IRQ : +return "IRQ-saving resource spin lock" ; +default : +return "lock" ; + } +} + +static void diag_res_report (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason) +{ +bpf_diag_header (env,RESOURCE_LIFETIME_SAFETY , problem); +diag_reason (env,"%s" , reason); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s" , problem); +} + +void bpf_diag_res (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion) +{ +diag_res_report (env, insn_idx, problem, reason); +diag_suggestion (env,"%s" , suggestion); +} + +void bpf_diag_lock (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion, +const struct bpf_reference_state *active_lock) +{ +diag_res_report (env, insn_idx, problem, reason); + +if (active_lock) { +diag_section (env,"Active lock" ); +bpf_diag_source (env, active_lock->insn_idx,"acquired" , +"active %s has verifier identity %d" , +diag_lock_name (active_lock), active_lock->id); + } + +diag_suggestion (env,"%s" , suggestion); +} + +void bpf_diag_irq (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion,u32 depth) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_CONTEXT , + .ctx_kind =BPF_DIAG_CONTEXT_IRQ , + .ctx_depth = depth, + }; + +bpf_diag_header (env,RESOURCE_LIFETIME_SAFETY , problem); +diag_reason (env,"%s" , reason); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s" , problem); + +if (depth) +diag_print_history (env, &opts); + +diag_suggestion (env,"%s" , suggestion); +} + +void bpf_diag_leak (struct bpf_verifier_env *env,u32 ref_id,u32 alloc_insn,u32 fail_insn) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_REF , + .ref_id = ref_id, + }; + +bpf_diag_header (env,RESOURCE_LIFETIME_SAFETY ,"unreleased resource" ); +diag_reason ( + env,"Owned resource (id=%u) was acquired at instruction %u and still needs to be released before this exit path." , + ref_id, alloc_insn); + +diag_section (env,"At" ); +bpf_diag_source (env, fail_insn,"error" , +"owned resource (id=%u) still needs release" , ref_id); + +diag_print_history (env, &opts); + +diag_suggestion ( + env,"Release or transfer ownership of the acquired resource on every path before the program exits." ); +} +static const char *diag_var_offset (struct bpf_verifier_env *env,const struct bpf_diag_reg_snapshot *snapshot) {
kernel/bpf/diagnostics.h +9 -0
@@ -8,6 +8,7 @@#include <linux/compiler_attributes.h> #include <linux/types.h> +struct bpf_reference_state ;struct bpf_reg_state ;struct bpf_verifier_env ;struct bpf_verifier_state ;
@@ -129,6 +130,14 @@ void bpf_diag_memory(struct bpf_verifier_env *env, u32 insn_idx, const char *provoid bpf_diag_mem_bounds (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *reg_name,const char *type_name,const char *proof,int off,int size,u32 mem_size,const struct bpf_reg_state *reg); +void bpf_diag_res (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion); +void bpf_diag_lock (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion, +const struct bpf_reference_state *active_lock); +void bpf_diag_irq (struct bpf_verifier_env *env,u32 insn_idx,const char *problem, +const char *reason,const char *suggestion,u32 depth); +void bpf_diag_leak (struct bpf_verifier_env *env,u32 ref_id,u32 alloc_insn,u32 fail_insn);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +146 -24
339 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH bpf-next v4 10/16] bpf: Report Resource Lifetime reference leaks
Quoted 7 lines · Kumar Kartikeya Dwivedi
Augment selected Resource Lifetime Safety failures with structured diagnostics while preserving the existing verifier messages.
Report unreleased references from
check_reference_leak()using reference-scoped diagnostic history, and add state reports for dynptr, iterator, lock, and IRQ-flag lifetime misuse.
Quoted 6 lines
IRQ restore mismatch and out-of-order diagnostics use IRQ context-scoped history when an IRQ-disabled region is active, so retained save/restore context is still visible after per-state history removal.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
...
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
...
kernel/bpf/verifier.c enum { · line 7162
Quoted 7 lines
kernel/bpf/verifier.c +2
-1@@ -7136,11 +7158,13 @@ enum {* env->cur_state->active_locks remembers which map value element or allocated * object got locked and clears it after bpf_spin_unlock. */ -static int process_spin_lock (struct bpf_verifier_env *env,struct bpf_reg_state *reg,argno_t argno,int flags) +static int process_spin_lock (struct bpf_verifier_env *env,struct bpf_reg_state *reg,argno_t argno, +int flags)
Nit: churn.
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 11/16 bpf: Report Call Type Safety argument errors
In reply to Kumar Kartikeya DwivediAugment selected helper and kfunc argument-contract failures with Call Type Safety reports. Keep the existing terse verifier messages and add reason, source context, causal register or stack-argument history, and targeted suggestions.
Cover helper register-type mismatch, helper and kfunc non-NULL pointer requirements, release-helper ownership requirements, scalar and constant kfunc arguments, trusted and RCU pointer contracts, kfunc memory arguments, memory/length pairs, refcounted kptrs, constant strings, and IRQ flag stack arguments.
--- kernel/bpf/diagnostics.c | 63 ++++- kernel/bpf/diagnostics.h | 4 + kernel/bpf/verifier.c | 228 ++++++++++++++++-- .../selftests/bpf/progs/verifier_map_in_map.c | 1 + 4 files changed, 268 insertions(+), 28 deletions(-)
kernel/bpf/diagnostics.c +51 -12
@@ -175,7 +175,6 @@ int bpf_diag_init(struct bpf_verifier_env *env) env->diag = kzalloc_obj(struct bpf_diag, GFP_KERNEL_ACCOUNT);if (!env->diag)return -ENOMEM ; -INIT_LIST_HEAD (&env->diag->fmt_chunks);return 0 ; }
@@ -927,6 +926,49 @@ static const char *diag_arg_ordinal(int argno) } } +void bpf_diag_call_type (struct bpf_verifier_env *env,u32 insn_idx,int argno,int regno, +int stack_arg_slot,const char *call_name,const char *arg_name, +const char *reason,const char *suggestion) +{ +struct bpf_diag_history_opts opts = { + .frameno =diag_current_frameno (env), + }; +const char *ordinal =diag_arg_ordinal (argno); +const char *arg_desc; +bool print_history = true; + +if (regno >=0 ) { + opts.scope =BPF_DIAG_HISTORY_SCOPE_REG ; + opts.regno = regno; + }else if (stack_arg_slot >=0 ) { + opts.scope =BPF_DIAG_HISTORY_SCOPE_STACK_ARG ; + opts.stack_arg_slot = stack_arg_slot; + }else { + print_history = false; + } + +if (ordinal && arg_name) + arg_desc =bpf_diag_fmt (env,"%s argument (%s)" , ordinal, arg_name); +else if (ordinal) + arg_desc =bpf_diag_fmt (env,"%s argument" , ordinal); +else if (arg_name) + arg_desc =bpf_diag_fmt (env,"argument %s" , arg_name); +else + arg_desc ="argument" ; + +bpf_diag_header (env,CALL_TYPE_SAFETY ,"invalid call argument" ); +diag_reason (env,"The %s to %s does not satisfy the verifier contract: %s." , + arg_desc, call_name, reason); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"invalid %s for %s" , arg_desc, call_name); + +if (print_history) +diag_print_history (env, &opts); + +diag_suggestion (env,"%s" , suggestion); +} +void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *reg_name,const struct bpf_reg_state *reg,enum bpf_diag_invalid_deref_kind kind,s64 offset)
@@ -1085,15 +1127,13 @@ void bpf_diag_stack_arg_uninit(struct bpf_verifier_env *env, u32 insn_idx, int n arg_buf ="" ;bpf_diag_header (env,REGISTER_TYPE_SAFETY ,"missing stack argument" );if (callee_name && *callee_name) -diag_reason (env, -"Function %s expects %d arguments, but %s is not initialized at " -"this call." , - callee_name, nargs, arg_buf); +diag_reason ( + env,"Function %s expects %d arguments, but %s is not initialized at this call." , + callee_name, nargs, arg_buf);else -diag_reason (env, -"The callee expects %d arguments, but %s is not initialized at " -"this call." , - nargs, arg_buf); +diag_reason ( + env,"The callee expects %d arguments, but %s is not initialized at this call." , + nargs, arg_buf);diag_section (env,"At" );bpf_diag_source (env, insn_idx,"error" ,"%s is not initialized" , arg_buf);
@@ -1101,9 +1141,8 @@ void bpf_diag_stack_arg_uninit(struct bpf_verifier_env *env, u32 insn_idx, int nif (stack_arg_slot >=0 )diag_print_history (env, &opts); -diag_suggestion (env,"Write the outgoing stack argument after any operation that " -"may invalidate stored pointer values, and before making this " -"call." ); +diag_suggestion ( + env,"Write the outgoing stack argument after any operation that may invalidate stored pointer values, and before making this call." ); } void bpf_diag_memory(struct bpf_verifier_env *env, u32 insn_idx, const char *problem,
kernel/bpf/diagnostics.h +4 -0
@@ -6,6 +6,7 @@#include <linux/bpf.h> #include <linux/compiler_attributes.h> +#include <linux/stdarg.h> #include <linux/types.h> struct bpf_reference_state ;
@@ -138,6 +139,9 @@ void bpf_diag_lock(struct bpf_verifier_env *env, u32 insn_idx, const char *problvoid bpf_diag_irq (struct bpf_verifier_env *env,u32 insn_idx,const char *problem,const char *reason,const char *suggestion,u32 depth);void bpf_diag_leak (struct bpf_verifier_env *env,u32 ref_id,u32 alloc_insn,u32 fail_insn); +void bpf_diag_call_type (struct bpf_verifier_env *env,u32 insn_idx,int argno,int regno, +int stack_arg_slot,const char *call_name,const char *arg_name, +const char *reason,const char *suggestion);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +212 -16
404 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_map_in_map.c +1 -0
@@ -155,6 +155,7 @@ l0_%=: r0 = 0; \ SEC("socket") __description("forgot null checking on the inner map pointer") __failure __msg("R1 type=map_ptr_or_null expected=map_ptr") +__msg("map_ptr_or_null, but this argument accepts map_ptr") __failure_unpriv__naked void on_the_inner_map_pointer (void ) {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 11/16] bpf: Report Call Type Safety argument errors
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Uninitialized variable `ref_id` used in `check_kfunc_args` for non-pointer release kfunc…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Uninitialized variable ref_id used in check_kfunc_args for non-pointer release kfunc arguments.
- [Low] Incorrect diagnostic message for kfunc fixed-size memory arguments failing bounds checks. --
--- Patch [11]: [PATCH bpf-next v4 11/16] bpf: Report Call Type Safety argument errors --- commit ea19a04c0e142c86cd13b8e8524d76e024167249
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Report Call Type Safety argument errors
Augment selected helper and kfunc argument-contract failures with Call Type Safety reports. Keep the existing terse verifier messages and add reason, source context, causal register or stack-argument history, and targeted suggestions.
Quoted 17 lines
kernel/bpf/verifier.c +7
-1@@ -12361,29 +12437,43 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me t =btf_type_skip_modifiers (btf, args[i].type,NULL ); -if (btf_type_is_ptr (t) && (bpf_register_is_null (reg) ||type_may_be_null (reg->type)) && +if (btf_type_is_ptr (t)) { + ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); + ref_tname =btf_name_by_offset (btf, ref_t->name_off); + } + +if (btf_type_is_ptr (t) && + (bpf_register_is_null (reg) ||type_may_be_null (reg->type)) && !type_may_be_null (kf_arg_type)) {
[ ... ]
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12464
Quoted 7 lines
kernel/bpf/verifier.c +3
-0}if (regno == meta->release_regno && !is_kfunc_arg_dynptr (meta->btf, &args[i]) && !reg_is_referenced (env, reg) && !bpf_register_is_null (reg)) { +const char *expected_type; + + expected_type =diag_btf_type_name (env, btf, ref_id);
[Severity: Medium]
Could this result in using an uninitialized ref_id? If a module registers a kfunc with KF_RELEASE that takes a non-pointer argument, btf_type_is_ptr(t) would be false, skipping the initialization of ref_id.
Then, on the release argument mismatch path here, diag_btf_type_name() would read uninitialized stack memory from ref_id, which might lead to out-of-bounds reads in btf_type_by_id() or leak stack contents into the verifier log.
[ ... ]
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -12706,8 +12863,17 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mereturn -EINVAL ; } ret =check_mem_reg (env, reg, argno, type_size,BPF_READ |BPF_WRITE , meta);
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12874
Quoted 10 lines
kernel/bpf/verifier.c +9
-1-if (ret <0 ) +if (ret <0 ) { +const char *expected_type; + + expected_type =diag_btf_type_name (env, btf, ref_id); +diag_call_arg_fmt (env, insn_idx, argno, func_name, +"Pass stack, map, context, or other verifier-known memory of the expected type and size, not an integer cast to a pointer." , +"the kfunc expects %u bytes of memory for %s, but it is %s and not verifier-known memory" , + type_size, expected_type, +bpf_diag_reg_type_plain (env, reg->type));
[Severity: Low]
Does this emit a misleading diagnostic when the bounds check fails? If a kfunc takes a fixed-size memory argument and the BPF program passes a valid verifier-known memory pointer (like PTR_TO_STACK) but the buffer is too small, check_mem_reg() returns an error.
This diagnostic would then unconditionally claim the argument is "not verifier-known memory" and advise not to pass an "integer cast to a pointer", which obscures the actual bounds-check issue.
Quoted 4 lines
kernel/bpf/verifier.c +1
-0return ret; + } }break ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Kumar Kartikeya Dwivedi
maintainerPATCH v4 12/16 bpf: Report Execution Context Safety errors
In reply to Kumar Kartikeya DwivediAugment selected sleepability and critical-section failures with Execution Context Safety reports. Keep the existing verifier messages and add source context, path history, and suggestions tied to the active context.
Use the context history recorded earlier to anchor causal paths to lock, IRQ, RCU, and preempt regions instead of unrelated register updates.
Cover global calls while holding a lock, sleepable global function calls, sleepable helpers, sleepable kfunc calls from disallowed contexts, operations that exit while a context is still active, and unmatched context exits.
--- kernel/bpf/diagnostics.c | 186 +++++++++++++++++++++++++++++++++++---- kernel/bpf/diagnostics.h | 12 +++ kernel/bpf/verifier.c | 130 ++++++++++++++++++++++----- 3 files changed, 288 insertions(+), 40 deletions(-)
kernel/bpf/diagnostics.c +169 -17
@@ -839,6 +839,23 @@ static u32 diag_current_frameno(const struct bpf_verifier_env *env)return env->cur_state->frame[env->cur_state->curframe]->frameno; } +static const char *diag_context_name (enum bpf_diag_context_kind kind) +{ +switch (kind) { +case BPF_DIAG_CONTEXT_RCU : +return "RCU read lock region" ; +case BPF_DIAG_CONTEXT_PREEMPT : +return "non-preemptible region" ; +case BPF_DIAG_CONTEXT_IRQ : +return "IRQ-disabled region" ; +case BPF_DIAG_CONTEXT_LOCK : +return "lock region" ; +case BPF_DIAG_CONTEXT_NONE : +default : +return "context" ; + } +} +void bpf_diag_register_type (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *problem,const char *reason,const char *suggestion) {
@@ -969,6 +986,158 @@ void bpf_diag_call_type(struct bpf_verifier_env *env, u32 insn_idx, int argno, idiag_suggestion (env,"%s" , suggestion); } +static const char *diag_context_constraint (enum bpf_diag_context_kind kind) +{ +switch (kind) { +case BPF_DIAG_CONTEXT_RCU : +return "RCU read-side critical sections cannot call operations that may sleep" ; +case BPF_DIAG_CONTEXT_PREEMPT : +return "preemption-disabled code cannot call operations that may sleep" ; +case BPF_DIAG_CONTEXT_IRQ : +return "IRQ-disabled code cannot call operations that may sleep" ; +case BPF_DIAG_CONTEXT_LOCK : +return "code holding a BPF spin lock cannot call operations that may sleep" ; +case BPF_DIAG_CONTEXT_NONE : +default : +return NULL ; + } +} + +static const char *diag_active_context (struct bpf_verifier_env *env,u32 depth, +const char *context) +{ +if (depth ==1 ) +return bpf_diag_fmt (env,"an active %s (depth 1)" , context); +return bpf_diag_fmt (env,"%u active %ss (depth %u)" , depth, context, depth); +} + +static u32 diag_context_depth (struct bpf_verifier_env *env,enum bpf_diag_context_kind kind) +{ +switch (kind) { +case BPF_DIAG_CONTEXT_RCU : +return env->cur_state->active_rcu_locks; +case BPF_DIAG_CONTEXT_PREEMPT : +return env->cur_state->active_preempt_locks; +case BPF_DIAG_CONTEXT_IRQ : +return bpf_diag_irq_depth (env->cur_state); +case BPF_DIAG_CONTEXT_LOCK : +return env->cur_state->active_locks; +case BPF_DIAG_CONTEXT_NONE : +default : +return 0 ; + } +} + +static void diag_ctx_forbidden (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +enum bpf_diag_context_kind ctx_kind,const char *context, +const char *constraint,const char *suggestion) +{ +u32 depth =diag_context_depth (env, ctx_kind); +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_CONTEXT , + .ctx_kind = ctx_kind, + .ctx_depth = depth, + }; + +bpf_diag_header (env,EXECUTION_CONTEXT_SAFETY , +"operation is not allowed in this context" ); +if (constraint) { +if (depth) { +diag_reason ( + env,"The operation %s cannot be used in %s because %s. This path is still inside %s." , + operation, context, constraint,diag_active_context (env, depth, context)); + }else { +diag_reason (env,"The operation %s cannot be used in %s because %s." , + operation, context, constraint); + } + }else { +diag_reason (env,"The operation %s cannot be used in %s." , operation, + context); + } + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s is not allowed in %s" , operation, + context); + +if (ctx_kind !=BPF_DIAG_CONTEXT_NONE ) +diag_print_history (env, &opts); + +diag_suggestion (env,"%s" , suggestion); +} + +static void diag_ctx_active (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +enum bpf_diag_context_kind ctx_kind,const char *context, +const char *suggestion) +{ +u32 depth =diag_context_depth (env, ctx_kind); +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_CONTEXT , + .ctx_kind = ctx_kind, + .ctx_depth = depth, + }; + +bpf_diag_header (env,EXECUTION_CONTEXT_SAFETY , +"operation is not allowed in this context" ); +diag_reason ( + env,"The operation %s cannot be used while this path is still inside %s. Leave the region before this operation." , + operation,diag_active_context (env, depth, context)); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s is not allowed before leaving %s" , + operation, context); + +diag_print_history (env, &opts); + +diag_suggestion (env,"%s" , suggestion); +} + +static void diag_ctx_underflow (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +enum bpf_diag_context_kind ctx_kind,const char *suggestion) +{ +struct bpf_diag_history_opts opts = { + .scope =BPF_DIAG_HISTORY_SCOPE_CONTEXT , + .ctx_kind = ctx_kind, + }; +const char *context =diag_context_name (ctx_kind); + +bpf_diag_header (env,EXECUTION_CONTEXT_SAFETY ,"unmatched context exit" ); +diag_reason ( + env,"The operation %s tries to leave %s, but this path has no active %s to leave. The current depth is 0." , + operation, context, context); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s has no matching enter on this path" , + operation); + +diag_print_history (env, &opts); + +diag_suggestion (env,"%s" , suggestion); +} + +void bpf_diag_ctx (struct bpf_verifier_env *env,enum bpf_diag_ctx_report report,u32 insn_idx, +const char *operation,enum bpf_diag_context_kind ctx_kind,const char *context, +const char *suggestion) +{ +switch (report) { +case BPF_DIAG_CTX_FORBIDDEN : +diag_ctx_forbidden (env, insn_idx, operation, ctx_kind, context, +diag_context_constraint (ctx_kind), suggestion); +return ; +case BPF_DIAG_CTX_ACTIVE : +diag_ctx_active (env, insn_idx, operation, ctx_kind, context, suggestion); +return ; +case BPF_DIAG_CTX_UNDERFLOW : +diag_ctx_underflow (env, insn_idx, operation, ctx_kind, suggestion); +return ; + } +} + +void bpf_diag_ctx_restricted (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +enum bpf_diag_context_kind ctx_kind,const char *context, +const char *constraint,const char *suggestion) +{ +diag_ctx_forbidden (env, insn_idx, operation, ctx_kind, context, constraint, suggestion); +}void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *reg_name,const struct bpf_reg_state *reg,enum bpf_diag_invalid_deref_kind kind,s64 offset)
@@ -2005,23 +2174,6 @@ static void diag_print_ref_event(struct bpf_verifier_env *env, event->ref.ref_id); } -static const char *diag_context_name (enum bpf_diag_context_kind kind) -{ -switch (kind) { -case BPF_DIAG_CONTEXT_RCU : -return "RCU read lock region" ; -case BPF_DIAG_CONTEXT_PREEMPT : -return "non-preemptible region" ; -case BPF_DIAG_CONTEXT_IRQ : -return "IRQ-disabled region" ; -case BPF_DIAG_CONTEXT_LOCK : -return "lock region" ; -case BPF_DIAG_CONTEXT_NONE : -default : -return "context" ; - } -} -static void diag_print_context_event (struct bpf_verifier_env *env,const struct bpf_diag_history_event *event) {
kernel/bpf/diagnostics.h +12 -0
@@ -94,6 +94,12 @@ enum bpf_diag_context_kind { BPF_DIAG_CONTEXT_LOCK, }; +enum bpf_diag_ctx_report { +BPF_DIAG_CTX_FORBIDDEN , +BPF_DIAG_CTX_ACTIVE , +BPF_DIAG_CTX_UNDERFLOW , +}; +enum bpf_diag_invalid_deref_kind {BPF_DIAG_DEREF_SCALAR ,BPF_DIAG_DEREF_NULLABLE_PTR ,
@@ -142,6 +148,12 @@ void bpf_diag_leak(struct bpf_verifier_env *env, u32 ref_id, u32 alloc_insn, u32void bpf_diag_call_type (struct bpf_verifier_env *env,u32 insn_idx,int argno,int regno,int stack_arg_slot,const char *call_name,const char *arg_name,const char *reason,const char *suggestion); +void bpf_diag_ctx (struct bpf_verifier_env *env,enum bpf_diag_ctx_report report,u32 insn_idx, +const char *operation,enum bpf_diag_context_kind ctx_kind,const char *context, +const char *suggestion); +void bpf_diag_ctx_restricted (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +enum bpf_diag_context_kind ctx_kind,const char *context, +const char *constraint,const char *suggestion);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +107 -23
259 lines not on this page · raw message
Signature · 1 line
2.53.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 12/16] bpf: Report Execution Context Safety errors
On Thu, 2026-08-13 at 01:33 +0200, Kumar Kartikeya Dwivedi wrote:
...
kernel/bpf/diagnostics.c +0 -0
...
+void bpf_diag_ctx(struct bpf_verifier_env *env, enum bpf_diag_ctx_report report, u32 insn_idx,
+ const char *operation, enum bpf_diag_context_kind ctx_kind, const char *context,
+ const char *suggestion)
Nit: expose diag_ctx_{forbidden,active,underflow} and drop this function and enum?
+{
+ switch (report) {
+ case BPF_DIAG_CTX_FORBIDDEN:
+ diag_ctx_forbidden(env, insn_idx, operation, ctx_kind, context,
+ diag_context_constraint(ctx_kind), suggestion);
+ return;
+ case BPF_DIAG_CTX_ACTIVE:
+ diag_ctx_active(env, insn_idx, operation, ctx_kind, context, suggestion);
+ return;
+ case BPF_DIAG_CTX_UNDERFLOW:
+ diag_ctx_underflow(env, insn_idx, operation, ctx_kind, suggestion);
+ return;
+ }
+}
...
Quoted 10 lines
(quoted diff) +0
-6@@ -2005,23 +2174,6 @@ static void diag_print_ref_event(struct bpf_verifier_env *env, event->ref.ref_id); } -static const char *diag_context_name(enum bpf_diag_context_kind kind) -{ - switch (kind) { - case BPF_DIAG_CONTEXT_RCU: - return "RCU read lock region"; - case BPF_DIAG_CONTEXT_PREEMPT:
Review static void diag_print_ref_event(struct bpf_verifier_env *env, · line 2023
Quoted 10 lines
(quoted diff) +0
-10- return "non-preemptible region"; - case BPF_DIAG_CONTEXT_IRQ: - return "IRQ-disabled region"; - case BPF_DIAG_CONTEXT_LOCK: - return "lock region"; - case BPF_DIAG_CONTEXT_NONE: - default: - return "context"; - } -}
Nit: the move is churn.
...
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
...
kernel/bpf/verifier.c static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r · line 1109
Quoted 8 lines
kernel/bpf/verifier.c +1
-3@@ -1104,9 +1106,7 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r st = &slot->spilled_ptr;if (st->irq.kfunc_class != kfunc_class) { -const char *fmt ="This IRQ flag was saved by %s IRQ kfuncs, but the restore call " -"belongs to the %s IRQ kfunc family. Save and restore operations " -"must use the same family." ; +const char *fmt ="This IRQ flag was saved by %s IRQ kfuncs, but the restore call belongs to the %s IRQ kfunc family. Save and restore operations must use the same family." ;
Churn.
Quoted 5 lines
kernel/bpf/verifier.c +0
-0const char *flag_kfunc = st->irq.kfunc_class ==IRQ_NATIVE_KFUNC ?"native" :"lock" ;const char *used_kfunc = kfunc_class ==IRQ_NATIVE_KFUNC ?"native" :"lock" ;@@ -1115,10 +1115,10 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r verbose(env, "irq flag acquired by %s kfuncs cannot be restored with %s kfuncs\n",
kernel/bpf/verifier.c static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r · line 1121
Quoted 10 lines
kernel/bpf/verifier.c +4
-4flag_kfunc, used_kfunc); reason =bpf_diag_fmt (env, fmt, flag_kfunc, used_kfunc); -bpf_diag_irq (env, env->insn_idx,"IRQ flag restore mismatch" , reason, -"Restore the flag with the matching IRQ restore kfunc for the save " -"operation that created it." , -bpf_diag_irq_depth (env->cur_state)); +bpf_diag_irq ( + env, env->insn_idx,"IRQ flag restore mismatch" , reason, +"Restore the flag with the matching IRQ restore kfunc for the save operation that created it." , +bpf_diag_irq_depth (env->cur_state));
Churn.
Quoted 8 lines
kernel/bpf/verifier.c +0
-1return -EINVAL ; }@@ -1136,11 +1136,11 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_rverbose (env,"cannot restore irq state out of order, expected id=%d acquired at insn_idx=%d\n" , env->cur_state->active_irq_id, insn_idx); - bpf_diag_irq(env, env->insn_idx, "IRQ flag restore out of order",
kernel/bpf/verifier.c static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r · line 1144
Quoted 10 lines
kernel/bpf/verifier.c +5
-4-"IRQ-disabled regions must be restored in last-in, first-out order, " -"but this restore does not match the currently active IRQ flag." , -"Restore nested IRQ flags in the reverse order they were saved." , - bpf_diag_irq_depth(env->cur_state)); +bpf_diag_irq ( + env, env->insn_idx,"IRQ flag restore out of order" , +"IRQ-disabled regions must be restored in last-in, first-out order, but this restore does not match the currently active IRQ flag." , +"Restore nested IRQ flags in the reverse order they were saved." , +bpf_diag_irq_depth (env->cur_state));return err;
Churn.
Quoted 2 lines
kernel/bpf/verifier.c +0
-0}
...
Quoted 8 lines
kernel/bpf/verifier.c +4
-0@@ -10551,6 +10578,36 @@ static const char *non_sleepable_context_description(struct bpf_verifier_env *enreturn "non-sleepable prog" ; } +static enum bpf_diag_context_kind non_sleepable_context_kind (struct bpf_verifier_env *env) +{ +if (env->cur_state->active_rcu_locks) +return BPF_DIAG_CONTEXT_RCU ;
kernel/bpf/verifier.c static const char *non_sleepable_context_description(struct bpf_verifier_env *en · line 10594
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+if (env->cur_state->active_preempt_locks) +return BPF_DIAG_CONTEXT_PREEMPT ; +if (env->cur_state->active_irq_id) +return BPF_DIAG_CONTEXT_IRQ ; +if (env->cur_state->active_locks) +return BPF_DIAG_CONTEXT_LOCK ; +return BPF_DIAG_CONTEXT_NONE ; +} + +static const char *non_sleepable_context_diag_description (struct bpf_verifier_env *env)
Drop the BPF_DIAG_CONTEXT_RCU & Co and use const char *?
Quoted 19 lines
kernel/bpf/verifier.c +16
-0+{ +switch (non_sleepable_context_kind (env)) { +case BPF_DIAG_CONTEXT_RCU : +return "RCU read lock region" ; +case BPF_DIAG_CONTEXT_PREEMPT : +return "non-preemptible region" ; +case BPF_DIAG_CONTEXT_IRQ : +return "IRQ-disabled region" ; +case BPF_DIAG_CONTEXT_LOCK : +return "lock region" ; +case BPF_DIAG_CONTEXT_NONE : +default : +return "non-sleepable program" ; + } +} +static int release_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg,bool convert_rcu,bool release_dynptr) {
...
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -11919,12 +11983,10 @@ static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *if (!is_irq_flag_reg_valid_uninit (env, reg)) {verbose (env,"expected uninitialized irq flag as %s\n" ,reg_arg_name (env, argno));
kernel/bpf/verifier.c static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state * · line 11989
Quoted 10 lines
kernel/bpf/verifier.c +4
-6-bpf_diag_res (env, env->insn_idx,"IRQ flag is already initialized" , -"Saving IRQ state requires an uninitialized stack slot for " -"the IRQ flag, but this slot already contains tracked IRQ " -"flag state." , -"Use a fresh stack slot for this save operation, or restore " -"the existing IRQ flag before reusing the slot." ); +bpf_diag_res ( + env, env->insn_idx,"IRQ flag is already initialized" , +"Saving IRQ state requires an uninitialized stack slot for the IRQ flag, but this slot already contains tracked IRQ flag state." , +"Use a fresh stack slot for this save operation, or restore the existing IRQ flag before reusing the slot." );
Churn.
Quoted 7 lines
kernel/bpf/verifier.c +0
-0return -EINVAL ; }@@ -11941,11 +12003,10 @@ static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *if (err) {verbose (env,"expected an initialized irq flag as %s\n" ,reg_arg_name (env, argno));
kernel/bpf/verifier.c static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state * · line 12010
Quoted 10 lines
kernel/bpf/verifier.c +4
-5-bpf_diag_res (env, env->insn_idx,"uninitialized IRQ flag restore" , -"Restoring IRQ state requires a stack slot that was " -"initialized by a matching IRQ save operation on this path." , -"Pass the same stack slot that was previously initialized by " -"the matching IRQ save kfunc." ); +bpf_diag_res ( + env, env->insn_idx,"uninitialized IRQ flag restore" , +"Restoring IRQ state requires a stack slot that was initialized by a matching IRQ save operation on this path." , +"Pass the same stack slot that was previously initialized by the matching IRQ save kfunc." );return err;
Churn.
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 13/16 bpf: Report Program Structure CFG errors
In reply to Kumar Kartikeya DwivediAugment selected subprogram CFG validation failures with Program Structure reports. These errors are structural rather than path-dependent, so the report focuses on source and instruction context instead of causal history.
Cover jumps that leave the current subprogram, subprograms whose last instruction can fall through into the next subprogram, and recursive bpf2bpf call graph edges.
Format long jump-range reasons directly in diagnostics.c, and keep the fallthrough suggestion aligned with the verifier check by suggesting exit or explicit jumps.
--- kernel/bpf/cfg.c | 35 +++++++++++++++++++++++++++++++++++ kernel/bpf/diagnostics.c | 19 +++++++++++++++++++ kernel/bpf/diagnostics.h | 3 +++ kernel/bpf/verifier.c | 16 ++++++++++++++++ 4 files changed, 73 insertions(+)
kernel/bpf/cfg.c +35 -0
@@ -5,6 +5,8 @@#include <linux/filter.h> #include <linux/sort.h> +#include "diagnostics.h" + #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)/* non-recursive DFS pseudo code
@@ -113,6 +115,10 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)if (w <0 || w >= env->prog->len) {verbose_linfo (env, t,"%d: " , t);verbose (env,"jump out of range from insn %d to %d\n" , t, w); +bpf_diag_program_structure ( + env, t,"jump out of range" ,"Keep branch targets inside the program." , +"Instruction %d jumps to instruction %d, but the program only contains instructions 0 through %d." , + t, w, env->prog->len -1 );return -EINVAL ; }
@@ -136,6 +142,11 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)verbose_linfo (env, t,"%d: " , t);verbose_linfo (env, w,"%d: " , w);verbose (env,"back-edge from insn %d to %d\n" , t, w); +bpf_diag_program_structure ( + env, t,"back-edge is not allowed" , +"Load with privileges that allow this back-edge, or rewrite the control flow so it does not branch backward." , +"Instruction %d branches back to instruction %d. This program is being rejected without the privilege needed for this back-edge." , + t, w);return -EINVAL ; }else if (insn_state[w] ==EXPLORED ) {/* forward- or cross-edge */
@@ -316,6 +327,11 @@ static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,if (!jt) {verbose (env,"no jump tables found for subprog starting at %u\n" , subprog_start); +bpf_diag_program_structure ( + env, subprog_start,"missing jump table" , +"Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms." , +"No jump table was found for the subprogram that starts at instruction %u." , + subprog_start);return ERR_PTR (-EINVAL ); }
@@ -343,6 +359,11 @@ create_jt(int t, struct bpf_verifier_env *env)if (jt->items[i] < subprog_start || jt->items[i] >= subprog_end) {verbose (env,"jump table for insn %d points outside of the subprog [%u,%u]\n" , t, subprog_start, subprog_end); +bpf_diag_program_structure ( + env, t,"jump table target out of range" , +"Keep every jump-table target inside the same subprogram." , +"The jump table for instruction %d points outside subprogram range [%u,%u)." , + t, subprog_start, subprog_end);kvfree (jt);return ERR_PTR (-EINVAL ); }
@@ -374,6 +395,11 @@ static int visit_gotox_insn(int t, struct bpf_verifier_env *env) w = jt->items[i];if (w <0 || w >= env->prog->len) {verbose (env,"indirect jump out of range from insn %d to %d\n" , t, w); +bpf_diag_program_structure ( + env, t,"indirect jump out of range" , +"Keep indirect jump targets inside the program." , +"Instruction %d can jump indirectly to instruction %d, but the program only contains instructions 0 through %d." , + t, w, env->prog->len -1 );return -EINVAL ; }
@@ -624,12 +650,21 @@ int bpf_check_cfg(struct bpf_verifier_env *env)if (insn_state[i] !=EXPLORED ) {verbose (env,"unreachable insn %d\n" , i); +bpf_diag_program_structure ( + env, i,"unreachable instruction" , +"Remove the unreachable instruction or add valid control flow that reaches it." , +"Instruction %d is not reachable from the program entry point." , i); ret = -EINVAL ; goto err_free; }if (bpf_is_ldimm64 (insn)) {if (insn_state[i +1 ] !=0 ) {verbose (env,"jump into the middle of ldimm64 insn %d\n" , i); +bpf_diag_program_structure ( + env, i,"jump into ldimm64 immediate" , +"Target the first instruction of the ldimm64 pair, or restructure the jump target." , +"Control flow reaches the second half of the ldimm64 instruction pair that starts at instruction %d." , + i); ret = -EINVAL ; goto err_free; }
kernel/bpf/diagnostics.c +19 -0
@@ -1138,6 +1138,25 @@ void bpf_diag_ctx_restricted(struct bpf_verifier_env *env, u32 insn_idx, const c {diag_ctx_forbidden (env, insn_idx, operation, ctx_kind, context, constraint, suggestion); } + +void bpf_diag_program_structure (struct bpf_verifier_env *env,u32 insn_idx, +const char *problem,const char *suggestion, +const char *reason_fmt, ...) +{ +va_list args; + +bpf_diag_header (env,PROGRAM_STRUCTURE , problem); +diag_section (env,"Reason" ); + +va_start (args, reason_fmt); +diag_vprint_indented (env, reason_fmt, args); +va_end (args); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"%s" , problem); + +diag_suggestion (env,"%s" , suggestion); +}void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *reg_name,const struct bpf_reg_state *reg,enum bpf_diag_invalid_deref_kind kind,s64 offset)
kernel/bpf/diagnostics.h +3 -0
@@ -154,6 +154,9 @@ void bpf_diag_ctx(struct bpf_verifier_env *env, enum bpf_diag_ctx_report report,void bpf_diag_ctx_restricted (struct bpf_verifier_env *env,u32 insn_idx,const char *operation,enum bpf_diag_context_kind ctx_kind,const char *context,const char *constraint,const char *suggestion); +void bpf_diag_program_structure(struct bpf_verifier_env *env, u32 insn_idx, + const char *problem, const char *suggestion, + const char *reason_fmt, ...) __printf(5, 6);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +16 -0
@@ -3000,6 +3000,12 @@ static int check_subprogs(struct bpf_verifier_env *env) off = i +bpf_jmp_offset (&insn[i]) +1 ;if (off < subprog_start || off >= subprog_end) {verbose (env,"jump out of range from insn %d to %d\n" , i, off); +bpf_diag_program_structure ( + env, i,"jump out of range" , +"Keep branch targets within the same subprogram, or use an explicit subprogram call." , +"Instruction %d jumps to instruction %d, but subprogram %d only contains instructions %d through %d. " +"A branch target must stay inside the same subprogram." , + i, off, cur_subprog, subprog_start, subprog_end -1 );return -EINVAL ; } next:
@@ -3012,6 +3018,11 @@ static int check_subprogs(struct bpf_verifier_env *env) code != (BPF_JMP32 | BPF_JA) && code != (BPF_JMP | BPF_JA)) {verbose (env,"last insn is not an exit or jmp\n" ); +bpf_diag_program_structure ( + env, i,"subprogram can fall through" , +"End each subprogram with an exit or an explicit jump that keeps control flow inside the subprogram." , +"Subprogram %d reaches its last instruction %d without an exit or jump, so control could continue into the next subprogram." , + cur_subprog, i);return -EINVAL ; } subprog_start = subprog_end;
@@ -3084,6 +3095,11 @@ static int sort_subprogs_topo(struct bpf_verifier_env *env)verbose (env,"recursive call from %s() to %s()\n" ,bpf_subprog_name (env, cur),bpf_subprog_name (env, callee)); +bpf_diag_program_structure ( + env, idx,"recursive subprogram call" , +"Rewrite the recursion as an explicit bounded loop, or split the logic so subprogram calls do not form a cycle." , +"This bpf2bpf call would make the subprogram call graph recursive. " +"The verifier requires a finite, acyclic call graph so it can bound stack depth and analysis." ); ret = -EINVAL ; goto out; }
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 14/16 bpf: Report Policy helper and kfunc errors
In reply to Kumar Kartikeya DwivediAugment selected helper and kfunc allowability failures with Policy reports. These reports explain which requested operation is forbidden and why, without adding path history for non-path-dependent policy checks.
Cover unprivileged bpf2bpf and kfunc use, helper program-type restrictions, GPL-only helpers, helper-specific allow callbacks, kfunc allowability, and destructive kfunc capability checks.
--- kernel/bpf/diagnostics.c | 13 +++++++++++++ kernel/bpf/diagnostics.h | 2 ++ kernel/bpf/verifier.c | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-)
kernel/bpf/diagnostics.c +13 -0
@@ -1157,6 +1157,19 @@ void bpf_diag_program_structure(struct bpf_verifier_env *env, u32 insn_idx,diag_suggestion (env,"%s" , suggestion); } + +void bpf_diag_policy (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +const char *reason,const char *suggestion) +{ +bpf_diag_header (env,POLICY ,"operation is not allowed" ); +diag_reason (env,"The %s is not allowed: %s." , operation, reason); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"policy check failed for %s" , operation); + +diag_suggestion (env,"%s" , suggestion); +} +void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *reg_name,const struct bpf_reg_state *reg,enum bpf_diag_invalid_deref_kind kind,s64 offset)
kernel/bpf/diagnostics.h +2 -0
@@ -157,6 +157,8 @@ void bpf_diag_ctx_restricted(struct bpf_verifier_env *env, u32 insn_idx, const c void bpf_diag_program_structure(struct bpf_verifier_env *env, u32 insn_idx, const char *problem, const char *suggestion, const char *reason_fmt, ...) __printf(5, 6); +void bpf_diag_policy (struct bpf_verifier_env *env,u32 insn_idx,const char *operation, +const char *reason,const char *suggestion);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/verifier.c +32 -1
@@ -2904,6 +2904,10 @@ static int add_subprogs(struct bpf_verifier_env *env)if (!env->bpf_capable) {verbose (env,"loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n" ); +bpf_diag_policy ( + env, i,"BPF-to-BPF function call" , +"loading or calling other BPF functions requires CAP_BPF or CAP_SYS_ADMIN" , +"Load this program with the required capability, or avoid BPF-to-BPF function calls in unprivileged programs." );return -EPERM ; }
@@ -2956,6 +2960,10 @@ static int add_kfuncs(struct bpf_verifier_env *env)if (!env->bpf_capable) {verbose (env,"loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n" ); +bpf_diag_policy ( + env, i,"kernel function call" , +"calling kernel functions requires CAP_BPF or CAP_SYS_ADMIN" , +"Load this program with the required capability, or avoid kernel function calls in unprivileged programs." );return -EPERM ; }
@@ -10668,17 +10676,31 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insnif (err) {verbose (env,"program of this type cannot use helper %s#%d\n" ,func_id_name (func_id), func_id); + operation =bpf_diag_fmt (env,"helper %s#%d" ,func_id_name (func_id), func_id); +bpf_diag_policy ( + env, insn_idx, operation,"this program type does not allow the helper" , +"Use a helper allowed for this program type, or move the logic to a compatible program type." );return err; }/* eBPF programs must be GPL compatible to use GPL-ed functions */ if (!env->prog->gpl_compatible && fn->gpl_only) {verbose (env,"cannot call GPL-restricted function from non-GPL compatible program\n" ); + operation =bpf_diag_fmt (env,"helper %s#%d" ,func_id_name (func_id), func_id); +bpf_diag_policy ( + env, insn_idx, operation, +"this helper is restricted to GPL-compatible programs" , +"Use a GPL-compatible license, or replace the helper with one that is available to non-GPL programs." );return -EINVAL ; }if (fn->allowed && !fn->allowed (env->prog)) {verbose (env,"helper call is not allowed in probe\n" ); + operation =bpf_diag_fmt (env,"helper %s#%d" ,func_id_name (func_id), func_id); +bpf_diag_policy ( + env, insn_idx, operation, +"the helper-specific policy callback rejected this program" , +"Use the helper only from an allowed attach point or program configuration." );return -EINVAL ; }
@@ -13539,8 +13561,13 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,return 0 ; err =bpf_fetch_kfunc_arg_meta (env, insn->imm, insn->off, &meta); -if (err == -EACCES && meta.func_name) +if (err == -EACCES && meta.func_name) {verbose (env,"calling kernel function %s is not allowed\n" , meta.func_name); + operation =bpf_diag_fmt (env,"kfunc %s" , meta.func_name); +bpf_diag_policy ( + env, insn_idx, operation,"this program cannot call the kfunc" , +"Use a kfunc allowed for this program type and attach point, or change the program context." ); + }if (err)return err; desc_btf = meta.btf;
@@ -13587,6 +13614,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (is_kfunc_destructive (&meta) && !capable (CAP_SYS_BOOT )) {verbose (env,"destructive kfunc calls require CAP_SYS_BOOT capability\n" ); + operation =bpf_diag_fmt (env,"destructive kfunc %s" , meta.func_name); +bpf_diag_policy ( + env, insn_idx, operation,"destructive kfuncs require CAP_SYS_BOOT" , +"Load the program with CAP_SYS_BOOT, or avoid destructive kfuncs." );return -EACCES ; }
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 15/16 bpf: Report Verifier Limit errors
In reply to Kumar Kartikeya DwivediAugment selected verifier limit failures with Verifier Limit reports. These reports focus on the limit that was exceeded and the observed value or condition, rather than causal branch history.
Cover tail-call stack constraints, per-subprogram stack depth, combined call stack depth, static and runtime bpf2bpf call-frame depth, processed-instruction complexity, and liveness analysis complexity.
Format reason text in diagnostics.c and allocate call-chain descriptions only on failure paths, preserving useful call-chain context without adding large local buffers to verifier frames.
--- kernel/bpf/diagnostics.c | 25 +++++ kernel/bpf/diagnostics.h | 2 + kernel/bpf/liveness.c | 6 ++ kernel/bpf/verifier.c | 97 ++++++++++++++++++- .../bpf/progs/test_global_func_deep_stack.c | 1 + 5 files changed, 130 insertions(+), 1 deletion(-)
kernel/bpf/diagnostics.c +25 -0
@@ -1170,6 +1170,31 @@ void bpf_diag_policy(struct bpf_verifier_env *env, u32 insn_idx, const char *opediag_suggestion (env,"%s" , suggestion); } +void bpf_diag_limit (struct bpf_verifier_env *env,u32 insn_idx,const char *limit, +const char *suggestion,const char *reason_fmt, ...) +{ +const char *reason, *text; +va_list args; + +if (!bpf_diag_enabled (env)) +return ; + +bpf_diag_header (env,VERIFIER_LIMIT ,"limit exceeded" ); +diag_section (env,"Reason" ); + +va_start (args, reason_fmt); + reason =bpf_diag_vfmt (env, reason_fmt, args); +va_end (args); + text =bpf_diag_fmt (env,"The %s limit was exceeded: %s." , limit, reason); + +diag_print_wrapped_text (env, text); + +diag_section (env,"At" ); +bpf_diag_source (env, insn_idx,"error" ,"limit exceeded: %s" , limit); + +diag_suggestion (env,"%s" , suggestion); +} +void bpf_diag_invalid_deref (struct bpf_verifier_env *env,u32 insn_idx,int regno,const char *reg_name,const struct bpf_reg_state *reg,enum bpf_diag_invalid_deref_kind kind,s64 offset)
kernel/bpf/diagnostics.h +2 -0
@@ -159,6 +159,8 @@ void bpf_diag_program_structure(struct bpf_verifier_env *env, u32 insn_idx, const char *reason_fmt, ...) __printf(5, 6);void bpf_diag_policy (struct bpf_verifier_env *env,u32 insn_idx,const char *operation,const char *reason,const char *suggestion); +void bpf_diag_limit(struct bpf_verifier_env *env, u32 insn_idx, const char *limit, + const char *suggestion, const char *reason_fmt, ...) __printf(5, 6);void bpf_diag_record_branch (struct bpf_verifier_env *env,u32 insn_idx,bool cond_true);void bpf_diag_mod_begin (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const struct bpf_reg_state *origin,enum bpf_diag_mod_reason reason);
kernel/bpf/liveness.c +6 -0
@@ -8,6 +8,8 @@#include <linux/slab.h> #include <linux/sort.h> +#include "diagnostics.h" + #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)struct per_frame_masks {
@@ -1856,6 +1858,10 @@ static int analyze_subprog(struct bpf_verifier_env *env,if (++env->liveness->subprog_calls >10000 ) {verbose (env,"liveness analysis exceeded complexity limit (%d calls)\n" , env->liveness->subprog_calls); +bpf_diag_limit ( + env, start,"liveness analysis complexity" , +"Reduce the number of distinct call paths or argument patterns reaching these subprograms." , +"The verifier recomputed subprogram liveness too many times while tracking stack and register reads across call paths" );return -E2BIG ; }
kernel/bpf/verifier.c +96 -1
166 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c +1 -0
@@ -89,6 +89,7 @@ int global_func_deep_stack_success(struct __sk_buff *skb)*/ SEC("syscall") __failure __msg("combined stack size of 34 calls") +__msg("Call chain ... -> f16")int global_func_deep_stack_fail (struct __sk_buff *skb) {return f32 (123 );
Signature · 1 line
2.53.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 15/16] bpf: Report Verifier Limit errors
On Thu, 2026-08-13 at 01:33 +0200, Kumar Kartikeya Dwivedi wrote:
...
Quoted 14 lines
kernel/bpf/liveness.c +2
-0@@ -8,6 +8,8 @@#include <linux/slab.h> #include <linux/sort.h> +#include "diagnostics.h" + #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)struct per_frame_masks {
kernel/bpf/liveness.c static int analyze_subprog(struct bpf_verifier_env *env, · line 1863
Quoted 6 lines
kernel/bpf/liveness.c +3
-0@@ -1856,6 +1858,10 @@ static int analyze_subprog(struct bpf_verifier_env *env,if (++env->liveness->subprog_calls >10000 ) {verbose (env,"liveness analysis exceeded complexity limit (%d calls)\n" , env->liveness->subprog_calls); + bpf_diag_limit( + env, start, "liveness analysis complexity", + "Reduce the number of distinct call paths or argument patterns reaching these subprograms.",
Nit: "these subprograms" is not very clear here, drop it?
kernel/bpf/liveness.c static int analyze_subprog(struct bpf_verifier_env *env, · line 1864
Quoted 1 line
kernel/bpf/liveness.c +1
-0+ "The verifier recomputed subprogram liveness too many times while tracking stack and register reads across call paths");
Nit: it's not a "subprogram liveness" ->
"Stack liveness analysis failed to reach a fixed point after %d iterations".
Quoted 16 lines
kernel/bpf/liveness.c +0
-0return -E2BIG ; }kernel/bpf/verifier.c +5
-0@@ -5252,6 +5252,38 @@ struct bpf_subprog_call_depth_info {int frame;/* # of consecutive static call stack frames on top of stack */ }; +static const char *bpf_diag_append_subprog_chain (struct bpf_verifier_env *env, +const char *chain,int subprog) +{ +const char *prefix = chain && *chain ?" -> " :"" ; +const char *name =bpf_subprog_name (env, subprog);
kernel/bpf/verifier.c struct bpf_subprog_call_depth_info { · line 5269
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+const char *old = chain ?:"" ; + +if (name && *name) +return bpf_diag_fmt (env,"%s%s%s" , old, prefix, name); +return bpf_diag_fmt (env,"%s%ssubprogram %d" , old, prefix, subprog); +} + +static const char *bpf_diag_alloc_subprog_call_chain (struct bpf_verifier_env *env, +struct bpf_subprog_call_depth_info *dinfo, +int idx)
Nit: drop the 'bpf_diag_' prefix.
Quoted 21 lines
kernel/bpf/verifier.c +17
-0+{ +int call_chain[MAX_CALL_FRAMES +1 ]; +int i, subprog, cnt =0 ; +const char *chain =NULL ; + +for (subprog = idx; subprog >=0 && cnt <ARRAY_SIZE (call_chain); + subprog = dinfo[subprog].caller) + call_chain[cnt++] = subprog; + +if (subprog >=0 ) + chain ="..." ; +for (i = cnt -1 ; i >=0 ; i--) + chain =bpf_diag_append_subprog_chain (env, chain, call_chain[i]); + +return chain; +} +/* starting from main bpf function walk all instructions of the function * and recursively walk all callees that given function can call. * Ignore jump and exit insns.
kernel/bpf/verifier.c static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx, · line 5329
Quoted 4 lines
kernel/bpf/verifier.c +1
-0@@ -5294,9 +5326,17 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,* of caller's stack as shown on the example above. */ if (idx && subprog[idx].has_tail_call && depth >=256 ) { +const char *chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx);
Nit: bubble up chain definition to the top of the function.
kernel/bpf/verifier.c static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx, · line 5335
Quoted 6 lines
kernel/bpf/verifier.c +3
-0+verbose (env,"tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n" , depth); + bpf_diag_limit( + env, subprog[idx].start, "call stack with tail calls",
"call stack with tail calls" -> "stack depth at tail call"
kernel/bpf/verifier.c static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx, · line 5339
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+"Reduce stack usage in caller frames, or avoid combining deep bpf2bpf calls with tail calls." , +"Call chain %s reaches a subprogram with tail calls after caller frames already use %d bytes; " +"tail-call paths are limited to 256 bytes in caller frames" , + chain ?: "the current call chain", depth);
"the current call chain" is useless, if it's a guard for ENOMEM, let's make it "<can't render callchain>".
Quoted 3 lines
kernel/bpf/verifier.c +0
-0return -EACCES ; }
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 16/16 bpf: Gate verifier diagnostics on log level
In reply to Kumar Kartikeya DwivediVerifier diagnostics collect active-path history and render richer reports for selected verifier failures. That work is useful only when the caller requests normal verifier log output.
Do not enable diagnostic collection or report rendering for BPF_LOG_STATS-only loads. Stats-only loads still need the verifier's summary counters, but not the extra path history used by diagnostic reports.
Keep enablement in the verifier environment and requested log mode so async callback verification uses the same diagnostic policy as the main verifier pass.
--- kernel/bpf/verifier.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +16 -9
@@ -5326,7 +5326,10 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,* of caller's stack as shown on the example above. */ if (idx && subprog[idx].has_tail_call && depth >=256 ) { -const char *chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); +const char *chain =NULL ; + +if (bpf_diag_enabled (env)) + chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); verbose(env, "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n",
@@ -5358,11 +5361,12 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,if (subprog_depth > env->max_stack_depth) env->max_stack_depth = subprog_depth;if (subprog_depth >MAX_BPF_STACK ) { -const char *chain; +const char *chain =NULL ;verbose (env,"stack size of subprog %d is %d. Too large\n" , idx, subprog_depth); - chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); +if (bpf_diag_enabled (env)) + chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); bpf_diag_limit( env, subprog[idx].start, "subprogram stack depth", "Reduce stack usage in this subprogram, or move large data out of the BPF stack.",
@@ -5382,9 +5386,10 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,verbose (env,"combined stack size of %d calls is %d. Too large\n" , total, depth); { -const char *chain; +const char *chain =NULL ; - chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); +if (bpf_diag_enabled (env)) + chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); bpf_diag_limit( env, subprog[idx].start, "combined call stack depth", "Reduce stack usage or call depth along this call chain.",
@@ -5456,11 +5461,12 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx, frame =bpf_subprog_is_global (env, idx) ?0 : frame +1 ;if (frame >=MAX_CALL_FRAMES ) { -const char *chain; +const char *chain =NULL ;verbose (env,"the call stack of %d frames is too deep !\n" , frame); - chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); +if (bpf_diag_enabled (env)) + chain =bpf_diag_alloc_subprog_call_chain (env, dinfo, idx); bpf_diag_limit( env, call_insn, "bpf2bpf call frames", "Reduce the number of nested bpf2bpf calls on this path.",
@@ -9585,11 +9591,12 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int callsint err;if (state->curframe +1 >=MAX_CALL_FRAMES ) { -const char *chain; +const char *chain =NULL ;verbose (env,"the call stack of %d frames is too deep\n" , state->curframe +2 ); - chain =bpf_diag_alloc_state_call_chain (env, state, subprog); +if (bpf_diag_enabled (env)) + chain =bpf_diag_alloc_state_call_chain (env, state, subprog); bpf_diag_limit( env, callsite, "bpf2bpf call frames", "Reduce the number of nested bpf2bpf calls on this path.",
Signature · 1 line
2.53.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 16/16] bpf: Gate verifier diagnostics on log level
Quoted 8 lines · Kumar Kartikeya Dwivedi
Verifier diagnostics collect active-path history and render richer reports for selected verifier failures. That work is useful only when the caller requests normal verifier log output.
Do not enable diagnostic collection or report rendering for BPF_LOG_STATS-only loads. Stats-only loads still need the verifier's summary counters, but not the extra path history used by diagnostic reports.
Quoted 6 lines
Keep enablement in the verifier environment and requested log mode so async callback verification uses the same diagnostic policy as the main verifier pass.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
Oh, I see why you needed the "current call chain".
Nope, let's keep the report useful on any log level, please figure out how. The "current call chain" is completely useless.
Also, this whole business with check_max_stack_depth_subprog() modification and limits reporting can be a follow-up.
...
Eduard Zingerman
maintainerQuoted 4 lines · Kumar Kartikeya Dwivedi
TL;DR: This set reworks verifier error messages to include source and instruction annotations, together with more causal context, making failures easier to understand and more actionable when debugging and repairing BPF programs.
This series is in conflict with14c950ac2be8 ("bpf: Track verifier instruction stats for each subprogram")