All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frank Heimes <frank.heimes@canonical.com>
To: linux-trace-devel@vger.kernel.org
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Benjamin Drung <benjamin.drung@canonical.com>,
	Jamie Dolan <jldolan@us.ibm.com>
Subject: [PATCH 0/2] kbuffer: Fix out-of-bounds write in kbuffer_read_buffer()
Date: Wed, 18 Nov 2026 08:40:22 +0000	[thread overview]
Message-ID: <cover.1794988946.git.frank.heimes@canonical.com> (raw)

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


             reply	other threads:[~2026-11-18  8:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:40 Frank Heimes [this message]
2026-11-18  8:40 ` [PATCH 1/2] kbuffer: Fix out-of-bounds write of missed events in kbuffer_read_buffer() Frank Heimes
2026-11-18  8:40 ` [PATCH 2/2] libtraceevent utest: Add tests for kbuffer_read_buffer() with missed events Frank Heimes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cover.1794988946.git.frank.heimes@canonical.com \
    --to=frank.heimes@canonical.com \
    --cc=benjamin.drung@canonical.com \
    --cc=jldolan@us.ibm.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.