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: 10390765 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 D16BF60318 for ; Wed, 9 May 2018 20:02:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B7CBF286C6 for ; Wed, 9 May 2018 20:02:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B4003286F9; Wed, 9 May 2018 20:02:59 +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 A328228709 for ; Wed, 9 May 2018 20:02:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDF7A6B0584; Wed, 9 May 2018 16:02:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E689C6B0585; Wed, 9 May 2018 16:02:42 -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 C43626B0586; Wed, 9 May 2018 16:02:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f200.google.com (mail-pf0-f200.google.com [209.85.192.200]) by kanga.kvack.org (Postfix) with ESMTP id 790DF6B0584 for ; Wed, 9 May 2018 16:02:42 -0400 (EDT) Received: by mail-pf0-f200.google.com with SMTP id q15so6513325pff.17 for ; Wed, 09 May 2018 13:02:42 -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=rlFpqHh5YJCe9+qA877Ga274kUj37G6leVO+OJbfv1c=; b=FbrNNBTu3ICAxP0uHY8+5LP2rEcL8tSqGDTIfoqi+WC/ByhyJrHyiWsSPo5YkJG44S zpQ5NWg3aVaeOc08+RPfQPQRpRLRj3kYGj2dyWPrUrHPFyi7BoJUDSPEaCz44jSF4Hu2 G7ICfa37G6x6vFCC1RI6FlpxfdFBNeuxbFjdiVU03B1t1bswQjvijzFeM1735dGbkCgO vf2I1snX7J5uC77waFpuEyMwmBnnVXBmOzgQPOoABrMyeeqwhZaN1aVkyL+tE/mR+YpL iA4NZ9vATUItOVqwNckaS9XN1CZ4TBlnsQcQcruQY9+mbx0bFBax8TOsQ1A+MkK0Md/x jzsA== X-Gm-Message-State: ALKqPwdxuXrkxEOxwp9lcKuFLwsQQ11osi9xM1KI+mM7p8HBA3cqaGp0 xI92zK8CsU7LcGJdF2nAjKr8t0oaOvM8ewdqSJwH9cUIx33UYKHN6smurNSKHFsY21l4SEUxakn v+5NsglF5dveAZwyUsCwbFGvHkKur8SSfS6l4ueNjR0YwPpVoe0um6X0R9k91jPU1GOU7c2ueOT gIiAAxHnTj7GlkCt9iRkbJIcYu6yIVp3cCvassWw3OxyDxq/BUX9sxL2/uNBmVOEB6Sd9XQ+jKC 0DY95n6bktif7qdMFAt16YMZ3M2eZopwRa/40VMAQBJo1FfSIXPqMMW64Ofii6MzmlWSFMd8ix5 wxcsxxURZyCFzVGGV+kXGeSFQ5P00Q5pydSptTa3jr4HaNjn6lyKUZKjmaVtoHdPyhjUMVxbe2z h X-Received: by 2002:a63:6e0e:: with SMTP id j14-v6mr6036342pgc.218.1525896162143; Wed, 09 May 2018 13:02:42 -0700 (PDT) X-Received: by 2002:a63:6e0e:: with SMTP id j14-v6mr6036302pgc.218.1525896161182; Wed, 09 May 2018 13:02:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525896161; cv=none; d=google.com; s=arc-20160816; b=yrq5LpqS8Uj+aH2xcw6YvcgcKcfLVbLuQNyHyoJvWukcNR5oWqdl+uULL+DVIfYYSb mZRnzjqAzwSVQj8CwVvNdoQWUuC5MyDSr/Wj5YWCVL0PrbYDXaMKof1aFTyQMfbDjuZY yTgBCmHmSOI3gYavwnFAuFQxRb6wsfiSWQZTRxrAPuKW6A9LihCHJHlb0VtytpKypoL/ MdeVbm4qGOIzPAfbSJwjYTvgkBhUj82JeMr18QjYZbYd3z1xl7CjgJlONxmhHAv76L8y sIUhp5hqUfSvG15cf5AdxoZD5LZRKO6O3H0MBGv/zZkvsIFlWf8tZALsGIcVgDDxUv5w d33Q== 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=rlFpqHh5YJCe9+qA877Ga274kUj37G6leVO+OJbfv1c=; b=MqqfNshQ/uVfRJaMTn9FQxeyaQ8e267CNAghPlt4y+1WHTZ4auORgKSC/nUykdZRKm 28WB2bAp9v3gteCUYNLXdAEi0Fv69V+e3kq0cBG7X3XEIYB4MeHzOnG+srAjZnfd9Jww 0aZr2uJ86lonzqihFHD7X5CRYjkRmUEwRUYt9PIFZUfnlHVJaOgOnUUBf7mYOo5jQf2R /Nkpj4iU5gyUjH/bxGWRgQwDunudbO9kIZSKt+NGiw/apEn5COP7yk8RchgkWZxYsy7j GOVtvFiqXvEyO9Q0DP1SwmkM47uXTmcNNkcemtWyz41QDmexHvVSgZ2hGOFHFfl//TLm cG+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CWiNdW06; 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 h132-v6sor5824802pgc.390.2018.05.09.13.02.41 for (Google Transport Security); Wed, 09 May 2018 13:02:41 -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=CWiNdW06; 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=rlFpqHh5YJCe9+qA877Ga274kUj37G6leVO+OJbfv1c=; b=CWiNdW06zWpt4oYD9Wfmdul56ZTlaQ6tK9dukEt9zVzCVTkquIneSg0FmexDkw9q2d afwJreAtw+1YH6FtbCkg+BIacf6DjOcRnlBDDq2zkaLeEnua1tILVWUIXkfbEMPq51/L 8DhC4o05CLxSXEEkBQkDnIJ5U5UZDv67DHtvY= 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) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id s2sm27201907pfb.127.2018.05.09.13.02.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 May 2018 13:02:39 -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-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 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)