linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Alan M Butler <alanbutty12@gmail.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>,
	aletes.xgr@gmail.com, linux-input@vger.kernel.org
Subject: Re: [PATCH v2] Input: gpio_keys - fix NULL pointer dereference for dt case
Date: Tue, 21 Aug 2012 22:58:16 -0700	[thread overview]
Message-ID: <20120822055816.GF20624@core.coreip.homeip.net> (raw)
In-Reply-To: <CADhrpJ4xYNjRxcfnN-+ijGE-fNSNYfsTqumiKsodZiw41yk9+w@mail.gmail.com>

On Mon, Aug 20, 2012 at 09:44:40PM +0100, Alan M Butler wrote:
> On 20 August 2012 19:48, Fabio Estevam <fabio.estevam@freescale.com> wrote:
> > Commit 30161f6b2e7d1 (Input: gpio_keys - clean up device tree parser)
> > introduced the following kernel crash for a dt based kernel:
> > ..
> > Unable to handle kernel NULL pointer dereference at virtual address 00000004
> > pgd = 80004000
> > [00000004] *pgd=00000000
> > Internal error: Oops: 805 [#1] ARM
> > Modules linked in:
> > CPU: 0    Not tainted  (3.5.0-next-20120802+ #1366)
> > PC is at gpio_keys_probe+0x154/0x8c0
> > LR is at of_gpiochip_find_and_xlate+0x54/0x70
> > pc : [<803ddbac>]    lr : [<801ed2f4>]    psr: 60000013
> > sp : 9f851df8  ip : 00000002  fp : 9f851e5c
> > r10: 9f873d00  r9 : 00000001  r8 : 9fa2f900
> > r7 : 809c0920  r6 : 9f873d08  r5 : 00000000  r4 : 809c09d4
> > r3 : 805af10c  r2 : 000000c0  r1 : 9f851db4  r0 : 000000d5
> > Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment kernel
> > Control: 10c5387d  Table: 90004019  DAC: 00000015
> > Process swapper (pid: 1, stack limit = 0x9f8502e8)
> > Stack: (0x9f851df8 to 0x9f852000)
> > ....
> >
> > The reason for the crash was due to 'button = &pdata->buttons[i++];'
> > returning a NULL pointer, and then 'button' was accessed afterwards
> > without checking for NULL.
> >
> > Fix this by correctly assigning 'pdata->buttons' and also add a NULL
> > pointer check for 'button' pointer.
> >
> > Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>
> > ---
> > Changes since v1:
> > - Fix the location of 'button = pdata->buttons'. This fixes the probing of multiple keys.
> >
> >  drivers/input/keyboard/gpio_keys.c |   13 ++++++++++---
> >  1 file changed, 10 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
> > index 6ee68ec..c90c152 100644
> > --- a/drivers/input/keyboard/gpio_keys.c
> > +++ b/drivers/input/keyboard/gpio_keys.c
> > @@ -559,7 +559,6 @@ gpio_keys_get_devtree_pdata(struct device *dev)
> >         struct gpio_keys_button *button;
> >         int error;
> >         int nbuttons;
> > -       int i;
> >
> >         node = dev->of_node;
> >         if (!node) {
> > @@ -580,9 +579,12 @@ gpio_keys_get_devtree_pdata(struct device *dev)
> >                 goto err_out;
> >         }
> >
> > +       pdata->buttons = (struct gpio_keys_button *) (pdata + 1);
> > +       pdata->nbuttons = nbuttons;
> > +
> >         pdata->rep = !!of_get_property(node, "autorepeat", NULL);
> > +       button = pdata->buttons;
> >
> > -       i = 0;
> >         for_each_child_of_node(node, pp) {
> >                 enum of_gpio_flags flags;
> >
> > @@ -592,7 +594,10 @@ gpio_keys_get_devtree_pdata(struct device *dev)
> >                         continue;
> >                 }
> >
> > -               button = &pdata->buttons[i++];
> > +               if (!button) {
> > +                       error = -ENOMEM;
> > +                       goto err_free_pdata;
> > +               }

This tests impossible condition.

> >
> >                 button->gpio = of_get_gpio_flags(pp, 0, &flags);
> >                 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
> > @@ -614,6 +619,8 @@ gpio_keys_get_devtree_pdata(struct device *dev)
> >                 if (of_property_read_u32(pp, "debounce-interval",
> >                                          &button->debounce_interval))
> >                         button->debounce_interval = 5;
> > +
> > +               button++;
> >         }
> >
> >         if (pdata->nbuttons == 0) {
> > --
> > 1.7.9.5
> >
> >
> 
> Tested-by: Alan M Butler <alanbutty12@gmail.com>

I took the part initializing pdata->buttons and pdata->nbuttons and
folded it into the offending patch.

Thank you!

-- 
Dmitry

      reply	other threads:[~2012-08-22  5:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-20 18:48 [PATCH v2] Input: gpio_keys - fix NULL pointer dereference for dt case Fabio Estevam
2012-08-20 20:44 ` Alan M Butler
2012-08-22  5:58   ` Dmitry Torokhov [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=20120822055816.GF20624@core.coreip.homeip.net \
    --to=dmitry.torokhov@gmail.com \
    --cc=alanbutty12@gmail.com \
    --cc=aletes.xgr@gmail.com \
    --cc=fabio.estevam@freescale.com \
    --cc=linux-input@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;
as well as URLs for NNTP newsgroup(s).