linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [drivers/gpio] Question about `ljca_gpio_config`: misuse of __counted_by
       [not found] <CAPbMC77iDKaJvc_8Qq2SqQ-mnkAJyeeADGhWx-jgUV7KsCi28Q@mail.gmail.com>
@ 2024-07-26 20:07 ` Linus Walleij
  2024-07-29 13:52   ` Gustavo A. R. Silva
  0 siblings, 1 reply; 2+ messages in thread
From: Linus Walleij @ 2024-07-26 20:07 UTC (permalink / raw)
  To: Haoyu Li, Gustavo A. R. Silva
  Cc: Bartosz Golaszewski, linux-gpio, linux-kernel

Hi Haoyu,

On Wed, Jul 24, 2024 at 11:12 AM Haoyu Li <lihaoyu499@gmail.com> wrote:

> Dear Linux Developers for GPIO SUBSYSTEM,
>
> We are curious about the use of `struct ljca_gpio_packet *packet` in the function `ljca_gpio_config` (https://elixir.bootlin.com/linux/v6.10/source/drivers/gpio/gpio-ljca.c#L80).
> ```
> static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
>    u8 config)
> {
> struct ljca_gpio_packet *packet =
> (struct ljca_gpio_packet *)ljca_gpio->obuf;
> int ret;
>
> mutex_lock(&ljca_gpio->trans_lock);
> packet->item[0].index = gpio_id;
> packet->item[0].value = config | ljca_gpio->connect_mode[gpio_id];
> packet->num = 1;
>
> ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_CONFIG, (u8 *)packet,
>    struct_size(packet, item, packet->num), NULL, 0);
> mutex_unlock(&ljca_gpio->trans_lock);
>
> return ret < 0 ? ret : 0;
> }
> ```
> The definition of `struct ljca_gpio_packet` is at https://elixir.bootlin.com/linux/v6.10/source/drivers/gpio/gpio-ljca.c#L53.
> ```
> struct ljca_gpio_packet {
> u8 num;
> struct ljca_gpio_op item[] __counted_by(num);
> } __packed;
> ```
>
> Our question is: The `item` member of `struct ljca_gpio_packet` is annotated with "__counted_by". Only if we set `packet->num = 1` before accessing `packet->item[0]`, the flexible member `item` can be properly bounds-checked at run-time when enabling CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Or there will be a warning from each access prior to the initialization because the number of elements is zero.
> So we think relocating `packet->num = 1` before accessing `packet->item[0]` is needed.
>
> Here is a fix example of a similar situation : https://lore.kernel.org/stable/20240613113225.898955993@linuxfoundation.org/.
>
> Please kindly correct us if we missed any key information. Looking forward to your response!

This is a Gustavo AR Silvia question, so let's loop him in.
(I think you're right, and we should make a patch.)

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [drivers/gpio] Question about `ljca_gpio_config`: misuse of __counted_by
  2024-07-26 20:07 ` [drivers/gpio] Question about `ljca_gpio_config`: misuse of __counted_by Linus Walleij
@ 2024-07-29 13:52   ` Gustavo A. R. Silva
  0 siblings, 0 replies; 2+ messages in thread
From: Gustavo A. R. Silva @ 2024-07-29 13:52 UTC (permalink / raw)
  To: Linus Walleij, Haoyu Li, Gustavo A. R. Silva
  Cc: Bartosz Golaszewski, linux-gpio, linux-kernel, linux-hardening

Hi all,

On 26/07/24 14:07, Linus Walleij wrote:
> Hi Haoyu,
> 
> On Wed, Jul 24, 2024 at 11:12 AM Haoyu Li <lihaoyu499@gmail.com> wrote:
> 
>> Dear Linux Developers for GPIO SUBSYSTEM,
>>
>> We are curious about the use of `struct ljca_gpio_packet *packet` in the function `ljca_gpio_config` (https://elixir.bootlin.com/linux/v6.10/source/drivers/gpio/gpio-ljca.c#L80).
>> ```
>> static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
>>     u8 config)
>> {
>> struct ljca_gpio_packet *packet =
>> (struct ljca_gpio_packet *)ljca_gpio->obuf;
>> int ret;
>>
>> mutex_lock(&ljca_gpio->trans_lock);
>> packet->item[0].index = gpio_id;
>> packet->item[0].value = config | ljca_gpio->connect_mode[gpio_id];
>> packet->num = 1;
>>
>> ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_CONFIG, (u8 *)packet,
>>     struct_size(packet, item, packet->num), NULL, 0);
>> mutex_unlock(&ljca_gpio->trans_lock);
>>
>> return ret < 0 ? ret : 0;
>> }
>> ```
>> The definition of `struct ljca_gpio_packet` is at https://elixir.bootlin.com/linux/v6.10/source/drivers/gpio/gpio-ljca.c#L53.
>> ```
>> struct ljca_gpio_packet {
>> u8 num;
>> struct ljca_gpio_op item[] __counted_by(num);
>> } __packed;
>> ```
>>
>> Our question is: The `item` member of `struct ljca_gpio_packet` is annotated with "__counted_by". Only if we set `packet->num = 1` before accessing `packet->item[0]`, the flexible member `item` can be properly bounds-checked at run-time when enabling CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Or there will be a warning from each access prior to the initialization because the number of elements is zero.
>> So we think relocating `packet->num = 1` before accessing `packet->item[0]` is needed.
>>
>> Here is a fix example of a similar situation : https://lore.kernel.org/stable/20240613113225.898955993@linuxfoundation.org/.
>>
>> Please kindly correct us if we missed any key information. Looking forward to your response!
> 
> This is a Gustavo AR Silvia question, so let's loop him in.
> (I think you're right, and we should make a patch.)

Yes! `packet->num = 1;` should be relocated:

diff --git a/drivers/gpio/gpio-ljca.c b/drivers/gpio/gpio-ljca.c
index dfec9fbfc7a9..c2a9b4253974 100644
--- a/drivers/gpio/gpio-ljca.c
+++ b/drivers/gpio/gpio-ljca.c
@@ -82,9 +82,9 @@ static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
         int ret;

         mutex_lock(&ljca_gpio->trans_lock);
+       packet->num = 1;
         packet->item[0].index = gpio_id;
         packet->item[0].value = config | ljca_gpio->connect_mode[gpio_id];
-       packet->num = 1;

         ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_CONFIG, (u8 *)packet,
                             struct_size(packet, item, packet->num), NULL, 0);

stable should be CC'd and the following tag included:

Fixes: 1034cc423f1b ("1034cc423f1b4a7a9a56d310ca980fcd2753e11d")

Thanks for catching this! :)
--
Gustavo

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-07-29 13:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CAPbMC77iDKaJvc_8Qq2SqQ-mnkAJyeeADGhWx-jgUV7KsCi28Q@mail.gmail.com>
2024-07-26 20:07 ` [drivers/gpio] Question about `ljca_gpio_config`: misuse of __counted_by Linus Walleij
2024-07-29 13:52   ` Gustavo A. R. Silva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).