lib/generic-radix-tree.c: Don't overflow in peek()
authorKent Overstreet <kent.overstreet@gmail.com>
Sat, 13 Feb 2021 01:11:25 +0000 (20:11 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 28 Nov 2023 17:19:35 +0000 (17:19 +0000)
[ Upstream commit 9492261ff2460252cf2d8de89cdf854c7e2b28a0 ]

When we started spreading new inode numbers throughout most of the 64
bit inode space, that triggered some corner case bugs, in particular
some integer overflows related to the radix tree code. Oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/linux/generic-radix-tree.h
lib/generic-radix-tree.c

index 107613f7d79202585cfa981f5f4b60e207195296..f6cd0f909d9fb9fd9c6fa59c26051e05383707c0 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <asm/page.h>
 #include <linux/bug.h>
+#include <linux/limits.h>
 #include <linux/log2.h>
 #include <linux/math.h>
 #include <linux/types.h>
@@ -184,6 +185,12 @@ void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t);
 static inline void __genradix_iter_advance(struct genradix_iter *iter,
                                           size_t obj_size)
 {
+       if (iter->offset + obj_size < iter->offset) {
+               iter->offset    = SIZE_MAX;
+               iter->pos       = SIZE_MAX;
+               return;
+       }
+
        iter->offset += obj_size;
 
        if (!is_power_of_2(obj_size) &&
index f25eb111c0516f0191477662a1a88211caca053e..7dfa88282b006a408c7a6e77f69c17353b333301 100644 (file)
@@ -166,6 +166,10 @@ void *__genradix_iter_peek(struct genradix_iter *iter,
        struct genradix_root *r;
        struct genradix_node *n;
        unsigned level, i;
+
+       if (iter->offset == SIZE_MAX)
+               return NULL;
+
 restart:
        r = READ_ONCE(radix->root);
        if (!r)
@@ -184,10 +188,17 @@ restart:
                        (GENRADIX_ARY - 1);
 
                while (!n->children[i]) {
+                       size_t objs_per_ptr = genradix_depth_size(level);
+
+                       if (iter->offset + objs_per_ptr < iter->offset) {
+                               iter->offset    = SIZE_MAX;
+                               iter->pos       = SIZE_MAX;
+                               return NULL;
+                       }
+
                        i++;
-                       iter->offset = round_down(iter->offset +
-                                          genradix_depth_size(level),
-                                          genradix_depth_size(level));
+                       iter->offset = round_down(iter->offset + objs_per_ptr,
+                                                 objs_per_ptr);
                        iter->pos = (iter->offset >> PAGE_SHIFT) *
                                objs_per_page;
                        if (i == GENRADIX_ARY)