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: 10442257 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 7A5B0602BC for ; Fri, 1 Jun 2018 00:45:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7802E28E43 for ; Fri, 1 Jun 2018 00:45:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6C65328E58; Fri, 1 Jun 2018 00:45:24 +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 9D6E328E43 for ; Fri, 1 Jun 2018 00:45:23 +0000 (UTC) Received: (qmail 28326 invoked by uid 550); 1 Jun 2018 00:44:04 -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 28163 invoked from network); 1 Jun 2018 00:44:00 -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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=UZ05RovQAWKIcizxOrVseQ9neuK9Ghr4QUCaj25icNPXKuEwLeVnuM07PwN677HMLg cO3W96aCDo6xpmIexSx1+PBiQcxkxhMOnA/k8GgYs7OztxTxn2b2Hxjz9NgfpiTVUYIO M/gCjO3t3CStaVomRtzIQtu+pQRvyeA0qwxsw= 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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=iXWAT20KhYEKMAmNCvZH7f2SCsCZz1dO4kKSknvmFXPTTQ3EFYJ2drUKqYXhcJjecw vFkGPAhzjYxfA3VsSF5hL0g4eXNZihVYhfhrSmt5MpnSejI3KNpK2XUDHG2zDHgRwf/A na2p7FcagQonZ/J7/8AnrsZEXyIMsqRV9nEGOPiRdpLSbFXEKlipz7oPn/wKsyWamvj8 Jh7bcnqNCih5YfxZn/UsGCtNcPc1qeDa85mpj1Zh0Sd1jaOWevxanlBunL/kycIUQrle Gq7IBhaJ0M1Dlp6mnMQ2JoioZTtx2mR87/wYKoLrZUBeTVtp8nB0KuYHTSnWm/Is3FAn G28w== X-Gm-Message-State: ALKqPwdkrm8rqVahZ324KRoXmtK+kPe5NqhK7XK597CimoTMPJlKoZP+ Knp5+F/IbctnQrKF9103heqNQg== 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) 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-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 */