All of lore.kernel.org
 help / color / mirror / Atom feed
* [bug report] media: allegro: possible NULL pointer dereference.
@ 2021-05-08 16:04 Yuri Savinykh
  2021-05-11  7:28 ` Michael Tretter
  0 siblings, 1 reply; 4+ messages in thread
From: Yuri Savinykh @ 2021-05-08 16:04 UTC (permalink / raw)
  To: Michael Tretter
  Cc: Yuri Savinykh, Pengutronix Kernel Team, Mauro Carvalho Chehab,
	linux-media, linux-kernel, ldv-project

Hello,

At the moment of enabling irq handling:

3166     ret = devm_request_threaded_irq(&pdev->dev, irq,
3167                     allegro_hardirq,
3168                     allegro_irq_thread,
3169                     IRQF_SHARED, dev_name(&pdev->dev), dev);

there is still uninitialized field mbox_status of struct allegro_dev *dev.
If an interrupt occurs in the interval between the installation of the
interrupt handler and the initialization of this field, NULL pointer
dereference happens.

This field is dereferenced in the handler function without any check:

1801 static irqreturn_t allegro_irq_thread(int irq, void *data)
1802 {
1803     struct allegro_dev *dev = data;
1804
1805     allegro_mbox_notify(dev->mbox_status);


and then:

752 static void allegro_mbox_notify(struct allegro_mbox *mbox)
753 {
754     struct allegro_dev *dev = mbox->dev;

The initialization of the mbox_status field happens asynchronously in
allegro_fw_callback() via allegro_mcu_hw_init(). 

Is it guaranteed that an interrupt does not occur in this interval?
If it is not, is it better to move interrupt handler installation
after initialization of this field has been completed?

Found by Linux Driver Verification project (linuxtesting.org).

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

* Re: [bug report] media: allegro: possible NULL pointer dereference.
  2021-05-08 16:04 [bug report] media: allegro: possible NULL pointer dereference Yuri Savinykh
@ 2021-05-11  7:28 ` Michael Tretter
  2021-05-11  8:49   ` Lucas Stach
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Tretter @ 2021-05-11  7:28 UTC (permalink / raw)
  To: Yuri Savinykh
  Cc: Pengutronix Kernel Team, Mauro Carvalho Chehab, linux-media,
	linux-kernel, ldv-project

Hello Yuri,

On Sat, 08 May 2021 19:04:55 +0300, Yuri Savinykh wrote:
> At the moment of enabling irq handling:
> 
> 3166     ret = devm_request_threaded_irq(&pdev->dev, irq,
> 3167                     allegro_hardirq,
> 3168                     allegro_irq_thread,
> 3169                     IRQF_SHARED, dev_name(&pdev->dev), dev);
> 
> there is still uninitialized field mbox_status of struct allegro_dev *dev.
> If an interrupt occurs in the interval between the installation of the
> interrupt handler and the initialization of this field, NULL pointer
> dereference happens.
> 
> This field is dereferenced in the handler function without any check:
> 
> 1801 static irqreturn_t allegro_irq_thread(int irq, void *data)
> 1802 {
> 1803     struct allegro_dev *dev = data;
> 1804
> 1805     allegro_mbox_notify(dev->mbox_status);
> 
> 
> and then:
> 
> 752 static void allegro_mbox_notify(struct allegro_mbox *mbox)
> 753 {
> 754     struct allegro_dev *dev = mbox->dev;
> 
> The initialization of the mbox_status field happens asynchronously in
> allegro_fw_callback() via allegro_mcu_hw_init(). 
> 
> Is it guaranteed that an interrupt does not occur in this interval?
> If it is not, is it better to move interrupt handler installation
> after initialization of this field has been completed?

Thanks for the report. The interrupt is triggered by the firmware, which is
only loaded in allegro_fw_callback(), and is enabled only after the
initialization of mbox_status in allegro_mcu_hw_init():

3507	allegro_mcu_enable_interrupts(dev)

The interrupt handler is installed in probe(), because that's where all the
platform information is retrieved. Unfortunately, at that time, the driver is
not able to setup the mailboxes, because the mailbox configuration depends on
the firmware and is only known in allegro_fw_callback().

It might be interesting to tie the interrupt more closely to the mailboxes,
because it is actually only used to notify the driver about mails in the
mailbox, but that's something I have not yet considered worth the effort.

Michael

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

* Re: [bug report] media: allegro: possible NULL pointer dereference.
  2021-05-11  7:28 ` Michael Tretter
@ 2021-05-11  8:49   ` Lucas Stach
  2021-05-11  9:08     ` Michael Tretter
  0 siblings, 1 reply; 4+ messages in thread
From: Lucas Stach @ 2021-05-11  8:49 UTC (permalink / raw)
  To: Michael Tretter, Yuri Savinykh
  Cc: ldv-project, Mauro Carvalho Chehab, linux-kernel,
	Pengutronix Kernel Team, linux-media

Hi Michael,

Am Dienstag, dem 11.05.2021 um 09:28 +0200 schrieb Michael Tretter:
> Hello Yuri,
> 
> On Sat, 08 May 2021 19:04:55 +0300, Yuri Savinykh wrote:
> > At the moment of enabling irq handling:
> > 
> > 3166     ret = devm_request_threaded_irq(&pdev->dev, irq,
> > 3167                     allegro_hardirq,
> > 3168                     allegro_irq_thread,
> > 3169                     IRQF_SHARED, dev_name(&pdev->dev), dev);
> > 
> > there is still uninitialized field mbox_status of struct allegro_dev *dev.
> > If an interrupt occurs in the interval between the installation of the
> > interrupt handler and the initialization of this field, NULL pointer
> > dereference happens.
> > 
> > This field is dereferenced in the handler function without any check:
> > 
> > 1801 static irqreturn_t allegro_irq_thread(int irq, void *data)
> > 1802 {
> > 1803     struct allegro_dev *dev = data;
> > 1804
> > 1805     allegro_mbox_notify(dev->mbox_status);
> > 
> > 
> > and then:
> > 
> > 752 static void allegro_mbox_notify(struct allegro_mbox *mbox)
> > 753 {
> > 754     struct allegro_dev *dev = mbox->dev;
> > 
> > The initialization of the mbox_status field happens asynchronously in
> > allegro_fw_callback() via allegro_mcu_hw_init(). 
> > 
> > Is it guaranteed that an interrupt does not occur in this interval?
> > If it is not, is it better to move interrupt handler installation
> > after initialization of this field has been completed?
> 
> Thanks for the report. The interrupt is triggered by the firmware, which is
> only loaded in allegro_fw_callback(), and is enabled only after the
> initialization of mbox_status in allegro_mcu_hw_init():
> 
> 3507	allegro_mcu_enable_interrupts(dev)
> 
> The interrupt handler is installed in probe(), because that's where all the
> platform information is retrieved. Unfortunately, at that time, the driver is
> not able to setup the mailboxes, because the mailbox configuration depends on
> the firmware and is only known in allegro_fw_callback().
> 
> It might be interesting to tie the interrupt more closely to the mailboxes,
> because it is actually only used to notify the driver about mails in the
> mailbox, but that's something I have not yet considered worth the effort.
> 

The interrupt is installed with IRQF_SHARED, so your IRQ handler must
be prepared to be called even if your device did not trigger an IRQ and
even before your initialization is done, as another device on the same
IRQ line might trigger the IRQ. In that case you must at least be able
to return IRQ_NONE from your handler without crashing the kernel.

Regards,
Lucas


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

* Re: [bug report] media: allegro: possible NULL pointer dereference.
  2021-05-11  8:49   ` Lucas Stach
@ 2021-05-11  9:08     ` Michael Tretter
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Tretter @ 2021-05-11  9:08 UTC (permalink / raw)
  To: Lucas Stach
  Cc: Yuri Savinykh, ldv-project, Mauro Carvalho Chehab, linux-kernel,
	Pengutronix Kernel Team, linux-media

Hi Lucas,

On Tue, 11 May 2021 10:49:16 +0200, Lucas Stach wrote:
> Am Dienstag, dem 11.05.2021 um 09:28 +0200 schrieb Michael Tretter:
> > On Sat, 08 May 2021 19:04:55 +0300, Yuri Savinykh wrote:
> > > At the moment of enabling irq handling:
> > > 
> > > 3166     ret = devm_request_threaded_irq(&pdev->dev, irq,
> > > 3167                     allegro_hardirq,
> > > 3168                     allegro_irq_thread,
> > > 3169                     IRQF_SHARED, dev_name(&pdev->dev), dev);
> > > 
> > > there is still uninitialized field mbox_status of struct allegro_dev *dev.
> > > If an interrupt occurs in the interval between the installation of the
> > > interrupt handler and the initialization of this field, NULL pointer
> > > dereference happens.
> > > 
> > > This field is dereferenced in the handler function without any check:
> > > 
> > > 1801 static irqreturn_t allegro_irq_thread(int irq, void *data)
> > > 1802 {
> > > 1803     struct allegro_dev *dev = data;
> > > 1804
> > > 1805     allegro_mbox_notify(dev->mbox_status);
> > > 
> > > 
> > > and then:
> > > 
> > > 752 static void allegro_mbox_notify(struct allegro_mbox *mbox)
> > > 753 {
> > > 754     struct allegro_dev *dev = mbox->dev;
> > > 
> > > The initialization of the mbox_status field happens asynchronously in
> > > allegro_fw_callback() via allegro_mcu_hw_init(). 
> > > 
> > > Is it guaranteed that an interrupt does not occur in this interval?
> > > If it is not, is it better to move interrupt handler installation
> > > after initialization of this field has been completed?
> > 
> > Thanks for the report. The interrupt is triggered by the firmware, which is
> > only loaded in allegro_fw_callback(), and is enabled only after the
> > initialization of mbox_status in allegro_mcu_hw_init():
> > 
> > 3507	allegro_mcu_enable_interrupts(dev)
> > 
> > The interrupt handler is installed in probe(), because that's where all the
> > platform information is retrieved. Unfortunately, at that time, the driver is
> > not able to setup the mailboxes, because the mailbox configuration depends on
> > the firmware and is only known in allegro_fw_callback().
> > 
> > It might be interesting to tie the interrupt more closely to the mailboxes,
> > because it is actually only used to notify the driver about mails in the
> > mailbox, but that's something I have not yet considered worth the effort.
> > 
> 
> The interrupt is installed with IRQF_SHARED, so your IRQ handler must
> be prepared to be called even if your device did not trigger an IRQ and
> even before your initialization is done, as another device on the same
> IRQ line might trigger the IRQ. In that case you must at least be able
> to return IRQ_NONE from your handler without crashing the kernel.

The allegro_hardirq() handler already checks the irq status register
(AL5_ITC_CPU_IRQ_STA) for the device and returns IRQ_NONE before even
dispatching the interrupt to the irq thread. In this case, the mailbox is not
read at all.

Michael

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

end of thread, other threads:[~2021-05-11  9:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-08 16:04 [bug report] media: allegro: possible NULL pointer dereference Yuri Savinykh
2021-05-11  7:28 ` Michael Tretter
2021-05-11  8:49   ` Lucas Stach
2021-05-11  9:08     ` Michael Tretter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.