linux-stable/drivers/base
Dirk Behme a42b0060d6 drivers: core: synchronize really_probe() and dev_uevent()
commit c0a40097f0 upstream.

Synchronize the dev->driver usage in really_probe() and dev_uevent().
These can run in different threads, what can result in the following
race condition for dev->driver uninitialization:

Thread #1:
==========

really_probe() {
...
probe_failed:
...
device_unbind_cleanup(dev) {
    ...
    dev->driver = NULL;   // <= Failed probe sets dev->driver to NULL
    ...
    }
...
}

Thread #2:
==========

dev_uevent() {
...
if (dev->driver)
      // If dev->driver is NULLed from really_probe() from here on,
      // after above check, the system crashes
      add_uevent_var(env, "DRIVER=%s", dev->driver->name);
...
}

really_probe() holds the lock, already. So nothing needs to be done
there. dev_uevent() is called with lock held, often, too. But not
always. What implies that we can't add any locking in dev_uevent()
itself. So fix this race by adding the lock to the non-protected
path. This is the path where above race is observed:

 dev_uevent+0x235/0x380
 uevent_show+0x10c/0x1f0  <= Add lock here
 dev_attr_show+0x3a/0xa0
 sysfs_kf_seq_show+0x17c/0x250
 kernfs_seq_show+0x7c/0x90
 seq_read_iter+0x2d7/0x940
 kernfs_fop_read_iter+0xc6/0x310
 vfs_read+0x5bc/0x6b0
 ksys_read+0xeb/0x1b0
 __x64_sys_read+0x42/0x50
 x64_sys_call+0x27ad/0x2d30
 do_syscall_64+0xcd/0x1d0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Similar cases are reported by syzkaller in

https://syzkaller.appspot.com/bug?extid=ffa8143439596313a85a

But these are regarding the *initialization* of dev->driver

dev->driver = drv;

As this switches dev->driver to non-NULL these reports can be considered
to be false-positives (which should be "fixed" by this commit, as well,
though).

The same issue was reported and tried to be fixed back in 2015 in

https://lore.kernel.org/lkml/1421259054-2574-1-git-send-email-a.sangwan@samsung.com/

already.

Fixes: 239378f16a ("Driver core: add uevent vars for devices of a class")
Cc: stable <stable@kernel.org>
Cc: syzbot+ffa8143439596313a85a@syzkaller.appspotmail.com
Cc: Ashish Sangwan <a.sangwan@samsung.com>
Cc: Namjae Jeon <namjae.jeon@samsung.com>
Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Link: https://lore.kernel.org/r/20240513050634.3964461-1-dirk.behme@de.bosch.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-06-21 14:38:37 +02:00
..
firmware_loader firmware_loader: Fix a NULL vs IS_ERR() check 2023-05-31 20:31:00 +01:00
power PM: sleep: wakeirq: fix wake irq warning in system suspend 2024-04-03 15:28:24 +02:00
regmap regmap: Add regmap_read_bypassed() 2024-05-17 12:01:59 +02:00
test drivers: base: test: Add missing MODULE_* macros to root device tests 2023-08-16 19:01:29 +02:00
Kconfig driver core: Add CONFIG_FW_DEVLINK_SYNC_STATE_TIMEOUT 2023-03-28 18:45:59 +02:00
Makefile genirq: Get rid of GENERIC_MSI_IRQ_DOMAIN 2022-11-17 15:15:20 +01:00
arch_numa.c arm64: irq: set the correct node for VMAP stack 2024-02-05 20:14:14 +00:00
arch_topology.c arch_topology: Remove early cacheinfo error message if -ENOENT 2023-04-14 10:13:38 +01:00
attribute_container.c
auxiliary.c driver core: make struct bus_type.uevent() take a const * 2023-01-27 13:45:52 +01:00
base.h module: don't ignore sysfs_create_link() failures 2024-06-12 11:12:17 +02:00
bus.c module: don't ignore sysfs_create_link() failures 2024-06-12 11:12:17 +02:00
cacheinfo.c drivers: base: cacheinfo: Update cpu_map_populated during CPU Hotplug 2023-05-31 20:36:47 +01:00
class.c class: fix use-after-free in class_register() 2024-01-25 15:35:41 -08:00
component.c drivers: base: component: fix memory leak with using debugfs_lookup() 2023-02-08 13:33:10 +01:00
container.c
core.c drivers: core: synchronize really_probe() and dev_uevent() 2024-06-21 14:38:37 +02:00
cpu.c x86/rfds: Mitigate Register File Data Sampling (RFDS) 2024-03-15 10:48:22 -04:00
dd.c driver core: Release all resources during unbind before updating device links 2023-11-28 17:20:05 +00:00
devcoredump.c devcoredump: Send uevent once devcd is ready 2023-12-13 18:45:33 +01:00
devres.c drivers/base: use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN 2023-06-19 16:19:20 -07:00
devtmpfs.c driver core: clean up the logic to determine which /sys/dev/ directory to use 2023-03-31 17:45:07 +02:00
driver.c driver core: create bus_is_registered() 2023-02-09 10:43:35 +01:00
firmware.c
hypervisor.c
init.c init: Initialize noop_backing_dev_info early 2022-06-16 10:55:57 +02:00
isa.c isa: Remove unnecessary checks 2023-05-31 19:03:39 +01:00
map.c
memory.c mm/memory_hotplug: add missing mem_hotplug_lock 2023-12-13 18:45:24 +01:00
module.c module: don't ignore sysfs_create_link() failures 2024-06-12 11:12:17 +02:00
node.c base/node.c: initialize the accessor list before registering 2024-01-25 15:35:51 -08:00
physical_location.c driver core: location: Free struct acpi_pld_info *pld before return false 2023-01-20 14:20:30 +01:00
physical_location.h driver core: physical_location.h remove extern from function prototypes 2023-03-24 15:35:48 +01:00
pinctrl.c
platform-msi.c genirq/msi, platform-msi: Ensure that MSI descriptors are unreferenced 2023-03-02 18:09:44 +01:00
platform.c of: Move of_platform_register_reconfig_notifier() into DT core 2023-08-04 11:36:33 -06:00
property.c drivers: fwnode: fix fwnode_irq_get[_byname]() 2023-06-15 13:37:35 +02:00
soc.c base: soc: populate machine name in soc_device_register if empty 2023-03-29 12:21:23 +02:00
swnode.c software node: Let args be NULL in software_node_get_reference_args 2024-01-25 15:35:51 -08:00
syscore.c
topology.c drivers/base: fix userspace break from using bin_attributes for cpumap and cpulist 2022-07-15 17:36:33 +02:00
trace.c
trace.h
transport_class.c drivers: base: transport_class: fix resource leak when transport_add_device() fails 2023-01-20 14:22:53 +01:00