diff mbox series

clk: vc5: Add memory check to prevent oops

Message ID 20200630210155.459250-1-aford173@gmail.com (mailing list archive)
State Changes Requested, archived
Headers show
Series clk: vc5: Add memory check to prevent oops | expand

Commit Message

Adam Ford June 30, 2020, 9:01 p.m. UTC
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.

Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Adam Ford <aford173@gmail.com>

Comments

Dan Carpenter July 2, 2020, 2:42 p.m. UTC | #1
Hi Adam,

Thank you for the patch! Perhaps something to improve:

url:    https://github.com/0day-ci/linux/commits/Adam-Ford/clk-vc5-Add-memory-check-to-prevent-oops/20200701-050451
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
config: i386-randconfig-m021-20200701 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-14) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

New smatch warnings:
drivers/clk/clk-versaclock5.c:818 vc5_get_output_config() error: uninitialized symbol 'np_output'.

# https://github.com/0day-ci/linux/commit/d445df5e4f918f08f66e20c366dc6c81dcdc8b57
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout d445df5e4f918f08f66e20c366dc6c81dcdc8b57
vim +/np_output +818 drivers/clk/clk-versaclock5.c

260249f929e81d Adam Ford 2020-06-03  783  
260249f929e81d Adam Ford 2020-06-03  784  static int vc5_get_output_config(struct i2c_client *client,
260249f929e81d Adam Ford 2020-06-03  785  				 struct vc5_hw_data *clk_out)
260249f929e81d Adam Ford 2020-06-03  786  {
260249f929e81d Adam Ford 2020-06-03  787  	struct device_node *np_output;
                                                                    ^^^^^^^^^

260249f929e81d Adam Ford 2020-06-03  788  	char *child_name;
260249f929e81d Adam Ford 2020-06-03  789  	int ret = 0;
260249f929e81d Adam Ford 2020-06-03  790  
260249f929e81d Adam Ford 2020-06-03  791  	child_name = kasprintf(GFP_KERNEL, "OUT%d", clk_out->num + 1);
d445df5e4f918f Adam Ford 2020-06-30  792  	if (!child_name) {
d445df5e4f918f Adam Ford 2020-06-30  793  		ret = -ENOMEM;
d445df5e4f918f Adam Ford 2020-06-30  794  		goto output_error;
                                                        ^^^^^^^^^^^^^^^^^^
Better to just return directly, because there is no clean up.

d445df5e4f918f Adam Ford 2020-06-30  795  	}
260249f929e81d Adam Ford 2020-06-03  796  	np_output = of_get_child_by_name(client->dev.of_node, child_name);
260249f929e81d Adam Ford 2020-06-03  797  	kfree(child_name);
260249f929e81d Adam Ford 2020-06-03  798  	if (!np_output)
d445df5e4f918f Adam Ford 2020-06-30  799  		return 0;
260249f929e81d Adam Ford 2020-06-03  800  
260249f929e81d Adam Ford 2020-06-03  801  	ret = vc5_update_mode(np_output, clk_out);
260249f929e81d Adam Ford 2020-06-03  802  	if (ret)
260249f929e81d Adam Ford 2020-06-03  803  		goto output_error;
260249f929e81d Adam Ford 2020-06-03  804  
260249f929e81d Adam Ford 2020-06-03  805  	ret = vc5_update_power(np_output, clk_out);
260249f929e81d Adam Ford 2020-06-03  806  	if (ret)
260249f929e81d Adam Ford 2020-06-03  807  		goto output_error;
260249f929e81d Adam Ford 2020-06-03  808  
260249f929e81d Adam Ford 2020-06-03  809  	ret = vc5_update_slew(np_output, clk_out);
260249f929e81d Adam Ford 2020-06-03  810  
260249f929e81d Adam Ford 2020-06-03  811  output_error:
260249f929e81d Adam Ford 2020-06-03  812  	if (ret) {
260249f929e81d Adam Ford 2020-06-03  813  		dev_err(&client->dev,
260249f929e81d Adam Ford 2020-06-03  814  			"Invalid clock output configuration OUT%d\n",
260249f929e81d Adam Ford 2020-06-03  815  			clk_out->num + 1);
260249f929e81d Adam Ford 2020-06-03  816  	}
260249f929e81d Adam Ford 2020-06-03  817  
260249f929e81d Adam Ford 2020-06-03 @818  	of_node_put(np_output);
                                                            ^^^^^^^^^
Uninitialized.

260249f929e81d Adam Ford 2020-06-03  819  
260249f929e81d Adam Ford 2020-06-03  820  	return ret;
260249f929e81d Adam Ford 2020-06-03  821  }
260249f929e81d Adam Ford 2020-06-03  822  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/clk/clk-versaclock5.c b/drivers/clk/clk-versaclock5.c
index 1d8ee4b8b1f5..29cdb38dc40b 100644
--- a/drivers/clk/clk-versaclock5.c
+++ b/drivers/clk/clk-versaclock5.c
@@ -789,10 +789,14 @@  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) {
+		ret = -ENOMEM;
+		goto output_error;
+	}
 	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 +817,6 @@  static int vc5_get_output_config(struct i2c_client *client,
 
 	of_node_put(np_output);
 
-output_done:
 	return ret;
 }