* [PATCH 1/2] kbuffer: Fix out-of-bounds write of missed events in kbuffer_read_buffer()
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 ` Frank Heimes
2026-11-18 8:40 ` [PATCH 2/2] libtraceevent utest: Add tests for kbuffer_read_buffer() with missed events Frank Heimes
1 sibling, 0 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 number of missed events after the
copied events, if the buffer has room for it. But the check for the
room is wrong:
if (len + kbuf->start <= word_size + buf_len)
It is true whenever the copied sub-buffer header and events fit into
the buffer, which is always the case at this point. If the events fill
up the buffer (which is common, as callers pass a buffer of the
sub-buffer size), the missed events count is written up to 8 bytes
beyond the end of the buffer, and the MISSING_STORED flag is set,
which makes kbuffer_load_subbuffer() read the count beyond the end of
the buffer as well.
For example, libtracefs tracefs_cpu_read() and tracefs_cpu_write()
use kbuffer_read_buffer() for memory mapped ring buffers with a buffer
of the sub-buffer size (in tracefs_cpu_write() on the stack). Running
the libtracefs unit tests under valgrind shows:
Test: trace cpu pipe ...missed events -1
==6341== Invalid read of size 8
==6341== at 0x48A5F34: ??? (in /usr/lib/x86_64-linux-gnu/libtraceevent.so.1.9.0)
==6341== by 0x48A9177: kbuffer_load_subbuffer (in /usr/lib/x86_64-linux-gnu/libtraceevent.so.1.9.0)
==6341== by 0x400D82C: read_trace_cpu_file (tracefs-utest.c:2015)
[..]
==6341== Address 0x8676a90 is 0 bytes after a block of size 4,096 alloc'd
missed events 796093548
Fix the check to only store the count when the header, the events and
the count fit into the buffer.
In addition, the flags were OR'ed into the int "len". As MISSING_EVENTS
is bit 31, this makes len negative and sets the upper 32 bits of the
8 byte commit field due to the sign extension. Use an unsigned long
long for the commit value instead.
Fixes: b3f5849527ae ("libtraceevent: Save missed events in kbuffer_read_buffer()")
Link: https://bugs.launchpad.net/bugs/2172071
Signed-off-by: Frank Heimes <frank.heimes@canonical.com>
---
src/kbuffer-parse.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/kbuffer-parse.c b/src/kbuffer-parse.c
index 4a38408..f5d3c38 100644
--- a/src/kbuffer-parse.c
+++ b/src/kbuffer-parse.c
@@ -955,6 +955,7 @@ kbuffer_raw_get(struct kbuffer *kbuf, void *subbuf, struct kbuffer_raw_info *inf
*/
int kbuffer_read_buffer(struct kbuffer *kbuf, void *buffer, int len)
{
+ unsigned long long commit;
unsigned long long ts;
unsigned int type_len_ts;
bool do_swap = false;
@@ -1010,6 +1011,8 @@ int kbuffer_read_buffer(struct kbuffer *kbuf, void *buffer, int len)
write_4(do_swap, buffer + kbuf->start, type_len_ts);
+ commit = len;
+
/*
* If reading the first event and there are lost events, add it
* to the buffer.
@@ -1024,7 +1027,8 @@ int kbuffer_read_buffer(struct kbuffer *kbuf, void *buffer, int len)
else
word_size = sizeof(cnt_4);
- if (len + kbuf->start <= word_size + buf_len) {
+ /* Only store the count if there's room for it */
+ if (kbuf->start + len + word_size <= buf_len) {
if (word_size == sizeof(cnt_8)) {
cnt_8 = kbuf->lost_events;
write_8(do_swap, buffer + len + kbuf->start, cnt_8);
@@ -1032,16 +1036,16 @@ int kbuffer_read_buffer(struct kbuffer *kbuf, void *buffer, int len)
cnt_4 = kbuf->lost_events;
write_4(do_swap, buffer + len + kbuf->start, cnt_4);
}
- len |= MISSING_STORED;
+ commit |= MISSING_STORED;
}
- len |= MISSING_EVENTS;
+ commit |= MISSING_EVENTS;
}
/* Update the size */
if (kbuf->read_long == __read_long_8)
- write_8(do_swap, buffer + 8, len);
+ write_8(do_swap, buffer + 8, commit);
else
- write_4(do_swap, buffer + 8, len);
+ write_4(do_swap, buffer + 8, commit);
return last_next;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] libtraceevent utest: Add tests for kbuffer_read_buffer() with missed events
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 ` Frank Heimes
1 sibling, 0 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
Add tests that copy a sub-buffer with missed events with
kbuffer_read_buffer() into a buffer of the sub-buffer size:
- for a full sub-buffer, where there is no room to store the missed
events count, nothing must be written past the end of the buffer
and MISSING_STORED must not be set.
- for a sub-buffer with room left, the missed events count must be
stored and be read back by kbuffer_load_subbuffer().
Signed-off-by: Frank Heimes <frank.heimes@canonical.com>
---
utest/traceevent-utest.c | 104 +++++++++++++++++++++++++++++++++++++++
1 file changed, 104 insertions(+)
diff --git a/utest/traceevent-utest.c b/utest/traceevent-utest.c
index 25dceb5..f181ff9 100644
--- a/utest/traceevent-utest.c
+++ b/utest/traceevent-utest.c
@@ -26,6 +26,7 @@
#include "event-parse.h"
#include "trace-seq.h"
+#include "kbuffer.h"
#define TRACEEVENT_SUITE "traceevent library"
@@ -715,6 +716,105 @@ static void test_btf_read(void)
CU_TEST(strcmp(s->buffer, expected) == 0);
}
+#define KBUF_SUBBUF_SIZE 4096
+#define KBUF_HDR_SIZE 16 /* 8 byte timestamp + 8 byte commit */
+#define KBUF_MISSING_EVENTS (1ULL << 31)
+#define KBUF_MISSING_STORED (1ULL << 30)
+#define KBUF_COMMIT_MASK ((1ULL << 27) - 1)
+#define KBUF_GUARD 0xdeadbeefdeadbeefULL
+
+/*
+ * Create a sub-buffer with @data_size bytes of 8 byte events, and the
+ * missed events flag set. If @lost is non-negative, it is stored after
+ * the events.
+ */
+static void kbuf_fill_subbuf(void *subbuf, unsigned int data_size, long long lost)
+{
+ uint64_t commit = data_size | KBUF_MISSING_EVENTS;
+ uint32_t one = 1;
+ bool big_endian = *(uint8_t *)&one == 0;
+ unsigned int i;
+
+ memset(subbuf, 0, KBUF_SUBBUF_SIZE);
+ *(uint64_t *)subbuf = 1000;
+
+ for (i = 0; i < data_size; i += 8) {
+ /* type_len = 1 (4 bytes of data), time delta = 1 */
+ uint32_t type_len_ts = big_endian ? (1 << 27) | 1 : (1 << 5) | 1;
+ uint32_t payload = i;
+
+ memcpy(subbuf + KBUF_HDR_SIZE + i, &type_len_ts, 4);
+ memcpy(subbuf + KBUF_HDR_SIZE + i + 4, &payload, 4);
+ }
+
+ if (lost >= 0) {
+ *(uint64_t *)(subbuf + KBUF_HDR_SIZE + data_size) = lost;
+ commit |= KBUF_MISSING_STORED;
+ }
+ *(uint64_t *)(subbuf + 8) = commit;
+}
+
+static void test_kbuffer_read_buffer_missed(unsigned int data_size, long long lost,
+ bool expect_stored)
+{
+ struct kbuffer *kbuf;
+ uint64_t commit;
+ void *src, *dst;
+ int ret;
+
+ kbuf = kbuffer_alloc(KBUFFER_LSIZE_8, KBUFFER_ENDIAN_SAME_AS_HOST);
+ CU_TEST(kbuf != NULL);
+ src = malloc(KBUF_SUBBUF_SIZE);
+ /* Add a guard after the sub-buffer to detect writes beyond it */
+ dst = malloc(KBUF_SUBBUF_SIZE + sizeof(uint64_t));
+ CU_TEST(src != NULL && dst != NULL);
+ if (!kbuf || !src || !dst)
+ goto out;
+
+ kbuf_fill_subbuf(src, data_size, lost);
+ memset(dst, 0, KBUF_SUBBUF_SIZE);
+ *(uint64_t *)(dst + KBUF_SUBBUF_SIZE) = KBUF_GUARD;
+
+ ret = kbuffer_load_subbuffer(kbuf, src);
+ CU_TEST(ret == 0);
+ CU_TEST(kbuffer_missed_events(kbuf) == (lost >= 0 ? lost : -1));
+
+ ret = kbuffer_read_buffer(kbuf, dst, KBUF_SUBBUF_SIZE);
+ CU_TEST(ret == (int)data_size);
+
+ /* Nothing may be written past the end of the buffer */
+ CU_TEST(*(uint64_t *)(dst + KBUF_SUBBUF_SIZE) == KBUF_GUARD);
+
+ commit = *(uint64_t *)(dst + 8);
+ CU_TEST((commit & KBUF_COMMIT_MASK) == data_size);
+ CU_TEST((commit & KBUF_MISSING_EVENTS) != 0);
+ CU_TEST(!!(commit & KBUF_MISSING_STORED) == expect_stored);
+ /* Only the commit size and flags may be set */
+ CU_TEST((commit & ~(KBUF_COMMIT_MASK | KBUF_MISSING_EVENTS |
+ KBUF_MISSING_STORED)) == 0);
+
+ /* The copy must be loadable and keep the missed events */
+ ret = kbuffer_load_subbuffer(kbuf, dst);
+ CU_TEST(ret == 0);
+ CU_TEST(kbuffer_missed_events(kbuf) == (expect_stored ? lost : -1));
+ out:
+ kbuffer_free(kbuf);
+ free(src);
+ free(dst);
+}
+
+static void test_kbuffer_read_buffer_missed_full(void)
+{
+ /* A full sub-buffer has no room to store the missed events count */
+ test_kbuffer_read_buffer_missed(KBUF_SUBBUF_SIZE - KBUF_HDR_SIZE, -1, false);
+}
+
+static void test_kbuffer_read_buffer_missed_stored(void)
+{
+ /* With enough room, the missed events count is stored */
+ test_kbuffer_read_buffer_missed(KBUF_SUBBUF_SIZE - KBUF_HDR_SIZE - 80, 42, true);
+}
+
static int test_suite_destroy(void)
{
tep_free(test_tep);
@@ -784,4 +884,8 @@ void test_traceevent_lib(void)
test_parse_dma_addr);
CU_add_test(suite, "read BTF",
test_btf_read);
+ CU_add_test(suite, "kbuffer read buffer with missed events, full",
+ test_kbuffer_read_buffer_missed_full);
+ CU_add_test(suite, "kbuffer read buffer with missed events, stored",
+ test_kbuffer_read_buffer_missed_stored);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread