firmware: Google VPD: Fix memory allocation error handling
authorChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Fri, 5 May 2017 19:08:44 +0000 (21:08 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 18 May 2017 15:01:46 +0000 (17:01 +0200)
This patch fixes several issues:
   - if the 1st 'kzalloc' fails, we dereference a NULL pointer
   - if the 2nd 'kzalloc' fails, there is a memory leak
   - if 'sysfs_create_bin_file' fails there is also a memory leak

Fix it by adding a test after the first memory allocation and some error
handling paths to correctly free memory if needed.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/firmware/google/vpd.c

index 3ce813110d5e11bca8f3ea3e87c50cd32562b930..1e7860f02f4fa99e821e6a71bec7758621bc9a7d 100644 (file)
@@ -116,9 +116,13 @@ static int vpd_section_attrib_add(const u8 *key, s32 key_len,
                return VPD_OK;
 
        info = kzalloc(sizeof(*info), GFP_KERNEL);
-       info->key = kzalloc(key_len + 1, GFP_KERNEL);
-       if (!info->key)
+       if (!info)
                return -ENOMEM;
+       info->key = kzalloc(key_len + 1, GFP_KERNEL);
+       if (!info->key) {
+               ret = -ENOMEM;
+               goto free_info;
+       }
 
        memcpy(info->key, key, key_len);
 
@@ -135,12 +139,17 @@ static int vpd_section_attrib_add(const u8 *key, s32 key_len,
        list_add_tail(&info->list, &sec->attribs);
 
        ret = sysfs_create_bin_file(sec->kobj, &info->bin_attr);
-       if (ret) {
-               kfree(info->key);
-               return ret;
-       }
+       if (ret)
+               goto free_info_key;
 
        return 0;
+
+free_info_key:
+       kfree(info->key);
+free_info:
+       kfree(info);
+
+       return ret;
 }
 
 static void vpd_section_attrib_destroy(struct vpd_section *sec)