[PATCH] gpio: avoid undersized array bitmap in get fastpath

From: pocj8ur4in

Date: Sat Nov 21 2026 - 23:38:31 EST


The array get fast path passes the caller's value bitmap directly to
get_multiple(). The bitmap is sized for the requested array, but
providers such as gpio-sim process it for the entire chip. With a
128-line chip and a one-line array on a 64-bit kernel, gpio-sim's
bitmap_replace() reads and writes two words when the caller has
allocated only one. The second word is accessed even though its mask
is zero.

Use the existing per-chip path when the chip needs more bitmap words
than the caller provides. That path uses a chip-sized temporary bitmap
and copies only the requested values back.

Fixes: b17566a6b08b ("gpiolib: Implement fast processing path in get/set array")
Signed-off-by: Baek Kyu, Kim <pocj8ur4in@xxxxxxxxx>
---
With an arm64 QEMU/KVM KASAN kernel, a gpio-virtuser read of the first
line of a 128-line gpio-sim chip reported an 8-byte out-of-bounds read
and write in __bitmap_replace() before this change. The changed kernel
completed the same read without a KASAN report. A 64-line control case
completed without a KASAN report on both kernels. No physical GPIO chip
was tested.

drivers/gpio/gpiolib.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index c0394c7d6279b..29c34c46433b0 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -3512,6 +3512,7 @@ int gpiod_get_array_value_complex(bool raw, bool
can_sleep,
*/
if (array_info && array_info->desc == desc_array &&
array_size <= array_info->size &&
+ BITS_TO_LONGS(array_info->gdev->ngpio) <= BITS_TO_LONGS(array_size) &&
(void *)array_info == desc_array + array_info->size) {
if (!can_sleep)
WARN_ON(array_info->gdev->can_sleep);
--
2.55.0