0001-btt-make-device-devno-use-PATH_MAX-to-avoid-overflow.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. From d61ff409cb4dda31386373d706ea0cfb1aaac5b7 Mon Sep 17 00:00:00 2001
  2. From: Jens Axboe <axboe@kernel.dk>
  3. Date: Wed, 2 May 2018 10:24:17 -0600
  4. Subject: btt: make device/devno use PATH_MAX to avoid overflow
  5. Herbo Zhang reports:
  6. I found a bug in blktrace/btt/devmap.c. The code is just as follows:
  7. https://git.kernel.org/pub/scm/linux/kernel/git/axboe/blktrace.git/tree/btt/devmap.c?id=8349ad2f2d19422a6241f94ea84d696b21de4757
  8. struct devmap {
  9. struct list_head head;
  10. char device[32], devno[32]; // #1
  11. };
  12. LIST_HEAD(all_devmaps);
  13. static int dev_map_add(char *line)
  14. {
  15. struct devmap *dmp;
  16. if (strstr(line, "Device") != NULL)
  17. return 1;
  18. dmp = malloc(sizeof(struct devmap));
  19. if (sscanf(line, "%s %s", dmp->device, dmp->devno) != 2) { //#2
  20. free(dmp);
  21. return 1;
  22. }
  23. list_add_tail(&dmp->head, &all_devmaps);
  24. return 0;
  25. }
  26. int dev_map_read(char *fname)
  27. {
  28. char line[256]; // #3
  29. FILE *fp = my_fopen(fname, "r");
  30. if (!fp) {
  31. perror(fname);
  32. return 1;
  33. }
  34. while (fscanf(fp, "%255[a-zA-Z0-9 :.,/_-]\n", line) == 1) {
  35. if (dev_map_add(line))
  36. break;
  37. }
  38. fclose(fp);
  39. return 0;
  40. }
  41. The line length is 256, but the dmp->device, dmp->devno max length
  42. is only 32. We can put strings longer than 32 into dmp->device and
  43. dmp->devno , and then they will be overflowed.
  44. we can trigger this bug just as follows:
  45. $ python -c "print 'A'*256" > ./test
  46. $ btt -M ./test
  47. *** Error in btt': free(): invalid next size (fast): 0x000055ad7349b250 ***
  48. ======= Backtrace: =========
  49. /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f7f158ce7e5]
  50. /lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7f7f158d6e0a]
  51. /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f7f158da98c]
  52. btt(+0x32e0)[0x55ad7306f2e0]
  53. btt(+0x2c5f)[0x55ad7306ec5f]
  54. btt(+0x251f)[0x55ad7306e51f]
  55. /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f7f15877830]
  56. btt(+0x26b9)[0x55ad7306e6b9]
  57. ======= Memory map: ========
  58. 55ad7306c000-55ad7307f000 r-xp 00000000 08:14 3698139
  59. /usr/bin/btt
  60. 55ad7327e000-55ad7327f000 r--p 00012000 08:14 3698139
  61. /usr/bin/btt
  62. 55ad7327f000-55ad73280000 rw-p 00013000 08:14 3698139
  63. /usr/bin/btt
  64. 55ad73280000-55ad73285000 rw-p 00000000 00:00 0
  65. 55ad7349a000-55ad734bb000 rw-p 00000000 00:00 0
  66. [heap]
  67. 7f7f10000000-7f7f10021000 rw-p 00000000 00:00 0
  68. 7f7f10021000-7f7f14000000 ---p 00000000 00:00 0
  69. 7f7f15640000-7f7f15656000 r-xp 00000000 08:14 14942237
  70. /lib/x86_64-linux-gnu/libgcc_s.so.1
  71. 7f7f15656000-7f7f15855000 ---p 00016000 08:14 14942237
  72. /lib/x86_64-linux-gnu/libgcc_s.so.1
  73. 7f7f15855000-7f7f15856000 r--p 00015000 08:14 14942237
  74. /lib/x86_64-linux-gnu/libgcc_s.so.1
  75. 7f7f15856000-7f7f15857000 rw-p 00016000 08:14 14942237
  76. /lib/x86_64-linux-gnu/libgcc_s.so.1
  77. 7f7f15857000-7f7f15a16000 r-xp 00000000 08:14 14948477
  78. /lib/x86_64-linux-gnu/libc-2.23.so
  79. 7f7f15a16000-7f7f15c16000 ---p 001bf000 08:14 14948477
  80. /lib/x86_64-linux-gnu/libc-2.23.so
  81. 7f7f15c16000-7f7f15c1a000 r--p 001bf000 08:14 14948477
  82. /lib/x86_64-linux-gnu/libc-2.23.so
  83. 7f7f15c1a000-7f7f15c1c000 rw-p 001c3000 08:14 14948477
  84. /lib/x86_64-linux-gnu/libc-2.23.so
  85. 7f7f15c1c000-7f7f15c20000 rw-p 00000000 00:00 0
  86. 7f7f15c20000-7f7f15c46000 r-xp 00000000 08:14 14948478
  87. /lib/x86_64-linux-gnu/ld-2.23.so
  88. 7f7f15e16000-7f7f15e19000 rw-p 00000000 00:00 0
  89. 7f7f15e42000-7f7f15e45000 rw-p 00000000 00:00 0
  90. 7f7f15e45000-7f7f15e46000 r--p 00025000 08:14 14948478
  91. /lib/x86_64-linux-gnu/ld-2.23.so
  92. 7f7f15e46000-7f7f15e47000 rw-p 00026000 08:14 14948478
  93. /lib/x86_64-linux-gnu/ld-2.23.so
  94. 7f7f15e47000-7f7f15e48000 rw-p 00000000 00:00 0
  95. 7ffdebe5c000-7ffdebe7d000 rw-p 00000000 00:00 0
  96. [stack]
  97. 7ffdebebc000-7ffdebebe000 r--p 00000000 00:00 0
  98. [vvar]
  99. 7ffdebebe000-7ffdebec0000 r-xp 00000000 00:00 0
  100. [vdso]
  101. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
  102. [vsyscall]
  103. [1] 6272 abort btt -M test
  104. Signed-off-by: Jens Axboe <axboe@kernel.dk>
  105. [Retrieved from:
  106. https://git.kernel.org/pub/scm/linux/kernel/git/axboe/blktrace.git/commit/?id=d61ff409cb4dda31386373d706ea0cfb1aaac5b7]
  107. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  108. ---
  109. btt/devmap.c | 2 +-
  110. 1 file changed, 1 insertion(+), 1 deletion(-)
  111. diff --git a/btt/devmap.c b/btt/devmap.c
  112. index 0553a9e..5fc1cb2 100644
  113. --- a/btt/devmap.c
  114. +++ b/btt/devmap.c
  115. @@ -23,7 +23,7 @@
  116. struct devmap {
  117. struct list_head head;
  118. - char device[32], devno[32];
  119. + char device[PATH_MAX], devno[PATH_MAX];
  120. };
  121. LIST_HEAD(all_devmaps);
  122. --
  123. cgit 1.2-0.3.lf.el7