libbpf: Fix out-of-bound read in bpf_linker__add_buf()
PATCH 1/1 libbpf: Fix out-of-bound read in bpf_linker__add_buf()
Fix a potential out-of-bound read in bpf_linker__add_buf() by advancing the buffer pointer and reducing the remaining buffer size passed to write() in each iteration. The bug is reported in [0].
[0]: https://github.com/libbpf/libbpf/issues/945
--- tools/lib/bpf/linker.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/linker.c +1 -1
@@ -581,7 +581,7 @@ int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz, written =0 ;while (written < buf_sz) { - ret =write (fd, buf, buf_sz); + ret =write (fd, buf + written, buf_sz - written);if (ret <0 ) { ret = -errno;pr_warn ("failed to write '%s': %s\n" , filename,errstr (ret));
Signature · 1 line
2.47.3
Quoted 8 lines · Amery Hung
Fix a potential out-of-bound read in
bpf_linker__add_buf()by advancing the buffer pointer and reducing the remaining buffer size passed towrite()in each iteration. The bug is reported in [0].[0]: https://github.com/libbpf/libbpf/issues/945
Fixes 6d5e5e5d7ce1 ("libbpf: Extend linker API to support in-memory ELF files")Signed-off-by Amery Hung <[email protected]>
jirka
Quoted 21 lines
--- tools/lib/bpf/linker.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/bpf/linker.c +1
-1@@ -581,7 +581,7 @@ int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz, written =0 ;while (written < buf_sz) { - ret =write (fd, buf, buf_sz); + ret =write (fd, buf + written, buf_sz - written);if (ret <0 ) { ret = -errno;pr_warn ("failed to write '%s': %s\n" , filename,errstr (ret));Signature · 1 line
2.47.3
Quoted 18 lines · Amery Hung
Fix a potential out-of-bound read in
bpf_linker__add_buf()by advancing the buffer pointer and reducing the remaining buffer size passed towrite()in each iteration. The bug is reported in [0].[0]: https://github.com/libbpf/libbpf/issues/945
Fixes 6d5e5e5d7ce1 ("libbpf: Extend linker API to support in-memory ELF files")Signed-off-by Amery Hung <[email protected]>--- tools/lib/bpf/linker.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/bpf/linker.c +0
-0
tools/lib/bpf/linker.c int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz, · line 584
Quoted 5 lines
tools/lib/bpf/linker.c +1
-1@@ -581,7 +581,7 @@ int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz, written =0 ;while (written < buf_sz) { - ret =write (fd, buf, buf_sz); + ret =write (fd, buf + written, buf_sz - written);
The bug fix is correct, but commit description is off. There will be no out-of-bounds reads, we'll just corrupt memfd contents and almost certainly won't be able to parse result ELF file correctly. I've adjusted the description as follows and pushed to bpf, thanks
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.
Quoted 7 lines
tools/lib/bpf/linker.c +0
-1if (ret <0 ) { ret = -errno;pr_warn ("failed to write '%s': %s\n" , filename,errstr (ret)); --2.47.3