clk: keystone: sci-clk: check devm_kmalloc_array() return value

Message ID 20240110134621.17209-1-adiupina@astralinux.ru
State New
Headers
Series clk: keystone: sci-clk: check devm_kmalloc_array() return value |

Commit Message

Alexandra Diupina Jan. 10, 2024, 1:46 p.m. UTC
  devm_kmalloc_array() may return NULL, so
check return value to avoid null pointer
dereferencing

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 3c13933c6033 ("clk: keystone: sci-clk: add support for dynamically probing clocks")
Signed-off-by: Alexandra Diupina <adiupina@astralinux.ru>
---
 drivers/clk/keystone/sci-clk.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Alexandra Diupina Feb. 1, 2024, 12:02 p.m. UTC | #1
just a friendly reminder

10/01/24 16:46, Alexandra Diupina:
> devm_kmalloc_array() may return NULL, so
> check return value to avoid null pointer
> dereferencing
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: 3c13933c6033 ("clk: keystone: sci-clk: add support for dynamically probing clocks")
> Signed-off-by: Alexandra Diupina <adiupina@astralinux.ru>
> ---
>   drivers/clk/keystone/sci-clk.c | 3 +++
>   1 file changed, 3 insertions(+)
>
> diff --git a/drivers/clk/keystone/sci-clk.c b/drivers/clk/keystone/sci-clk.c
> index 35fe197dd303..a2fa24e4f88a 100644
> --- a/drivers/clk/keystone/sci-clk.c
> +++ b/drivers/clk/keystone/sci-clk.c
> @@ -459,6 +459,9 @@ static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
>   			tmp_clks = devm_kmalloc_array(dev, max_clks + 64,
>   						      sizeof(sci_clk),
>   						      GFP_KERNEL);
> +			if (!tmp_clks)
> +				return -ENOMEM;
> +
>   			memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
>   			if (max_clks)
>   				devm_kfree(dev, clks);
  

Patch

diff --git a/drivers/clk/keystone/sci-clk.c b/drivers/clk/keystone/sci-clk.c
index 35fe197dd303..a2fa24e4f88a 100644
--- a/drivers/clk/keystone/sci-clk.c
+++ b/drivers/clk/keystone/sci-clk.c
@@ -459,6 +459,9 @@  static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
 			tmp_clks = devm_kmalloc_array(dev, max_clks + 64,
 						      sizeof(sci_clk),
 						      GFP_KERNEL);
+			if (!tmp_clks)
+				return -ENOMEM;
+
 			memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
 			if (max_clks)
 				devm_kfree(dev, clks);