Skip to contentSkip to conversation

bpf: Fix func_info_aux desync after dead code elimination

PATCH New Kumar Kartikeya Dwivedi 3 participants 3 messages Aug 12 – Aug 13 BPF

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

PATCH 1/1 bpf: Fix func_info_aux desync after dead code elimination

The verifier keeps per-subprogram metadata in three parallel arrays:
subprog_info, func_info, and func_info_aux. Dead code elimination can remove whole subprograms, and adjust_subprog_starts_after_remove() shifts subprog_info and func_info to close the gap, but leaves func_info_aux in place. From that point on, func_info_aux[i] no longer describes subprogram i.

Shift func_info_aux together with func_info so the three arrays stay aligned after subprogram removal.

---
 kernel/bpf/fixups.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/fixups.c +5 -1
index c4bd70befbb5..37ea85e974f1 100644
@@ -401,13 +401,17 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
 			sizeof(*env->subprog_info) * move);
 		env->subprog_cnt -= j - i;
 
-		/* remove func_info */
+		/* remove func_info and its aux */
 		if (aux->func_info) {
 			move = aux->func_info_cnt - j;
 
 			memmove(aux->func_info + i,
 				aux->func_info + j,
 				sizeof(*aux->func_info) * move);
+			if (aux->func_info_aux)
+				memmove(aux->func_info_aux + i,
+					aux->func_info_aux + j,
+					sizeof(*aux->func_info_aux) * move);
 			aux->func_info_cnt -= j - i;
 			/* func_info->insn_off is set after all code rewrites,
 			 * in adjust_btf_func() - no need to adjust

--
2.53.0

#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Failure to update env->exception_callback_subprog during dead code elimination leads to an out-of-bounds array read and potential crash during JIT. --

--- Patch [1]: [PATCH bpf-next v1] bpf: Fix func_info_aux desync after dead code elimination --- commit f1d27d196a7979de10f6e09614e6262cf8069cae
Author: Kumar Kartikeya Dwivedi <[email protected]>

bpf: Fix func_info_aux desync after dead code elimination

The commit shifts func_info_aux together with func_info so the three parallel arrays stay aligned after subprogram removal during dead code elimination.

kernel/bpf/fixups.c static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, · line 402

Quoted 7 lines
kernel/bpf/fixups.c +0 -0
index c4bd70befbb5c..37ea85e974f14 100644
@@ -401,13 +401,17 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
 			sizeof(*env->subprog_info) * move);
 		env->subprog_cnt -= j - i;

[Severity: High]
This isn't a bug introduced by this patch, but should we also be adjusting env->exception_callback_subprog here?

When dead code elimination removes an unused static function that comes before an exception callback, env->subprog_cnt is decremented and subprograms are shifted down. However, env->exception_callback_subprog is left unadjusted.

Later, in jit_subprogs(), the func array is allocated with the new, smaller env->subprog_cnt. Accessing it using the outdated env->exception_callback_subprog index could result in an out-of-bounds array read and a potential kernel panic:

kernel/bpf/fixups.c:jit_subprogs() {
    ...
    prog->aux->bpf_exception_cb = (void *)func[env->exception_callback_subprog]->bpf_func;
    ...
}

Does env->exception_callback_subprog need to be updated to reflect the shift when subprograms are removed?

Quoted 16 lines
kernel/bpf/fixups.c +5 -1
-		/* remove func_info */
+		/* remove func_info and its aux */
 		if (aux->func_info) {
 			move = aux->func_info_cnt - j;
 
 			memmove(aux->func_info + i,
 				aux->func_info + j,
 				sizeof(*aux->func_info) * move);
+			if (aux->func_info_aux)
+				memmove(aux->func_info_aux + i,
+					aux->func_info_aux + j,
+					sizeof(*aux->func_info_aux) * move);
 			aux->func_info_cnt -= j - i;
 			/* func_info->insn_off is set after all code rewrites,
 			 * in adjust_btf_func() - no need to adjust
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Kumar Kartikeya Dwivedi
This looks like a bug fix for func_info_aux array alignment. Should this include:
Quoted 9 lines

bpf: Fix func_info_aux desync after dead code elimination

The verifier keeps per-subprogram metadata in three parallel arrays:
subprog_info, func_info, and func_info_aux. Dead code elimination can remove whole subprograms, and adjust_subprog_starts_after_remove() shifts subprog_info and func_info to close the gap, but leaves func_info_aux in place. From that point on, func_info_aux[i] no longer describes subprogram i.

Quoted 6 lines

Shift func_info_aux together with func_info so the three arrays stay aligned after subprogram removal.

This looks like a bug fix for func_info_aux array alignment. Should this include:

Fixes 51c39bb1d5d1 ("bpf: Introduce function-by-function verification")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31651414371