From patchwork Fri Jun 1 00:42:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10442217 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 5E6A4602BC for ; Fri, 1 Jun 2018 00:44:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 587CE28E43 for ; Fri, 1 Jun 2018 00:44:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4CC0B28E58; Fri, 1 Jun 2018 00:44:02 +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 D203628E43 for ; Fri, 1 Jun 2018 00:44:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C3466B026A; Thu, 31 May 2018 20:43:50 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 373EA6B026B; Thu, 31 May 2018 20:43:50 -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 23EB06B026C; Thu, 31 May 2018 20:43:50 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg0-f71.google.com (mail-pg0-f71.google.com [74.125.83.71]) by kanga.kvack.org (Postfix) with ESMTP id D13696B026A for ; Thu, 31 May 2018 20:43:49 -0400 (EDT) Received: by mail-pg0-f71.google.com with SMTP id t5-v6so5789587pgt.18 for ; Thu, 31 May 2018 17:43:49 -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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=TCTLTtdDk93uJ+5sgDLZREGJVhbdBHxmFiLTQGgI9AAFbZSO66971LUmpJZUW+l+W2 bAiH0d/sbocYgU4AgYtpY0h/h1K13+tS+gpbGSTsRlPOW307lHuebWP31IQbWdo1jHs8 YfcT15ZsLHfLqXGv3/0kzCqlKC1l50tyQAKEpLACYjFEWEnn+qkrpwTmHuU6u7FsKrde U/AEMvFsncIdzKCxDuisojoTRGr401UnwkswTV8ZMjR9n5g0In/NkkkOfkd347pLczef v+E+NjIUc+qCX/Uc7jVbUu7MY1UVNl4V1t65NipJRyllGc4Ht+wdwr/ilPJ9jkcAEXyt buKQ== X-Gm-Message-State: ALKqPwextovfjKQlcx8LIUXcujjoe+0Ja8CrbBllMrcv03efxhzhvKmY vWiGgm1weCTvC60DDrO0GP0MxWqewZlL3zHBwl+/2lfQWds53QpZkgMf0OedmT6HaM2dXJkHeee NUXOIJLORoXqt1zgOnXEs3eKe9Hcdk6eGx1Nk4DrqrceuaksoBr6umNDrlHlh/bEESxt50EXfW1 F37PF52qcXDrUfSzw9kEwsfShyfq8uv+dM35toPiOOVcoBs5WvO8Y1SjoUnL1tZJ4Ji/1x7o/8D OzWkd+oH3yxED4fkCFP9G9O8KC+/Bu1yabe9uoFQfPppAHssAgqqM5Jmwlqgo8UrbFTgsqSSz5r nNq5NqKmMCEad4QyBlrPYIyAJREKlwrtWyWgDW3RHPntokedpkAvyzoIR8xS6yf1Ghbh0kNqLvh e X-Received: by 2002:a17:902:bb8d:: with SMTP id m13-v6mr9099552pls.46.1527813829562; Thu, 31 May 2018 17:43:49 -0700 (PDT) X-Received: by 2002:a17:902:bb8d:: with SMTP id m13-v6mr9099523pls.46.1527813828787; Thu, 31 May 2018 17:43:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527813828; cv=none; d=google.com; s=arc-20160816; b=K0Yjr9tjqky1RwexMY1RcVQBvH8IjvdG1PxbK2S15yvmIadpNrd52v28Mf97wIY76U Q757WuCzZIWw5fJhAOZT2xVMYxdDJYV7uTTD5Jitxx05iBhqAWTWU7w5KM2X9bPG4oGa cTYLpY0aAExbjIukcml4E30v0/HImp/eUtNZAJPTZc+jmlRRU5XGoGT+LFFVAX1/9QNv rBjxUs/wX5asC9nrQBjKzzdfvma0uChQ7Q54XItqt8sQdrc0BfME00y6qblPVOhoCl9E OkIjjygVJN1urxRwvg+J9wlXolu/OWcFeNJuJO+/tFCzgtdGpSaDc9+2T4D6/V+o2iwX 0dAQ== 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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=AsDdZZa63TjFNh8KJkYbJhf4HsxnaTcNImRbWW1FbLFbykhf+eRTs49ceSnh25zMCs wIdXKvF2j6lKhvWUz3l1VGO3aiJN0ccbQpn0h8+gh9M80WGQBo/WptX/fXaDmi7pZT6L 1ojMeueA2pJBYb74oUfFUtnkLTjFbQiVCtP9px+zk1BB7OEFGc907UXI5PUgFeXv1gvg fW2ZAbuyAsDqDO3DkQGUinfVi/0CZz4eANbLe7GtbqYtlObutJFsYBzo9Nwt936Dg8HV /a+wg2+1EFbU4O6rjrFjb7ubl3Srwaia7p7gGw2rlMORm4OzMVq6C4eWvRvpwelrAxe9 y9zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UZ05RovQ; 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 a129-v6sor14769668pfb.146.2018.05.31.17.43.48 for (Google Transport Security); Thu, 31 May 2018 17:43:48 -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=UZ05RovQ; 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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=UZ05RovQAWKIcizxOrVseQ9neuK9Ghr4QUCaj25icNPXKuEwLeVnuM07PwN677HMLg cO3W96aCDo6xpmIexSx1+PBiQcxkxhMOnA/k8GgYs7OztxTxn2b2Hxjz9NgfpiTVUYIO M/gCjO3t3CStaVomRtzIQtu+pQRvyeA0qwxsw= X-Google-Smtp-Source: ADUXVKJsfjvQ9Sy8pdfgoRt3efcbfpSQ2TqD3Ux2xPfO9aH3xfZWFRslDE8gIvR4kOeym7cM00LkRA== X-Received: by 2002:a62:ee14:: with SMTP id e20-v6mr8855355pfi.130.1527813828539; Thu, 31 May 2018 17:43:48 -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 j186-v6sm37776387pgc.10.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 07/16] mm: Use overflow helpers in kvmalloc() Date: Thu, 31 May 2018 17:42:24 -0700 Message-Id: <20180601004233.37822-8-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. Additionally prepare for vmalloc() users to add array_size()-family helpers in the future. Signed-off-by: Kees Cook --- include/linux/mm.h | 7 +++++-- include/linux/vmalloc.h | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 1ac1f06a4be6..7cb1c6a6bf82 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,12 @@ 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) + size_t bytes; + + if (unlikely(check_mul_overflow(n, size, &bytes))) return NULL; - 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 */