From patchwork Fri Jun 1 00:42:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10442267 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 4ACB6602BC for ; Fri, 1 Jun 2018 00:46:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 49F4428E54 for ; Fri, 1 Jun 2018 00:46:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3ED3D29020; Fri, 1 Jun 2018 00:46:07 +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 6002128E54 for ; Fri, 1 Jun 2018 00:46:06 +0000 (UTC) Received: (qmail 28450 invoked by uid 550); 1 Jun 2018 00:44:15 -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 28199 invoked from network); 1 Jun 2018 00:44:02 -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=YPLBQvsIedsEzQUz3dNwDzhBI3aYQkAyJbwJV6Dl/5v8Ifr8L+hZRvWnJK6cDIZHNZ KJV2UfpluAqs1syomAVjtjzN6iK7KJJz9cItRtqaWrNST6LVO5yATzBqNTyo67qUABct nLS6Afr9gXshsQTVDqgcT9+GTusKiza3KlNAE= 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=CPZ89JCYsH7pbzWBQL80n7zb4Yn3nZhySYqGKb/mAbDyVnKgt6YhE2zlsXoorjofYb lulrsF5Vz6WMwQzocrqgRQSIjwasxJUNOyzho9GTOu1I+dOShoPHnySZyOG+xReV1GpJ 3DNq9pTMJvSy2fHmQVEhrYuxSLHYtMZ+HsPfebqsNQG00aSS2IUoI24MO9KmSNZ4aez0 OGsAW9+xygOHPt/fYf42fvyu8BhuZlbeMyM/ojF86Vmcfmq/H1fVS1gyOp1uBLxNCxhP NNBkum/t9S/Ze7SzZlb+4hyna9k8bZtT0KnexDcP0q4sPDv/k6i1A8ir+JyzTxRgQ/3a bP1A== X-Gm-Message-State: ALKqPwezMAXVeK69JcChsSPSAdOVw74H38bcGG6UiydRGxG1VrltS+bR 2EHdbu8MSTBaM/KrR93IDC5V4LMe/w4= X-Google-Smtp-Source: ADUXVKJX9TW3gi+cqvdEbUht32fQfADsCnB16Yvm8Tm3U7acQY03/IFZcA3i1+tdguO6nvvhxXRwTw== X-Received: by 2002:aa7:8084:: with SMTP id v4-v6mr8744516pff.105.1527813830338; Thu, 31 May 2018 17:43:50 -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 10/16] treewide: Use struct_size() for vmalloc()-family Date: Thu, 31 May 2018 17:42:27 -0700 Message-Id: <20180601004233.37822-11-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 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;