Commit

bd22f7e63509fa758cf500d9af3e88a3d3df96be

gpio: shared: fix use-after-free bug when adding shared proxies

The GPIO lookup table can stay alive for longer than the kobject whose
name string's address we're assigning to lookup->dev_id. Fix a potential
UAF by duplicating the string.

Cc: stable@vger.kernel.org
Fixes: a060b8c511ab ("gpiolib: implement low-level, shared GPIO support")
Reviewed-by: Linus Walleij <linusw@kernel.org>
Link: https://patch.msgid.link/20261014-gpio-shared-fwnode-lookup-v4-1-2253ad645fd6@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com> · 2026-10-14 09:38 UTC

Browse files
1 file changed, +8 −4
modified drivers/gpio/gpiolib-shared.c
@@ -450,7 +450,6 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
450 450 const char *con_id, unsigned long lflags)
451 451 {
452 452 const char *dev_id = dev_name(consumer);
453 - struct gpiod_lookup_table *lookup;
454 453 struct gpio_shared_entry *entry;
455 454 struct gpio_shared_ref *ref;
456 455
@@ -480,18 +479,22 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
480 479 if (!key)
481 480 return -ENOMEM;
482 481
483 - lookup = kzalloc_flex(*lookup, table, 2);
482 + struct gpiod_lookup_table *lookup __free(kfree) =
483 + kzalloc_flex(*lookup, table, 2);
484 484 if (!lookup)
485 485 return -ENOMEM;
486 486
487 487 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n",
488 488 dev_id, key, ref->con_id ?: "none");
489 489
490 - lookup->dev_id = dev_id;
490 + lookup->dev_id = kstrdup(dev_id, GFP_KERNEL);
491 + if (!lookup->dev_id)
492 + return -ENOMEM;
493 +
491 494 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0,
492 495 ref->con_id, lflags);
493 496
494 - ref->lookup = lookup;
497 + ref->lookup = no_free_ptr(lookup);
495 498 gpiod_add_lookup_table(ref->lookup);
496 499
497 500 return 0;
@@ -611,6 +614,7 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
611 614 if (ref->lookup) {
612 615 gpiod_remove_lookup_table(ref->lookup);
613 616 kfree(ref->lookup->table[0].key);
617 + kfree(ref->lookup->dev_id);
614 618 kfree(ref->lookup);
615 619 ref->lookup = NULL;
616 620 }