libbpf: Fix invalid write loop logic in bpf_linker__add_buf()
authorAmery Hung <ameryhung@gmail.com>
Mon, 9 Feb 2026 23:01:34 +0000 (15:01 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 13 Feb 2026 22:14:27 +0000 (14:14 -0800)
commit04999b99e81eaa7b6223ec1c03af3bcb4ac57aaa
treedaed62d6c22ce41e01edfeb9272872bbc484ac2d
parent728ff167910ef16e97717719c749ddf4064c653b
libbpf: Fix invalid write loop logic in bpf_linker__add_buf()

Fix bpf_linker__add_buf()'s logic of copying data from memory buffer into
memfd. In the event of short write not writing entire buf_sz bytes into memfd
file, we'll append bytes from the beginning of buf *again* (corrupting ELF
file contents) instead of correctly appending the rest of not-yet-read buf
contents.

Closes: https://github.com/libbpf/libbpf/issues/945
Fixes: 6d5e5e5d7ce1 ("libbpf: Extend linker API to support in-memory ELF files")
Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20260209230134.3530521-1-ameryhung@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/linker.c