Commit 9492261f authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

lib/generic-radix-tree.c: Don't overflow in peek()

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: default avatarKent Overstreet <kent.overstreet@gmail.com>
parent 0fb5d567
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <asm/page.h> #include <asm/page.h>
#include <linux/bug.h> #include <linux/bug.h>
#include <linux/limits.h>
#include <linux/log2.h> #include <linux/log2.h>
#include <linux/math.h> #include <linux/math.h>
#include <linux/types.h> #include <linux/types.h>
...@@ -184,6 +185,12 @@ void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t); ...@@ -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, static inline void __genradix_iter_advance(struct genradix_iter *iter,
size_t obj_size) size_t obj_size)
{ {
if (iter->offset + obj_size < iter->offset) {
iter->offset = SIZE_MAX;
iter->pos = SIZE_MAX;
return;
}
iter->offset += obj_size; iter->offset += obj_size;
if (!is_power_of_2(obj_size) && if (!is_power_of_2(obj_size) &&
......
...@@ -166,6 +166,10 @@ void *__genradix_iter_peek(struct genradix_iter *iter, ...@@ -166,6 +166,10 @@ void *__genradix_iter_peek(struct genradix_iter *iter,
struct genradix_root *r; struct genradix_root *r;
struct genradix_node *n; struct genradix_node *n;
unsigned level, i; unsigned level, i;
if (iter->offset == SIZE_MAX)
return NULL;
restart: restart:
r = READ_ONCE(radix->root); r = READ_ONCE(radix->root);
if (!r) if (!r)
...@@ -184,10 +188,17 @@ void *__genradix_iter_peek(struct genradix_iter *iter, ...@@ -184,10 +188,17 @@ void *__genradix_iter_peek(struct genradix_iter *iter,
(GENRADIX_ARY - 1); (GENRADIX_ARY - 1);
while (!n->children[i]) { 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++; i++;
iter->offset = round_down(iter->offset + iter->offset = round_down(iter->offset + objs_per_ptr,
genradix_depth_size(level), objs_per_ptr);
genradix_depth_size(level));
iter->pos = (iter->offset >> PAGE_SHIFT) * iter->pos = (iter->offset >> PAGE_SHIFT) *
objs_per_page; objs_per_page;
if (i == GENRADIX_ARY) if (i == GENRADIX_ARY)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment