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

gpio: prevent potential speculation leaks in gpio_device_get_desc()

Userspace may trigger a speculative read of an address outside the gpio
descriptor array.
Users can do that by calling gpio_ioctl() with an offset out of range.
Offset is copied from user and then used as an array index to get
the gpio descriptor without sanitization in gpio_device_get_desc().

This change ensures that the offset is sanitized by using
array_index_nospec() to mitigate any possibility of speculative
information leaks.

This bug was discovered and resolved using Coverity Static Analysis
Security Testing (SAST) by Synopsys, Inc.

Signed-off-by: Hagar Hemdan <hagarhem@amazon.com>
Link: https://lore.kernel.org/r/20240523085332.1801-1-hagarhem@amazon.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

authored by

Hagar Hemdan and committed by
Bartosz Golaszewski
d795848e 447e140e

+2 -1
+2 -1
drivers/gpio/gpiolib.c
··· 17 17 #include <linux/list.h> 18 18 #include <linux/lockdep.h> 19 19 #include <linux/module.h> 20 + #include <linux/nospec.h> 20 21 #include <linux/of.h> 21 22 #include <linux/pinctrl/consumer.h> 22 23 #include <linux/seq_file.h> ··· 199 198 if (hwnum >= gdev->ngpio) 200 199 return ERR_PTR(-EINVAL); 201 200 202 - return &gdev->descs[hwnum]; 201 + return &gdev->descs[array_index_nospec(hwnum, gdev->ngpio)]; 203 202 } 204 203 EXPORT_SYMBOL_GPL(gpio_device_get_desc); 205 204