Re: [PATCH] comedi: Annotate struct comedi_lrange with __counted_by

From: Kees Cook
Date: Sun Oct 01 2023 - 11:27:05 EST


On October 1, 2023 12:45:41 AM PDT, Julia Lawall <julia.lawall@xxxxxxxx> 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@xxxxxxxxxx>
>> > ---
>> > 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 Cook