All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] kbuffer: Fix out-of-bounds write in kbuffer_read_buffer()
@ 2026-11-18  8:40 Frank Heimes
  2026-11-18  8:40 ` [PATCH 1/2] kbuffer: Fix out-of-bounds write of missed events " Frank Heimes
  2026-11-18  8:40 ` [PATCH 2/2] libtraceevent utest: Add tests for kbuffer_read_buffer() with missed events Frank Heimes
  0 siblings, 2 replies; 3+ messages in thread
From: Frank Heimes @ 2026-11-18  8:40 UTC (permalink / raw)
  To: linux-trace-devel; +Cc: Steven Rostedt, Benjamin Drung, Jamie Dolan

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/39efe87bec205b7f809b8aa49db635b6465ee530.camel@canonical.com/

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(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18  8:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18  8:40 [PATCH 0/2] kbuffer: Fix out-of-bounds write in kbuffer_read_buffer() Frank Heimes
2026-11-18  8:40 ` [PATCH 1/2] kbuffer: Fix out-of-bounds write of missed events " Frank Heimes
2026-11-18  8:40 ` [PATCH 2/2] libtraceevent utest: Add tests for kbuffer_read_buffer() with missed events Frank Heimes

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.