From patchwork Fri Jun 1 00:42:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10442221 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 145D6603D7 for ; Fri, 1 Jun 2018 00:44:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F7D128E43 for ; Fri, 1 Jun 2018 00:44:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0432328E58; Fri, 1 Jun 2018 00:44:05 +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 956C128E43 for ; Fri, 1 Jun 2018 00:44:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29E476B026B; Thu, 31 May 2018 20:43:51 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 19E2D6B026D; Thu, 31 May 2018 20:43:51 -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 0161B6B026E; 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-pl0-f71.google.com (mail-pl0-f71.google.com [209.85.160.71]) by kanga.kvack.org (Postfix) with ESMTP id B55B76B026B for ; Thu, 31 May 2018 20:43:50 -0400 (EDT) Received: by mail-pl0-f71.google.com with SMTP id i1-v6so14218206pld.11 for ; Thu, 31 May 2018 17:43:50 -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=7XKqD0uwptRTWTsv7lmAcV/cMTd4waW5o5MaWTUpnRc=; b=Ti/yjC061EJnsvtyCYNWp7CcCXaJR7qeYxadpaMrfan5XFiuOGJpjQDshuunyFn114 DO919ACg9cSdX3A6yImmA8pqYcV0Ckm+0RxQAGxbTiQog7qtN2TS1yLhQaD9sIb3R3bm CpzuwjC9EM4ONSpNql0acL4jffEG+Ss5ukJy/ID4U9Pya7oJCDs0B2v+aXrh24XJKCS1 R8D/qzjMaM5ApSgfkQNASZKKAKvIIb47Qg9BfPk8/2ntpjbJs8EV4saaBuTJV9LlOcMO MMw2GdozOEnuYjNr5Wd7laMDYj371Wpfrlng6vrDUI9ohttT/Jd/l4+BRtGTjkdLt5il xXRQ== X-Gm-Message-State: ALKqPwe9Hg9jgcBaTgsPmcYaOHfoncI3XGPbEfChKyBm7eBj3UddQ3fR iEhGL7/UENwKD3QL/92bptIU4vLlBzHLS7+PJxU6bFSspEa8omTz41VjHclU4oTt8H/CToijA6E 2ML6H/bR/6QkUmtf0927Of7lWnQnWtIV11Inh0dIOmk9Bpml3SPF10+q5Tp2KmEMQdkmBbmRbCg EpDpWlQV9dU6zJ64/8+Fn5QbPLRhuU9QbWuIq0pGRbvQL1J9K6aqHDS0s7NfQWIybS0BcHN51JA PT3HbSVfmJQxXKrPbKfF7l3qZGia24Q76R09fIOByAgd+VJIP83VZbOrrLt4DRTu8+EIvpnpSpN 0rMKEnetbtkoQJu8mlTBpiAMbJ/oGmTmwQbqM0ws0nBxzrzA1O/ajoFDUiUd1Lr5o+6cbFiE30w j X-Received: by 2002:a17:902:8d8e:: with SMTP id v14-v6mr9016091plo.387.1527813830438; Thu, 31 May 2018 17:43:50 -0700 (PDT) X-Received: by 2002:a17:902:8d8e:: with SMTP id v14-v6mr9016053plo.387.1527813829703; Thu, 31 May 2018 17:43:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527813829; cv=none; d=google.com; s=arc-20160816; b=oTxl8QxNxOCe/2vsLXuwn9r3gZgvymjNNaLXcLZmz0igJAAIpvttAx0E+Z8/YQRAF4 tbvGBgBQH65W/HlfnDMI9ZNjBRFVRhOUREAkNXV50hctaY/PBmb8V/mCE3T3ItU2ettE Wo6mnCn6+RkaQZKdqR3RL8yxdBmKlLCbKMl66tfKXjVABpFxsyjslg0K33kwVYtuiP0K QjhpZ+3WJUM3mUK3ttpMERL1WbDvXRNDMxqnRYsY49K0n+e9HvykJk88UXcqJ5bBQvpw Ms7oP1rIQxTES4H9OmkEp4UiJROw+ywyTyhfTPRl31Vh85GmtzmFLqkdXJ9ICBGosbVD oitQ== 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=7XKqD0uwptRTWTsv7lmAcV/cMTd4waW5o5MaWTUpnRc=; b=owOtLl7pyQPD3Jn0ejhqDbiZ33/7D1RjhbPzJ49k7qvKjFg5n+75Ug8UOHlll/gp++ 0ycKzdYCjJUYEvnBai9JD0iPlgppH3SN8kTujxg+HMNGBF9NfVYvBLza276Rf/W5tfsk I/kzikqqxzdkkWzS6bYvjy2wKTazYSnOd8ya6Spljyjwn3FIIJTbCGnZgi6i2u7E65Sc qLAVfxIaUzMQ1gl1jl7KUFP/u+x4N3uCb5ktyCPFC1jHp209wvs+Z1++LOemvks7p/0q NjxLn6CICfEWjuDh2bMGFNhVemSh470ybd/OHoQUNgK1BFZqC3F5OzUx6sviak+TgUHH +7Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=k+ROhkPj; 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 s65-v6sor12588588pfj.137.2018.05.31.17.43.49 for (Google Transport Security); Thu, 31 May 2018 17:43:49 -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=k+ROhkPj; 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=7XKqD0uwptRTWTsv7lmAcV/cMTd4waW5o5MaWTUpnRc=; b=k+ROhkPj4yqwXzrpj5XXCx+1cCW3riU2WUCBTey2JW0RKCk13UnwxNldvienBOMZIX 42n9Am2s7LoM/fBRp2UtOymC6coonvsOpI17OvSnv7vPkKdxFbJl+cB7vxck3mRfZa/N jFDvFgq60fOdVgQGuOFqyHDAKuAnL054qoQoc= X-Google-Smtp-Source: ADUXVKISgGjeSJ7yvGy03E9ZNVTwpF/bRLjF6zTIpPg40zADjKtGiQFKPU6MnNexQEErTH/giXgGCA== X-Received: by 2002:a62:ab0e:: with SMTP id p14-v6mr3114266pff.211.1527813829443; Thu, 31 May 2018 17:43:49 -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 x4-v6sm65267683pfb.99.2018.05.31.17.43.43 (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 08/16] device: Use overflow helpers for devm_kmalloc() Date: Thu, 31 May 2018 17:42:25 -0700 Message-Id: <20180601004233.37822-9-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 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 477956990f5e..897efa647203 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -25,6 +25,7 @@ #include #include #include +#include #include struct device; @@ -668,9 +669,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)