kbuffer: Fix out-of-bounds write in kbuffer_read_buffer()
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest2 patches 2 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | kbuffer: Fix out-of-bounds write of missed events in kbuffer_read_buffer()src/ | +9 | 0 replies | |
| 2/2 | libtraceevent utest: Add tests for kbuffer_read_buffer() with missed eventsutest/ | +104 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH 0/2] kbuffer: Fix out-of-bounds write in kbuffer_read_buffer()
kbuffer_read_buffer() stores the missed events count after the copied events, but the check whether there is room for it is wrong. If the events fill up the destination buffer (which is the normal case, as callers pass a buffer of the sub-buffer size), the count is written up to 8 bytes beyond the end of the buffer, and MISSING_STORED is set although there is no room for the count.
This was found while looking into the libtracefs unit test failures reported here:
https://lore.kernel.org/linux-trace-devel/[email protected]/
libtracefs uses kbuffer_read_buffer() for memory mapped ring buffers (tracefs_cpu_read()/tracefs_cpu_write(), the latter with a buffer on the stack). That's what makes the "trace cpu pipe" test show an "Invalid read of size 8" in kbuffer_load_subbuffer() and random "missed events" values under valgrind ("make test_mem").
It can be reproduced without tracing by copying a full sub-buffer with missed events with kbuffer_read_buffer() into a malloc()ed buffer of the sub-buffer size. Under valgrind (amd64, libtraceevent 1.9.0):
==2504915== Invalid write of size 8
==2504915== at 0x48820FA: write_8 (kbuffer-parse.c:114) ==2504915== by 0x48834A0: kbuffer_read_buffer (kbuffer-parse.c:1030) ==2504915== by 0x40013F7: main (kbuf-repro.c:61)
==2504915== Address 0x4ac3120 is 0 bytes after a block of size 4,096 alloc'd
and the commit field of the copy is 0xffffffffc0000ff0. With the fix it is 0x80000ff0 and valgrind reports no errors.
Patch 1 fixes the check (and the sign extension of the flags into the upper 32 bits of the 8 byte commit field), patch 2 adds unit tests, which fail without patch 1:
Test: kbuffer read buffer with missed events, full ...FAILED
1. traceevent-utest.c:786 - *(uint64_t *)(dst + KBUF_SUBBUF_SIZE) == KBUF_GUARD
2. traceevent-utest.c:791 - !!(commit & KBUF_MISSING_STORED) == expect_stored
3. traceevent-utest.c:793 - (commit & ~(KBUF_COMMIT_MASK | KBUF_MISSING_EVENTS | KBUF_MISSING_STORED)) == 0 Test: kbuffer read buffer with missed events, stored ...FAILED
1. traceevent-utest.c:793 - (commit & ~(KBUF_COMMIT_MASK | KBUF_MISSING_EVENTS | KBUF_MISSING_STORED)) == 0
Tested on amd64 and ppc64el with the reproducer and the unit tests, and on ppc64el with the libtracefs unit tests ("make test_mem" no longer shows the invalid read).
Frank Heimes (2):
kbuffer: Fix out-of-bounds write of missed events in
kbuffer_read_buffer()
libtraceevent utest: Add tests for kbuffer_read_buffer() with missed
events
src/kbuffer-parse.c | 14 ++++-- utest/traceevent-utest.c | 104 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 5 deletions(-)
Signature · 1 line
2.53.0