From patchwork Wed May 9 00:42:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10387817 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7447560548 for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4BE472871B for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3D56529541; Wed, 9 May 2018 00:44:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id EA490292F3 for ; Wed, 9 May 2018 00:42:55 +0000 (UTC) Received: (qmail 20160 invoked by uid 550); 9 May 2018 00:42:53 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 20124 invoked from network); 9 May 2018 00:42:52 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=EchP3bqB/MjDE14k+xBKevFL0oQNq7ckw924bxj0KKXVwxJnicKHnyIUG6XOllhmaU msI0qtl3lu4fBqgaUM8BAP8Z+zTpeCimJvk6wN9o2GYbTTrCocSzp1SzwWzZjkAPG7Jk n7EhcOOpirAZc+CccSDLzI14Us0ZjWdW5qyC0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=eKNlgFp731mWh3LJjAHe/9v9ULbFs4BjBTaBmwo0Nn7fuu7L0Stwj98CRwHkY+eM6l vpWOKE1H0yrYJ2CW0r4f4nh17hw4b434ET/l8iw5BiwzDXUvlvmAZ+65x/iwS5pvszvm BZNikHdr6g5UsO9TFZrJhmxtVEybcFmzsE8v+gD4tQXa6rrRKtD1HPtvwVf2NFFEys6D q6x3q8/nywGjODdchMb1u3MSq4MlRphs9EoVvawxZOHV+5F43W2Tb4QRDDph+KM23dk7 v3pjk4W2Rl1QIO10MFIwMpADNroxbTdvzJhBl3nhaha6UTKF9wkDDJsERrWzjpwy05Jb /S0g== X-Gm-Message-State: ALQs6tDoNuZO8dClfyfwlVgTkzNjtREP0Oxm7eqSwt1ueoflNJEznW+7 IMtDVP2IrWKEmqBAcVEu+ADP4Q== X-Google-Smtp-Source: AB8JxZq+1cd9h2Iu2geiWXeoy6buXWBqLBJHuUM3jeMZVeE06fXlIoZK+VxlIFki6YyUOW8Al4n8Ig== X-Received: by 2002:a63:69c3:: with SMTP id e186-v6mr34219080pgc.353.1525826560635; Tue, 08 May 2018 17:42:40 -0700 (PDT) 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 04/13] mm: Use array_size() helpers for kmalloc() Date: Tue, 8 May 2018 17:42:20 -0700 Message-Id: <20180509004229.36341-5-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> X-Virus-Scanned: ClamAV using ClamSMTP Instead of open-coded multiplication, use the new array_size() helper to detect overflow in kmalloc()-family functions. Signed-off-by: Kees Cook --- include/linux/slab.h | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 81ebd71f8c03..d03e0726e136 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -13,6 +13,7 @@ #define _LINUX_SLAB_H #include +#include #include #include @@ -499,6 +500,8 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) */ static __always_inline void *kmalloc(size_t size, gfp_t flags) { + if (size == SIZE_MAX) + return NULL; if (__builtin_constant_p(size)) { if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large(size, flags); @@ -539,6 +542,8 @@ static __always_inline unsigned int kmalloc_size(unsigned int n) static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) { + if (size == SIZE_MAX) + return NULL; #ifndef CONFIG_SLOB if (__builtin_constant_p(size) && size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) { @@ -624,11 +629,13 @@ int memcg_update_all_caches(int num_memcgs); */ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) { - if (size != 0 && n > SIZE_MAX / size) + size_t bytes = array_size(n, size); + + if (bytes == SIZE_MAX) return NULL; if (__builtin_constant_p(n) && __builtin_constant_p(size)) - return kmalloc(n * size, flags); - return __kmalloc(n * size, flags); + return kmalloc(bytes, flags); + return __kmalloc(bytes, flags); } /** @@ -639,7 +646,9 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) */ static inline void *kcalloc(size_t n, size_t size, gfp_t flags) { - return kmalloc_array(n, size, flags | __GFP_ZERO); + size_t bytes = array_size(n, size); + + return kmalloc(bytes, flags | __GFP_ZERO); } /* @@ -657,16 +666,22 @@ extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long); static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node) { - if (size != 0 && n > SIZE_MAX / size) + size_t bytes = array_size(n, size); + + if (bytes == SIZE_MAX) return NULL; if (__builtin_constant_p(n) && __builtin_constant_p(size)) - return kmalloc_node(n * size, flags, node); - return __kmalloc_node(n * size, flags, node); + return kmalloc_node(bytes, flags, node); + return __kmalloc_node(bytes, flags, node); } static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node) { - return kmalloc_array_node(n, size, flags | __GFP_ZERO, node); + size_t bytes = array_size(n, size); + + if (bytes == SIZE_MAX) + return NULL; + return kmalloc_node(bytes, flags | __GFP_ZERO, node); }