linux-stable/drivers/base/regmap
Yang Yingliang 1cead23c1c 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 19:46:13 +01:00
..
internal.h regmap: fix page selection for noinc writes 2020-10-01 13:18:20 +02:00
Kconfig soundwire: fix regmap dependencies and align with other serial links 2019-08-09 10:20:40 +05:30
Makefile regmap: add i3c bus support 2019-06-07 13:09:55 +01:00
regcache-flat.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regcache-lzo.c regmap: lzo: Switch to bitmap_zalloc() 2019-06-17 15:18:44 +01:00
regcache-rbtree.c regmap: Fix possible double-free in regcache_rbtree_exit() 2021-11-02 19:46:13 +01:00
regcache.c regmap: fix page selection for noinc writes 2020-10-01 13:18:20 +02:00
regmap-ac97.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regmap-debugfs.c regmap: set debugfs_name to NULL after it is freed 2021-05-14 09:44:17 +02:00
regmap-i2c.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regmap-i3c.c regmap: add i3c bus support 2019-06-07 13:09:55 +01:00
regmap-irq.c regmap-irq: Correct error paths in regmap_irq_thread for pm_runtime 2019-08-12 14:03:20 +01:00
regmap-mmio.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regmap-sccb.c regmap: sccb: fix typo and sort headers alphabetically 2018-07-23 18:05:08 +01:00
regmap-sdw.c regmap: sdw: use _no_pm functions in regmap_read/write 2021-03-04 10:26:37 +01:00
regmap-slimbus.c regmap: slimbus: add support to multi read/write 2018-07-11 11:58:04 +01:00
regmap-spi.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regmap-spmi.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regmap-w1.c regmap: add proper SPDX identifiers on files that did not have them. 2019-04-25 20:22:15 +01:00
regmap.c regmap: fix the offset of register error log 2021-09-15 09:47:26 +02:00
trace.h