From patchwork Wed May 9 20:02:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10390775 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 9A22B60353 for ; Wed, 9 May 2018 20:03:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8CA332853A for ; Wed, 9 May 2018 20:03:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 878922866D; Wed, 9 May 2018 20:03:30 +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 9C74C2853A for ; Wed, 9 May 2018 20:03:29 +0000 (UTC) Received: (qmail 25993 invoked by uid 550); 9 May 2018 20:02:52 -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 25703 invoked from network); 9 May 2018 20:02:49 -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=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=l4JGB4NTXIiFb1xgFodm5N4pxF7ISMiF8B+4zgZ91q8UxcH1IBzMSpDf++qA2eApBn f8GQ06CdOZcYeNUPyvZKu839LFvPSuXHok5WRU7O29zciOfyhCYkhy3d5x1ZSXhZPZNl B4NHMWjE+3XDmye2Z4Tz5X+xMIVfj1i2fxsZE= 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=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=QTBiuc5MK3adDY9z3AK+PC9wPbktghp0T7BiFJPtncl9JmiFg1Eki3jpsmZdpeNdgN b2qbQcdEcEKrXGsV/KMMhTzYDCUs3BfDPlfr5dpb9j6rg6kh5a7vqubJoyx5moapJUmA lZAH3urYSHyTi7ZzExhi4bQxAtOxWh7a6JeO9yNDEvyqDcwspkyuCa6Gepn3yAUKIBqy Qv3UfbV9utMaVnDkQvNCRK8brPzX5eB0bXDx7Vpz+W8Hih8HiVaXMz+eEmz4VBCdjec2 kAExjmfaU9vh9vghguJulPi+jXDvPqyGIsOnjbhvQNke+/gflpZtLrZzBAG0q1oRP6ga oIRg== X-Gm-Message-State: ALQs6tAvB5QKukCPLNdysJUUz3k8NpKoFxwiq6L6wsYTrgy7/JRU1r5H ZUNRFSMgYogTsy/e2yBeiNqtqA== X-Google-Smtp-Source: AB8JxZrz3qi3VvWSpXGg9QCtopHzBoT1QMSf60kc8ao6cS9OeTr2rdSpipTahO2gnHIJsGJwtzag3A== X-Received: by 2002:a17:902:e8:: with SMTP id a95-v6mr46699399pla.274.1525896156873; Wed, 09 May 2018 13:02:36 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Rasmus Villemoes , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening Subject: [PATCH v2 4/6] mm: Use overflow helpers in kmalloc_array*() Date: Wed, 9 May 2018 13:02:21 -0700 Message-Id: <20180509200223.22451-5-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509200223.22451-1-keescook@chromium.org> References: <20180509200223.22451-1-keescook@chromium.org> X-Virus-Scanned: ClamAV using ClamSMTP Instead of open-coded multiplication and bounds checking, use the new overflow helper. Signed-off-by: Kees Cook --- include/linux/slab.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 81ebd71f8c03..4d759e1ddc33 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -13,6 +13,7 @@ #define _LINUX_SLAB_H #include +#include #include #include @@ -624,11 +625,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; + + if (unlikely(check_mul_overflow(n, size, &bytes))) 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); } /** @@ -657,11 +660,13 @@ 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; + + if (unlikely(check_mul_overflow(n, size, &bytes))) 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)