* [PATCH] gpiolib: fix invalid pointer access in debugfs
@ 2025-10-31 15:06 Bartosz Golaszewski
2025-11-01 8:55 ` Ilya Gavrilov
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2025-10-31 15:06 UTC (permalink / raw)
To: Linus Walleij, Andy Shevchenko
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
If the memory allocation in gpiolib_seq_start() fails, the s->private
field remains uninitialized and is later dereferenced without checking
in gpiolib_seq_stop(). Initialize s->private to NULL before calling
kzalloc() and check it before dereferencing it.
Fixes: e348544f7994 ("gpio: protect the list of GPIO devices with SRCU")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
This was brought to my attention by a person under sanctions. This is a
simplified version of their initial patch.
drivers/gpio/gpiolib.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a81981336b36..fdb6a002dbda 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -5303,6 +5303,8 @@ static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
struct gpio_device *gdev;
loff_t index = *pos;
+ s->private = NULL;
+
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv)
return NULL;
@@ -5338,6 +5340,9 @@ static void gpiolib_seq_stop(struct seq_file *s, void *v)
{
struct gpiolib_seq_priv *priv = s->private;
+ if (!priv)
+ return;
+
srcu_read_unlock(&gpio_devices_srcu, priv->idx);
kfree(priv);
}
--
2.48.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] gpiolib: fix invalid pointer access in debugfs
2025-10-31 15:06 [PATCH] gpiolib: fix invalid pointer access in debugfs Bartosz Golaszewski
@ 2025-11-01 8:55 ` Ilya Gavrilov
2025-11-01 22:49 ` Linus Walleij
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ilya Gavrilov @ 2025-11-01 8:55 UTC (permalink / raw)
To: Bartosz Golaszewski, Linus Walleij, Andy Shevchenko
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
Hi Bartosz,
On 10/31/25 18:06, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> If the memory allocation in gpiolib_seq_start() fails, the s->private
> field remains uninitialized and is later dereferenced without checking
> in gpiolib_seq_stop(). Initialize s->private to NULL before calling
> kzalloc() and check it before dereferencing it.
>
> Fixes: e348544f7994 ("gpio: protect the list of GPIO devices with SRCU")
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
> This was brought to my attention by a person under sanctions. This is a
> simplified version of their initial patch.
>
> drivers/gpio/gpiolib.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index a81981336b36..fdb6a002dbda 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -5303,6 +5303,8 @@ static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
> struct gpio_device *gdev;
> loff_t index = *pos;
>
> + s->private = NULL;
> +
> priv = kzalloc(sizeof(*priv), GFP_KERNEL);
> if (!priv)
> return NULL;
> @@ -5338,6 +5340,9 @@ static void gpiolib_seq_stop(struct seq_file *s, void *v)
> {
> struct gpiolib_seq_priv *priv = s->private;
>
> + if (!priv)
> + return;
> +
> srcu_read_unlock(&gpio_devices_srcu, priv->idx);
> kfree(priv);
> }
Maybe in this case it is better to signal an error and
explicitly return ERR_PTR(-ENOMEM) in gpiolib_seq_start(), rather than ignore it?
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] gpiolib: fix invalid pointer access in debugfs
2025-10-31 15:06 [PATCH] gpiolib: fix invalid pointer access in debugfs Bartosz Golaszewski
2025-11-01 8:55 ` Ilya Gavrilov
@ 2025-11-01 22:49 ` Linus Walleij
2025-11-02 8:40 ` Markus Elfring
2025-11-03 7:45 ` Andy Shevchenko
3 siblings, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2025-11-01 22:49 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Andy Shevchenko, linux-gpio, linux-kernel, Bartosz Golaszewski
On Fri, Oct 31, 2025 at 4:06 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> If the memory allocation in gpiolib_seq_start() fails, the s->private
> field remains uninitialized and is later dereferenced without checking
> in gpiolib_seq_stop(). Initialize s->private to NULL before calling
> kzalloc() and check it before dereferencing it.
>
> Fixes: e348544f7994 ("gpio: protect the list of GPIO devices with SRCU")
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] gpiolib: fix invalid pointer access in debugfs
2025-10-31 15:06 [PATCH] gpiolib: fix invalid pointer access in debugfs Bartosz Golaszewski
2025-11-01 8:55 ` Ilya Gavrilov
2025-11-01 22:49 ` Linus Walleij
@ 2025-11-02 8:40 ` Markus Elfring
2025-11-03 7:45 ` Andy Shevchenko
3 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2025-11-02 8:40 UTC (permalink / raw)
To: Bartosz Golaszewski, linux-gpio, Andy Shevchenko, Linus Walleij
Cc: Bartosz Golaszewski, LKML
…
> ---
> This was brought to my attention by a person under sanctions. This is a
> simplified version of their initial patch.
>
> drivers/gpio/gpiolib.c | 5 +++++
…
Would the tag “Reported-by” become relevant here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.18-rc3#n539
Regards,
Markus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] gpiolib: fix invalid pointer access in debugfs
2025-10-31 15:06 [PATCH] gpiolib: fix invalid pointer access in debugfs Bartosz Golaszewski
` (2 preceding siblings ...)
2025-11-02 8:40 ` Markus Elfring
@ 2025-11-03 7:45 ` Andy Shevchenko
3 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2025-11-03 7:45 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Linus Walleij, linux-gpio, linux-kernel, Bartosz Golaszewski
On Fri, Oct 31, 2025 at 04:06:31PM +0100, Bartosz Golaszewski wrote:
>
> If the memory allocation in gpiolib_seq_start() fails, the s->private
> field remains uninitialized and is later dereferenced without checking
> in gpiolib_seq_stop(). Initialize s->private to NULL before calling
> kzalloc() and check it before dereferencing it.
...
> static void gpiolib_seq_stop(struct seq_file *s, void *v)
> {
> struct gpiolib_seq_priv *priv = s->private;
>
> + if (!priv)
> + return;
My preference is to have the assignment be decoupled in such a case:
struct gpiolib_seq_priv *priv;
priv = s->private;
if (!priv)
return;
This will prevent from doing subtle mistakes (as dereferencing before check and
so on) in the future. Not that I expect this function to grow that way, but still...
always keep in mind that somebody who is not familiar with the code may take the
piece as high standard in the kernel and copy to their code without much thinking.
> srcu_read_unlock(&gpio_devices_srcu, priv->idx);
> kfree(priv);
> }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-11-03 7:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-31 15:06 [PATCH] gpiolib: fix invalid pointer access in debugfs Bartosz Golaszewski
2025-11-01 8:55 ` Ilya Gavrilov
2025-11-01 22:49 ` Linus Walleij
2025-11-02 8:40 ` Markus Elfring
2025-11-03 7:45 ` Andy Shevchenko
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).