2
1

0045-disk-Use-safe-math-macros-to-prevent-overflows.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. From b6bdea00ea1a3d6b0b7551133279cbc7ff23bdf6 Mon Sep 17 00:00:00 2001
  2. From: Alec Brown <alec.r.brown@oracle.com>
  3. Date: Wed, 22 Jan 2025 02:55:09 +0000
  4. Subject: [PATCH] disk: Use safe math macros to prevent overflows
  5. Replace direct arithmetic operations with macros from include/grub/safemath.h
  6. to prevent potential overflow issues when calculating the memory sizes.
  7. Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
  8. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  9. Conflicts:
  10. grub-core/disk/cryptodisk.c
  11. Upstream: c407724dad6c3e2fc1571e57adbda71cc03f82aa
  12. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  13. ---
  14. grub-core/disk/cryptodisk.c | 36 ++++++++++++++-----
  15. grub-core/disk/diskfilter.c | 9 +++--
  16. grub-core/disk/ieee1275/obdisk.c | 43 +++++++++++++++++++----
  17. grub-core/disk/ieee1275/ofdisk.c | 59 +++++++++++++++++++++++++++-----
  18. grub-core/disk/ldm.c | 36 ++++++++++++++++---
  19. grub-core/disk/luks2.c | 7 +++-
  20. grub-core/disk/memdisk.c | 7 +++-
  21. grub-core/disk/plainmount.c | 9 +++--
  22. 8 files changed, 172 insertions(+), 34 deletions(-)
  23. diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
  24. index 2246af51b..6f7445665 100644
  25. --- a/grub-core/disk/cryptodisk.c
  26. +++ b/grub-core/disk/cryptodisk.c
  27. @@ -26,6 +26,7 @@
  28. #include <grub/file.h>
  29. #include <grub/procfs.h>
  30. #include <grub/partition.h>
  31. +#include <grub/safemath.h>
  32. #ifdef GRUB_UTIL
  33. #include <grub/emu/hostdisk.h>
  34. @@ -1473,7 +1474,7 @@ static char *
  35. luks_script_get (grub_size_t *sz)
  36. {
  37. grub_cryptodisk_t i;
  38. - grub_size_t size = 0;
  39. + grub_size_t size = 0, mul;
  40. char *ptr, *ret;
  41. *sz = 0;
  42. @@ -1482,10 +1483,6 @@ luks_script_get (grub_size_t *sz)
  43. if (grub_strcmp (i->modname, "luks") == 0 ||
  44. grub_strcmp (i->modname, "luks2") == 0)
  45. {
  46. - size += grub_strlen (i->modname);
  47. - size += sizeof ("_mount");
  48. - size += grub_strlen (i->uuid);
  49. - size += grub_strlen (i->cipher->cipher->name);
  50. /*
  51. * Add space in the line for (in order) spaces, cipher mode, cipher IV
  52. * mode, sector offset, sector size and the trailing newline. This is
  53. @@ -1493,14 +1490,35 @@ luks_script_get (grub_size_t *sz)
  54. * in an earlier version of this code that are unaccounted for. It is
  55. * left in the calculations in case it is needed. At worst, its short-
  56. * lived wasted space.
  57. + *
  58. + * 60 = 5 + 5 + 8 + 20 + 6 + 1 + 15
  59. */
  60. - size += 5 + 5 + 8 + 20 + 6 + 1 + 15;
  61. + if (grub_add (size, grub_strlen (i->modname), &size) ||
  62. + grub_add (size, sizeof ("_mount") + 60, &size) ||
  63. + grub_add (size, grub_strlen (i->uuid), &size) ||
  64. + grub_add (size, grub_strlen (i->cipher->cipher->name), &size) ||
  65. + grub_mul (i->keysize, 2, &mul) ||
  66. + grub_add (size, mul, &size))
  67. + {
  68. + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script");
  69. + return 0;
  70. + }
  71. if (i->essiv_hash)
  72. - size += grub_strlen (i->essiv_hash->name);
  73. - size += i->keysize * 2;
  74. + {
  75. + if (grub_add (size, grub_strlen (i->essiv_hash->name), &size))
  76. + {
  77. + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script");
  78. + return 0;
  79. + }
  80. + }
  81. }
  82. + if (grub_add (size, 1, &size))
  83. + {
  84. + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script");
  85. + return 0;
  86. + }
  87. - ret = grub_malloc (size + 1);
  88. + ret = grub_malloc (size);
  89. if (!ret)
  90. return 0;
  91. diff --git a/grub-core/disk/diskfilter.c b/grub-core/disk/diskfilter.c
  92. index 21e239511..de5a564d4 100644
  93. --- a/grub-core/disk/diskfilter.c
  94. +++ b/grub-core/disk/diskfilter.c
  95. @@ -24,6 +24,7 @@
  96. #include <grub/misc.h>
  97. #include <grub/diskfilter.h>
  98. #include <grub/partition.h>
  99. +#include <grub/safemath.h>
  100. #ifdef GRUB_UTIL
  101. #include <grub/i18n.h>
  102. #include <grub/util/misc.h>
  103. @@ -1039,7 +1040,7 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
  104. {
  105. struct grub_diskfilter_vg *array;
  106. int i;
  107. - grub_size_t j;
  108. + grub_size_t j, sz;
  109. grub_uint64_t totsize;
  110. struct grub_diskfilter_pv *pv;
  111. grub_err_t err;
  112. @@ -1140,7 +1141,11 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
  113. }
  114. array->lvs->vg = array;
  115. - array->lvs->idname = grub_malloc (sizeof ("mduuid/") + 2 * uuidlen);
  116. + if (grub_mul (uuidlen, 2, &sz) ||
  117. + grub_add (sz, sizeof ("mduuid/"), &sz))
  118. + goto fail;
  119. +
  120. + array->lvs->idname = grub_malloc (sz);
  121. if (!array->lvs->idname)
  122. goto fail;
  123. diff --git a/grub-core/disk/ieee1275/obdisk.c b/grub-core/disk/ieee1275/obdisk.c
  124. index cd923b90f..9d4c42665 100644
  125. --- a/grub-core/disk/ieee1275/obdisk.c
  126. +++ b/grub-core/disk/ieee1275/obdisk.c
  127. @@ -26,6 +26,7 @@
  128. #include <grub/mm.h>
  129. #include <grub/scsicmd.h>
  130. #include <grub/time.h>
  131. +#include <grub/safemath.h>
  132. #include <grub/ieee1275/ieee1275.h>
  133. #include <grub/ieee1275/obdisk.h>
  134. @@ -128,9 +129,17 @@ count_commas (const char *src)
  135. static char *
  136. decode_grub_devname (const char *name)
  137. {
  138. - char *devpath = grub_malloc (grub_strlen (name) + 1);
  139. + char *devpath;
  140. char *p, c;
  141. + grub_size_t sz;
  142. + if (grub_add (grub_strlen (name), 1, &sz))
  143. + {
  144. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device name"));
  145. + return NULL;
  146. + }
  147. +
  148. + devpath = grub_malloc (sz);
  149. if (devpath == NULL)
  150. return NULL;
  151. @@ -156,12 +165,20 @@ static char *
  152. encode_grub_devname (const char *path)
  153. {
  154. char *encoding, *optr;
  155. + grub_size_t sz;
  156. if (path == NULL)
  157. return NULL;
  158. - encoding = grub_malloc (sizeof (IEEE1275_DEV) + count_commas (path) +
  159. - grub_strlen (path) + 1);
  160. + if (grub_add (sizeof (IEEE1275_DEV) + 1, count_commas (path), &sz) ||
  161. + grub_add (sz, grub_strlen (path), &sz))
  162. + {
  163. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining encoding size"));
  164. + grub_print_error ();
  165. + return NULL;
  166. + }
  167. +
  168. + encoding = grub_malloc (sz);
  169. if (encoding == NULL)
  170. {
  171. @@ -396,6 +413,14 @@ canonicalise_disk (const char *devname)
  172. real_unit_str_len = grub_strlen (op->name) + sizeof (IEEE1275_DISK_ALIAS)
  173. + grub_strlen (real_unit_address);
  174. + if (grub_add (grub_strlen (op->name), sizeof (IEEE1275_DISK_ALIAS), &real_unit_str_len) ||
  175. + grub_add (real_unit_str_len, grub_strlen (real_unit_address), &real_unit_str_len))
  176. + {
  177. + grub_free (parent);
  178. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of canonical name"));
  179. + grub_print_error ();
  180. + return NULL;
  181. + }
  182. real_canon = grub_malloc (real_unit_str_len);
  183. @@ -413,6 +438,7 @@ canonicalise_disk (const char *devname)
  184. static struct disk_dev *
  185. add_canon_disk (const char *cname)
  186. {
  187. + grub_size_t sz;
  188. struct disk_dev *dev;
  189. dev = grub_zalloc (sizeof (struct disk_dev));
  190. @@ -428,13 +454,18 @@ add_canon_disk (const char *cname)
  191. * arguments and allows a client program to open
  192. * the entire (raw) disk. Any disk label is ignored.
  193. */
  194. - dev->raw_name = grub_malloc (grub_strlen (cname) + sizeof (":nolabel"));
  195. + if (grub_add (grub_strlen (cname), sizeof (":nolabel"), &sz))
  196. + {
  197. + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while appending :nolabel to end of canonical name");
  198. + goto failed;
  199. + }
  200. +
  201. + dev->raw_name = grub_malloc (sz);
  202. if (dev->raw_name == NULL)
  203. goto failed;
  204. - grub_snprintf (dev->raw_name, grub_strlen (cname) + sizeof (":nolabel"),
  205. - "%s:nolabel", cname);
  206. + grub_snprintf (dev->raw_name, sz, "%s:nolabel", cname);
  207. }
  208. /*
  209. diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
  210. index c6cba0c8a..4c5b89cbc 100644
  211. --- a/grub-core/disk/ieee1275/ofdisk.c
  212. +++ b/grub-core/disk/ieee1275/ofdisk.c
  213. @@ -24,6 +24,7 @@
  214. #include <grub/ieee1275/ofdisk.h>
  215. #include <grub/i18n.h>
  216. #include <grub/time.h>
  217. +#include <grub/safemath.h>
  218. static char *last_devpath;
  219. static grub_ieee1275_ihandle_t last_ihandle;
  220. @@ -80,6 +81,7 @@ ofdisk_hash_add_real (char *devpath)
  221. struct ofdisk_hash_ent **head = &ofdisk_hash[ofdisk_hash_fn(devpath)];
  222. const char *iptr;
  223. char *optr;
  224. + grub_size_t sz;
  225. p = grub_zalloc (sizeof (*p));
  226. if (!p)
  227. @@ -87,8 +89,14 @@ ofdisk_hash_add_real (char *devpath)
  228. p->devpath = devpath;
  229. - p->grub_devpath = grub_malloc (sizeof ("ieee1275/")
  230. - + 2 * grub_strlen (p->devpath));
  231. + if (grub_mul (grub_strlen (p->devpath), 2, &sz) ||
  232. + grub_add (sz, sizeof ("ieee1275/"), &sz))
  233. + {
  234. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device path"));
  235. + return NULL;
  236. + }
  237. +
  238. + p->grub_devpath = grub_malloc (sz);
  239. if (!p->grub_devpath)
  240. {
  241. @@ -98,7 +106,13 @@ ofdisk_hash_add_real (char *devpath)
  242. if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0))
  243. {
  244. - p->open_path = grub_malloc (grub_strlen (p->devpath) + 3);
  245. + if (grub_add (grub_strlen (p->devpath), 3, &sz))
  246. + {
  247. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of an open path"));
  248. + return NULL;
  249. + }
  250. +
  251. + p->open_path = grub_malloc (sz);
  252. if (!p->open_path)
  253. {
  254. grub_free (p->grub_devpath);
  255. @@ -224,6 +238,7 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
  256. args;
  257. char *buf, *bufptr;
  258. unsigned i;
  259. + grub_size_t sz;
  260. if (grub_ieee1275_open (alias->path, &ihandle))
  261. return;
  262. @@ -243,7 +258,14 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
  263. return;
  264. }
  265. - buf = grub_malloc (grub_strlen (alias->path) + 32);
  266. + if (grub_add (grub_strlen (alias->path), 32, &sz))
  267. + {
  268. + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while creating buffer for vscsi");
  269. + grub_ieee1275_close (ihandle);
  270. + return;
  271. + }
  272. +
  273. + buf = grub_malloc (sz);
  274. if (!buf)
  275. return;
  276. bufptr = grub_stpcpy (buf, alias->path);
  277. @@ -287,9 +309,15 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
  278. grub_uint64_t *table;
  279. grub_uint16_t table_size;
  280. grub_ieee1275_ihandle_t ihandle;
  281. + grub_size_t sz;
  282. - buf = grub_malloc (grub_strlen (alias->path) +
  283. - sizeof ("/disk@7766554433221100"));
  284. + if (grub_add (grub_strlen (alias->path), sizeof ("/disk@7766554433221100"), &sz))
  285. + {
  286. + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while creating buffer for sas_ioa");
  287. + return;
  288. + }
  289. +
  290. + buf = grub_malloc (sz);
  291. if (!buf)
  292. return;
  293. bufptr = grub_stpcpy (buf, alias->path);
  294. @@ -427,9 +455,17 @@ grub_ofdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  295. static char *
  296. compute_dev_path (const char *name)
  297. {
  298. - char *devpath = grub_malloc (grub_strlen (name) + 3);
  299. + char *devpath;
  300. char *p, c;
  301. + grub_size_t sz;
  302. + if (grub_add (grub_strlen (name), 3, &sz))
  303. + {
  304. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device path"));
  305. + return NULL;
  306. + }
  307. +
  308. + devpath = grub_malloc (sz);
  309. if (!devpath)
  310. return NULL;
  311. @@ -625,6 +661,7 @@ insert_bootpath (void)
  312. char *bootpath;
  313. grub_ssize_t bootpath_size;
  314. char *type;
  315. + grub_size_t sz;
  316. if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath",
  317. &bootpath_size)
  318. @@ -635,7 +672,13 @@ insert_bootpath (void)
  319. return;
  320. }
  321. - bootpath = (char *) grub_malloc ((grub_size_t) bootpath_size + 64);
  322. + if (grub_add (bootpath_size, 64, &sz))
  323. + {
  324. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining bootpath size"));
  325. + return;
  326. + }
  327. +
  328. + bootpath = (char *) grub_malloc (sz);
  329. if (! bootpath)
  330. {
  331. grub_print_error ();
  332. diff --git a/grub-core/disk/ldm.c b/grub-core/disk/ldm.c
  333. index 34bfe6bd1..4101b15d8 100644
  334. --- a/grub-core/disk/ldm.c
  335. +++ b/grub-core/disk/ldm.c
  336. @@ -220,6 +220,7 @@ make_vg (grub_disk_t disk,
  337. struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE
  338. / sizeof (struct grub_ldm_vblk)];
  339. unsigned i;
  340. + grub_size_t sz;
  341. err = grub_disk_read (disk, cursec, 0,
  342. sizeof(vblk), &vblk);
  343. if (err)
  344. @@ -251,7 +252,13 @@ make_vg (grub_disk_t disk,
  345. grub_free (pv);
  346. goto fail2;
  347. }
  348. - pv->internal_id = grub_malloc (ptr[0] + 2);
  349. + if (grub_add (ptr[0], 2, &sz))
  350. + {
  351. + grub_free (pv);
  352. + goto fail2;
  353. + }
  354. +
  355. + pv->internal_id = grub_malloc (sz);
  356. if (!pv->internal_id)
  357. {
  358. grub_free (pv);
  359. @@ -276,7 +283,15 @@ make_vg (grub_disk_t disk,
  360. goto fail2;
  361. }
  362. pv->id.uuidlen = *ptr;
  363. - pv->id.uuid = grub_malloc (pv->id.uuidlen + 1);
  364. +
  365. + if (grub_add (pv->id.uuidlen, 1, &sz))
  366. + {
  367. + grub_free (pv->internal_id);
  368. + grub_free (pv);
  369. + goto fail2;
  370. + }
  371. +
  372. + pv->id.uuid = grub_malloc (sz);
  373. grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen);
  374. pv->id.uuid[pv->id.uuidlen] = 0;
  375. @@ -343,7 +358,13 @@ make_vg (grub_disk_t disk,
  376. grub_free (lv);
  377. goto fail2;
  378. }
  379. - lv->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2);
  380. + if (grub_add (ptr[0], 2, &sz))
  381. + {
  382. + grub_free (lv->segments);
  383. + grub_free (lv);
  384. + goto fail2;
  385. + }
  386. + lv->internal_id = grub_malloc (sz);
  387. if (!lv->internal_id)
  388. {
  389. grub_free (lv);
  390. @@ -455,6 +476,7 @@ make_vg (grub_disk_t disk,
  391. struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE
  392. / sizeof (struct grub_ldm_vblk)];
  393. unsigned i;
  394. + grub_size_t sz;
  395. err = grub_disk_read (disk, cursec, 0,
  396. sizeof(vblk), &vblk);
  397. if (err)
  398. @@ -490,7 +512,12 @@ make_vg (grub_disk_t disk,
  399. grub_free (comp);
  400. goto fail2;
  401. }
  402. - comp->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2);
  403. + if (grub_add (ptr[0], 2, &sz))
  404. + {
  405. + grub_free (comp);
  406. + goto fail2;
  407. + }
  408. + comp->internal_id = grub_malloc (sz);
  409. if (!comp->internal_id)
  410. {
  411. grub_free (comp);
  412. @@ -640,7 +667,6 @@ make_vg (grub_disk_t disk,
  413. if (lv->segments->node_alloc == lv->segments->node_count)
  414. {
  415. void *t;
  416. - grub_size_t sz;
  417. if (grub_mul (lv->segments->node_alloc, 2, &lv->segments->node_alloc) ||
  418. grub_mul (lv->segments->node_alloc, sizeof (*lv->segments->nodes), &sz))
  419. diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c
  420. index d5106402f..8036d76ff 100644
  421. --- a/grub-core/disk/luks2.c
  422. +++ b/grub-core/disk/luks2.c
  423. @@ -26,6 +26,7 @@
  424. #include <grub/crypto.h>
  425. #include <grub/partition.h>
  426. #include <grub/i18n.h>
  427. +#include <grub/safemath.h>
  428. #include <base64.h>
  429. #include <json.h>
  430. @@ -569,6 +570,7 @@ luks2_recover_key (grub_disk_t source,
  431. gcry_err_code_t gcry_ret;
  432. grub_json_t *json = NULL, keyslots;
  433. grub_err_t ret;
  434. + grub_size_t sz;
  435. if (cargs->key_data == NULL || cargs->key_len == 0)
  436. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no key data");
  437. @@ -577,7 +579,10 @@ luks2_recover_key (grub_disk_t source,
  438. if (ret)
  439. return ret;
  440. - json_header = grub_zalloc (grub_be_to_cpu64 (header.hdr_size) - sizeof (header));
  441. + if (grub_sub (grub_be_to_cpu64 (header.hdr_size), sizeof (header), &sz))
  442. + return grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while calculating json header size");
  443. +
  444. + json_header = grub_zalloc (sz);
  445. if (!json_header)
  446. return GRUB_ERR_OUT_OF_MEMORY;
  447. diff --git a/grub-core/disk/memdisk.c b/grub-core/disk/memdisk.c
  448. index 613779cf3..36de3bfab 100644
  449. --- a/grub-core/disk/memdisk.c
  450. +++ b/grub-core/disk/memdisk.c
  451. @@ -23,6 +23,7 @@
  452. #include <grub/misc.h>
  453. #include <grub/mm.h>
  454. #include <grub/types.h>
  455. +#include <grub/safemath.h>
  456. GRUB_MOD_LICENSE ("GPLv3+");
  457. @@ -96,7 +97,11 @@ GRUB_MOD_INIT(memdisk)
  458. grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr);
  459. - memdisk_size = header->size - sizeof (struct grub_module_header);
  460. + if (grub_sub (header->size, sizeof (struct grub_module_header), &memdisk_size))
  461. + {
  462. + grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while obtaining memdisk size");
  463. + return;
  464. + }
  465. memdisk_addr = grub_malloc (memdisk_size);
  466. grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
  467. diff --git a/grub-core/disk/plainmount.c b/grub-core/disk/plainmount.c
  468. index 47e64805f..21ec4072c 100644
  469. --- a/grub-core/disk/plainmount.c
  470. +++ b/grub-core/disk/plainmount.c
  471. @@ -24,6 +24,7 @@
  472. #include <grub/extcmd.h>
  473. #include <grub/partition.h>
  474. #include <grub/file.h>
  475. +#include <grub/safemath.h>
  476. GRUB_MOD_LICENSE ("GPLv3+");
  477. @@ -126,7 +127,7 @@ plainmount_configure_password (grub_cryptodisk_t dev, const char *hash,
  478. grub_uint8_t *derived_hash, *dh;
  479. char *p;
  480. unsigned int round, i, len, size;
  481. - grub_size_t alloc_size;
  482. + grub_size_t alloc_size, sz;
  483. grub_err_t err = GRUB_ERR_NONE;
  484. /* Support none (plain) hash */
  485. @@ -145,7 +146,11 @@ plainmount_configure_password (grub_cryptodisk_t dev, const char *hash,
  486. * Allocate buffer for the password and for an added prefix character
  487. * for each hash round ('alloc_size' may not be a multiple of 'len').
  488. */
  489. - p = grub_zalloc (alloc_size + (alloc_size / len) + 1);
  490. + if (grub_add (alloc_size, (alloc_size / len), &sz) ||
  491. + grub_add (sz, 1, &sz))
  492. + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while allocating size of password buffer"));
  493. +
  494. + p = grub_zalloc (sz);
  495. derived_hash = grub_zalloc (GRUB_CRYPTODISK_MAX_KEYLEN * 2);
  496. if (p == NULL || derived_hash == NULL)
  497. {
  498. --
  499. 2.50.1