diff options
author | Kees Cook <keescook@chromium.org> | 2018-05-08 15:55:26 -0400 |
---|---|---|
committer | Kees Cook <keescook@chromium.org> | 2018-06-05 15:16:51 -0400 |
commit | 3b3b1a29eb89ba93f91213cbebb332a2ac31fa8b (patch) | |
tree | f163f64ea43d4afee78f1bc0ad5eadc882e40343 | |
parent | 49b7f8983aa78581bfd511a26891b26cd734e293 (diff) |
mm: Use overflow helpers in kvmalloc()
Instead of open-coded multiplication and bounds checking, use the new
overflow helper. Additionally prepare for vmalloc() users to add
array_size()-family helpers in the future.
Signed-off-by: Kees Cook <keescook@chromium.org>
-rw-r--r-- | include/linux/mm.h | 7 | ||||
-rw-r--r-- | include/linux/vmalloc.h | 1 |
2 files changed, 6 insertions, 2 deletions
diff --git a/include/linux/mm.h b/include/linux/mm.h index 1ac1f06a4be6..7cb1c6a6bf82 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h | |||
@@ -25,6 +25,7 @@ | |||
25 | #include <linux/err.h> | 25 | #include <linux/err.h> |
26 | #include <linux/page_ref.h> | 26 | #include <linux/page_ref.h> |
27 | #include <linux/memremap.h> | 27 | #include <linux/memremap.h> |
28 | #include <linux/overflow.h> | ||
28 | 29 | ||
29 | struct mempolicy; | 30 | struct mempolicy; |
30 | struct anon_vma; | 31 | struct anon_vma; |
@@ -560,10 +561,12 @@ static inline void *kvzalloc(size_t size, gfp_t flags) | |||
560 | 561 | ||
561 | static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) | 562 | static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) |
562 | { | 563 | { |
563 | if (size != 0 && n > SIZE_MAX / size) | 564 | size_t bytes; |
565 | |||
566 | if (unlikely(check_mul_overflow(n, size, &bytes))) | ||
564 | return NULL; | 567 | return NULL; |
565 | 568 | ||
566 | return kvmalloc(n * size, flags); | 569 | return kvmalloc(bytes, flags); |
567 | } | 570 | } |
568 | 571 | ||
569 | extern void kvfree(const void *addr); | 572 | extern void kvfree(const void *addr); |
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 1e5d8c392f15..398e9c95cd61 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h | |||
@@ -8,6 +8,7 @@ | |||
8 | #include <linux/llist.h> | 8 | #include <linux/llist.h> |
9 | #include <asm/page.h> /* pgprot_t */ | 9 | #include <asm/page.h> /* pgprot_t */ |
10 | #include <linux/rbtree.h> | 10 | #include <linux/rbtree.h> |
11 | #include <linux/overflow.h> | ||
11 | 12 | ||
12 | struct vm_area_struct; /* vma defining user mapping in mm_types.h */ | 13 | struct vm_area_struct; /* vma defining user mapping in mm_types.h */ |
13 | struct notifier_block; /* in notifier.h */ | 14 | struct notifier_block; /* in notifier.h */ |