From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C09A1C4332E for ; Sun, 22 Mar 2020 16:16:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9616120732 for ; Sun, 22 Mar 2020 16:16:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="WD3m1vYk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726986AbgCVQQk (ORCPT ); Sun, 22 Mar 2020 12:16:40 -0400 Received: from mail-pj1-f68.google.com ([209.85.216.68]:38317 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726809AbgCVQQj (ORCPT ); Sun, 22 Mar 2020 12:16:39 -0400 Received: by mail-pj1-f68.google.com with SMTP id m15so4881522pje.3 for ; Sun, 22 Mar 2020 09:16:38 -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=lPrpjX3DIANTx2/srJAYBr3XZH1bElctL0wHyLULZ6Y=; b=WD3m1vYkX0pfhbxJVj6RQoGnfbpuvgEa7hUI61NCROsrtiT8Ue+lqyIyCLZX17Yf7f 4HqoeW8gMzuXJAUOL2ulkpp+CPjAYO6Y4vWOdfMM4c+y6bZtP2rmq33lzX7H2bMmbrgD TkvaX6gs8yldXpQNf2jfu6AfNfAjkWicNsh7I= 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=lPrpjX3DIANTx2/srJAYBr3XZH1bElctL0wHyLULZ6Y=; b=mycFlfPvthZw7bq012r4h/Oy80tUXLBtj4uN62nns9x/LhutBMdEOgGRK3iE02uu/X L/+wZRlALvmg5zZ8xrtAZbuMRmy2Nr7I3ajoSwLNKBgaU4ar+PsRFi4JCCQ9Hh+SvGWD Pt72QkJe2VlHr7rE0gWfEL3Z5DnLQSjcOmMep3dj6em35PHLjswKh3pief27sdfQ7Ikt Vo9ztxhiuyMhhopLOK/5XpRDzsSyVJTv3OEJuVF4HNJdK4jKrnJ7Ox1B4lwt/+qPtZwb 1TtGoPSUf0lv9OTvrRCXrUgPrhT70MGpg2WgijxTDErV72gyQveqxq3hsyt5wgTcNHvE u7Gg== X-Gm-Message-State: ANhLgQ0o05WTMWgsQ/zzfEn2bO9Jp0oF7NhJhRtVEJydP5PzeOAb3JLv 1cszFrsqBGFnSyDOBR1NeHSl/tQPdCA= X-Google-Smtp-Source: ADFU+vtp5cYRDmRZyGTSvbeiQHuj7NGAgNa6e4Lbwa7tEggV7tzNrwpzBOvpDq00Q3v84HJgt0kkeQ== X-Received: by 2002:a17:90a:c583:: with SMTP id l3mr6918945pjt.84.1584893798370; Sun, 22 Mar 2020 09:16:38 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p14sm9723743pgm.49.2020.03.22.09.16.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Mar 2020 09:16:37 -0700 (PDT) Date: Sun, 22 Mar 2020 09:16:36 -0700 From: Kees Cook To: Andy Shevchenko Cc: "Ardelean, Alexandru" , "lars@metafoo.de" , "linux-iio@vger.kernel.org" , "Grozav, Andrei" , "devicetree@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "jic23@kernel.org" , "Hennerich, Michael" , "Nagy, Laszlo" , "Csomortani, Istvan" , "robh+dt@kernel.org" , "Bogdan, Dragos" , "Costina, Adrian" Subject: Re: [PATCH v11 5/8] iio: adc: adi-axi-adc: add support for AXI ADC IP core Message-ID: <202003220901.880A6DF@keescook> References: <20200321085315.11030-1-alexandru.ardelean@analog.com> <20200321085315.11030-6-alexandru.ardelean@analog.com> <979ef870a4f0935e41e95e7759847eba8bd0407c.camel@analog.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 22, 2020 at 12:45:39PM +0200, Andy Shevchenko wrote: > +Cc Kees (see below about allocation size checks) > > On Sun, Mar 22, 2020 at 11:36 AM Ardelean, Alexandru > wrote: > > On Sat, 2020-03-21 at 23:38 +0200, Andy Shevchenko wrote: > > > On Sat, Mar 21, 2020 at 10:55 AM Alexandru Ardelean > > > wrote: > > ... > > > > > +static struct adi_axi_adc_conv *adi_axi_adc_conv_register(struct device > > > > *dev, > > > > + int sizeof_priv) > > > > +{ > > > > + struct adi_axi_adc_client *cl; > > > > + size_t alloc_size; > > > > + > > > > + alloc_size = sizeof(struct adi_axi_adc_client); > > > > + if (sizeof_priv) { > > > > + alloc_size = ALIGN(alloc_size, IIO_ALIGN); > > > > + alloc_size += sizeof_priv; > > > > + } > > > > + alloc_size += IIO_ALIGN - 1; > > > > > > Have you looked at linux/overflow.h? > > > > i did now; > > any hints where i should look closer? > > It seems it lacks of this kind of allocation size checks... Perhaps add one? > Kees, what do you think? > > > > > + cl = kzalloc(alloc_size, GFP_KERNEL); > > > > + if (!cl) > > > > + return ERR_PTR(-ENOMEM); My head hurts trying to read this! ;) Okay, so the base size is sizeof(struct adi_axi_adc_client). But if sizeof_priv is non-zero (this arg should be size_t not int), then we need to make the struct size ALIGNed? And then what is the "+= IIO_ALIGN - 1" for? It's not clear to me what the expect alignment/padding is here. I would probably construct this as: sizeof_self = sizeof(struct adi_axi_adc_client); if (sizeof_priv) sizeof_self = ALIGN(sizeof_self, IIO_ALIGN); if (check_add_overflow(sizeof_self, sizeof_priv, &sizeof_alloc)) return ERR_PTR(-ENOMEM); if (check_add_overflow(sizeof_alloc, IIO_ALIGN - 1, &sizeof_alloc)) return ERR_PTR(-ENOMEM); But I don't understand the "IIO_ALIGN - 1" part, so I assume this could be shortened with better use of ALIGN()? Also, this feels like a weird driver allocation overall: + struct adi_axi_adc_conv **ptr, *conv; + + ptr = devres_alloc(devm_adi_axi_adc_conv_release, sizeof(*ptr), + GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + conv = adi_axi_adc_conv_register(dev, sizeof_priv); devres_alloc() allocates storage for a _single pointer_. :P That's not useful for resource tracking. Why is devres_alloc() being called here and not down in adi_axi_adc_conv_register() and just passing the pointer back up? -- Kees Cook