linux/kernel/irq
Thomas Gleixner 570540d507 genirq: Validate action before dereferencing it in handle_irq_event_percpu()
commit 71f64340fc changed the handling of irq_desc->action from

CPU 0                   CPU 1
free_irq()              lock(desc)
  lock(desc)            handle_edge_irq()
                        if (desc->action) {
                          handle_irq_event()
                            action = desc->action
                            unlock(desc)
  desc->action = NULL       handle_irq_event_percpu(desc, action)
                              action->xxx
to

CPU 0                   CPU 1
free_irq()              lock(desc)
  lock(desc)            handle_edge_irq()
                        if (desc->action) {
                          handle_irq_event()
                            unlock(desc)
  desc->action = NULL       handle_irq_event_percpu(desc, action)
                              action = desc->action
                              action->xxx

So if free_irq manages to set the action to NULL between the unlock and before
the readout, we happily dereference a null pointer.

We could simply revert 71f64340fc, but we want to preserve the better code
generation. A simple solution is to change the action loop from a do {} while
to a while {} loop.

This is safe because we either see a valid desc->action or NULL. If the action
is about to be removed it is still valid as free_irq() is blocked on
synchronize_irq().

CPU 0                   CPU 1
free_irq()              lock(desc)
  lock(desc)            handle_edge_irq()
                          handle_irq_event(desc)
                            set(INPROGRESS)
                            unlock(desc)
                            handle_irq_event_percpu(desc)
                            action = desc->action
  desc->action = NULL           while (action) {
                                  action->xxx
                                  ...
                                  action = action->next;
  sychronize_irq()
    while(INPROGRESS);      lock(desc)
                            clr(INPROGRESS)
free(action)

That's basically the same mechanism as we have for shared
interrupts. action->next can become NULL while handle_irq_event_percpu()
runs. Either it sees the action or NULL. It does not matter, because action
itself cannot go away before the interrupt in progress flag has been cleared.

Fixes: commit 71f64340fc "genirq: Remove the second parameter from handle_irq_event_percpu()"
Reported-by: zyjzyj2000@gmail.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Huang Shijie <shijie.huang@arm.com>
Cc: Jiang Liu <jiang.liu@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: http://lkml.kernel.org/r/alpine.DEB.2.11.1601131224190.3575@nanos
2016-01-14 20:09:49 +01:00
..
autoprobe.c genirq: Handle pending irqs in irq_startup() 2012-02-15 11:56:59 +01:00
chip.c genirq/msi: Export functions to allow MSI domains in modules 2015-12-20 12:26:52 +01:00
cpuhotplug.c genirq: Make the cpuhotplug migration code less noisy 2015-10-22 14:34:57 +02:00
debug.h irq: hide debug macros so they don't collide with others. 2012-04-23 12:30:03 -04:00
devres.c genirq: devres: Fix testing return value of request_any_context_irq() 2015-05-13 10:47:37 +02:00
dummychip.c Merge branch 'linus' into irq/core 2015-06-05 22:25:01 +02:00
generic-chip.c genirq: Add chip_[suspend|resume] PM support to irq_chip 2015-07-27 08:09:38 +02:00
handle.c genirq: Validate action before dereferencing it in handle_irq_event_percpu() 2016-01-14 20:09:49 +01:00
internals.h genirq/PM: Restore system wake up from chained interrupts 2015-11-10 09:11:31 -05:00
irqdesc.c genirq: Free irq_desc with rcu 2015-12-14 10:03:46 +01:00
irqdomain.c Merge branch 'irq/gic-v2m-acpi' of git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms into irq/core 2015-12-29 10:08:45 +01:00
Kconfig genirq: Introduce generic irq migration for cpu hotunplug 2015-10-01 14:51:15 +02:00
Makefile genirq: Introduce generic irq migration for cpu hotunplug 2015-10-01 14:51:15 +02:00
manage.c Merge branch 'irq-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2016-01-11 18:28:06 -08:00
migration.c genirq: Remove bogus restriction in irq_move_mask_irq() 2015-06-20 19:05:14 +02:00
msi.c genirq/msi: Add msi_domain_populate_irqs 2015-12-16 15:29:44 +00:00
pm.c Merge branches 'irq-urgent-for-linus' and 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2015-11-15 09:30:48 -08:00
proc.c genirq/PM: Restore system wake up from chained interrupts 2015-11-10 09:11:31 -05:00
resend.c genirq: Remove irq argument from irq flow handlers 2015-09-16 15:47:51 +02:00
settings.h genirq: Add flag to force mask in disable_irq[_nosync]() 2015-10-11 11:33:42 +02:00
spurious.c genirq: Remove the irq argument from note_interrupt() 2015-07-11 23:14:25 +02:00