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: 10390751 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 DD27060318 for ; Wed, 9 May 2018 20:02:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CEB59286AD for ; Wed, 9 May 2018 20:02:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C36F0286C4; Wed, 9 May 2018 20:02:44 +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 54D81286AD for ; Wed, 9 May 2018 20:02:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B34EB6B0580; Wed, 9 May 2018 16:02:38 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id ABF8A6B0581; Wed, 9 May 2018 16:02:38 -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 8C1F66B0582; Wed, 9 May 2018 16:02:38 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f198.google.com (mail-pf0-f198.google.com [209.85.192.198]) by kanga.kvack.org (Postfix) with ESMTP id 459506B0580 for ; Wed, 9 May 2018 16:02:38 -0400 (EDT) Received: by mail-pf0-f198.google.com with SMTP id x21so10009603pfn.23 for ; Wed, 09 May 2018 13:02:38 -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=WsTLHR1kgaQKUU9TK3F8A03+/rMieZmdnHKKwGxNefGa8h+6upUzYrUjWphaGrKH3L D1tj2kDBGPOnwEm170QB2IpIL7cNhA5Ewq9FxQgi3qMETO2kDBtxBC4hPtqnrA4GJANJ fjvKct7ZFAUX6+Jahw4x34RWhcXuaP9tgB/5IASIKjWlOsnR1TmNICpLWXN0biVRRY5m RttmIs3X987Y+9YmzHzWq1WHLly+UYLcVxmPm0vW31Ajc/jtJqOBrFkOdBDFynnEbAUQ QyWcrzSz3dRL4ROD9wFjbxcWQEi4CzYDSWWP9Y0FV2P/DLalwqS4IvHWqKEXqjmkf15+ S/8g== X-Gm-Message-State: ALQs6tDJD28CtXDs5UbTXpIPPL5Tgg2sLfcOMMpGsE/MqWBEE5yUGvG/ LH9h8LPAKmE4elfJpLB7830/GSAdXgH/LLPU3P5PI49U0a+wzrhrkruf3Kxcul9Oqr2pwIzlMKX NAntctcLtQS/8QkezS7+EY9Hjy6S5pQ3se23ED15wUuy2qIUwexR4wo4+Vyty72nEdMJpmpap7c HnEL/ZqcMGqL4XbVs9wA8dNpLRa6AobUOR24h5uKsLohTGM8znOS4GvpYQNZiAgWaIGVGP4vZUq zvi41o61OQC3jKBZtHrrt6kmNMjk5VnvCj8bXpmcU77Ud3K+QNEiJMRsCcpIwnfU7Q/nTxNr0jK sgvvSjl2mIF17/CWGwYLzX4H/cRU0fhhB+i/E82LSOkqsEOdRV0PHJC0+4yKep0VyMQ5b/cT60k S X-Received: by 2002:a65:635a:: with SMTP id p26-v6mr36704411pgv.163.1525896157988; Wed, 09 May 2018 13:02:37 -0700 (PDT) X-Received: by 2002:a65:635a:: with SMTP id p26-v6mr36704380pgv.163.1525896157305; Wed, 09 May 2018 13:02:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525896157; cv=none; d=google.com; s=arc-20160816; b=PrvF5bQ+SnLCQaaAvvCv87WI4frO4jrlx2bb26larzvFnDcFQ3XI+yYqHuGszV1dVw pfKsn2Y83aRgR605wcvS7OkIml2r76kmdT32nqoRNM/tN0H+QHa7qsxtYEf1+yPLIBJM 7tMrGJN+iLdl8MR6E+VDZFgvFQySUrjjXZCMjziUUGZ99JyzUOka3ZejkcQpilvXaC+Z C5w+LtjDUqpGpY5oSz22/Dva071ledHd1xfEcSMVhHZndhR/ALLwALHRCLVMx6EBRzSA Z8qehRB9ABfdyTWo+iAHrFc8XAxJ3uzCPdGzjqfZdc3+0tJv38g1O8v6cn+8Fue8cF13 RVrQ== 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=gD7xiZkOdsDZmy4X3v+pbsnZFNs33xuACgwQ09Ra1Y+WE22NIiHyLh8LIlDtjPZU5W hOLTXuEsCixs+p09j/G2Wi3up5NtKJ/TTjjuTTU0K8sDRLBBiLxurOnnMgG//49CZfrn Ez+Lb9/gPV/GHYNf61qZET/Wo/Ws363cj71Zia9TA1pZYN0JLHBO8y/vuyN8PU41O6nk IXfxcC+eAjHrA29WeVAG/csj4vRb/rwy6eFYqd5yqryKCeUqJDC29mQkVycoGytknQd3 biIiTWXX3Bh8rhssHeJa9YHly/FVHFZ3iVHulmrqV+fwwZiHzrWdN1NtADr7CMx1AKOy 5RXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=l4JGB4NT; 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 b15-v6sor4280225plk.105.2018.05.09.13.02.37 for (Google Transport Security); Wed, 09 May 2018 13:02:37 -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=l4JGB4NT; 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=l4JGB4NTXIiFb1xgFodm5N4pxF7ISMiF8B+4zgZ91q8UxcH1IBzMSpDf++qA2eApBn f8GQ06CdOZcYeNUPyvZKu839LFvPSuXHok5WRU7O29zciOfyhCYkhy3d5x1ZSXhZPZNl B4NHMWjE+3XDmye2Z4Tz5X+xMIVfj1i2fxsZE= 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) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id 184sm30510285pfg.89.2018.05.09.13.02.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 May 2018 13:02:33 -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-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)