0052-fs-zfs-Use-safe-math-macros-to-prevent-overflows.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. From bd4fcbdbd9835716213debce07f08c81311ce9a6 Mon Sep 17 00:00:00 2001
  2. From: Lidong Chen <lidong.chen@oracle.com>
  3. Date: Wed, 22 Jan 2025 07:17:02 +0000
  4. Subject: [PATCH] fs/zfs: 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: Lidong Chen <lidong.chen@oracle.com>
  8. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  9. Upstream: 88e491a0f744c6b19b6d4caa300a576ba56db7c9
  10. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  11. ---
  12. grub-core/fs/zfs/zfs.c | 50 +++++++++++++++++++++++++++++++++++++-----
  13. 1 file changed, 44 insertions(+), 6 deletions(-)
  14. diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c
  15. index a497b1869..2f303d655 100644
  16. --- a/grub-core/fs/zfs/zfs.c
  17. +++ b/grub-core/fs/zfs/zfs.c
  18. @@ -2387,6 +2387,7 @@ fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap,
  19. zap_dnode->endian) << DNODE_SHIFT);
  20. grub_err_t err;
  21. grub_zfs_endian_t endian;
  22. + grub_size_t sz;
  23. if (zap_verify (zap, zap_dnode->endian))
  24. return 0;
  25. @@ -2448,8 +2449,14 @@ fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap,
  26. if (le->le_type != ZAP_CHUNK_ENTRY)
  27. continue;
  28. - buf = grub_malloc (grub_zfs_to_cpu16 (le->le_name_length, endian)
  29. - * name_elem_length + 1);
  30. + if (grub_mul (grub_zfs_to_cpu16 (le->le_name_length, endian), name_elem_length, &sz) ||
  31. + grub_add (sz, 1, &sz))
  32. + {
  33. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("buffer size overflow"));
  34. + grub_free (l);
  35. + return grub_errno;
  36. + }
  37. + buf = grub_malloc (sz);
  38. if (zap_leaf_array_get (l, endian, blksft,
  39. grub_zfs_to_cpu16 (le->le_name_chunk,
  40. endian),
  41. @@ -2872,6 +2879,7 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn,
  42. && ((grub_zfs_to_cpu64(((znode_phys_t *) DN_BONUS (&dnode_path->dn.dn))->zp_mode, dnode_path->dn.endian) >> 12) & 0xf) == 0xa)
  43. {
  44. char *sym_value;
  45. + grub_size_t sz;
  46. grub_size_t sym_sz;
  47. int free_symval = 0;
  48. char *oldpath = path, *oldpathbuf = path_buf;
  49. @@ -2923,7 +2931,18 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn,
  50. break;
  51. free_symval = 1;
  52. }
  53. - path = path_buf = grub_malloc (sym_sz + grub_strlen (oldpath) + 1);
  54. +
  55. + if (grub_add (sym_sz, grub_strlen (oldpath), &sz) ||
  56. + grub_add (sz, 1, &sz))
  57. + {
  58. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("path buffer size overflow"));
  59. + grub_free (oldpathbuf);
  60. + if (free_symval)
  61. + grub_free (sym_value);
  62. + err = grub_errno;
  63. + break;
  64. + }
  65. + path = path_buf = grub_malloc (sz);
  66. if (!path_buf)
  67. {
  68. grub_free (oldpathbuf);
  69. @@ -2960,6 +2979,7 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn,
  70. {
  71. void *sahdrp;
  72. int hdrsize;
  73. + grub_size_t sz;
  74. if (dnode_path->dn.dn.dn_bonuslen != 0)
  75. {
  76. @@ -2993,7 +3013,15 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn,
  77. + SA_SIZE_OFFSET),
  78. dnode_path->dn.endian);
  79. char *oldpath = path, *oldpathbuf = path_buf;
  80. - path = path_buf = grub_malloc (sym_sz + grub_strlen (oldpath) + 1);
  81. + if (grub_add (sym_sz, grub_strlen (oldpath), &sz) ||
  82. + grub_add (sz, 1, &sz))
  83. + {
  84. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("path buffer size overflow"));
  85. + grub_free (oldpathbuf);
  86. + err = grub_errno;
  87. + break;
  88. + }
  89. + path = path_buf = grub_malloc (sz);
  90. if (!path_buf)
  91. {
  92. grub_free (oldpathbuf);
  93. @@ -3568,6 +3596,7 @@ grub_zfs_nvlist_lookup_nvlist_array (const char *nvlist, const char *name,
  94. unsigned i;
  95. grub_size_t nelm;
  96. int elemsize = 0;
  97. + int sz;
  98. found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST_ARRAY, &nvpair,
  99. &size, &nelm);
  100. @@ -3602,7 +3631,12 @@ grub_zfs_nvlist_lookup_nvlist_array (const char *nvlist, const char *name,
  101. return 0;
  102. }
  103. - ret = grub_zalloc (elemsize + sizeof (grub_uint32_t));
  104. + if (grub_add (elemsize, sizeof (grub_uint32_t), &sz))
  105. + {
  106. + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("elemsize overflow"));
  107. + return 0;
  108. + }
  109. + ret = grub_zalloc (sz);
  110. if (!ret)
  111. return 0;
  112. grub_memcpy (ret, nvlist, sizeof (grub_uint32_t));
  113. @@ -4193,6 +4227,7 @@ iterate_zap_snap (const char *name, grub_uint64_t val,
  114. struct grub_dirhook_info info;
  115. char *name2;
  116. int ret;
  117. + grub_size_t sz;
  118. dnode_end_t mdn;
  119. @@ -4213,7 +4248,10 @@ iterate_zap_snap (const char *name, grub_uint64_t val,
  120. return 0;
  121. }
  122. - name2 = grub_malloc (grub_strlen (name) + 2);
  123. + if (grub_add (grub_strlen (name), 2, &sz))
  124. + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("name length overflow"));
  125. +
  126. + name2 = grub_malloc (sz);
  127. name2[0] = '@';
  128. grub_memcpy (name2 + 1, name, grub_strlen (name) + 1);
  129. ret = ctx->hook (name2, &info, ctx->hook_data);
  130. --
  131. 2.50.1