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

libbpf: Make remark about zero-initializing bpf_*_info structs

In some situations, if you fail to zero-initialize the
bpf_{prog,map,btf,link}_info structs supplied to the set of LIBBPF
helpers bpf_{prog,map,btf,link}_get_info_by_fd(), you can expect the
helper to return an error. This can possibly leave people in a
situation where they're scratching their heads for an unnnecessary
amount of time. Make an explicit remark about the requirement of
zero-initializing the supplied bpf_{prog,map,btf,link}_info structs
for the respective LIBBPF helpers.

Internally, LIBBPF helpers bpf_{prog,map,btf,link}_get_info_by_fd()
call into bpf_obj_get_info_by_fd() where the bpf(2)
BPF_OBJ_GET_INFO_BY_FD command is used. This specific command is
effectively backed by restrictions enforced by the
bpf_check_uarg_tail_zero() helper. This function ensures that if the
size of the supplied bpf_{prog,map,btf,link}_info structs are larger
than what the kernel can handle, trailing bits are zeroed. This can be
a problem when compiling against UAPI headers that don't necessarily
match the sizes of the same underlying types known to the kernel.

Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/ZcyEb8x4VbhieWsL@google.com

authored by

Matt Bobrowski and committed by
Andrii Nakryiko
1159d278 7cc13adb

+17 -5
+17 -5
tools/lib/bpf/bpf.h
··· 500 500 * program corresponding to *prog_fd*. 501 501 * 502 502 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 503 - * actual number of bytes written to *info*. 503 + * actual number of bytes written to *info*. Note that *info* should be 504 + * zero-initialized or initialized as expected by the requested *info* 505 + * type. Failing to (zero-)initialize *info* under certain circumstances can 506 + * result in this helper returning an error. 504 507 * 505 508 * @param prog_fd BPF program file descriptor 506 509 * @param info pointer to **struct bpf_prog_info** that will be populated with ··· 520 517 * map corresponding to *map_fd*. 521 518 * 522 519 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 523 - * actual number of bytes written to *info*. 520 + * actual number of bytes written to *info*. Note that *info* should be 521 + * zero-initialized or initialized as expected by the requested *info* 522 + * type. Failing to (zero-)initialize *info* under certain circumstances can 523 + * result in this helper returning an error. 524 524 * 525 525 * @param map_fd BPF map file descriptor 526 526 * @param info pointer to **struct bpf_map_info** that will be populated with ··· 536 530 LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len); 537 531 538 532 /** 539 - * @brief **bpf_btf_get_info_by_fd()** obtains information about the 533 + * @brief **bpf_btf_get_info_by_fd()** obtains information about the 540 534 * BTF object corresponding to *btf_fd*. 541 535 * 542 536 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 543 - * actual number of bytes written to *info*. 537 + * actual number of bytes written to *info*. Note that *info* should be 538 + * zero-initialized or initialized as expected by the requested *info* 539 + * type. Failing to (zero-)initialize *info* under certain circumstances can 540 + * result in this helper returning an error. 544 541 * 545 542 * @param btf_fd BTF object file descriptor 546 543 * @param info pointer to **struct bpf_btf_info** that will be populated with ··· 560 551 * link corresponding to *link_fd*. 561 552 * 562 553 * Populates up to *info_len* bytes of *info* and updates *info_len* with the 563 - * actual number of bytes written to *info*. 554 + * actual number of bytes written to *info*. Note that *info* should be 555 + * zero-initialized or initialized as expected by the requested *info* 556 + * type. Failing to (zero-)initialize *info* under certain circumstances can 557 + * result in this helper returning an error. 564 558 * 565 559 * @param link_fd BPF link file descriptor 566 560 * @param info pointer to **struct bpf_link_info** that will be populated with