From patchwork Wed May 9 00:42: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: 10387845 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 B800660353 for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 924D229065 for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D7512901D; Wed, 9 May 2018 00:44:47 +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 E3CD228977 for ; Wed, 9 May 2018 00:44:43 +0000 (UTC) Received: (qmail 21776 invoked by uid 550); 9 May 2018 00:43:09 -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 20349 invoked from network); 9 May 2018 00:42:57 -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=PPeI6Fc1WIiUv3CI2eKARVZpHUloUTnaY/Tk9c6TzH0=; b=Ti3M4KhF8uBei+5fApihTwmQe/yaG8eyb6/Tt0dLmJsP0iqKp6AionjXWz46yB0cfJ XDx+0VVKV3kPkjM089dvRQ07dECcMqUGPU8dVHrYG75VDd8X2Aw5F19W60mEu51dXq8K 5PjbSkp9b4UdkoBMyGxkwFCfrqwMV1BsgcqLs= 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=PPeI6Fc1WIiUv3CI2eKARVZpHUloUTnaY/Tk9c6TzH0=; b=QcVidB4u1o7Ha8uQfCA8C3jjChu9x4o0CkjqP3qt2SfeiCZGdVTp7MTf5MxicFwFc8 GUE4GzG8ZSGpbayREuNBlMO5cbWyNnpmpjhJ4qv5r5o7uX7kBvusz8D1OWx142At1mko 8zWHO+73UrI82+teTtbdxtdgh1ii3cDLmNmzRk8prU+0tICuA2o/rDoqdh/mUn1UD1Ly NV+BZoCXexx9eiqyua8dFnN0fVM+2TzXgppaEhKtU0XXtbu8SGh4FioV610ulpW1Yr0+ OoQrxcqyQYQoGmHjuwo4Q8QiJpsT7usTg8sJbYVYXABnzxNtc/HJGNRFh5NlnQT4Y+Kj +QeQ== X-Gm-Message-State: ALQs6tBvIoT1ucFwLsq7KmPTCg0QhODP5jRZkDSPM62adR45xSNFVp6i p90gYgekqyGfF86cFAc0e2Ms2w== X-Google-Smtp-Source: AB8JxZoCGNoAm3MjQMCf7bkdg8CG2jZziC0BxdS1BbDCtlXHrA+HFi52gwv8E2P1aDc0chEnTVlbPg== X-Received: by 2002:a17:902:d808:: with SMTP id a8-v6mr43744255plz.177.1525826565494; Tue, 08 May 2018 17:42:45 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH 07/13] treewide: Use struct_size() for vmalloc()-family Date: Tue, 8 May 2018 17:42:23 -0700 Message-Id: <20180509004229.36341-8-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509004229.36341-1-keescook@chromium.org> References: <20180509004229.36341-1-keescook@chromium.org> X-Virus-Scanned: ClamAV using ClamSMTP This only finds one hit in the entire tree, but here's the Coccinelle: // Directly refer to structure's field @@ identifier alloc =~ "vmalloc|vzalloc"; identifier VAR, ELEMENT; expression COUNT; @@ - alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT)) + alloc(struct_size(VAR, ELEMENT, COUNT)) // mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL); @@ identifier alloc =~ "vmalloc|vzalloc"; identifier VAR, ELEMENT; expression COUNT; @@ - alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0])) + alloc(struct_size(VAR, ELEMENT, COUNT)) // Same pattern, but can't trivially locate the trailing element name, // or variable name. @@ identifier alloc =~ "vmalloc|vzalloc"; expression SOMETHING, COUNT, ELEMENT; @@ - alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT)) + alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT)) Signed-off-by: Kees Cook --- drivers/gpu/drm/nouveau/nvkm/core/ramht.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nvkm/core/ramht.c b/drivers/gpu/drm/nouveau/nvkm/core/ramht.c index ccba4ae73cc5..8162e3d2359c 100644 --- a/drivers/gpu/drm/nouveau/nvkm/core/ramht.c +++ b/drivers/gpu/drm/nouveau/nvkm/core/ramht.c @@ -144,8 +144,7 @@ nvkm_ramht_new(struct nvkm_device *device, u32 size, u32 align, struct nvkm_ramht *ramht; int ret, i; - if (!(ramht = *pramht = vzalloc(sizeof(*ramht) + - (size >> 3) * sizeof(*ramht->data)))) + if (!(ramht = *pramht = vzalloc(struct_size(ramht, data, (size >> 3))))) return -ENOMEM; ramht->device = device;