ARM: OMAP2+: Fix null pointer dereference and memory leak in omap_soc_device_init
authorKunwu Chan <chentao@kylinos.cn>
Thu, 23 Nov 2023 14:52:37 +0000 (22:52 +0800)
committerTony Lindgren <tony@atomide.com>
Thu, 30 Nov 2023 11:57:00 +0000 (13:57 +0200)
kasprintf() returns a pointer to dynamically allocated memory which can
be NULL upon failure. When 'soc_dev_attr->family' is NULL,it'll trigger
the null pointer dereference issue, such as in 'soc_info_show'.

And when 'soc_device_register' fails, it's necessary to release
'soc_dev_attr->family' to avoid memory leaks.

Fixes: 6770b2114325 ("ARM: OMAP2+: Export SoC information to userspace")
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Message-ID: <20231123145237.609442-1-chentao@kylinos.cn>
Signed-off-by: Tony Lindgren <tony@atomide.com>
arch/arm/mach-omap2/id.c

index 98999aa8cc0c092a72afb578cb3461e40280688c..7f387706368a6896bac12a5373b8c7887c56dc82 100644 (file)
@@ -793,11 +793,16 @@ void __init omap_soc_device_init(void)
 
        soc_dev_attr->machine  = soc_name;
        soc_dev_attr->family   = omap_get_family();
+       if (!soc_dev_attr->family) {
+               kfree(soc_dev_attr);
+               return;
+       }
        soc_dev_attr->revision = soc_rev;
        soc_dev_attr->custom_attr_group = omap_soc_groups[0];
 
        soc_dev = soc_device_register(soc_dev_attr);
        if (IS_ERR(soc_dev)) {
+               kfree(soc_dev_attr->family);
                kfree(soc_dev_attr);
                return;
        }