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

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