0050-fs-Prevent-overflows-when-allocating-memory-for-arra.patch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. From 593d7b8659bef80b1f6ae3b793332d8eca8b8131 Mon Sep 17 00:00:00 2001
  2. From: Lidong Chen <lidong.chen@oracle.com>
  3. Date: Tue, 21 Jan 2025 19:02:37 +0000
  4. Subject: [PATCH] fs: Prevent overflows when allocating memory for arrays
  5. Use grub_calloc() when allocating memory for arrays to ensure proper
  6. overflow checks are in place.
  7. The HFS+ and squash4 security vulnerabilities were reported by
  8. Jonathan Bar Or <jonathanbaror@gmail.com>.
  9. Fixes: CVE-2025-0678
  10. Fixes: CVE-2025-1125
  11. Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
  12. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  13. Upstream: 84bc0a9a68835952ae69165c11709811dae7634e
  14. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  15. ---
  16. grub-core/fs/btrfs.c | 4 ++--
  17. grub-core/fs/hfspluscomp.c | 9 +++++++--
  18. grub-core/fs/squash4.c | 8 ++++----
  19. 3 files changed, 13 insertions(+), 8 deletions(-)
  20. diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
  21. index 0625b1166..9c1e925c9 100644
  22. --- a/grub-core/fs/btrfs.c
  23. +++ b/grub-core/fs/btrfs.c
  24. @@ -1276,8 +1276,8 @@ grub_btrfs_mount (grub_device_t dev)
  25. }
  26. data->n_devices_allocated = 16;
  27. - data->devices_attached = grub_malloc (sizeof (data->devices_attached[0])
  28. - * data->n_devices_allocated);
  29. + data->devices_attached = grub_calloc (data->n_devices_allocated,
  30. + sizeof (data->devices_attached[0]));
  31. if (!data->devices_attached)
  32. {
  33. grub_free (data);
  34. diff --git a/grub-core/fs/hfspluscomp.c b/grub-core/fs/hfspluscomp.c
  35. index 48ae438d8..a80954ee6 100644
  36. --- a/grub-core/fs/hfspluscomp.c
  37. +++ b/grub-core/fs/hfspluscomp.c
  38. @@ -244,14 +244,19 @@ hfsplus_open_compressed_real (struct grub_hfsplus_file *node)
  39. return 0;
  40. }
  41. node->compress_index_size = grub_le_to_cpu32 (index_size);
  42. - node->compress_index = grub_malloc (node->compress_index_size
  43. - * sizeof (node->compress_index[0]));
  44. + node->compress_index = grub_calloc (node->compress_index_size,
  45. + sizeof (node->compress_index[0]));
  46. if (!node->compress_index)
  47. {
  48. node->compressed = 0;
  49. grub_free (attr_node);
  50. return grub_errno;
  51. }
  52. +
  53. + /*
  54. + * The node->compress_index_size * sizeof (node->compress_index[0]) is safe here
  55. + * due to relevant checks done in grub_calloc() above.
  56. + */
  57. if (grub_hfsplus_read_file (node, 0, 0,
  58. 0x104 + sizeof (index_size),
  59. node->compress_index_size
  60. diff --git a/grub-core/fs/squash4.c b/grub-core/fs/squash4.c
  61. index f91ff3bfa..cf2bca822 100644
  62. --- a/grub-core/fs/squash4.c
  63. +++ b/grub-core/fs/squash4.c
  64. @@ -822,10 +822,10 @@ direct_read (struct grub_squash_data *data,
  65. break;
  66. }
  67. total_blocks = ((total_size + data->blksz - 1) >> data->log2_blksz);
  68. - ino->block_sizes = grub_malloc (total_blocks
  69. - * sizeof (ino->block_sizes[0]));
  70. - ino->cumulated_block_sizes = grub_malloc (total_blocks
  71. - * sizeof (ino->cumulated_block_sizes[0]));
  72. + ino->block_sizes = grub_calloc (total_blocks,
  73. + sizeof (ino->block_sizes[0]));
  74. + ino->cumulated_block_sizes = grub_calloc (total_blocks,
  75. + sizeof (ino->cumulated_block_sizes[0]));
  76. if (!ino->block_sizes || !ino->cumulated_block_sizes)
  77. {
  78. grub_free (ino->block_sizes);
  79. --
  80. 2.50.1