Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp263186ybl; Thu, 29 Aug 2019 22:43:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqy9oQ6TrEBRXzmL8HUO53cD7+yNIoXWbaRzyjm1Z4fTDmf25uzPsemPzw2LN6EdK7yzK98s X-Received: by 2002:a63:5920:: with SMTP id n32mr11204073pgb.352.1567143800816; Thu, 29 Aug 2019 22:43:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567143800; cv=none; d=google.com; s=arc-20160816; b=D1xz7WAdoJjwTxggz9/4sT6TIUQ3mmd+ZVKXs/vU024fj6HX8s4eL4JKxxkxXmBZpO ETx1GPMYCxp5XL9sivCQ59cWYbfadcI/ojogeSeAPfwUW3WNjZ2/GC4GwWEGOqC0ixie 6TWgHEsIhmGeRuBZGpDcbUggP+ETrtiAIrRuv3ShLu8XVGRR/35taVuTwJpA7K66SFOC QXSrMn+Ux7Hv56l2UFwP88cw1YxMqnsOaNp7GgLwjnizr50tYNzY9oNhRXtySWbzTWiW eyyAeUpObvrPpAOU4l2U9/AxzBPw682j2mPdvFjVnqa+Z9/vFqT9ehzNA+ByrWAlI+IZ R1kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=eVvtzN7Igkuh4Imleg3djnnr5OtlI8MGXuQLQiEidZU=; b=PVbqLfXzqH8CAnZLWiT4vcWdd1Cz3gCufqZbSFgSS/M2/VdTNsIA9R0td7fHOBU3rg gXyfKm7APYRGumxPLY0iXuj3tb++Y24j5MLCKWN2hrj5Cqkg0qDgSh87kGy1N89ymV6a D3xT8TG4gv7k+y/YaEcuixWOzZgxJDdbK2ZX9Q/YYp49NKGOxK83/VuFhQ4WBecw3ENy f+NQT+RDV1ai8b8T9d4LWp6PbLr2m49D2GlFw3J8GV4jHyq/9Qq8qQw3ZVv+D+sr50yt Z5HP6fkIlgFZ8xd+27TIkEXa+gHrUci7i/4JOrrhZvz87x5RwXKcee/ieL50aqA8Z/Fb BDsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hnMZlylC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id br3si4112156pjb.6.2019.08.29.22.43.05; Thu, 29 Aug 2019 22:43:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hnMZlylC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728165AbfH3Fkp (ORCPT + 99 others); Fri, 30 Aug 2019 01:40:45 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:40899 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725902AbfH3Fko (ORCPT ); Fri, 30 Aug 2019 01:40:44 -0400 Received: by mail-pf1-f193.google.com with SMTP id w16so3850428pfn.7 for ; Thu, 29 Aug 2019 22:40:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=eVvtzN7Igkuh4Imleg3djnnr5OtlI8MGXuQLQiEidZU=; b=hnMZlylCor+dZUvWVSWYiA8UIOvAlApi2I0f+Ywdk+k9sW5KnRJJeMF1IJd3tX7vkU oaZHglBJbWbFB/jvIbYVjQaAnporW/L9dZx1dWJmGIbQMgt5u8KzMnu0pZMZ3WsDg/DF ut4lcjfTVbUwEvfyvTQgRkFXWxUnAhD0nSlEQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=eVvtzN7Igkuh4Imleg3djnnr5OtlI8MGXuQLQiEidZU=; b=HSwr2YnuqcEBNynd0uU2zZFikJL/RXyPwzqkezlqk2JW1kQiDAZFQWDnWUF9kARgyv t5vfuTTkVK6dOolB6NxbJ9EO4V3qQjQj+fsxeDSZExU+RsTXEqrn1Fh3uFFjXphdHYfB FGvV4rSYxd5MXWnfgacE5f5oNZiGruH8aCAFfL00fO4Urh1+MWgEud/NpaommrT0a3Va ZgBqfu6xvMc7Ty6GbylNLy4bS0WbLnkN48CebRRGSs+m8b2bhJBAwL7Ra42btA/KyqeU AfL8st+xdCoslqd25C2qzLBE25KiymhK4IxZnMyqCgv2PH6DOf9nF3wmrgC+QTa7OlCs eldA== X-Gm-Message-State: APjAAAVhPVU4+2OvspxyxD72+4MX5xu4hL4VodZ1JAzFQwEtl9UpQCmg 1xMdFNLsj0v24l5yQB/Ieerx30la9Ko= X-Received: by 2002:a17:90a:a40e:: with SMTP id y14mr13342365pjp.83.1567143643954; Thu, 29 Aug 2019 22:40:43 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u4sm4937379pfh.186.2019.08.29.22.40.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2019 22:40:43 -0700 (PDT) Date: Thu, 29 Aug 2019 22:40:42 -0700 From: Kees Cook To: "Gustavo A. R. Silva" Cc: Jacek Anaszewski , Pavel Machek , Dan Murphy , linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] leds: pwm: Use struct_size() helper Message-ID: <201908292240.E0C345D884@keescook> References: <20190830005320.GA15267@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190830005320.GA15267@embeddedor> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 29, 2019 at 07:53:20PM -0500, Gustavo A. R. Silva wrote: > One of the more common cases of allocation size calculations is finding > the size of a structure that has a zero-sized array at the end, along > with memory for some number of elements for that array. For example: > > struct led_pwm_priv { > ... > struct led_pwm_data leds[0]; > }; > > Make use of the struct_size() helper instead of an open-coded version > in order to avoid any potential type mistakes. > > So, replace the following function: > > static inline size_t sizeof_pwm_leds_priv(int num_leds) > { > return sizeof(struct led_pwm_priv) + > (sizeof(struct led_pwm_data) * num_leds); > } > > with: > > struct_size(priv, leds, count) > > This code was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva Reviewed-by: Kees Cook -Kees > --- > drivers/leds/leds-pwm.c | 8 +------- > 1 file changed, 1 insertion(+), 7 deletions(-) > > diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c > index d0e1f2710351..8b6965a563e9 100644 > --- a/drivers/leds/leds-pwm.c > +++ b/drivers/leds/leds-pwm.c > @@ -65,12 +65,6 @@ static int led_pwm_set(struct led_classdev *led_cdev, > return 0; > } > > -static inline size_t sizeof_pwm_leds_priv(int num_leds) > -{ > - return sizeof(struct led_pwm_priv) + > - (sizeof(struct led_pwm_data) * num_leds); > -} > - > static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, > struct led_pwm *led, struct fwnode_handle *fwnode) > { > @@ -174,7 +168,7 @@ static int led_pwm_probe(struct platform_device *pdev) > if (!count) > return -EINVAL; > > - priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count), > + priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count), > GFP_KERNEL); > if (!priv) > return -ENOMEM; > -- > 2.23.0 > -- Kees Cook