Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Fabrizio Lamarque <fl.scratchpad@gmail.com>
Cc: alexandru.tachici@analog.com, linux-iio@vger.kernel.org
Subject: Re: [PATCH 1/2] ad7192 driver: fix null pointer dereference in probe when populating adc input ranges
Date: Sat, 1 Apr 2023 15:28:27 +0100	[thread overview]
Message-ID: <20230401152827.179d1a01@jic23-huawei> (raw)
In-Reply-To: <CAPJMGm4GDVdAmwB4sHVkg78UhtVpmbCL6KT8-KbEY7cRSD5UZg@mail.gmail.com>

On Mon, 27 Mar 2023 22:02:48 +0200
Fabrizio Lamarque <fl.scratchpad@gmail.com> wrote:

> Fix ad7192.c NULL pointer dereference in ad7192_setup() when accessing
> indio_dev structure while populating input rages, causing a kernel
> panic.
> Fixed by calling spi_set_drvdata after indio_dev is allocated.
> 
> Additional details
> 
> Kernel panic log
> [    5.763067] Unable to handle kernel NULL pointer dereference at
> virtual address 00000208
> [...]
> [    6.265076] [<c063b94c>] (driver_register) from [<c070e59c>]
> (__spi_register_driver+0xd8/0xe4)
> [    6.273757]  r5:c0b88c7c r4:00000000
> [    6.277351] [<c070e4c4>] (__spi_register_driver) from [<c0e4c288>]
> (ad7192_driver_init+0x20/0x28)
> [    6.286305]  r9:c107fe00 r8:c107fe00 r7:00000000 r6:c0e56854
> r5:c0e4c268 r4:c40fa000
> [    6.294070] [<c0e4c268>] (ad7192_driver_init) from [<c01023d0>]
> (do_one_initcall+0x58/0x2ac)
> [    6.302569] [<c0102378>] (do_one_initcall) from [<c0e01594>]
> (kernel_init_freeable+0x1c4/0x254)
> [...]
> [    6.387349] Kernel panic - not syncing: Attempted to kill init!
> exitcode=0x0000000b
> [    6.395049] ---[ end Kernel panic - not syncing: Attempted to kill
> init! exitcode=0x0000000b ]
> 
> The patch is against the current tree, but it applies without
> modifications to 5.x (the driver has not changed much since then).
> Reproduced in kernel version 5.15.x. Newer driver versions are
> affected by the same issue.
> 
> Pointer to indio_dev structure is obtained via spi_get_drvdata() at
> the beginning of function ad7192_setup(), but the
> spi->dev->driver_data member is not initialized here, hence a NULL
> pointer is returned.
> 
> By comparing every other iio adc driver, whenever there is a call to
> spi_get_drvdata() there is also one to spi_set_drvdata() within probe
> function.
> It should also be noted that the indio_dev structure is accessed just
> to get the number of bits for the converter, and no other driver calls
> spi_get_drvdata within probe.
> After the patch is applied the system boots correctly and the ADC is
> mapped within sysfs.

I'd prefer to fix this by changing the ad7192_setup() to take the
struct iio_dev (available at it's call site) and avoid the dance
that is currently going on entirely.
Drop the struct ad7192_state *st parameter and get that via
st = iio_priv(indio_dev);

Thanks,

Jonathan


> 
> Signed-off-by: Fabrizio Lamarque <fl.scratchpad@gmail.com>
> --- linux/drivers/iio/adc/ad7192.c  2023-03-13 19:32:42.646239506 +0100
> +++ linux/drivers/iio/adc/ad7192.c  2023-03-13 19:33:41.654803797 +0100
> @@ -997,7 +997,7 @@ static int ad7192_probe(struct spi_devic
>             return -ENOMEM;
> 
>       st = iio_priv(indio_dev);
> -
> +     spi_set_drvdata(spi, indio_dev);
>       mutex_init(&st->lock);
> 
>       st->avdd = devm_regulator_get(&spi->dev, "avdd");


  parent reply	other threads:[~2023-04-01 14:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-27 20:02 [PATCH 1/2] ad7192 driver: fix null pointer dereference in probe when populating adc input ranges Fabrizio Lamarque
2023-03-29 15:01 ` Nuno Sá
2023-03-31  6:27   ` Nuno Sá
2023-04-01 14:28 ` Jonathan Cameron [this message]
2023-04-04  6:57   ` Fabrizio Lamarque
2023-04-08 10:36     ` Jonathan Cameron

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=20230401152827.179d1a01@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=alexandru.tachici@analog.com \
    --cc=fl.scratchpad@gmail.com \
    --cc=linux-iio@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