From patchwork Wed May 9 00:42: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: 10387825 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 8E0A660555 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 614C72871B for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 55BD1287AE; Wed, 9 May 2018 00:44: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=-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 A666829573 for ; Wed, 9 May 2018 00:43:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94AC76B02F3; Tue, 8 May 2018 20:42:46 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8567D6B02F6; Tue, 8 May 2018 20:42:46 -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 6A7336B02F5; Tue, 8 May 2018 20:42:46 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f70.google.com (mail-pl0-f70.google.com [209.85.160.70]) by kanga.kvack.org (Postfix) with ESMTP id 17B076B02F2 for ; Tue, 8 May 2018 20:42:46 -0400 (EDT) Received: by mail-pl0-f70.google.com with SMTP id x2-v6so2741836plv.0 for ; Tue, 08 May 2018 17:42:46 -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=kacq6440mdTwv10+JXG8+ILiqdEXJe+Qny7L57GcEUI=; b=c9YRlw+qJLZyJMRyPjAhhmByVGUoxAugoZWT8HTkvQ8pkC1cQnTCAgmB/XDtgVvIQp NcEFY0rddAlDDYlEQg48aynD8BxUiPlwXfDz6i3vmEWay9cRWDOKZRYNRF0lDV5XGXhJ 3Slq99CsuI/ItKC2Vd4y6Os9qPiLdcRtkm82QU+lKy3Xi2K0gxfYLCYQ/vD5Y9+Yg338 6Tif/jCmDB3s9K55vVNPRawMKPsXVIVZgU+JW5lXHpWiUL1tSfqAFWNUvmY/FgOpl4wc G+HGh52WnUC1XAreQudW1X7ca/DlJLx2/VAUFf98up+JDYnvNWHFewI26pgNHQ2BIXv3 b6kw== X-Gm-Message-State: ALQs6tDnIFl91rIDz3mD0W4L//oUEJN+oaaO5//QYYqNpsPJJH9WzT2R VFkZ+xOcWZzkyDRpxM5HNndzLiu2C92zLKR5aA7bttvdHNhEoHW4r1HBzl9gHbUz6CFac8wk1mQ nPWb+FtDy9ETYTHCAGth5DTq7/mUjdowjkmjfoLw4lUPEtAZBT73znFyyFcEmVavqP9KUaiafvV YYnvpaVdeNsmmagh41zOGwdIbvdFfMRzqTP8ibIUbwmEYZYrIXomN3AOQh4eCxoecH8dqU9ur48 HjKXSTtgVtkGZtxT3W10TwpNQQWSSL9oJ1GN/gbvpWyzVjq6YVvGdjftRRPqWXiKpdTXYRjJ4A4 pZvcgYwD7wsyfByN4RR9zWu5hN7jARr6+AzdYrIfXHgCtzVekWl1WTY3h9bOzgkJ8PONUKmaACz z X-Received: by 2002:a63:9401:: with SMTP id m1-v6mr34359850pge.140.1525826565759; Tue, 08 May 2018 17:42:45 -0700 (PDT) X-Received: by 2002:a63:9401:: with SMTP id m1-v6mr34359828pge.140.1525826565016; Tue, 08 May 2018 17:42:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525826564; cv=none; d=google.com; s=arc-20160816; b=P00A1DzX55N7Re37YRZqvC7hZL7JzJgaizIxRaHJdoY7ZmBIeTW+nUm/t09sCxdwYk fhCjKUSF0fG70pDGlRY5ejim08I7/xIh+21yzulaOFf9g3DDvtlYgEnduTGh0DGRn7wz x19/CY1zTf4vE853FJb0LFiNlgNPheuwEofFbaf1H+ZFqH52XPPT+lXi0YoKzbWGdADK aWELyfbKfVBYw+amDV2nMqgbPaq1++dQ41+NWeohCuTTQr1QSy86CIFGVkZRC/AosQB4 RZGvp2t5KFzEeOL2Wq2cKnOUyvlPU7cHWz1GYSP2UDmYrK9Cu8hvc5NKib2c2JV0acxE +jfg== 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=kacq6440mdTwv10+JXG8+ILiqdEXJe+Qny7L57GcEUI=; b=mh6XR5+UwbmwmgJt1pXjkUTGEvUpdGMgY7/3FwxsB7AMLnnS+JRxhHw1md/qZYJhof A9fuVQ4e1CVfEaSQRWfioh5LID71kG6AOCrHwPfy3bO8UPvyiPiSuHBkD/HdRS5NAJKs BI+wDtCuCoG3CzInBFdwg5faBtpUTUgtHIwhPpxVYYBuZIsr+v58NMeMrkZq/6g1RwBU WCBSzIsYNqnwVF5gT8WWpkjG1iNZqctr+RRJ50SKYCUPFOkjAxttjJ+3KenZEO/fshT+ yybA4xQS1epaE1IwKZ+N8BYVcQ3PFYiungRGB0rYC27wY3dQ8FyEC++rkH9wU8wLPWiG O8mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HrIc2zBp; 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 a33-v6sor8246907pli.106.2018.05.08.17.42.44 for (Google Transport Security); Tue, 08 May 2018 17:42:44 -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=HrIc2zBp; 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=kacq6440mdTwv10+JXG8+ILiqdEXJe+Qny7L57GcEUI=; b=HrIc2zBpMlKCaD303NPVf5xNwccD63d6byjHONBlTfRaozEoYFQVPWYnAICxIJoZyK Ok6aHjkCIS7J4kJZMId6UzdfKJEOk1zxh1b3LDrGTQk/dv7Ae4Czh2QfOBmaEQPH+27N H0zjttfhDbQq889l8qPS9qYT5Lm9CgPoRqwas= X-Google-Smtp-Source: AB8JxZpp5Rwf49tLxkQMoP9hyCo2xHoMK0tV/97awXn0+pSfChRwTi2tF9r8BjKRX6hDjHwFoT7i3w== X-Received: by 2002:a17:902:be0e:: with SMTP id r14-v6mr11498583pls.158.1525826564784; Tue, 08 May 2018 17:42:44 -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 d186sm2072694pfa.79.2018.05.08.17.42.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 May 2018 17:42:43 -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 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> 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, 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 */