linux-stable/drivers/base/regmap
Yang Yingliang 55e6d80378
regmap: Fix possible double-free in regcache_rbtree_exit()
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>
2021-10-12 11:48:43 +01:00
..
internal.h regmap: teach regmap to use raw spinlocks if requested in the config 2021-08-26 12:07:32 +01:00
Kconfig regmap: Add MDIO bus support 2021-05-19 14:19:10 +01:00
Makefile regmap: Add MDIO bus support 2021-05-19 14:19:10 +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-10-12 11:48:43 +01:00
regcache.c regmap: Assign boolean values to a bool variable 2021-01-21 12:42:20 +00: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: Prefer unsigned int to bare use of unsigned 2021-07-11 23:48:43 +01:00
regmap-i2c.c regmap-i2c: Set regmap max raw r/w from quirks 2021-05-14 13:25:03 +01:00
regmap-i3c.c regmap: add i3c bus support 2019-06-07 13:09:55 +01:00
regmap-irq.c regmap-irq: Introduce inverted status registers support 2021-06-07 16:20:04 +01:00
regmap-mdio.c regmap: mdio: Reject invalid addresses 2021-06-14 15:00:29 +01:00
regmap-mmio.c regmap: Prefer unsigned int to bare use of unsigned 2021-07-11 23:48:43 +01:00
regmap-sccb.c regmap: sccb: fix typo and sort headers alphabetically 2018-07-23 18:05:08 +01:00
regmap-sdw-mbq.c regmap: sdw-mbq: use MODULE_LICENSE("GPL") 2021-02-11 10:54:00 +05:30
regmap-sdw.c regmap: sdw: use _no_pm functions in regmap_read/write 2021-02-11 10:54:00 +05:30
regmap-slimbus.c regmap: slimbus: add support to multi read/write 2018-07-11 11:58:04 +01:00
regmap-spi-avmm.c regmap: add Intel SPI Slave to AVMM Bus Bridge support 2020-08-26 19:46:38 +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: regmap-w1: Drop unreachable code 2019-11-19 13:09:20 +00:00
regmap.c Merge series "Use raw spinlocks in the ls-extirq driver" from Vladimir Oltean <vladimir.oltean@nxp.com>: 2021-08-26 13:40:35 +01:00
trace.h regmap: Remove duplicate type field from regmap regcache_sync trace event 2020-11-24 14:14:00 +00:00