0022-lvm-Fix-two-more-potential-data-dependent-alloc-over.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. From a1845e90fc19fb5e904091bad8a378f458798e4a Mon Sep 17 00:00:00 2001
  2. From: Peter Jones <pjones@redhat.com>
  3. Date: Sun, 19 Jul 2020 15:48:20 -0400
  4. Subject: [PATCH] lvm: Fix two more potential data-dependent alloc
  5. overflows
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. It appears to be possible to make a (possibly invalid) lvm PV with
  10. a metadata size field that overflows our type when adding it to the
  11. address we've allocated. Even if it doesn't, it may be possible to do so
  12. with the math using the outcome of that as an operand. Check them both.
  13. Signed-off-by: Peter Jones <pjones@redhat.com>
  14. Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
  15. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  16. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  17. ---
  18. grub-core/disk/lvm.c | 48 ++++++++++++++++++++++++++++++++++++--------
  19. 1 file changed, 40 insertions(+), 8 deletions(-)
  20. diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c
  21. index d1df640b3..139fafd47 100644
  22. --- a/grub-core/disk/lvm.c
  23. +++ b/grub-core/disk/lvm.c
  24. @@ -25,6 +25,7 @@
  25. #include <grub/lvm.h>
  26. #include <grub/partition.h>
  27. #include <grub/i18n.h>
  28. +#include <grub/safemath.h>
  29. #ifdef GRUB_UTIL
  30. #include <grub/emu/misc.h>
  31. @@ -102,10 +103,11 @@ grub_lvm_detect (grub_disk_t disk,
  32. {
  33. grub_err_t err;
  34. grub_uint64_t mda_offset, mda_size;
  35. + grub_size_t ptr;
  36. char buf[GRUB_LVM_LABEL_SIZE];
  37. char vg_id[GRUB_LVM_ID_STRLEN+1];
  38. char pv_id[GRUB_LVM_ID_STRLEN+1];
  39. - char *metadatabuf, *p, *q, *vgname;
  40. + char *metadatabuf, *p, *q, *mda_end, *vgname;
  41. struct grub_lvm_label_header *lh = (struct grub_lvm_label_header *) buf;
  42. struct grub_lvm_pv_header *pvh;
  43. struct grub_lvm_disk_locn *dlocn;
  44. @@ -205,19 +207,31 @@ grub_lvm_detect (grub_disk_t disk,
  45. grub_le_to_cpu64 (rlocn->size) -
  46. grub_le_to_cpu64 (mdah->size));
  47. }
  48. - p = q = metadatabuf + grub_le_to_cpu64 (rlocn->offset);
  49. - while (*q != ' ' && q < metadatabuf + mda_size)
  50. - q++;
  51. -
  52. - if (q == metadatabuf + mda_size)
  53. + if (grub_add ((grub_size_t)metadatabuf,
  54. + (grub_size_t)grub_le_to_cpu64 (rlocn->offset),
  55. + &ptr))
  56. {
  57. + error_parsing_metadata:
  58. #ifdef GRUB_UTIL
  59. grub_util_info ("error parsing metadata");
  60. #endif
  61. goto fail2;
  62. }
  63. + p = q = (char *)ptr;
  64. +
  65. + if (grub_add ((grub_size_t)metadatabuf, (grub_size_t)mda_size, &ptr))
  66. + goto error_parsing_metadata;
  67. +
  68. + mda_end = (char *)ptr;
  69. +
  70. + while (*q != ' ' && q < mda_end)
  71. + q++;
  72. +
  73. + if (q == mda_end)
  74. + goto error_parsing_metadata;
  75. +
  76. vgname_len = q - p;
  77. vgname = grub_malloc (vgname_len + 1);
  78. if (!vgname)
  79. @@ -367,8 +381,26 @@ grub_lvm_detect (grub_disk_t disk,
  80. {
  81. const char *iptr;
  82. char *optr;
  83. - lv->fullname = grub_malloc (sizeof ("lvm/") - 1 + 2 * vgname_len
  84. - + 1 + 2 * s + 1);
  85. +
  86. + /*
  87. + * This is kind of hard to read with our safe (but rather
  88. + * baroque) math primatives, but it boils down to:
  89. + *
  90. + * sz0 = vgname_len * 2 + 1 +
  91. + * s * 2 + 1 +
  92. + * sizeof ("lvm/") - 1;
  93. + */
  94. + grub_size_t sz0 = vgname_len, sz1 = s;
  95. +
  96. + if (grub_mul (sz0, 2, &sz0) ||
  97. + grub_add (sz0, 1, &sz0) ||
  98. + grub_mul (sz1, 2, &sz1) ||
  99. + grub_add (sz1, 1, &sz1) ||
  100. + grub_add (sz0, sz1, &sz0) ||
  101. + grub_add (sz0, sizeof ("lvm/") - 1, &sz0))
  102. + goto lvs_fail;
  103. +
  104. + lv->fullname = grub_malloc (sz0);
  105. if (!lv->fullname)
  106. goto lvs_fail;
  107. --
  108. 2.26.2