Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: handle yet another corner case of nulling out struct_ops program

There is yet another corner case where user can set STRUCT_OPS program
reference in STRUCT_OPS map to NULL, but libbpf will fail to disable
autoload for such BPF program. This time it's the case of "new" kernel
which has type information about callback field, but user explicitly
nulled-out program reference from user-space after opening BPF object.

Fix, hopefully, the last remaining unhandled case.

Fixes: 0737df6de946 ("libbpf: better fix for handling nulled-out struct_ops program")
Fixes: f973fccd43d3 ("libbpf: handle nulled-out program in struct_ops correctly")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240507001335.1445325-3-andrii@kernel.org
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

authored by

Andrii Nakryiko and committed by
Martin KaFai Lau
e18e2e70 8374b56b

+9 -1
+9 -1
tools/lib/bpf/libbpf.c
··· 1193 1193 } 1194 1194 1195 1195 if (btf_is_ptr(mtype)) { 1196 - /* Update the value from the shadow type */ 1197 1196 prog = *(void **)mdata; 1197 + /* just like for !kern_member case above, reset declaratively 1198 + * set (at compile time) program's autload to false, 1199 + * if user replaced it with another program or NULL 1200 + */ 1201 + if (st_ops->progs[i] && st_ops->progs[i] != prog) 1202 + st_ops->progs[i]->autoload = false; 1203 + 1204 + /* Update the value from the shadow type */ 1198 1205 st_ops->progs[i] = prog; 1199 1206 if (!prog) 1200 1207 continue; 1208 + 1201 1209 if (!is_valid_st_ops_program(obj, prog)) { 1202 1210 pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n", 1203 1211 map->name, mname);