regmap: debugfs: Duplicate name string if delaying debugfs init
Subsystems (2)
Quoted 10 lines · Mark Brown
drivers/base/regmap/regmap-debugfs.c void regmap_debugfs_init(struct regmap *map, const char *name) · line 573
Quoted 3 lines · Charles Keepax
drivers/base/regmap/regmap-debugfs.c +2
-1- node->name = name; + node->name =kstrdup (name,GFP_KERNEL ); +if (!node->name) {Two things here - one is that this should be
kstrdup_const(), the other is that we already took a copy of the name in__regmap_init()so the thing to do here is to change theregmap_debugfs_init()call there to use the copy we just made rather than the copy in the config.
Thanks Mark, I will have a look and update.
Thanks,
Charles
PATCH 1/1 regmap: debugfs: Duplicate name string if delaying debugfs init
In regmap_debugfs_init the initialisation of the debugfs is delayed if the root node isn't ready yet. Most callers of regmap_debugfs_init pass the name from the regmap_config, which is considered temporary ie. may be unallocated after the regmap_init call returns. This leads to a potential use after free, where config->name has been freed by the time it is used in regmap_debugfs_initcall.
This situation can be seen on Zynq, where the architecture init_irq callback registers a syscon device, using a local variable for the regmap_config. As init_irq is very early in the platform bring up the regmap debugfs root isn't ready yet. Although this doesn't crash it does result in the debugfs entry not having the correct name.
--- drivers/base/regmap/regmap-debugfs.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/base/regmap/regmap-debugfs.c +7 -1
@@ -569,7 +569,12 @@ void regmap_debugfs_init(struct regmap *map, const char *name)if (!node)return ; node->map = map; - node->name = name; + node->name =kstrdup (name,GFP_KERNEL ); +if (!node->name) { +kfree (node); +return ; + } +mutex_lock (®map_debugfs_early_lock);list_add (&node->link, ®map_debugfs_early_list);mutex_unlock (®map_debugfs_early_lock);
@@ -681,6 +686,7 @@ void regmap_debugfs_initcall(void)list_for_each_entry_safe (node, tmp, ®map_debugfs_early_list, link) {regmap_debugfs_init (node->map, node->name);list_del (&node->link); +kfree (node->name);kfree (node); }mutex_unlock (®map_debugfs_early_lock);
Signature · 1 line
2.11.0
Mark Brown
maintainerdrivers/base/regmap/regmap-debugfs.c void regmap_debugfs_init(struct regmap *map, const char *name) · line 573
Quoted 3 lines · Charles Keepax
drivers/base/regmap/regmap-debugfs.c +2
-1- node->name = name; + node->name =kstrdup (name,GFP_KERNEL ); +if (!node->name) {
Two things here - one is that this should be kstrdup_const(), the other is that we already took a copy of the name in __regmap_init() so the thing to do here is to change the regmap_debugfs_init() call there to use the copy we just made rather than the copy in the config.