Message ID | 5c3b7459b820e22e2ac6ce892d4aadcc119cc919.1696065263.git.christophe.jaillet@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp299903vqb; Sat, 30 Sep 2023 03:11:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELkkxfLtMuhT5H8jCqNQQ8re+2Bw607AvH0qKwq3hOo3S3ZxZFnR7OhqVWqzzNwbmtIUkQ X-Received: by 2002:a17:902:c409:b0:1c1:e818:1e76 with SMTP id k9-20020a170902c40900b001c1e8181e76mr9921644plk.6.1696068684248; Sat, 30 Sep 2023 03:11:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696068684; cv=none; d=google.com; s=arc-20160816; b=IY9BlK1xbOKbluryo8DsjtZERwnGYHTmxQ5QjR9Xn4PXffAIR9kaYg9wIxLTzKun+S adXZsIIYz58opuwoTQ00rKtkj53rUd92jzsE9+yrGcirgM6UTCvPl90vo6dzkTqUqQEl fpIXtIORtMr6iTlz55z4sqSaPGPofCM5t0drAUDguUW/qnGWTmLiXcQseqdPv4k54oUn fxJbQcXsG6yMXDqsCwa6ck1DAJVzY3bbhIL8Z0PPUr696f9lsers8pqdv7Ko0gDlQV+E t6PNzMEfYfFjRWC3mcxckcVGvdm1E1C3io03wJoXLFBJrLI72ZmFSDQCGWa44ttgLUR5 kNcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=HqO0OJ0xpYTouMij+n4RJh5b8XJeCUxxVmU/L5T7xtU=; fh=htcDEAxhw/vxfwAtSQYEOhUKWajuzulmhoNFgL9sXck=; b=xTNXI7bPazOeoLkrLqR4/hWUdHgPgI772P7g2lgqNyhAB4kpdwQUk1LaYPhlb/dhA1 nwJZAUXzewPFqsTVMUtJxwRyYwTdaTZfB9ea9RPdeKx/LIv0IwusqKG0oFgN0NBa6uIq 9KVQNnNY5IFg5FCLjXy6yQPhGRer/SlcF49LCNqncdS3qepMXNIAI9ddjPcjiPo3RdSV +qC/8qHpmlFKyfCkxNu7eEjzs0IsrtjkIUvkTP0JJ9GUEyZOUY1rsv7Tc6iYhCD1SKte 3bPHApzS4plpI6kELSU01y9VFxZ6FWlVDdfgaOu54zTBbj4/QQ/JSiWE+wa0dUnez5ip GaRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=EpHIjI+H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id kn11-20020a170903078b00b001c74d844254si2121475plb.101.2023.09.30.03.11.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Sep 2023 03:11:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=EpHIjI+H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 37BE480236EB; Sat, 30 Sep 2023 02:17:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233945AbjI3JRF (ORCPT <rfc822;chrisfriedt@gmail.com> + 20 others); Sat, 30 Sep 2023 05:17:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229588AbjI3JRC (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 30 Sep 2023 05:17:02 -0400 Received: from smtp.smtpout.orange.fr (smtp-21.smtpout.orange.fr [80.12.242.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 058B7195 for <linux-kernel@vger.kernel.org>; Sat, 30 Sep 2023 02:16:56 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id mW5lqciwUlLhemW5mqgUyK; Sat, 30 Sep 2023 11:16:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1696065415; bh=HqO0OJ0xpYTouMij+n4RJh5b8XJeCUxxVmU/L5T7xtU=; h=From:To:Cc:Subject:Date; b=EpHIjI+HuMMZ8HK0f8SwmZJ28a0d+04wdYHD9bRONTstCAVZiuB6avxQIos6ZEItc nVbK0FhaDctKX08sYwo+8tmAJEnv6ltQgDnG9fYwZpZoBVUgsKFrz+BMHupi59MPeM lgimnXmj+nnzQTAeRwa0bWoRLIp/jaeZ1pwjp3cFsxAdixy+FaKMD6mojz70CMqFDW MqyCIxpgCkZeOjHqiuGZYZcPItC1BeAai4TnekU0s1DSIfshfJMrRT8p3JZGtV0XHt MrFDwIJTX7PV+l0ZqFNDsYVj+zmnJRpnzwhfzLuVIWS231jkCTFUOwZBBvYjegsqtY 7l1TLWdCU9YMw== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 30 Sep 2023 11:16:55 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Ian Abbott <abbotti@mev.co.uk>, H Hartley Sweeten <hsweeten@visionengravers.com>, Kees Cook <keescook@chromium.org>, "Gustavo A. R. Silva" <gustavoars@kernel.org>, Nathan Chancellor <nathan@kernel.org>, Nick Desaulniers <ndesaulniers@google.com>, Tom Rix <trix@redhat.com> Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET <christophe.jaillet@wanadoo.fr>, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] comedi: Annotate struct comedi_lrange with __counted_by Date: Sat, 30 Sep 2023 11:14:47 +0200 Message-Id: <5c3b7459b820e22e2ac6ce892d4aadcc119cc919.1696065263.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sat, 30 Sep 2023 02:17:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778456916450940366 X-GMAIL-MSGID: 1778456916450940366 |
Series |
comedi: Annotate struct comedi_lrange with __counted_by
|
|
Commit Message
Christophe JAILLET
Sept. 30, 2023, 9:14 a.m. UTC
Prepare for the coming implementation by GCC and Clang of the __counted_by
attribute. Flexible array members annotated with __counted_by can have
their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS
(for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
functions).
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This patch is part of a work done in parallel of what is currently worked
on by Kees Cook.
My patches are only related to corner cases that do NOT match the
semantic of his Coccinelle script[1].
In this case, it is been spotted because of comedi_alloc_spriv().
All other usages of struct comedi_lrange seem to be static definition of
the structure that explicitly set the .length field.
[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
---
include/linux/comedi/comedidev.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Sat, Sep 30, 2023 at 11:14:47AM +0200, Christophe JAILLET wrote: > Prepare for the coming implementation by GCC and Clang of the __counted_by > attribute. Flexible array members annotated with __counted_by can have > their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS > (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family > functions). > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > This patch is part of a work done in parallel of what is currently worked > on by Kees Cook. > > My patches are only related to corner cases that do NOT match the > semantic of his Coccinelle script[1]. Nice! struct comedi_lrange { int length; struct comedi_krange range[]; }; ... static const struct comedi_lrange range_rti800_ai_10_bipolar = { 4, { BIP_RANGE(10), BIP_RANGE(1), BIP_RANGE(0.1), BIP_RANGE(0.02) } }; I'm struggling to come up with a way for Coccinelle to find this kind of thing in other places... > In this case, it is been spotted because of comedi_alloc_spriv(). > All other usages of struct comedi_lrange seem to be static definition of > the structure that explicitly set the .length field. Ah-ha, I found it in drivers/comedi/drivers/das16.c das16_ai_range(): lrange = comedi_alloc_spriv(s, struct_size(lrange, range, 1)); I was also able to find this: union jr3_pci_single_range { struct comedi_lrange l; char _reserved[offsetof(struct comedi_lrange, range[1])]; }; Which looks a lot like DEFINE_FLEX: https://lore.kernel.org/linux-hardening/20230912115937.1645707-2-przemyslaw.kitszel@intel.com/ But that above for stack varaibles rather than globals. But I'm way off topic now. ;) Reviewed-by: Kees Cook <keescook@chromium.org> > > [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci > --- > include/linux/comedi/comedidev.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/include/linux/comedi/comedidev.h b/include/linux/comedi/comedidev.h > index 0a1150900ef3..c08416a7364b 100644 > --- a/include/linux/comedi/comedidev.h > +++ b/include/linux/comedi/comedidev.h > @@ -633,7 +633,7 @@ extern const struct comedi_lrange range_unknown; > */ > struct comedi_lrange { > int length; > - struct comedi_krange range[]; > + struct comedi_krange range[] __counted_by(length); > }; > > /** > -- > 2.34.1 >
On 9/30/23 11:14, Christophe JAILLET wrote: > Prepare for the coming implementation by GCC and Clang of the __counted_by > attribute. Flexible array members annotated with __counted_by can have > their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS > (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family > functions). > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org> Thanks
On Sat, 30 Sep 2023, Kees Cook wrote: > On Sat, Sep 30, 2023 at 11:14:47AM +0200, Christophe JAILLET wrote: > > Prepare for the coming implementation by GCC and Clang of the __counted_by > > attribute. Flexible array members annotated with __counted_by can have > > their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS > > (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family > > functions). > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > --- > > This patch is part of a work done in parallel of what is currently worked > > on by Kees Cook. > > > > My patches are only related to corner cases that do NOT match the > > semantic of his Coccinelle script[1]. > > Nice! > > struct comedi_lrange { > int length; > struct comedi_krange range[]; > }; > ... > static const struct comedi_lrange range_rti800_ai_10_bipolar = { > 4, { > BIP_RANGE(10), > BIP_RANGE(1), > BIP_RANGE(0.1), > BIP_RANGE(0.02) > } > }; > > I'm struggling to come up with a way for Coccinelle to find this kind of > thing in other places... > > > In this case, it is been spotted because of comedi_alloc_spriv(). > > All other usages of struct comedi_lrange seem to be static definition of > > the structure that explicitly set the .length field. > > Ah-ha, I found it in drivers/comedi/drivers/das16.c das16_ai_range(): > > lrange = comedi_alloc_spriv(s, > struct_size(lrange, range, 1)); This is not found due to the regular expression used for the name of the alloc function. Maybe you could drop it entirely? Maybe you could just check for alloc somewhere in the string? identifier ALLOC =~ "alloc"; works in this case. Also, I see in the link that you have: // Options: --all-includes You can actually force this by putting #spatch --all-includes and any other options you want. julia > > I was also able to find this: > > union jr3_pci_single_range { > struct comedi_lrange l; > char _reserved[offsetof(struct comedi_lrange, range[1])]; > }; > > Which looks a lot like DEFINE_FLEX: > https://lore.kernel.org/linux-hardening/20230912115937.1645707-2-przemyslaw.kitszel@intel.com/ > But that above for stack varaibles rather than globals. But I'm way off > topic now. ;) > > Reviewed-by: Kees Cook <keescook@chromium.org> > > > > > [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci > > --- > > include/linux/comedi/comedidev.h | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/include/linux/comedi/comedidev.h b/include/linux/comedi/comedidev.h > > index 0a1150900ef3..c08416a7364b 100644 > > --- a/include/linux/comedi/comedidev.h > > +++ b/include/linux/comedi/comedidev.h > > @@ -633,7 +633,7 @@ extern const struct comedi_lrange range_unknown; > > */ > > struct comedi_lrange { > > int length; > > - struct comedi_krange range[]; > > + struct comedi_krange range[] __counted_by(length); > > }; > > > > /** > > -- > > 2.34.1 > > > > -- > Kees Cook >
On Sat, 30 Sep 2023, Kees Cook wrote: > On Sat, Sep 30, 2023 at 11:14:47AM +0200, Christophe JAILLET wrote: > > Prepare for the coming implementation by GCC and Clang of the __counted_by > > attribute. Flexible array members annotated with __counted_by can have > > their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS > > (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family > > functions). > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > --- > > This patch is part of a work done in parallel of what is currently worked > > on by Kees Cook. > > > > My patches are only related to corner cases that do NOT match the > > semantic of his Coccinelle script[1]. > > Nice! > > struct comedi_lrange { > int length; > struct comedi_krange range[]; > }; > ... > static const struct comedi_lrange range_rti800_ai_10_bipolar = { > 4, { > BIP_RANGE(10), > BIP_RANGE(1), > BIP_RANGE(0.1), > BIP_RANGE(0.02) > } > }; > > I'm struggling to come up with a way for Coccinelle to find this kind of > thing in other places... Kees, what exactly are you trying to match? Static allocations? julia > > > In this case, it is been spotted because of comedi_alloc_spriv(). > > All other usages of struct comedi_lrange seem to be static definition of > > the structure that explicitly set the .length field. > > Ah-ha, I found it in drivers/comedi/drivers/das16.c das16_ai_range(): > > lrange = comedi_alloc_spriv(s, > struct_size(lrange, range, 1)); > > I was also able to find this: > > union jr3_pci_single_range { > struct comedi_lrange l; > char _reserved[offsetof(struct comedi_lrange, range[1])]; > }; > > Which looks a lot like DEFINE_FLEX: > https://lore.kernel.org/linux-hardening/20230912115937.1645707-2-przemyslaw.kitszel@intel.com/ > But that above for stack varaibles rather than globals. But I'm way off > topic now. ;) > > Reviewed-by: Kees Cook <keescook@chromium.org> > > > > > [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci > > --- > > include/linux/comedi/comedidev.h | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/include/linux/comedi/comedidev.h b/include/linux/comedi/comedidev.h > > index 0a1150900ef3..c08416a7364b 100644 > > --- a/include/linux/comedi/comedidev.h > > +++ b/include/linux/comedi/comedidev.h > > @@ -633,7 +633,7 @@ extern const struct comedi_lrange range_unknown; > > */ > > struct comedi_lrange { > > int length; > > - struct comedi_krange range[]; > > + struct comedi_krange range[] __counted_by(length); > > }; > > > > /** > > -- > > 2.34.1 > > > > -- > Kees Cook >
Le 01/10/2023 à 09:25, Julia Lawall a écrit : > > This is not found due to the regular expression used for the name of the > alloc function. Maybe you could drop it entirely? Maybe you could just > check for alloc somewhere in the string? That's how I found it. I simplified a lot Kees's script and looked for function names that did *not* match his regex. Functions that: - return a pointer to a struct - are used with struct_size() - store the value used to compite the size in another field of the struct are good enough candidates. I think that removing the regex all together would be just good enough. CJ > > identifier ALLOC =~ "alloc"; > > works in this case. > > Also, I see in the link that you have: > > // Options: --all-includes > > You can actually force this by putting > > #spatch --all-includes Nice, thanks for the tip. > > and any other options you want. > > julia > >
On October 1, 2023 12:45:41 AM PDT, Julia Lawall <julia.lawall@inria.fr> wrote: > > >On Sat, 30 Sep 2023, Kees Cook wrote: > >> On Sat, Sep 30, 2023 at 11:14:47AM +0200, Christophe JAILLET wrote: >> > Prepare for the coming implementation by GCC and Clang of the __counted_by >> > attribute. Flexible array members annotated with __counted_by can have >> > their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS >> > (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family >> > functions). >> > >> > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> >> > --- >> > This patch is part of a work done in parallel of what is currently worked >> > on by Kees Cook. >> > >> > My patches are only related to corner cases that do NOT match the >> > semantic of his Coccinelle script[1]. >> >> Nice! >> >> struct comedi_lrange { >> int length; >> struct comedi_krange range[]; >> }; >> ... >> static const struct comedi_lrange range_rti800_ai_10_bipolar = { >> 4, { >> BIP_RANGE(10), >> BIP_RANGE(1), >> BIP_RANGE(0.1), >> BIP_RANGE(0.02) >> } >> }; >> >> I'm struggling to come up with a way for Coccinelle to find this kind of >> thing in other places... > >Kees, what exactly are you trying to match? Static allocations? I need to count the number of initialized elements in the flexible array that is the last member and see if it matches a value set in another member. E.g. the above sets 4 values for the last array member and then sets another member to 4. -Kees
Kees, You can try the following. julia #spatch --all-includes @r@ identifier i,j; type T; @@ struct i { ... T j[]; } @s@ identifier r.i; constant ini; identifier j; initializer list [n] is2; position p; identifier x; @@ struct i@p x = { ..., .j = ini, ..., { is2 } } ; @script:ocaml@ ini << s.ini; i << r.i; j << s.j; n << s.n; p << s.p; @@ try let ini = int_of_string ini in if n = ini then Printf.printf "%s:%d: struct %s: field %s is the counter for the flex array\n" (List.hd p).file (List.hd p).line i j with _-> () (* not an explicit integer *) @s2@ identifier r.i; constant ini; initializer list [n] is; initializer list [n2] is2; position p; identifier x; @@ struct i@p x = { is, ini, ..., { is2 } }; @script:ocaml@ ini << s2.ini; i << r.i; n << s2.n; n2 << s2.n2; p << s2.p; @@ try let ini = int_of_string ini in if n2 = ini then Printf.printf "%s:%d: struct %s: field at offset %d is the counter for the flex array\n" (List.hd p).file (List.hd p).line i n with _-> () (* not an explicit integer *)
On Sun, Oct 01, 2023 at 09:14:02PM +0200, Julia Lawall wrote: > Kees, > > You can try the following. Cool! Yeah, this finds the example: drivers/comedi/drivers/rti800.c:74: struct comedi_lrange: field at offset 0 is the counter for the flex array drivers/comedi/drivers/rti800.c:83: struct comedi_lrange: field at offset 0 is the counter for the flex array drivers/comedi/drivers/rti800.c:92: struct comedi_lrange: field at offset 0 is the counter for the flex array I'll run it on the whole codebase...
On Sun, Oct 01, 2023 at 02:05:46PM -0700, Kees Cook wrote: > On Sun, Oct 01, 2023 at 09:14:02PM +0200, Julia Lawall wrote: > > Kees, > > > > You can try the following. > > Cool! Yeah, this finds the example: > > drivers/comedi/drivers/rti800.c:74: struct comedi_lrange: field at offset 0 is the counter for the flex array > drivers/comedi/drivers/rti800.c:83: struct comedi_lrange: field at offset 0 is the counter for the flex array > drivers/comedi/drivers/rti800.c:92: struct comedi_lrange: field at offset 0 is the counter for the flex array > > I'll run it on the whole codebase... It found only the struct comedi_lrange instances, but that's good to know. :)
On Sun, Oct 01, 2023 at 02:22:17PM -0700, Kees Cook wrote: > On Sun, Oct 01, 2023 at 02:05:46PM -0700, Kees Cook wrote: > > On Sun, Oct 01, 2023 at 09:14:02PM +0200, Julia Lawall wrote: > > > Kees, > > > > > > You can try the following. > > > > Cool! Yeah, this finds the example: > > > > drivers/comedi/drivers/rti800.c:74: struct comedi_lrange: field at offset 0 is the counter for the flex array > > drivers/comedi/drivers/rti800.c:83: struct comedi_lrange: field at offset 0 is the counter for the flex array > > drivers/comedi/drivers/rti800.c:92: struct comedi_lrange: field at offset 0 is the counter for the flex array > > > > I'll run it on the whole codebase... > > It found only the struct comedi_lrange instances, but that's good to > know. :) On a related note, why doesn't this work? @allocated@ identifier STRUCT, ARRAY; expression COUNT; struct STRUCT *PTR; identifier ALLOC; type ELEMENT_TYPE; @@ PTR = ALLOC(..., sizeof(\(*PTR\|struct STRUCT\)) + COUNT * sizeof(\(*PTR->ARRAY\|PTR->ARRAY[0]\|ELEMENT_TYPE\)), ...); minus: parse error: File "alloc.cocci", line 15, column 34, charpos = 485 around = 'struct', whole content = PTR = ALLOC(..., sizeof(\(*PTR\|struct STRUCT\)) + if I drop "struct", then it complains about ELEMENT_TYPE... -Kees
On Sun, 1 Oct 2023, Kees Cook wrote: > On Sun, Oct 01, 2023 at 02:22:17PM -0700, Kees Cook wrote: > > On Sun, Oct 01, 2023 at 02:05:46PM -0700, Kees Cook wrote: > > > On Sun, Oct 01, 2023 at 09:14:02PM +0200, Julia Lawall wrote: > > > > Kees, > > > > > > > > You can try the following. > > > > > > Cool! Yeah, this finds the example: > > > > > > drivers/comedi/drivers/rti800.c:74: struct comedi_lrange: field at offset 0 is the counter for the flex array > > > drivers/comedi/drivers/rti800.c:83: struct comedi_lrange: field at offset 0 is the counter for the flex array > > > drivers/comedi/drivers/rti800.c:92: struct comedi_lrange: field at offset 0 is the counter for the flex array > > > > > > I'll run it on the whole codebase... > > > > It found only the struct comedi_lrange instances, but that's good to > > know. :) > > On a related note, why doesn't this work? > > @allocated@ > identifier STRUCT, ARRAY; > expression COUNT; > struct STRUCT *PTR; > identifier ALLOC; > type ELEMENT_TYPE; > @@ > > PTR = ALLOC(..., sizeof(\(*PTR\|struct STRUCT\)) + > COUNT * sizeof(\(*PTR->ARRAY\|PTR->ARRAY[0]\|ELEMENT_TYPE\)), ...); > > > minus: parse error: > File "alloc.cocci", line 15, column 34, charpos = 485 > around = 'struct', > whole content = PTR = ALLOC(..., sizeof(\(*PTR\|struct STRUCT\)) + > > if I drop "struct", then it complains about ELEMENT_TYPE... The sizeof with an expression argument is treated differently than the sizeof with a type argument. So you need to write: @allocated@ identifier STRUCT, ARRAY; expression COUNT; struct STRUCT *PTR; identifier ALLOC; type ELEMENT_TYPE; @@ PTR = ALLOC(..., \(sizeof(*PTR)\|sizeof(struct STRUCT)\) + COUNT * \(sizeof(*PTR->ARRAY)\|sizeof(PTR->ARRAY[0])\|sizeof(ELEMENT_TYPE)\), ...); julia
On Mon, Oct 02, 2023 at 07:38:42AM +0200, Julia Lawall wrote: > The sizeof with an expression argument is treated differently than the > sizeof with a type argument. So you need to write: > > @allocated@ > identifier STRUCT, ARRAY; > expression COUNT; > struct STRUCT *PTR; > identifier ALLOC; > type ELEMENT_TYPE; > @@ > > PTR = ALLOC(..., \(sizeof(*PTR)\|sizeof(struct STRUCT)\) + > COUNT * \(sizeof(*PTR->ARRAY)\|sizeof(PTR->ARRAY[0])\|sizeof(ELEMENT_TYPE)\), ...); Ah! Thank you thank you! Yes, this works great now. :)
Le 01/10/2023 à 23:22, Kees Cook a écrit : > On Sun, Oct 01, 2023 at 02:05:46PM -0700, Kees Cook wrote: >> On Sun, Oct 01, 2023 at 09:14:02PM +0200, Julia Lawall wrote: >>> Kees, >>> >>> You can try the following. >> >> Cool! Yeah, this finds the example: >> >> drivers/comedi/drivers/rti800.c:74: struct comedi_lrange: field at offset 0 is the counter for the flex array >> drivers/comedi/drivers/rti800.c:83: struct comedi_lrange: field at offset 0 is the counter for the flex array >> drivers/comedi/drivers/rti800.c:92: struct comedi_lrange: field at offset 0 is the counter for the flex array >> >> I'll run it on the whole codebase... > > It found only the struct comedi_lrange instances, but that's good to > know. :) > Hi, (removing most of people from the thread) I found another one. struct ocotp_devtype_data has a field 'num_entry' which is the number of entries in 'entry' flex array. [1]: https://elixir.bootlin.com/linux/v6.6/source/drivers/nvmem/imx-ocotp-ele.c#L28 [2]: https://elixir.bootlin.com/linux/v6.6/source/drivers/nvmem/imx-ocotp-ele.c#L143 CJ
diff --git a/include/linux/comedi/comedidev.h b/include/linux/comedi/comedidev.h index 0a1150900ef3..c08416a7364b 100644 --- a/include/linux/comedi/comedidev.h +++ b/include/linux/comedi/comedidev.h @@ -633,7 +633,7 @@ extern const struct comedi_lrange range_unknown; */ struct comedi_lrange { int length; - struct comedi_krange range[]; + struct comedi_krange range[] __counted_by(length); }; /**