Skip to content

Commit

Permalink
mm: Use overflow helpers in kvmalloc()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Kees Cook committed Jun 5, 2018
1 parent 49b7f89 commit 3b3b1a2
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
#include <linux/err.h>
#include <linux/page_ref.h>
#include <linux/memremap.h>
#include <linux/overflow.h>

struct mempolicy;
struct anon_vma;
@@ -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)
{
if (size != 0 && n > SIZE_MAX / size)
size_t bytes;

if (unlikely(check_mul_overflow(n, size, &bytes)))
return NULL;

return kvmalloc(n * size, flags);
return kvmalloc(bytes, flags);
}

extern void kvfree(const void *addr);
1 change: 1 addition & 0 deletions include/linux/vmalloc.h
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
#include <linux/llist.h>
#include <asm/page.h> /* pgprot_t */
#include <linux/rbtree.h>
#include <linux/overflow.h>

struct vm_area_struct; /* vma defining user mapping in mm_types.h */
struct notifier_block; /* in notifier.h */

0 comments on commit 3b3b1a2

Please sign in to comment.