platform/x86: thinkpad_acpi: Switch to common use of attributes
authorLen Baker <len.baker@gmx.com>
Sun, 26 Sep 2021 11:19:08 +0000 (13:19 +0200)
committerHans de Goede <hdegoede@redhat.com>
Tue, 28 Sep 2021 15:02:11 +0000 (17:02 +0200)
commitc99ca78d67a67e2828be417ed4006f1a3c0addb5
treeca7fd5c23f55d2182b287caebe329cab13d8c3bc
parent599482c58ebd007f24f8eba99fbda5d957e773e5
platform/x86: thinkpad_acpi: Switch to common use of attributes

As noted in the "Deprecated Interfaces, Language Features, Attributes,
and Conventions" documentation [1], size calculations (especially
multiplication) should not be performed in memory allocator (or similar)
function arguments due to the risk of them overflowing. This could lead
to values wrapping around and a smaller allocation being made than the
caller was expecting. Using those allocations could lead to linear
overflows of heap memory and other misbehaviors.

So, to avoid open-coded arithmetic in the kzalloc() call inside the
create_attr_set() function the code must be refactored. Using the
struct_size() helper is the fast solution but it is better to switch
this code to common use of attributes.

Then, remove all the custom code to manage hotkey attributes and use the
attribute_group structure instead, refactoring the code accordingly.
Also, to manage the optional hotkey attributes (hotkey_tablet_mode and
hotkey_radio_sw) use the is_visible callback from the same structure.

Moreover, now the hotkey_init_tablet_mode() function never returns a
negative number. So, the check after the call can be safely removed.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Len Baker <len.baker@gmx.com>
Link: https://lore.kernel.org/r/20210926111908.6950-1-len.baker@gmx.com
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
drivers/platform/x86/thinkpad_acpi.c