Skip to contentSkip to conversation

hdac_sysfs: Fix a memory leaking bug in sound/hda/hdac_sysfs.c file of Linux 5.1

PATCH In review Gen Zhang 2 participants 3 messages May 16, 2019 Sound

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 hdac_sysfs: Fix a memory leaking bug in sound/hda/hdac_sysfs.c file of Linux 5.1

tree->root and tree->nodes are allocated by memory allocation functions. And tree is also an allocated memory. When allocation of tree->root and tree->nodes fails, not freeing tree will leak memory. Thus we should free tree in this situation.

---

sound/hda/hdac_sysfs.c +5 -2
index fb2aa34..5d8a939 100644
@@ -370,12 +370,12 @@ static int widget_tree_create(struct hdac_device *codec)
 
 	tree->root = kobject_create_and_add("widgets", &codec->dev.kobj);
 	if (!tree->root)
-		return -ENOMEM;
+		goto free_tree;
 
 	tree->nodes = kcalloc(codec->num_nodes + 1, sizeof(*tree->nodes),
 			      GFP_KERNEL);
 	if (!tree->nodes)
-		return -ENOMEM;
+		goto free_tree;
 
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		err = add_widget_node(tree->root, nid, &widget_node_group,
@@ -393,6 +393,9 @@ static int widget_tree_create(struct hdac_device *codec)
 
 	kobject_uevent(tree->root, KOBJ_CHANGE);
 	return 0;
+free_tree:
+	kfree(tree);
+	return -ENOMEM;
 }
 
 int hda_widget_sysfs_init(struct hdac_device *codec)

---

#1 lore raw
Quoted 5 lines · Gen Zhang

tree->root and tree->nodes are allocated by memory allocation functions. And tree is also an allocated memory. When allocation of tree->root and tree->nodes fails, not freeing tree will leak memory. Thus we should free tree in this situation.

No, the leftover is freed in the caller side by calling widget_tree_free().

thanks,

Takashi

Quoted 35 lines

---

sound/hda/hdac_sysfs.c +5 -3
index fb2aa34..5d8a939 100644
@@ -370,12 +370,12 @@ static int widget_tree_create(struct hdac_device *codec)
 
 	tree->root = kobject_create_and_add("widgets", &codec->dev.kobj);
 	if (!tree->root)
-		return -ENOMEM;
+		goto free_tree;
 
 	tree->nodes = kcalloc(codec->num_nodes + 1, sizeof(*tree->nodes),
 			      GFP_KERNEL);
 	if (!tree->nodes)
-		return -ENOMEM;
+		goto free_tree;
 
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		err = add_widget_node(tree->root, nid, &widget_node_group,
@@ -393,6 +393,9 @@ static int widget_tree_create(struct hdac_device *codec)
 
 	kobject_uevent(tree->root, KOBJ_CHANGE);
 	return 0;
+free_tree:
+	kfree(tree);
+	return -ENOMEM;
 }
 
 int hda_widget_sysfs_init(struct hdac_device *codec)
---
author
#2 lore raw
Quoted 48 lines · Takashi Iwai
Quoted 5 lines · Gen Zhang

tree->root and tree->nodes are allocated by memory allocation functions. And tree is also an allocated memory. When allocation of tree->root and tree->nodes fails, not freeing tree will leak memory. Thus we should free tree in this situation.

No, the leftover is freed in the caller side by calling widget_tree_free().

thanks,

Takashi

Quoted 32 lines

---

sound/hda/hdac_sysfs.c +5 -2
index fb2aa34..5d8a939 100644
@@ -370,12 +370,12 @@ static int widget_tree_create(struct hdac_device *codec)
 
 	tree->root = kobject_create_and_add("widgets", &codec->dev.kobj);
 	if (!tree->root)
-		return -ENOMEM;
+		goto free_tree;
 
 	tree->nodes = kcalloc(codec->num_nodes + 1, sizeof(*tree->nodes),
 			      GFP_KERNEL);
 	if (!tree->nodes)
-		return -ENOMEM;
+		goto free_tree;
 
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		err = add_widget_node(tree->root, nid, &widget_node_group,
@@ -393,6 +393,9 @@ static int widget_tree_create(struct hdac_device *codec)
 
 	kobject_uevent(tree->root, KOBJ_CHANGE);
 	return 0;
+free_tree:
+	kfree(tree);
+	return -ENOMEM;
 }
 
Quoted 3 lines

sound/hda/hdac_sysfs.c static int widget_tree_create(struct hdac_device *codec)

Quoted 3 lines
sound/hda/hdac_sysfs.c +0 -1
 int hda_widget_sysfs_init(struct hdac_device *codec)
---

It is freed in the caller site :)
But it is really a rare free usage.
Thanks
Gen