[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <90d4a3da144059c5d680eef68ca0979662e35e31adc0f8076fff364ed80757b0@mail.kernel.org>
Date: Fri, 14 Nov 2025 22:58:54 +0000 (UTC)
From: bot+bpf-ci@...nel.org
To: ameryhung@...il.com,bpf@...r.kernel.org
Cc: netdev@...r.kernel.org,alexei.starovoitov@...il.com,andrii@...nel.org,daniel@...earbox.net,tj@...nel.org,martin.lau@...nel.org,ameryhung@...il.com,kernel-team@...a.com,ast@...nel.org,andrii@...nel.org,daniel@...earbox.net,martin.lau@...nel.org,eddyz87@...il.com,yonghong.song@...ux.dev,clm@...a.com,ihor.solodrai@...ux.dev
Subject: Re: [PATCH bpf-next v6 3/6] libbpf: Add support for associating BPF program with struct_ops
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index b66f5fbfb..21b57a629 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
[ ... ]
> +int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd,
> + struct bpf_prog_assoc_struct_ops_opts *opts)
> +{
> + const size_t attr_sz = offsetofend(union bpf_attr, prog_assoc_struct_ops);
> + union bpf_attr attr;
> + int err;
> +
> + if (!OPTS_VALID(opts, bpf_prog_assoc_struct_ops_opts))
> + return libbpf_err(-EINVAL);
> +
> + memset(&attr, 0, attr_sz);
> + attr.prog_assoc_struct_ops.map_fd = map_fd;
> + attr.prog_assoc_struct_ops.prog_fd = prog_fd;
> + attr.prog_assoc_struct_ops.flags = OPTS_GET(opts, flags, 0);
> +
> + err = sys_bpf(BPF_PROG_ASSOC_STRUCT_OPS, &attr, attr_sz);
> + return libbpf_err_errno(err);
> +}
[ ... ]
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 706e7481b..1d5424276 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
[ ... ]
> +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, map_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 this crash if prog is NULL? bpf_program__fd() handles NULL by
returning libbpf_err(-EINVAL), so prog_fd will be negative and we
enter this error path. Then prog->name dereferences the NULL pointer.
> + return -EINVAL;
> + }
> +
> + if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
^^^^^^^^^
Similarly, is prog guaranteed non-NULL here? We know prog_fd is valid,
but that doesn't prove prog itself was checked.
> + pr_warn("prog '%s': can't associate struct_ops program\n", prog->name);
> + return -EINVAL;
> + }
> +
> + map_fd = bpf_map__fd(map);
> + if (map_fd < 0) {
> + pr_warn("map '%s': can't associate BPF map without FD (was it created?)\n", map->name);
^^^^^^^^^^
Same issue with map - bpf_map__fd() handles NULL by returning an error,
but then we dereference map->name in the error message.
> + return -EINVAL;
> + }
> +
> + if (!bpf_map__is_struct_ops(map)) {
^^^^^^^^^^^^^^^^^^^^^^^^
bpf_map__is_struct_ops() also dereferences map->def.type without a NULL
check, which would crash here if map is NULL.
> + 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);
> +}
Note: This follows the same pattern as existing functions like
bpf_program__pin() which also dereference prog->name without checking
prog for NULL. However, since the helper functions (bpf_program__fd,
bpf_map__fd) do handle NULL, it seems inconsistent to crash in the
error message when reporting that NULL was passed.
[ ... ]
---
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/19379369447
Powered by blists - more mailing lists