From patchwork Fri Jun 1 00:42:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10442215 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 91126602BC for ; Fri, 1 Jun 2018 00:43:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B4A728E43 for ; Fri, 1 Jun 2018 00:43:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7FBA428E58; Fri, 1 Jun 2018 00:43:59 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 22BFE28E54 for ; Fri, 1 Jun 2018 00:43:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E2F96B0269; Thu, 31 May 2018 20:43:49 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3BFF86B026B; Thu, 31 May 2018 20:43:49 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A6366B026A; Thu, 31 May 2018 20:43:49 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f71.google.com (mail-pl0-f71.google.com [209.85.160.71]) by kanga.kvack.org (Postfix) with ESMTP id CADA06B0266 for ; Thu, 31 May 2018 20:43:48 -0400 (EDT) Received: by mail-pl0-f71.google.com with SMTP id e1-v6so14161127pld.23 for ; Thu, 31 May 2018 17:43:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=O0GUFUHH3cyJJFG7HMfooYX/uI2LSgVasKGhU1+/rah7Q0W9GZ72kcuLJpoIp9wZQC O83QTMFFI+NLirQSDYj/OBNZXNF3eQYSPauEG7lG0oXlBlAB5LOATNZabkSuI2ag1qXq +4uaGL/Cx5ryQV1bLsQEk6WSjkbATOKCe4q42hwQQR1kI1Fwij8/pPSvtoU+8R9uDaQ2 7+0tEqelxKhbhcp647Opkqsa8mxnZ8qARt8Da0wAD5V7ftPUgPtsz0G2hisGCzsRuOxs IxXEqt9a6Kxe0Mx9jRYovRKFTu5WRXX9qStWsAHL83PGlxfs9ClduuMwyjASPEZ4N9VR 1LRg== X-Gm-Message-State: ALKqPwf27Aks4pKe1jVwYVhKt+J93gdJ2ZfRKR1evL81SFZlMaRSfKJf aHm/e9ol85iZjFYTC6hGhS1Ca9TadICD/L/lpHREMLBNYIsB/+AbqGQ33a9bGcbJ9nYU/7A9qCT DjYx8Et/rtHISsIe8fnjo8JCn7T+vPMbX4jUrsfJPGHm8L/xpXT0KWQ/XSzed6Ov1p8FghUVFSU yQfXtZC5MbvwzlggSbHiR0rKXbLV0dcG6nhksG4HB93ncAIa5tJfU1cVFbbeYnR1RPR1mfLpLrB 5qWUOZX4d/Lj8qz0DpoYtLf0zMoe003/gfAU9OvK3lL3mJ1fTapQ8hqiNTrMkaWXG3gRSZR7uOh xxUkK0pkC8X+D1+t6t9hpGm5VQfUea9H8swlqkZhjFA3f3kf/mzElgPJm+az92MMTkBBCzZyoeS a X-Received: by 2002:a62:1358:: with SMTP id b85-v6mr3865698pfj.238.1527813828533; Thu, 31 May 2018 17:43:48 -0700 (PDT) X-Received: by 2002:a62:1358:: with SMTP id b85-v6mr3865666pfj.238.1527813827766; Thu, 31 May 2018 17:43:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527813827; cv=none; d=google.com; s=arc-20160816; b=p44dC9YGY/Qcb/QLzfgMJ/5E/ns3eNKhDrH/4RpCSG54cq7M7y41j0qNsY8aLVxCuK NTrJ6Aq31mYg/aPJ8oiaNvZgw/jaVYLgWcs/mAnAPoID4He12ThjuUW2iWlY/QBFJsGg NdIBOd4PaxW4O1WNQ0QYL8d09iLA4WnkQoGArn3Tcdg+8YbM8fznjnS528TiUE/EqReR B5pdTQ0mIfIstwZHCkKQJV2xkcU8VK1D39uIMx5MbCx70sXAftCR4Gl486lxz9cTMp8B L2VNa4oGvfkbqeSutZtnHaeK8SS+1nczzv/16/WAxFdBavHBL0KwYU7czzfq42yXOtJu F/ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=FvXCv8zoSWuMxhD/DCRUuBR1YeRWdsB8FBCUm9MlZACTTGfHcdoB06QxZ1kfadwXCL vXoNfsQjJvvr/apICdXKuKIIheymbIGvv/3kbkllcPKm5GPMSIh2gf0GWBOxwZ8yyd1Y A51KiVCxDUnpk3BEraR/aq3AAB1jcBM2XdoR9Kss4dKRBLFCEjywHA3Ksffazc/feE9g 54hNiR9OjudB16dugIMlMd0vTqml2zgGQOpX7VNBqnCQfTYPwRFAuIy3ozGxFmDhtbKj BhW3DKLtuIUfVHabelFHwbsrvYkGJwU/1Azz8XaPT2XJfjxqo3ZFhLnMWCcvWMM6LxF+ 5UmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KdLg8OKY; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id z10-v6sor3998451pgu.421.2018.05.31.17.43.47 for (Google Transport Security); Thu, 31 May 2018 17:43:47 -0700 (PDT) Received-SPF: pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KdLg8OKY; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org 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=KdLg8OKY5+GoGGKyMoIaTOU1cr3IKoW6xKmTzeEB3wnfilft6/nWwMaw/LhPPeP50Q T+ZwzCzSfrKewfzcfr000yginL9eIbc6vPBW82RVKAlkgYb1SyCs+15fC/0tkvWOoY/E wKezlvLC5qmORsKCHduE9Y3Zl4k0SBWx+3ZWM= X-Google-Smtp-Source: ADUXVKLhpQY2Ue1FUR6jY3ghSEs9wqh0bmNXd3qgkEVcRBQLMHpyxC9PY1iyZLXBC5PZf4UkcJlPCw== X-Received: by 2002:a63:b705:: with SMTP id t5-v6mr10452pgf.343.1527813827534; Thu, 31 May 2018 17:43:47 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id s17-v6sm84877527pfi.165.2018.05.31.17.43.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 May 2018 17:43:45 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Linus Torvalds , Rasmus Villemoes , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening Subject: [PATCH v3 06/16] mm: Use overflow helpers in kmalloc_array*() Date: Thu, 31 May 2018 17:42:23 -0700 Message-Id: <20180601004233.37822-7-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180601004233.37822-1-keescook@chromium.org> References: <20180601004233.37822-1-keescook@chromium.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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)