Skip to contentSkip to conversation

clk: vc5: Add memory check to prevent oops

PATCH Applied Adam Ford 3 participants 4 messages Jul 16, 2020 – Jul 23, 2020

Subsystems (2)
lore
Share & tools
lore

Adam Ford

author
#0 lore raw

PATCH v3 1/1 clk: vc5: Add memory check to prevent oops

When getting the names of the child nodes, kasprintf is used to allocate memory which is used to create the string for the node name. Unfortunately, there is no memory check to determine if this allocation fails, it may cause an error when trying to get child node name.

This patch will check if the memory allocation fails, and returns and -ENOMEM error instead of blindly moving on.

Fixes 260249f929e8 ("clk: vc5: Enable addition output configurations of the Versaclock")

---
V3: Fix spelling error, and use the style of checking (!variable) instead of

      (variable == NULL)

V2: Fix an issue where a goto was going to use an unitialized variable.

drivers/clk/clk-versaclock5.c +5 -3
index 9a5fb3834b9a..9a37168c4afc 100644
@@ -789,10 +789,13 @@ static int vc5_get_output_config(struct i2c_client *client,
 	int ret = 0;
 
 	child_name = kasprintf(GFP_KERNEL, "OUT%d", clk_out->num + 1);
+	if (!child_name)
+		return -ENOMEM;
+
 	np_output = of_get_child_by_name(client->dev.of_node, child_name);
 	kfree(child_name);
 	if (!np_output)
-		goto output_done;
+		return 0;
 
 	ret = vc5_update_mode(np_output, clk_out);
 	if (ret)
@@ -813,7 +816,6 @@ static int vc5_get_output_config(struct i2c_client *client,
 
 	of_node_put(np_output);
 
-output_done:
 	return ret;
 }
 
@@ -828,7 +830,7 @@ static int vc5_probe(struct i2c_client *client, const struct i2c_device_id *id)
 	int ret;
 
 	vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
-	if (vc5 == NULL)
+	if (!vc5)
 		return -ENOMEM;
 
 	i2c_set_clientdata(client, vc5);
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Quoting Adam Ford (2020-07-16 05:26:20)

Quoted 11 lines

When getting the names of the child nodes, kasprintf is used to allocate memory which is used to create the string for the node name. Unfortunately, there is no memory check to determine if this allocation fails, it may cause an error when trying to get child node name.

This patch will check if the memory allocation fails, and returns and -ENOMEM error instead of blindly moving on.

Fixes 260249f929e8 ("clk: vc5: Enable addition output configurations of the Versaclock")
Quoted 8 lines

---
V3: Fix spelling error, and use the style of checking (!variable) instead of

      (variable == NULL)

V2: Fix an issue where a goto was going to use an unitialized variable.

Is the patch from Colin also needed?
https://lore.kernel.org/r/[email protected]

maintainer
#2 lore raw

Hi Stephen,

Quoted 19 lines · Stephen Boyd

Quoting Adam Ford (2020-07-16 05:26:20)

Quoted 18 lines

When getting the names of the child nodes, kasprintf is used to allocate memory which is used to create the string for the node name. Unfortunately, there is no memory check to determine if this allocation fails, it may cause an error when trying to get child node name.

This patch will check if the memory allocation fails, and returns and -ENOMEM error instead of blindly moving on.

Fixes 260249f929e8 ("clk: vc5: Enable addition output configurations of the Versaclock")

---
V3: Fix spelling error, and use the style of checking (!variable) instead of

      (variable == NULL)
Quoted 4 lines
Quoted 1 line

V2: Fix an issue where a goto was going to use an unitialized variable.

Is the patch from Colin also needed?
https://lore.kernel.org/r/[email protected]

The two patches look completely orthogonal.

Signature · 1 line
Luca
maintainer Applied
#3 lore raw
In reply to Adam Ford

Quoting Adam Ford (2020-07-16 05:26:20)

Quoted 9 lines

When getting the names of the child nodes, kasprintf is used to allocate memory which is used to create the string for the node name. Unfortunately, there is no memory check to determine if this allocation fails, it may cause an error when trying to get child node name.

This patch will check if the memory allocation fails, and returns and -ENOMEM error instead of blindly moving on.

Quoted 6 lines
Fixes 260249f929e8 ("clk: vc5: Enable addition output configurations of the Versaclock")

---

Applied to clk-next