Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: Restrict usage of GPIO chip irq members before initialization

GPIO chip irq members are exposed before they could be completely
initialized and this leads to race conditions.

One such issue was observed for the gc->irq.domain variable which
was accessed through the I2C interface in gpiochip_to_irq() before
it could be initialized by gpiochip_add_irqchip(). This resulted in
Kernel NULL pointer dereference.

Following are the logs for reference :-

kernel: Call Trace:
kernel: gpiod_to_irq+0x53/0x70
kernel: acpi_dev_gpio_irq_get_by+0x113/0x1f0
kernel: i2c_acpi_get_irq+0xc0/0xd0
kernel: i2c_device_probe+0x28a/0x2a0
kernel: really_probe+0xf2/0x460
kernel: RIP: 0010:gpiochip_to_irq+0x47/0xc0

To avoid such scenarios, restrict usage of GPIO chip irq members before
they are completely initialized.

Signed-off-by: Shreeya Patel <shreeya.patel@collabora.com>
Cc: stable@vger.kernel.org
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

authored by

Shreeya Patel and committed by
Bartosz Golaszewski
5467801f 31231092

+28
+19
drivers/gpio/gpiolib.c
··· 1404 1404 { 1405 1405 struct irq_domain *domain = gc->irq.domain; 1406 1406 1407 + #ifdef CONFIG_GPIOLIB_IRQCHIP 1408 + /* 1409 + * Avoid race condition with other code, which tries to lookup 1410 + * an IRQ before the irqchip has been properly registered, 1411 + * i.e. while gpiochip is still being brought up. 1412 + */ 1413 + if (!gc->irq.initialized) 1414 + return -EPROBE_DEFER; 1415 + #endif 1416 + 1407 1417 if (!gpiochip_irqchip_irq_valid(gc, offset)) 1408 1418 return -ENXIO; 1409 1419 ··· 1602 1592 gpiochip_set_irq_hooks(gc); 1603 1593 1604 1594 acpi_gpiochip_request_interrupts(gc); 1595 + 1596 + /* 1597 + * Using barrier() here to prevent compiler from reordering 1598 + * gc->irq.initialized before initialization of above 1599 + * GPIO chip irq members. 1600 + */ 1601 + barrier(); 1602 + 1603 + gc->irq.initialized = true; 1605 1604 1606 1605 return 0; 1607 1606 }
+9
include/linux/gpio/driver.h
··· 222 222 bool per_parent_data; 223 223 224 224 /** 225 + * @initialized: 226 + * 227 + * Flag to track GPIO chip irq member's initialization. 228 + * This flag will make sure GPIO chip irq members are not used 229 + * before they are initialized. 230 + */ 231 + bool initialized; 232 + 233 + /** 225 234 * @init_hw: optional routine to initialize hardware before 226 235 * an IRQ chip will be added. This is quite useful when 227 236 * a particular driver wants to clear IRQ related registers