0112-fs-nilfs2-Don-t-search-children-if-provided-number-i.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. From 37c0eb05cdcc64c28d31c4ebd300f14d5239d05e Mon Sep 17 00:00:00 2001
  2. From: Daniel Axtens <dja@axtens.net>
  3. Date: Mon, 18 Jan 2021 16:49:44 +1100
  4. Subject: [PATCH] fs/nilfs2: Don't search children if provided number is too
  5. large
  6. NILFS2 reads the number of children a node has from the node. Unfortunately,
  7. that's not trustworthy. Check if it's beyond what the filesystem permits and
  8. reject it if so.
  9. This blocks some OOB reads. I'm not sure how controllable the read is and what
  10. could be done with invalidly read data later on.
  11. Signed-off-by: Daniel Axtens <dja@axtens.net>
  12. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  13. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  14. ---
  15. grub-core/fs/nilfs2.c | 38 +++++++++++++++++++++++---------------
  16. 1 file changed, 23 insertions(+), 15 deletions(-)
  17. diff --git a/grub-core/fs/nilfs2.c b/grub-core/fs/nilfs2.c
  18. index fee2242..43ac1ad 100644
  19. --- a/grub-core/fs/nilfs2.c
  20. +++ b/grub-core/fs/nilfs2.c
  21. @@ -416,14 +416,34 @@ grub_nilfs2_btree_node_get_key (struct grub_nilfs2_btree_node *node,
  22. }
  23. static inline int
  24. -grub_nilfs2_btree_node_lookup (struct grub_nilfs2_btree_node *node,
  25. +grub_nilfs2_btree_node_nchildren_max (struct grub_nilfs2_data *data,
  26. + struct grub_nilfs2_btree_node *node)
  27. +{
  28. + int node_children_max = ((NILFS2_BLOCK_SIZE (data) -
  29. + sizeof (struct grub_nilfs2_btree_node) -
  30. + NILFS_BTREE_NODE_EXTRA_PAD_SIZE) /
  31. + (sizeof (grub_uint64_t) + sizeof (grub_uint64_t)));
  32. +
  33. + return (node->bn_flags & NILFS_BTREE_NODE_ROOT) ? 3 : node_children_max;
  34. +}
  35. +
  36. +static inline int
  37. +grub_nilfs2_btree_node_lookup (struct grub_nilfs2_data *data,
  38. + struct grub_nilfs2_btree_node *node,
  39. grub_uint64_t key, int *indexp)
  40. {
  41. grub_uint64_t nkey;
  42. int index, low, high, s;
  43. low = 0;
  44. +
  45. high = grub_le_to_cpu16 (node->bn_nchildren) - 1;
  46. + if (high >= grub_nilfs2_btree_node_nchildren_max (data, node))
  47. + {
  48. + grub_error (GRUB_ERR_BAD_FS, "too many children");
  49. + return 0;
  50. + }
  51. +
  52. index = 0;
  53. s = 0;
  54. while (low <= high)
  55. @@ -459,18 +479,6 @@ grub_nilfs2_btree_node_lookup (struct grub_nilfs2_btree_node *node,
  56. return s == 0;
  57. }
  58. -static inline int
  59. -grub_nilfs2_btree_node_nchildren_max (struct grub_nilfs2_data *data,
  60. - struct grub_nilfs2_btree_node *node)
  61. -{
  62. - int node_children_max = ((NILFS2_BLOCK_SIZE (data) -
  63. - sizeof (struct grub_nilfs2_btree_node) -
  64. - NILFS_BTREE_NODE_EXTRA_PAD_SIZE) /
  65. - (sizeof (grub_uint64_t) + sizeof (grub_uint64_t)));
  66. -
  67. - return (node->bn_flags & NILFS_BTREE_NODE_ROOT) ? 3 : node_children_max;
  68. -}
  69. -
  70. static inline grub_uint64_t *
  71. grub_nilfs2_btree_node_dptrs (struct grub_nilfs2_data *data,
  72. struct grub_nilfs2_btree_node *node)
  73. @@ -517,7 +525,7 @@ grub_nilfs2_btree_lookup (struct grub_nilfs2_data *data,
  74. node = grub_nilfs2_btree_get_root (inode);
  75. level = grub_nilfs2_btree_get_level (node);
  76. - found = grub_nilfs2_btree_node_lookup (node, key, &index);
  77. + found = grub_nilfs2_btree_node_lookup (data, node, key, &index);
  78. ptr = grub_nilfs2_btree_node_get_ptr (data, node, index);
  79. if (need_translate)
  80. ptr = grub_nilfs2_dat_translate (data, ptr);
  81. @@ -538,7 +546,7 @@ grub_nilfs2_btree_lookup (struct grub_nilfs2_data *data,
  82. }
  83. if (!found)
  84. - found = grub_nilfs2_btree_node_lookup (node, key, &index);
  85. + found = grub_nilfs2_btree_node_lookup (data, node, key, &index);
  86. else
  87. index = 0;
  88. --
  89. 2.14.2