Skip to contentSkip to conversation

clk: vc5: Add memory check to prevent oops

PATCH Superseded Adam Ford 2 participants 2 messages Jul 6, 2020 – Jul 7, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Adam Ford

author
#0 lore raw

PATCH v2 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 -NOMEM error instead of blindly moving on.

Fixes 260249f929e8 ("clk: vc5: Enable addition output configurations of the Versaclock")
drivers/clk/clk-versaclock5.c +4 -2
index 9a5fb3834b9a..926a370a0eda 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 == NULL)
+		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;
 }
 
Signature · 1 line
2.17.1
maintainer Reviewed-by on v2 1/1
#1 lore raw

Hi Adam,

Quoted 8 lines · Adam Ford

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 -NOMEM error instead of blindly moving on.

s/-NOMEM/-ENOMEM/

With this possibly fixed:

Signature · 1 line
Luca