Blame SOURCES/blktrace-fix-btt-overflow.patch

f31040
From d61ff409cb4dda31386373d706ea0cfb1aaac5b7 Mon Sep 17 00:00:00 2001
f31040
From: Jens Axboe <axboe@kernel.dk>
f31040
Date: Wed, 2 May 2018 10:24:17 -0600
f31040
Subject: btt: make device/devno use PATH_MAX to avoid overflow
f31040
f31040
Herbo Zhang reports:
f31040
f31040
I found a bug in blktrace/btt/devmap.c. The code is just as follows:
f31040
f31040
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/blktrace.git/tree/btt/devmap.c?id=8349ad2f2d19422a6241f94ea84d696b21de4757
f31040
f31040
       struct devmap {
f31040
f31040
struct list_head head;
f31040
char device[32], devno[32];    // #1
f31040
};
f31040
f31040
LIST_HEAD(all_devmaps);
f31040
f31040
static int dev_map_add(char *line)
f31040
{
f31040
struct devmap *dmp;
f31040
f31040
if (strstr(line, "Device") != NULL)
f31040
return 1;
f31040
f31040
dmp = malloc(sizeof(struct devmap));
f31040
if (sscanf(line, "%s %s", dmp->device, dmp->devno) != 2) {  //#2
f31040
free(dmp);
f31040
return 1;
f31040
}
f31040
f31040
list_add_tail(&dmp->head, &all_devmaps);
f31040
return 0;
f31040
}
f31040
f31040
int dev_map_read(char *fname)
f31040
{
f31040
char line[256];   // #3
f31040
FILE *fp = my_fopen(fname, "r");
f31040
f31040
if (!fp) {
f31040
perror(fname);
f31040
return 1;
f31040
}
f31040
f31040
while (fscanf(fp, "%255[a-zA-Z0-9 :.,/_-]\n", line) == 1) {
f31040
if (dev_map_add(line))
f31040
break;
f31040
}
f31040
f31040
fclose(fp);
f31040
return 0;
f31040
}
f31040
f31040
 The line length is 256, but the dmp->device, dmp->devno  max length
f31040
is only 32. We can put strings longer than 32 into dmp->device and
f31040
dmp->devno , and then they will be overflowed.
f31040
f31040
 we can trigger this bug just as follows:
f31040
f31040
 $ python -c "print 'A'*256" > ./test
f31040
    $ btt -M ./test
f31040
f31040
    *** Error in btt': free(): invalid next size (fast): 0x000055ad7349b250 ***
f31040
    ======= Backtrace: =========
f31040
    /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f7f158ce7e5]
f31040
    /lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7f7f158d6e0a]
f31040
    /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f7f158da98c]
f31040
    btt(+0x32e0)[0x55ad7306f2e0]
f31040
    btt(+0x2c5f)[0x55ad7306ec5f]
f31040
    btt(+0x251f)[0x55ad7306e51f]
f31040
    /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f7f15877830]
f31040
    btt(+0x26b9)[0x55ad7306e6b9]
f31040
    ======= Memory map: ========
f31040
    55ad7306c000-55ad7307f000 r-xp 00000000 08:14 3698139
f31040
      /usr/bin/btt
f31040
    55ad7327e000-55ad7327f000 r--p 00012000 08:14 3698139
f31040
      /usr/bin/btt
f31040
    55ad7327f000-55ad73280000 rw-p 00013000 08:14 3698139
f31040
      /usr/bin/btt
f31040
    55ad73280000-55ad73285000 rw-p 00000000 00:00 0
f31040
    55ad7349a000-55ad734bb000 rw-p 00000000 00:00 0
f31040
      [heap]
f31040
    7f7f10000000-7f7f10021000 rw-p 00000000 00:00 0
f31040
    7f7f10021000-7f7f14000000 ---p 00000000 00:00 0
f31040
    7f7f15640000-7f7f15656000 r-xp 00000000 08:14 14942237
f31040
      /lib/x86_64-linux-gnu/libgcc_s.so.1
f31040
    7f7f15656000-7f7f15855000 ---p 00016000 08:14 14942237
f31040
      /lib/x86_64-linux-gnu/libgcc_s.so.1
f31040
    7f7f15855000-7f7f15856000 r--p 00015000 08:14 14942237
f31040
      /lib/x86_64-linux-gnu/libgcc_s.so.1
f31040
    7f7f15856000-7f7f15857000 rw-p 00016000 08:14 14942237
f31040
      /lib/x86_64-linux-gnu/libgcc_s.so.1
f31040
    7f7f15857000-7f7f15a16000 r-xp 00000000 08:14 14948477
f31040
      /lib/x86_64-linux-gnu/libc-2.23.so
f31040
    7f7f15a16000-7f7f15c16000 ---p 001bf000 08:14 14948477
f31040
      /lib/x86_64-linux-gnu/libc-2.23.so
f31040
    7f7f15c16000-7f7f15c1a000 r--p 001bf000 08:14 14948477
f31040
      /lib/x86_64-linux-gnu/libc-2.23.so
f31040
    7f7f15c1a000-7f7f15c1c000 rw-p 001c3000 08:14 14948477
f31040
      /lib/x86_64-linux-gnu/libc-2.23.so
f31040
    7f7f15c1c000-7f7f15c20000 rw-p 00000000 00:00 0
f31040
    7f7f15c20000-7f7f15c46000 r-xp 00000000 08:14 14948478
f31040
      /lib/x86_64-linux-gnu/ld-2.23.so
f31040
    7f7f15e16000-7f7f15e19000 rw-p 00000000 00:00 0
f31040
    7f7f15e42000-7f7f15e45000 rw-p 00000000 00:00 0
f31040
    7f7f15e45000-7f7f15e46000 r--p 00025000 08:14 14948478
f31040
      /lib/x86_64-linux-gnu/ld-2.23.so
f31040
    7f7f15e46000-7f7f15e47000 rw-p 00026000 08:14 14948478
f31040
      /lib/x86_64-linux-gnu/ld-2.23.so
f31040
    7f7f15e47000-7f7f15e48000 rw-p 00000000 00:00 0
f31040
    7ffdebe5c000-7ffdebe7d000 rw-p 00000000 00:00 0
f31040
      [stack]
f31040
    7ffdebebc000-7ffdebebe000 r--p 00000000 00:00 0
f31040
      [vvar]
f31040
    7ffdebebe000-7ffdebec0000 r-xp 00000000 00:00 0
f31040
      [vdso]
f31040
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
f31040
      [vsyscall]
f31040
    [1]    6272 abort      btt -M test
f31040
f31040
Signed-off-by: Jens Axboe <axboe@kernel.dk>
f31040
---
f31040
 btt/devmap.c | 2 +-
f31040
 1 file changed, 1 insertion(+), 1 deletion(-)
f31040
f31040
diff --git a/btt/devmap.c b/btt/devmap.c
f31040
index 0553a9e..5fc1cb2 100644
f31040
--- a/btt/devmap.c
f31040
+++ b/btt/devmap.c
f31040
@@ -23,7 +23,7 @@
f31040
 
f31040
 struct devmap {
f31040
 	struct list_head head;
f31040
-	char device[32], devno[32];
f31040
+	char device[PATH_MAX], devno[PATH_MAX];
f31040
 };
f31040
 
f31040
 LIST_HEAD(all_devmaps);
f31040
-- 
f31040
cgit v1.1
f31040
f31040