linux-stable/drivers/base
Yang Yingliang 758ced2c38 regmap: Fix possible double-free in regcache_rbtree_exit()
commit 55e6d80378 upstream.

In regcache_rbtree_insert_to_block(), when 'present' realloc failed,
the 'blk' which is supposed to assign to 'rbnode->block' will be freed,
so 'rbnode->block' points a freed memory, in the error handling path of
regcache_rbtree_init(), 'rbnode->block' will be freed again in
regcache_rbtree_exit(), KASAN will report double-free as follows:

BUG: KASAN: double-free or invalid-free in kfree+0xce/0x390
Call Trace:
 slab_free_freelist_hook+0x10d/0x240
 kfree+0xce/0x390
 regcache_rbtree_exit+0x15d/0x1a0
 regcache_rbtree_init+0x224/0x2c0
 regcache_init+0x88d/0x1310
 __regmap_init+0x3151/0x4a80
 __devm_regmap_init+0x7d/0x100
 madera_spi_probe+0x10f/0x333 [madera_spi]
 spi_probe+0x183/0x210
 really_probe+0x285/0xc30

To fix this, moving up the assignment of rbnode->block to immediately after
the reallocation has succeeded so that the data structure stays valid even
if the second reallocation fails.

Reported-by: Hulk Robot <hulkci@huawei.com>
Fixes: 3f4ff561bc ("regmap: rbtree: Make cache_present bitmap per node")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Link: https://lore.kernel.org/r/20211012023735.1632786-1-yangyingliang@huawei.com
Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-11-02 18:25:12 +01:00
..
power PM: base: power: don't try to use non-existing RTC for storing data 2021-09-22 11:45:33 +02:00
regmap regmap: Fix possible double-free in regcache_rbtree_exit() 2021-11-02 18:25:12 +01:00
test License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
arch_topology.c arch_topology: Fix section miss match warning due to free_raw_capacity() 2020-04-24 08:01:03 +02:00
attribute_container.c
base.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
bus.c kobject: return error code if writing /sys/.../uevent fails 2019-02-12 19:46:02 +01:00
cacheinfo.c drivers: base: cacheinfo: Ensure cpu hotplug work is done before Intel RDT 2019-07-21 09:04:16 +02:00
class.c
component.c component: Silence bind error on -EPROBE_DEFER 2020-05-27 16:42:57 +02:00
container.c
core.c PCI/MSI: Protect msi_desc::masked for multi-MSI 2021-08-26 08:37:04 -04:00
cpu.c x86/speculation: Add Special Register Buffer Data Sampling (SRBDS) mitigation 2020-06-11 09:23:01 +02:00
dd.c PM: runtime: Resume the device earlier in __device_release_driver() 2020-11-10 10:29:07 +01:00
devcoredump.c
devres.c devres: Align data[] to ARCH_KMALLOC_MINALIGN 2019-05-16 19:42:29 +02:00
devtmpfs.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
dma-coherent.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
dma-contiguous.c
dma-mapping.c
driver.c
firmware.c
firmware_class.c
hypervisor.c
init.c
isa.c isa: Prevent NULL dereference in isa_bus driver callbacks 2017-12-14 09:52:51 +01:00
Kconfig dma-buf/fence: Fix lock inversion within dma-fence-array 2018-03-19 08:42:47 +01:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
map.c
memory.c mm/memory_hotplug: Do not unlock when fails to take the device_hotplug_lock 2019-12-01 09:14:14 +01:00
module.c
node.c mm: hide incomplete nr_indirectly_reclaimable in sysfs 2019-04-20 09:15:10 +02:00
pinctrl.c
platform-msi.c platform-msi: Free descriptors in platform_msi_domain_free() 2019-01-09 17:14:48 +01:00
platform.c drivers: base: Fix NULL pointer exception in __platform_driver_probe() if a driver developer is foolish 2020-06-25 15:41:51 +02:00
property.c
soc.c base: soc: Export soc_device_register/unregister APIs 2019-10-05 12:47:47 +02:00
syscore.c
topology.c
transport_class.c