The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
To: Linus Walleij <linus.walleij@linaro.org>,
	Haoyu Li <lihaoyu499@gmail.com>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Bartosz Golaszewski <brgl@bgdev.pl>,
	linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [drivers/gpio] Question about `ljca_gpio_config`: misuse of __counted_by
Date: Mon, 29 Jul 2024 07:52:39 -0600	[thread overview]
Message-ID: <d5b55e8d-a346-4dd8-adbe-bafafa43175e@embeddedor.com> (raw)
In-Reply-To: <CACRpkdbaRNigfU_mLqcJJckwFz0+14NqWP_TpYqbONCa6wAH6g@mail.gmail.com>

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

      reply	other threads:[~2024-07-29 13:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d5b55e8d-a346-4dd8-adbe-bafafa43175e@embeddedor.com \
    --to=gustavo@embeddedor.com \
    --cc=brgl@bgdev.pl \
    --cc=gustavoars@kernel.org \
    --cc=lihaoyu499@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox