linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpiolib: cdev: Don't access uninitialized descriptor
@ 2022-07-08 11:25 Viresh Kumar
  2022-07-08 13:31 ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Viresh Kumar @ 2022-07-08 11:25 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Dipen Patel, Thierry Reding
  Cc: Viresh Kumar, Vincent Guittot, linux-gpio, linux-kernel

linereq_free() can be called from in the middle of errors, where the
descriptor may be NULL for few lines. Don't access uninitialized
descriptor pointer as it leads to kernel crash:

    Unable to handle kernel NULL pointer dereference at virtual address 0000000000000008

    [...]

    Call trace:
     linereq_free+0x54/0xb8
     linereq_create+0x424/0x570
     gpio_ioctl+0x94/0x640
     __arm64_sys_ioctl+0xac/0xf0
     invoke_syscall+0x44/0x100
     el0_svc_common.constprop.3+0x6c/0xf0
     do_el0_svc+0x2c/0xb8
     el0_svc+0x20/0x60
     el0t_64_sync_handler+0x98/0xc0
     el0t_64_sync+0x170/0x174

Fixes: 2068339a6c35 ("gpiolib: cdev: Add hardware timestamp clock type")
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/gpio/gpiolib-cdev.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index f5aa5f93342a..d3d1b5aed282 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -1460,11 +1460,13 @@ static ssize_t linereq_read(struct file *file,
 static void linereq_free(struct linereq *lr)
 {
 	unsigned int i;
-	bool hte;
+	bool hte = false;
 
 	for (i = 0; i < lr->num_lines; i++) {
-		hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
-				 &lr->lines[i].desc->flags);
+		if (lr->lines[i].desc) {
+			hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
+					 &lr->lines[i].desc->flags);
+		}
 		edge_detector_stop(&lr->lines[i], hte);
 		if (lr->lines[i].desc)
 			gpiod_free(lr->lines[i].desc);
-- 
2.31.1.272.g89b43f80a514


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

* Re: [PATCH] gpiolib: cdev: Don't access uninitialized descriptor
  2022-07-08 11:25 [PATCH] gpiolib: cdev: Don't access uninitialized descriptor Viresh Kumar
@ 2022-07-08 13:31 ` Bartosz Golaszewski
  2022-07-08 15:13   ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-07-08 13:31 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Linus Walleij, Dipen Patel, Thierry Reding, Vincent Guittot,
	open list:GPIO SUBSYSTEM, Linux Kernel Mailing List

On Fri, Jul 8, 2022 at 1:25 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> linereq_free() can be called from in the middle of errors, where the
> descriptor may be NULL for few lines. Don't access uninitialized
> descriptor pointer as it leads to kernel crash:
>
>     Unable to handle kernel NULL pointer dereference at virtual address 0000000000000008
>
>     [...]
>
>     Call trace:
>      linereq_free+0x54/0xb8
>      linereq_create+0x424/0x570
>      gpio_ioctl+0x94/0x640
>      __arm64_sys_ioctl+0xac/0xf0
>      invoke_syscall+0x44/0x100
>      el0_svc_common.constprop.3+0x6c/0xf0
>      do_el0_svc+0x2c/0xb8
>      el0_svc+0x20/0x60
>      el0t_64_sync_handler+0x98/0xc0
>      el0t_64_sync+0x170/0x174
>
> Fixes: 2068339a6c35 ("gpiolib: cdev: Add hardware timestamp clock type")
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  drivers/gpio/gpiolib-cdev.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
> index f5aa5f93342a..d3d1b5aed282 100644
> --- a/drivers/gpio/gpiolib-cdev.c
> +++ b/drivers/gpio/gpiolib-cdev.c
> @@ -1460,11 +1460,13 @@ static ssize_t linereq_read(struct file *file,
>  static void linereq_free(struct linereq *lr)
>  {
>         unsigned int i;
> -       bool hte;
> +       bool hte = false;
>
>         for (i = 0; i < lr->num_lines; i++) {
> -               hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
> -                                &lr->lines[i].desc->flags);
> +               if (lr->lines[i].desc) {
> +                       hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
> +                                        &lr->lines[i].desc->flags);
> +               }
>                 edge_detector_stop(&lr->lines[i], hte);
>                 if (lr->lines[i].desc)
>                         gpiod_free(lr->lines[i].desc);
> --
> 2.31.1.272.g89b43f80a514
>

Hey Viresh!

Kent beat you to it with commit c8e27a4a5136e7230f9e4ffcf132705bf56864cc.

Bart

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

* Re: [PATCH] gpiolib: cdev: Don't access uninitialized descriptor
  2022-07-08 13:31 ` Bartosz Golaszewski
@ 2022-07-08 15:13   ` Andy Shevchenko
  2022-07-08 15:48     ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2022-07-08 15:13 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Viresh Kumar, Linus Walleij, Dipen Patel, Thierry Reding,
	Vincent Guittot, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List

On Fri, Jul 8, 2022 at 3:32 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> On Fri, Jul 8, 2022 at 1:25 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:

...

> Kent beat you to it with commit c8e27a4a5136e7230f9e4ffcf132705bf56864cc.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c8e27a4a5136e7230f9e4ffcf132705bf56864cc
Notice: this object is not reachable from any branch.

Rebased?

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] gpiolib: cdev: Don't access uninitialized descriptor
  2022-07-08 15:13   ` Andy Shevchenko
@ 2022-07-08 15:48     ` Bartosz Golaszewski
  0 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-07-08 15:48 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Viresh Kumar, Linus Walleij, Dipen Patel, Thierry Reding,
	Vincent Guittot, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List

On Fri, Jul 8, 2022 at 5:14 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Fri, Jul 8, 2022 at 3:32 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> > On Fri, Jul 8, 2022 at 1:25 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> ...
>
> > Kent beat you to it with commit c8e27a4a5136e7230f9e4ffcf132705bf56864cc.
>
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c8e27a4a5136e7230f9e4ffcf132705bf56864cc
> Notice: this object is not reachable from any branch.
>
> Rebased?
>
> --
> With Best Regards,
> Andy Shevchenko

It's in next and I just sent it out to Torvalds too.

Bart

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

end of thread, other threads:[~2022-07-08 15:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-08 11:25 [PATCH] gpiolib: cdev: Don't access uninitialized descriptor Viresh Kumar
2022-07-08 13:31 ` Bartosz Golaszewski
2022-07-08 15:13   ` Andy Shevchenko
2022-07-08 15:48     ` Bartosz Golaszewski

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).