Re: [PATCH bpf-next v5 4/7] libbpf: Add support for associating BPF program with struct_ops

Andrii Nakryiko
2025-11-04T23:28:01Z

On Tue, Nov 4, 2025 at 9:54 AM <bot+bpf-ci@kernel.org> wrote:
>
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index fbe74686c..260e1feaa 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
>
> [ ... ]
>
> > @@ -13891,6 +13891,36 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
> > return 0;
> > }
> >
> > +int bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map,
> > + struct bpf_prog_assoc_struct_ops_opts *opts)
> > +{
> > + int prog_fd;
> > +
> > + prog_fd = bpf_program__fd(prog);
> > + if (prog_fd < 0) {
> > + pr_warn("prog '%s': can't associate BPF program without FD (was it loaded?)\n",
> > + prog->name);
> ^^^^^^^^^^
>
> Can prog be NULL here? If bpf_program__fd() returns -EINVAL because prog

libbpf APIs generally do not tolerate passing NULL for bpf_program or
bpf_map pointers. This is fine, this shouldn't happen with valid usage
of an API (and we don't guard against this).

> was NULL, this dereferences prog->name without a NULL check. Looking at
> bpf_program__set_attach_target() just above, it checks "if (!prog)"
> before any dereferences.
>
> > + return -EINVAL;
> > + }
> > +
> > + if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
> ^^^^^^^^^^
>
> Same issue - prog->type is dereferenced without verifying prog is not
> NULL.
>
> > + pr_warn("prog '%s': can't associate struct_ops program\n", prog->name);
> > + return -EINVAL;
> > + }
> > +
> > + if (map->fd < 0) {
> ^^^^^^
>
> Can map be NULL? There's no NULL check before dereferencing map->fd.
> Other map APIs like bpf_map__fd() and bpf_map__name() check for NULL
> before use.
>
> > + pr_warn("map '%s': can't associate BPF map without FD (was it created?)\n", map->name);
> > + return -EINVAL;
> > + }
> > +
> > + if (!bpf_map__is_struct_ops(map)) {
> > + pr_warn("map '%s': can't associate non-struct_ops map\n", map->name);
> > + return -EINVAL;
> > + }
> > +
> > + return bpf_prog_assoc_struct_ops(prog_fd, map->fd, opts);
> > +}
> > +
>
>
> ---
> 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/19077679684