From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933522AbeEIArK (ORCPT ); Tue, 8 May 2018 20:47:10 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:36233 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933361AbeEIAmp (ORCPT ); Tue, 8 May 2018 20:42:45 -0400 X-Google-Smtp-Source: AB8JxZpp5Rwf49tLxkQMoP9hyCo2xHoMK0tV/97awXn0+pSfChRwTi2tF9r8BjKRX6hDjHwFoT7i3w== From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH 05/13] mm: Use array_size() helpers for kvmalloc() Date: Tue, 8 May 2018 17:42:21 -0700 Message-Id: <20180509004229.36341-6-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509004229.36341-1-keescook@chromium.org> References: <20180509004229.36341-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Instead of open-coded multiplication, use the new array_size() helper to detect overflow in kvmalloc()-family functions. Signed-off-by: Kees Cook --- include/linux/mm.h | 6 +++--- include/linux/vmalloc.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 1ac1f06a4be6..c97ed9aa3412 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -25,6 +25,7 @@ #include #include #include +#include struct mempolicy; struct anon_vma; @@ -560,10 +561,9 @@ 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) - return NULL; + size_t bytes = array_size(n, size); - return kvmalloc(n * size, flags); + return kvmalloc(bytes, flags); } 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 @@ #include #include /* pgprot_t */ #include +#include struct vm_area_struct; /* vma defining user mapping in mm_types.h */ struct notifier_block; /* in notifier.h */ -- 2.17.0