Re: [RESEND PATCH v2] params: Annotate struct module_param_attrs with __counted_by()

From: Thorsten Blum
Date: Fri Sep 13 2024 - 20:23:38 EST


On 14. Sep 2024, at 01:44, Nathan Chancellor <nathan@xxxxxxxxxx> wrote:
> On Sat, Sep 14, 2024 at 01:32:19AM +0200, Thorsten Blum wrote:
>> Thanks for reporting this.
>>
>> Changing
>>
>> memset(&mk->mp->attrs[mk->mp->num - 1], 0, sizeof(mk->mp->attrs[0]));
>>
>> to
>>
>> memset(mk->mp->attrs + mk->mp->num - 1, 0, sizeof(mk->mp->attrs[0]));
>>
>> fixes the false-positive warning
>>
>> memset: detected buffer overflow: 32 byte write of buffer size 0
>>
>> even though the pointers have the same value. Does anyone know why?
>
> Might be a good question for Bill? The full context is available
> starting at:
>
> https://lore.kernel.org/all/20240913164630.GA4091534@thelio-3990X/
>
> I wonder if the krealloc() has something to do with it? I should try GCC
> but I don't have a tip of tree copy handy at the moment and I am also
> rushing at the end of my day to pack for my travels to LPC :)

I think the problem is with __builtin_dynamic_object_size().

memset(p,,) calls __struct_size(p), which calls
__builtin_dynamic_object_size(p, 0) and this behaves weirdly:

__builtin_dynamic_object_size(&mk->mp->attrs[mk->mp->num - 1], 0);

evaluates to 0, but

__builtin_dynamic_object_size(mk->mp->attrs + mk->mp->num - 1, 0);

evaluates to 4294967295.

Both values are wrong, but the latter doesn't trigger the
false-positive warning.