ti_sci: Address some issues found by Smatch
cover ti_sci: Address some issues found by Smatch
Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.
---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to
drivers/firmware/ti_sci.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
---
base-commit: c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdba
Best regards,
Signature · 1 line
Andrew Goodbody <[email protected]>
PATCH 1/3 ti_sci: Prevent memory leak
temp is assigned the pointer returned by malloc which is used without a NULL check and then never freed. Add a NULL check and ensure temp is freed on all return paths.
This issue was found by Smatch.
--- drivers/firmware/ti_sci.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/ti_sci.c +13 -3
@@ -3084,17 +3084,24 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,return ERR_PTR (sets); } temp =malloc (sets); +if (!temp) +return ERR_PTR (-ENOMEM ); + sets /=sizeof (u32); res->sets = sets; res->desc =devm_kcalloc (dev, res->sets,sizeof (*res->desc),GFP_KERNEL ); -if (!res->desc) +if (!res->desc) { +free (temp);return ERR_PTR (-ENOMEM ); + } ret =dev_read_u32_array (dev, of_prop, temp, res->sets); -if (ret) +if (ret) { +free (temp);return ERR_PTR (-EINVAL ); + }for (i =0 ; i < res->sets; i++) { resource_subtype = temp[i];
@@ -3119,10 +3126,13 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle, res->desc[i].res_map =devm_kzalloc (dev,BITS_TO_LONGS (res->desc[i].num) *sizeof (*res->desc[i].res_map),GFP_KERNEL ); -if (!res->desc[i].res_map) +if (!res->desc[i].res_map) { +free (temp);return ERR_PTR (-ENOMEM ); + } } +free (temp);if (valid_set)return res;
Signature · 1 line
2.47.3
Re: [PATCH 1/3] ti_sci: Prevent memory leak
Quoted 16 lines · Andrew Goodbody
temp is assigned the pointer returned by malloc which is used without a NULL check and then never freed. Add a NULL check and ensure temp is freed on all return paths.
This issue was found by Smatch.
Signed-off-by Andrew Goodbody <[email protected]>--- drivers/firmware/ti_sci.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)drivers/firmware/ti_sci.c +0
-0
drivers/firmware/ti_sci.c devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle, · line 3089
Quoted 6 lines
drivers/firmware/ti_sci.c +3
-0@@ -3084,17 +3084,24 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,return ERR_PTR (sets); } temp =malloc (sets); +if (!temp) +return ERR_PTR (-ENOMEM ); +
I suggest to move malloc to devm_kmalloc,
Quoted 22 lines
drivers/firmware/ti_sci.c +6
-2sets /=sizeof (u32); res->sets = sets; res->desc =devm_kcalloc (dev, res->sets,sizeof (*res->desc),GFP_KERNEL ); -if (!res->desc) +if (!res->desc) { +free (temp);return ERR_PTR (-ENOMEM ); + } ret =dev_read_u32_array (dev, of_prop, temp, res->sets); -if (ret) +if (ret) { +free (temp);return ERR_PTR (-EINVAL ); + }for (i =0 ; i < res->sets; i++) { resource_subtype = temp[i];@@ -3119,10 +3126,13 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle, res->desc[i].res_map =
drivers/firmware/ti_sci.c devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle, · line 3135
Quoted 10 lines
drivers/firmware/ti_sci.c +4
-1devm_kzalloc (dev,BITS_TO_LONGS (res->desc[i].num) *sizeof (*res->desc[i].res_map),GFP_KERNEL ); -if (!res->desc[i].res_map) +if (!res->desc[i].res_map) { +free (temp);return ERR_PTR (-ENOMEM ); + } } +free (temp);
Please move to devm_kfree(temp),
rest of free(temp) in if condition could be dropped.
Quoted 4 lines
drivers/firmware/ti_sci.c +0
-0if (valid_set)return res;
PATCH 2/3 ti_sci: Provide error code on error exit
In reply to Andrew GoodbodyIn ti_sci_get_response the check for message sequence will return ret on a fail but ret will be 0 at that point. Instead return -EINVAL.
This issue was found by Smatch.
--- drivers/firmware/ti_sci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/ti_sci.c +1 -1
@@ -193,7 +193,7 @@ static int ti_sci_get_response(struct ti_sci_info *info,if (hdr->seq != info->seq) {dev_dbg (info->dev,"%s: Message for %d is not expected\n" , __func__, hdr->seq); -return ret; +return -EINVAL ; }if (msg->len > info->desc->max_msg_size) {
Signature · 1 line
2.47.3
PATCH 3/3 ti_sci: Pointer is never assigned to
In reply to Andrew GoodbodyThe pointer resp is declared but never assigned a value but is then dereferenced. Fix this by assigning the pointer to the message buffer.
This issue was found by Smatch.
--- drivers/firmware/ti_sci.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/firmware/ti_sci.c +2 -0
@@ -1365,6 +1365,8 @@ static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,if (ret)return ret; + resp = xfer->tx_message.buf; + *parent_id = resp->parent_id;return ret;
Signature · 1 line
2.47.3
Re: [PATCH 3/3] ti_sci: Pointer is never assigned to
Quoted 15 lines · Andrew Goodbody
The pointer resp is declared but never assigned a value but is then dereferenced. Fix this by assigning the pointer to the message buffer.
This issue was found by Smatch.
Signed-off-by Andrew Goodbody <[email protected]>--- drivers/firmware/ti_sci.c | 2 ++ 1 file changed, 2 insertions(+)drivers/firmware/ti_sci.c +0
-0
drivers/firmware/ti_sci.c static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle, · line 1369
Quoted 5 lines
drivers/firmware/ti_sci.c +2
-0@@ -1365,6 +1365,8 @@ static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,if (ret)return ret; + resp = xfer->tx_message.buf; +
Thanks for catching this. buf is u32*, consider to do typecasting
resp = (struct ti_sci_msg_resp_get_clock_parent *) xfer->tx_message.buf;
Quoted 4 lines
drivers/firmware/ti_sci.c +0
-0*parent_id = resp->parent_id;return ret;