Skip to contentSkip to conversation

kbuffer: Fix out-of-bounds write in kbuffer_read_buffer()

PATCH In review Frank Heimes 2 participants 5 messages Nov 18

lore
Share & tools
lore
#0 lore raw

cover 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
#1 lore raw

PATCH 1/2 kbuffer: Fix out-of-bounds write of missed events in kbuffer_read_buffer()

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()")
---
 src/kbuffer-parse.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
src/kbuffer-parse.c +9 -5
index 4a38408..f5d3c38 100644
@@ -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;
 }
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 libtraceevent utest: Add tests for kbuffer_read_buffer() with missed events

In reply to Frank Heimes

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

---
 utest/traceevent-utest.c | 104 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)
utest/traceevent-utest.c +104 -0
index 25dceb5..f181ff9 100644
@@ -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);
 }
Signature · 1 line
2.53.0
#3 lore raw
In reply to Frank Heimes

On Wed, 18 Nov 2026 08:40:22 +0000
Frank Heimes <[email protected]> wrote:

Hi Frank,

Quoted 52 lines

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

Quoted 3 lines
 src/kbuffer-parse.c      |  14 ++++--
 utest/traceevent-utest.c | 104 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+), 5 deletions(-)

Thanks for doing all this. I'm currently way behind in my patch queue on the kernel side so it may be a while before I get to the trace-cmd tooling. I'm just letting you know so you do not think I'm ignoring your patches.

-- Steve

#4 lore raw
Quoted 3 lines · On Wed, 18 Nov 2026 09:40:10 -0500 Steven Rostedt <[email protected]> wrote:

Thanks for doing all this. I'm currently way behind in my patch queue on the kernel side so it may be a while before I get to the trace-cmd tooling. I'm just letting you know so you do not think I'm ignoring your patches.

Thanks Steve for the heads-up.

Frank