[PATCH] btt: Fix a potential coredump issue

[email protected] Fri, 21 Mar 2025 03:32:01 +0800
Newsgroups org.kernel.vger.linux-btrace
Message-ID <[email protected]>
From: Kou Wenqi <[email protected]>

Executing "btt -i sdb.blktrace.bin" may cause coredump due to
accessing to a page of the buffer that lies beyond the end of
the mapped file.

-rw-r--r-- 1 root root 177169768  3月 13 19:33 sdb.blktrace.bin

(gdb) b mmap.c:134 if cur >= 177169768
Breakpoint 4, move_map () at mmap.c:88
(gdb) l
83              if (cur_map != MAP_FAILED)
84                      munmap(cur_map, len);
85
86              cur_min = (cur & ~(pgsz-1));
87              len = min_len(DEF_LEN, total_size - cur_min);
88              if (len < sizeof(*next_t))
89                      return 0;
90
91              cur_map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd,
92                             cur_min);

(gdb) p total_size
$1 = 177169768
(gdb) p cur
$2 = 177175095
(gdb) p cur_min
$3 = 177172480
(gdb) p cur_max
$4 = 177169768
(gdb) p len
$5 = 16777216

(gdb) c
Continuing.

Program received signal SIGBUS, Bus error.
next_trace (t=t@entry=0x418e70, pdu=pdu@entry=0x418e18) at mmap.c:141
(gdb) p cur
$11 = 177175095
(gdb) p cur_min
$12 = 177172480
(gdb) p total_size
$13 = 177169768
(gdb) l
136                             cleanup_ifile();
137                             return 0;
138                     }
139
140             next_t = cur_map + (cur - cur_min);
141             this_len = convert_to_cpu(next_t, t, pdu);
142             cur += this_len;
143
144             return 1;
145     }

Signed-off-by: Kou Wenqi <[email protected]>
---
 btt/mmap.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/btt/mmap.c b/btt/mmap.c
index 9d4eb3d..1f9acbc 100644
--- a/btt/mmap.c
+++ b/btt/mmap.c
@@ -84,6 +84,8 @@ static int move_map(void)
 		munmap(cur_map, len);
 
 	cur_min = (cur & ~(pgsz-1));
+	if (total_size < cur_min)
+		return 0;
 	len = min_len(DEF_LEN, total_size - cur_min);
 	if (len < sizeof(*next_t))
 		return 0;
-- 
2.43.0