From patchwork Wed May 9 20:02: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: 10390785 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 0DCD260318 for ; Wed, 9 May 2018 20:03:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0064228538 for ; Wed, 9 May 2018 20:03:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E9123286BD; Wed, 9 May 2018 20:03:55 +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 068FB286DD for ; Wed, 9 May 2018 20:03:54 +0000 (UTC) Received: (qmail 26157 invoked by uid 550); 9 May 2018 20:03:00 -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 26006 invoked from network); 9 May 2018 20:02:52 -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=rlFpqHh5YJCe9+qA877Ga274kUj37G6leVO+OJbfv1c=; b=CWiNdW06zWpt4oYD9Wfmdul56ZTlaQ6tK9dukEt9zVzCVTkquIneSg0FmexDkw9q2d afwJreAtw+1YH6FtbCkg+BIacf6DjOcRnlBDDq2zkaLeEnua1tILVWUIXkfbEMPq51/L 8DhC4o05CLxSXEEkBQkDnIJ5U5UZDv67DHtvY= 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=rlFpqHh5YJCe9+qA877Ga274kUj37G6leVO+OJbfv1c=; b=Neuc3qtA/gitbHgR7C0jrCMAv2IZLwCOwv+GEHvFZH8DiPHwxDeEBxh2d7TjZbIf7l 6mTxq2FxSDijNdA5hjxtkgknsjaKKt0wRvaKhIT47G27vXgXfYFZQ5HgGAfQXxNSP7Ml H1DlqA+iHbieL2bayph7A4lHvC9sy8VWXvRuyZ98iPq9QzqT7GDIx1DM1GC95zMQnKpT M9OXvKDeix2gsm6D2Sc5cX02+yB2MLrL5K+iwhhHeixwdqsKgWrD5LBX57k/j5t9LLM1 qEpCRXFUBBwb3jPTa/tbx1I8aeiUECckaAorMJAt3nNknaW9WcKMhXBStElpqpfPlWbH Nuig== X-Gm-Message-State: ALQs6tByk3H42Hjgt8MGrJb4rkJZM+2JMr6fEmJbhB/Dnz/Pp+kRAWea bKURDhCsrsSoGWzabPFMulWeJw== X-Google-Smtp-Source: AB8JxZpmTv9DePWTcE6DQP+G+RMeiXdAnMC5HvCSkUpEv9y22NHo9AfGN2Knl7hyHaLC5OSURDOZOQ== X-Received: by 2002:a63:6406:: with SMTP id y6-v6mr36866294pgb.205.1525896160884; Wed, 09 May 2018 13:02:40 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Rasmus Villemoes , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening Subject: [PATCH v2 6/6] device: Use overflow helpers for devm_kmalloc() Date: Wed, 9 May 2018 13:02:23 -0700 Message-Id: <20180509200223.22451-7-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-Virus-Scanned: ClamAV using ClamSMTP Use the overflow helpers both in existing multiplication-using inlines as well as the addition-overflow case in the core allocation routine. Signed-off-by: Kees Cook --- drivers/base/devres.c | 7 ++++++- include/linux/device.h | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 95b67281cd2a..f98a097e73f2 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c @@ -84,9 +84,14 @@ static struct devres_group * node_to_group(struct devres_node *node) static __always_inline struct devres * alloc_dr(dr_release_t release, size_t size, gfp_t gfp, int nid) { - size_t tot_size = sizeof(struct devres) + size; + size_t tot_size; struct devres *dr; + /* We must catch any near-SIZE_MAX cases that could overflow. */ + if (unlikely(check_add_overflow(sizeof(struct devres), size, + &tot_size))) + return NULL; + dr = kmalloc_node_track_caller(tot_size, gfp, nid); if (unlikely(!dr)) return NULL; diff --git a/include/linux/device.h b/include/linux/device.h index 0059b99e1f25..e78a050d6e7e 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -25,6 +25,7 @@ #include #include #include +#include #include struct device; @@ -666,9 +667,12 @@ static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) static inline void *devm_kmalloc_array(struct device *dev, 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 devm_kmalloc(dev, n * size, flags); + + return devm_kmalloc(dev, bytes, flags); } static inline void *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_t flags)