linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ACPI/IORT: Fix memory leak in iort_rmr_alloc_sids()
@ 2025-08-28 11:22 Miaoqian Lin
  2025-09-01 12:10 ` Hanjun Guo
  2025-09-02 12:23 ` Markus Elfring
  0 siblings, 2 replies; 3+ messages in thread
From: Miaoqian Lin @ 2025-08-28 11:22 UTC (permalink / raw)
  To: Lorenzo Pieralisi, Hanjun Guo, Sudeep Holla, Rafael J. Wysocki,
	Len Brown, Joerg Roedel, Shameer Kolothum, Robin Murphy,
	linux-acpi, linux-arm-kernel, linux-kernel, bpf
  Cc: linmq006, stable

If krealloc_array() fails in iort_rmr_alloc_sids(), the function returns
NULL but does not free the original 'sids' allocation. This results in a
memory leak since the caller overwrites the original pointer with the
NULL return value.

Fixes: 491cf4a6735a ("ACPI/IORT: Add support to retrieve IORT RMR reserved regions")
Cc: <stable@vger.kernel.org>
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
This follows the same pattern as the fix in commit 06615967d488
("bpf, verifier: Fix memory leak in array reallocation for stack state").
---
 drivers/acpi/arm64/iort.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
index 98759d6199d3..65f0f56ad753 100644
--- a/drivers/acpi/arm64/iort.c
+++ b/drivers/acpi/arm64/iort.c
@@ -937,8 +937,10 @@ static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start,
 
 	new_sids = krealloc_array(sids, count + new_count,
 				  sizeof(*new_sids), GFP_KERNEL);
-	if (!new_sids)
+	if (!new_sids) {
+		kfree(sids);
 		return NULL;
+	}
 
 	for (i = count; i < total_count; i++)
 		new_sids[i] = id_start++;
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] ACPI/IORT: Fix memory leak in iort_rmr_alloc_sids()
  2025-08-28 11:22 [PATCH] ACPI/IORT: Fix memory leak in iort_rmr_alloc_sids() Miaoqian Lin
@ 2025-09-01 12:10 ` Hanjun Guo
  2025-09-02 12:23 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Hanjun Guo @ 2025-09-01 12:10 UTC (permalink / raw)
  To: Miaoqian Lin, Lorenzo Pieralisi, Sudeep Holla, Rafael J. Wysocki,
	Len Brown, Joerg Roedel, Shameer Kolothum, Robin Murphy,
	linux-acpi, linux-arm-kernel, linux-kernel, bpf
  Cc: stable, Catalin Marinas, Will Deacon

+Cc Catalin, Will

On 2025/8/28 19:22, Miaoqian Lin wrote:
> If krealloc_array() fails in iort_rmr_alloc_sids(), the function returns
> NULL but does not free the original 'sids' allocation. This results in a
> memory leak since the caller overwrites the original pointer with the
> NULL return value.
> 
> Fixes: 491cf4a6735a ("ACPI/IORT: Add support to retrieve IORT RMR reserved regions")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
> This follows the same pattern as the fix in commit 06615967d488
> ("bpf, verifier: Fix memory leak in array reallocation for stack state").
> ---
>   drivers/acpi/arm64/iort.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
> index 98759d6199d3..65f0f56ad753 100644
> --- a/drivers/acpi/arm64/iort.c
> +++ b/drivers/acpi/arm64/iort.c
> @@ -937,8 +937,10 @@ static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start,
>   
>   	new_sids = krealloc_array(sids, count + new_count,
>   				  sizeof(*new_sids), GFP_KERNEL);
> -	if (!new_sids)
> +	if (!new_sids) {
> +		kfree(sids);
>   		return NULL;
> +	}
>   
>   	for (i = count; i < total_count; i++)
>   		new_sids[i] = id_start++;

Good catch!

Reviewed-by: Hanjun Guo <guohanjun@huawei.com>

Thanks
Hanjun

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ACPI/IORT: Fix memory leak in iort_rmr_alloc_sids()
  2025-08-28 11:22 [PATCH] ACPI/IORT: Fix memory leak in iort_rmr_alloc_sids() Miaoqian Lin
  2025-09-01 12:10 ` Hanjun Guo
@ 2025-09-02 12:23 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2025-09-02 12:23 UTC (permalink / raw)
  To: Miaoqian Lin, linux-acpi, bpf, linux-arm-kernel
  Cc: stable, LKML, Hanjun Guo, Jörg Rödel, Len Brown,
	Lorenzo Pieralisi, Rafael J. Wysocki, Robin Murphy,
	Shameer Kolothum, Sudeep Holla

> If krealloc_array() fails in iort_rmr_alloc_sids(), the function returns
> NULL but does not free the original 'sids' allocation. …

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc4#n94> ---
> This follows the same pattern as the fix in commit 06615967d488
> ("bpf, verifier: Fix memory leak in array reallocation for stack state").
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v6.17-rc4&id=42378a9ca55347102bbf86708776061d8fe3ece2

Regards,
Markus

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-02 12:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-28 11:22 [PATCH] ACPI/IORT: Fix memory leak in iort_rmr_alloc_sids() Miaoqian Lin
2025-09-01 12:10 ` Hanjun Guo
2025-09-02 12:23 ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).