Commit 3b3b1a29 authored by Kees Cook's avatar Kees Cook

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: default avatarKees Cook <keescook@chromium.org>
parent 49b7f898
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <linux/err.h> #include <linux/err.h>
#include <linux/page_ref.h> #include <linux/page_ref.h>
#include <linux/memremap.h> #include <linux/memremap.h>
#include <linux/overflow.h>
struct mempolicy; struct mempolicy;
struct anon_vma; struct anon_vma;
...@@ -560,10 +561,12 @@ static inline void *kvzalloc(size_t size, gfp_t flags) ...@@ -560,10 +561,12 @@ static inline void *kvzalloc(size_t size, gfp_t flags)
static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
{ {
if (size != 0 && n > SIZE_MAX / size) size_t bytes;
if (unlikely(check_mul_overflow(n, size, &bytes)))
return NULL; return NULL;
return kvmalloc(n * size, flags); return kvmalloc(bytes, flags);
} }
extern void kvfree(const void *addr); extern void kvfree(const void *addr);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <linux/llist.h> #include <linux/llist.h>
#include <asm/page.h> /* pgprot_t */ #include <asm/page.h> /* pgprot_t */
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/overflow.h>
struct vm_area_struct; /* vma defining user mapping in mm_types.h */ struct vm_area_struct; /* vma defining user mapping in mm_types.h */
struct notifier_block; /* in notifier.h */ struct notifier_block; /* in notifier.h */
......
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