ACPICA: Fix error code path in acpi_ds_call_control_method()

Message ID 2669303.mvXUDI8C0e@kreacher
State New
Headers
Series ACPICA: Fix error code path in acpi_ds_call_control_method() |

Commit Message

Rafael J. Wysocki Nov. 7, 2022, 5:42 p.m. UTC
  From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

A use-after-free in acpi_ps_parse_aml() after a failing invocaion of
acpi_ds_call_control_method() is reported by KASAN [1] and code
inspection reveals that next_walk_state pushed to the thread by
acpi_ds_create_walk_state() is freed on errors, but it is not popped
from the thread beforehand.  Thus acpi_ds_get_current_walk_state()
called by acpi_ps_parse_aml() subsequently returns it as the new
walk state which is incorrect.

To address this, make acpi_ds_call_control_method() call
acpi_ds_pop_walk_state() to pop next_walk_state from the thread before
returning an error.

Link: https://lore.kernel.org/linux-acpi/20221019073443.248215-1-chenzhongjin@huawei.com/
Reported-by: Chen Zhongjin <chenzhongjin@huawei.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/acpica/dsmethod.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
  

Comments

Chen Zhongjin Nov. 8, 2022, 1:16 a.m. UTC | #1
On 2022/11/8 1:42, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>
> A use-after-free in acpi_ps_parse_aml() after a failing invocaion of
> acpi_ds_call_control_method() is reported by KASAN [1] and code
> inspection reveals that next_walk_state pushed to the thread by
> acpi_ds_create_walk_state() is freed on errors, but it is not popped
> from the thread beforehand.  Thus acpi_ds_get_current_walk_state()
> called by acpi_ps_parse_aml() subsequently returns it as the new
> walk state which is incorrect.
>
> To address this, make acpi_ds_call_control_method() call
> acpi_ds_pop_walk_state() to pop next_walk_state from the thread before
> returning an error.
>
> Link: https://lore.kernel.org/linux-acpi/20221019073443.248215-1-chenzhongjin@huawei.com/
> Reported-by: Chen Zhongjin <chenzhongjin@huawei.com>
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
>   drivers/acpi/acpica/dsmethod.c |   10 ++++++++--
>   1 file changed, 8 insertions(+), 2 deletions(-)
>
> Index: linux-pm/drivers/acpi/acpica/dsmethod.c
> ===================================================================
> --- linux-pm.orig/drivers/acpi/acpica/dsmethod.c
> +++ linux-pm/drivers/acpi/acpica/dsmethod.c
> @@ -517,7 +517,7 @@ acpi_ds_call_control_method(struct acpi_
>   	info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
>   	if (!info) {
>   		status = AE_NO_MEMORY;
> -		goto cleanup;
> +		goto pop_walk_state;
>   	}
>   
>   	info->parameters = &this_walk_state->operands[0];
> @@ -529,7 +529,7 @@ acpi_ds_call_control_method(struct acpi_
>   
>   	ACPI_FREE(info);
>   	if (ACPI_FAILURE(status)) {
> -		goto cleanup;
> +		goto pop_walk_state;
>   	}
>   
>   	next_walk_state->method_nesting_depth =
> @@ -575,6 +575,12 @@ acpi_ds_call_control_method(struct acpi_
>   
>   	return_ACPI_STATUS(status);
>   
> +pop_walk_state:
> +
> +	/* On error, pop the walk state to be deleted from thread */
> +
> +	acpi_ds_pop_walk_state(thread);
> +
>   cleanup:
>   
>   	/* On error, we must terminate the method properly */
>
>
LGTM

Reviewed-by: Chen Zhongjin <chenzhongjin@huawei.com>
  

Patch

Index: linux-pm/drivers/acpi/acpica/dsmethod.c
===================================================================
--- linux-pm.orig/drivers/acpi/acpica/dsmethod.c
+++ linux-pm/drivers/acpi/acpica/dsmethod.c
@@ -517,7 +517,7 @@  acpi_ds_call_control_method(struct acpi_
 	info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
 	if (!info) {
 		status = AE_NO_MEMORY;
-		goto cleanup;
+		goto pop_walk_state;
 	}
 
 	info->parameters = &this_walk_state->operands[0];
@@ -529,7 +529,7 @@  acpi_ds_call_control_method(struct acpi_
 
 	ACPI_FREE(info);
 	if (ACPI_FAILURE(status)) {
-		goto cleanup;
+		goto pop_walk_state;
 	}
 
 	next_walk_state->method_nesting_depth =
@@ -575,6 +575,12 @@  acpi_ds_call_control_method(struct acpi_
 
 	return_ACPI_STATUS(status);
 
+pop_walk_state:
+
+	/* On error, pop the walk state to be deleted from thread */
+
+	acpi_ds_pop_walk_state(thread);
+
 cleanup:
 
 	/* On error, we must terminate the method properly */