0024-efi-Fix-some-malformed-device-path-arithmetic-errors.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. From c330aa099a38bc5c4d3066954fe35767cc06adb1 Mon Sep 17 00:00:00 2001
  2. From: Peter Jones <pjones@redhat.com>
  3. Date: Sun, 19 Jul 2020 16:53:27 -0400
  4. Subject: [PATCH] efi: Fix some malformed device path arithmetic errors
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Several places we take the length of a device path and subtract 4 from
  9. it, without ever checking that it's >= 4. There are also cases where
  10. this kind of malformation will result in unpredictable iteration,
  11. including treating the length from one dp node as the type in the next
  12. node. These are all errors, no matter where the data comes from.
  13. This patch adds a checking macro, GRUB_EFI_DEVICE_PATH_VALID(), which
  14. can be used in several places, and makes GRUB_EFI_NEXT_DEVICE_PATH()
  15. return NULL and GRUB_EFI_END_ENTIRE_DEVICE_PATH() evaluate as true when
  16. the length is too small. Additionally, it makes several places in the
  17. code check for and return errors in these cases.
  18. Signed-off-by: Peter Jones <pjones@redhat.com>
  19. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  20. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  21. ---
  22. grub-core/kern/efi/efi.c | 64 +++++++++++++++++++++++++-----
  23. grub-core/loader/efi/chainloader.c | 13 +++++-
  24. grub-core/loader/i386/xnu.c | 9 +++--
  25. include/grub/efi/api.h | 14 ++++---
  26. 4 files changed, 79 insertions(+), 21 deletions(-)
  27. diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
  28. index dc31caa21..c97969a65 100644
  29. --- a/grub-core/kern/efi/efi.c
  30. +++ b/grub-core/kern/efi/efi.c
  31. @@ -332,7 +332,7 @@ grub_efi_get_filename (grub_efi_device_path_t *dp0)
  32. dp = dp0;
  33. - while (1)
  34. + while (dp)
  35. {
  36. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  37. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  38. @@ -342,9 +342,15 @@ grub_efi_get_filename (grub_efi_device_path_t *dp0)
  39. if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
  40. && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
  41. {
  42. - grub_efi_uint16_t len;
  43. - len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
  44. - / sizeof (grub_efi_char16_t));
  45. + grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
  46. +
  47. + if (len < 4)
  48. + {
  49. + grub_error (GRUB_ERR_OUT_OF_RANGE,
  50. + "malformed EFI Device Path node has length=%d", len);
  51. + return NULL;
  52. + }
  53. + len = (len - 4) / sizeof (grub_efi_char16_t);
  54. filesize += GRUB_MAX_UTF8_PER_UTF16 * len + 2;
  55. }
  56. @@ -360,7 +366,7 @@ grub_efi_get_filename (grub_efi_device_path_t *dp0)
  57. if (!name)
  58. return NULL;
  59. - while (1)
  60. + while (dp)
  61. {
  62. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  63. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  64. @@ -376,8 +382,15 @@ grub_efi_get_filename (grub_efi_device_path_t *dp0)
  65. *p++ = '/';
  66. - len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
  67. - / sizeof (grub_efi_char16_t));
  68. + len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
  69. + if (len < 4)
  70. + {
  71. + grub_error (GRUB_ERR_OUT_OF_RANGE,
  72. + "malformed EFI Device Path node has length=%d", len);
  73. + return NULL;
  74. + }
  75. +
  76. + len = (len - 4) / sizeof (grub_efi_char16_t);
  77. fp = (grub_efi_file_path_device_path_t *) dp;
  78. /* According to EFI spec Path Name is NULL terminated */
  79. while (len > 0 && fp->path_name[len - 1] == 0)
  80. @@ -452,7 +465,26 @@ grub_efi_duplicate_device_path (const grub_efi_device_path_t *dp)
  81. ;
  82. p = GRUB_EFI_NEXT_DEVICE_PATH (p))
  83. {
  84. - total_size += GRUB_EFI_DEVICE_PATH_LENGTH (p);
  85. + grub_size_t len = GRUB_EFI_DEVICE_PATH_LENGTH (p);
  86. +
  87. + /*
  88. + * In the event that we find a node that's completely garbage, for
  89. + * example if we get to 0x7f 0x01 0x02 0x00 ... (EndInstance with a size
  90. + * of 2), GRUB_EFI_END_ENTIRE_DEVICE_PATH() will be true and
  91. + * GRUB_EFI_NEXT_DEVICE_PATH() will return NULL, so we won't continue,
  92. + * and neither should our consumers, but there won't be any error raised
  93. + * even though the device path is junk.
  94. + *
  95. + * This keeps us from passing junk down back to our caller.
  96. + */
  97. + if (len < 4)
  98. + {
  99. + grub_error (GRUB_ERR_OUT_OF_RANGE,
  100. + "malformed EFI Device Path node has length=%d", len);
  101. + return NULL;
  102. + }
  103. +
  104. + total_size += len;
  105. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
  106. break;
  107. }
  108. @@ -497,7 +529,7 @@ dump_vendor_path (const char *type, grub_efi_vendor_device_path_t *vendor)
  109. void
  110. grub_efi_print_device_path (grub_efi_device_path_t *dp)
  111. {
  112. - while (1)
  113. + while (GRUB_EFI_DEVICE_PATH_VALID (dp))
  114. {
  115. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  116. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  117. @@ -909,7 +941,10 @@ grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
  118. /* Return non-zero. */
  119. return 1;
  120. - while (1)
  121. + if (dp1 == dp2)
  122. + return 0;
  123. +
  124. + while (GRUB_EFI_DEVICE_PATH_VALID (dp1) && GRUB_EFI_DEVICE_PATH_VALID (dp2))
  125. {
  126. grub_efi_uint8_t type1, type2;
  127. grub_efi_uint8_t subtype1, subtype2;
  128. @@ -945,5 +980,14 @@ grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
  129. dp2 = (grub_efi_device_path_t *) ((char *) dp2 + len2);
  130. }
  131. + /*
  132. + * There's no "right" answer here, but we probably don't want to call a valid
  133. + * dp and an invalid dp equal, so pick one way or the other.
  134. + */
  135. + if (GRUB_EFI_DEVICE_PATH_VALID (dp1) && !GRUB_EFI_DEVICE_PATH_VALID (dp2))
  136. + return 1;
  137. + else if (!GRUB_EFI_DEVICE_PATH_VALID (dp1) && GRUB_EFI_DEVICE_PATH_VALID (dp2))
  138. + return -1;
  139. +
  140. return 0;
  141. }
  142. diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
  143. index daf8c6b54..a8d7b9155 100644
  144. --- a/grub-core/loader/efi/chainloader.c
  145. +++ b/grub-core/loader/efi/chainloader.c
  146. @@ -156,9 +156,18 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
  147. size = 0;
  148. d = dp;
  149. - while (1)
  150. + while (d)
  151. {
  152. - size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
  153. + grub_size_t len = GRUB_EFI_DEVICE_PATH_LENGTH (d);
  154. +
  155. + if (len < 4)
  156. + {
  157. + grub_error (GRUB_ERR_OUT_OF_RANGE,
  158. + "malformed EFI Device Path node has length=%d", len);
  159. + return NULL;
  160. + }
  161. +
  162. + size += len;
  163. if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
  164. break;
  165. d = GRUB_EFI_NEXT_DEVICE_PATH (d);
  166. diff --git a/grub-core/loader/i386/xnu.c b/grub-core/loader/i386/xnu.c
  167. index e9e119259..a70093607 100644
  168. --- a/grub-core/loader/i386/xnu.c
  169. +++ b/grub-core/loader/i386/xnu.c
  170. @@ -515,14 +515,15 @@ grub_cmd_devprop_load (grub_command_t cmd __attribute__ ((unused)),
  171. devhead = buf;
  172. buf = devhead + 1;
  173. - dpstart = buf;
  174. + dp = dpstart = buf;
  175. - do
  176. + while (GRUB_EFI_DEVICE_PATH_VALID (dp) && buf < bufend)
  177. {
  178. - dp = buf;
  179. buf = (char *) buf + GRUB_EFI_DEVICE_PATH_LENGTH (dp);
  180. + if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  181. + break;
  182. + dp = buf;
  183. }
  184. - while (!GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp) && buf < bufend);
  185. dev = grub_xnu_devprop_add_device (dpstart, (char *) buf
  186. - (char *) dpstart);
  187. diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
  188. index addcbfa8f..cf1355a8c 100644
  189. --- a/include/grub/efi/api.h
  190. +++ b/include/grub/efi/api.h
  191. @@ -625,6 +625,7 @@ typedef struct grub_efi_device_path grub_efi_device_path_protocol_t;
  192. #define GRUB_EFI_DEVICE_PATH_TYPE(dp) ((dp)->type & 0x7f)
  193. #define GRUB_EFI_DEVICE_PATH_SUBTYPE(dp) ((dp)->subtype)
  194. #define GRUB_EFI_DEVICE_PATH_LENGTH(dp) ((dp)->length)
  195. +#define GRUB_EFI_DEVICE_PATH_VALID(dp) ((dp) != NULL && GRUB_EFI_DEVICE_PATH_LENGTH (dp) >= 4)
  196. /* The End of Device Path nodes. */
  197. #define GRUB_EFI_END_DEVICE_PATH_TYPE (0xff & 0x7f)
  198. @@ -633,13 +634,16 @@ typedef struct grub_efi_device_path grub_efi_device_path_protocol_t;
  199. #define GRUB_EFI_END_THIS_DEVICE_PATH_SUBTYPE 0x01
  200. #define GRUB_EFI_END_ENTIRE_DEVICE_PATH(dp) \
  201. - (GRUB_EFI_DEVICE_PATH_TYPE (dp) == GRUB_EFI_END_DEVICE_PATH_TYPE \
  202. - && (GRUB_EFI_DEVICE_PATH_SUBTYPE (dp) \
  203. - == GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE))
  204. + (!GRUB_EFI_DEVICE_PATH_VALID (dp) || \
  205. + (GRUB_EFI_DEVICE_PATH_TYPE (dp) == GRUB_EFI_END_DEVICE_PATH_TYPE \
  206. + && (GRUB_EFI_DEVICE_PATH_SUBTYPE (dp) \
  207. + == GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE)))
  208. #define GRUB_EFI_NEXT_DEVICE_PATH(dp) \
  209. - ((grub_efi_device_path_t *) ((char *) (dp) \
  210. - + GRUB_EFI_DEVICE_PATH_LENGTH (dp)))
  211. + (GRUB_EFI_DEVICE_PATH_VALID (dp) \
  212. + ? ((grub_efi_device_path_t *) \
  213. + ((char *) (dp) + GRUB_EFI_DEVICE_PATH_LENGTH (dp))) \
  214. + : NULL)
  215. /* Hardware Device Path. */
  216. #define GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE 1
  217. --
  218. 2.26.2