Re: [PATCH bpf-next 5/5] selftests/bpf: add loader_load_fd tests

[email protected]
Newsgroups org.kernel.vger.selinux,org.kernel.vger.bpf
Message-ID <[email protected]>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unchecked pointer returns from libelf functions in `create_loader_elf` will cause a SIGSEGV upon allocation failure.
--

--- Patch [5]: [PATCH bpf-next 5/5] selftests/bpf: add loader_load_fd tests ---
commit b9a0b98c13acbc44d106f86733ac4cdd18481ce7
Author: Thiébaud Weksteen <[email protected]>

selftests/bpf: add loader_load_fd tests

This commit adds user-space selftests for the BPF_LOADER_LOAD_FD command.
The test ELF is generated based on the existing light skeleton generator,
using an awk script to extract the loader program and map.

> diff --git a/tools/testing/selftests/bpf/prog_tests/loader_load_fd.c b/tools/testing/selftests/bpf/prog_tests/loader_load_fd.c
> new file mode 100644
> index 0000000000000..ab971662dd047
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/loader_load_fd.c
[ ... ]
> +	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
> +	ehdr->e_machine = EM_BPF;
> +	ehdr->e_type = ET_REL;
> +	ehdr->e_version = EV_CURRENT;
> +
> +	shstr_scn = elf_newscn(elf);
> +	shstr_shdr = elf64_getshdr(shstr_scn);
> +	shstr_shdr->sh_name = shstr_off;
> +	shstr_shdr->sh_type = SHT_STRTAB;
> +	shstr_shdr->sh_flags = 0;
> +
> +	shstr_data = elf_newdata(shstr_scn);
> +	shstr_data->d_buf = shstrtab;

[Severity: Medium]
Could this crash the test runner if libelf fails to allocate memory?

Earlier in create_loader_elf(), ASSERT_OK_PTR is used to check the return
values of elf_begin() and elf64_newehdr(). However, the pointers returned
by elf_newscn(), elf64_getshdr(), and elf_newdata() are dereferenced here
without prior validation.

If these functions return NULL, the test will abort with a segmentation
fault, preventing subsequent tests from running. This pattern also appears
to be repeated for the .prog, .map, and license sections below.

Would it be better to add ASSERT_OK_PTR checks for these allocations?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.