ACPI: processor: idle: Add missing bounds check in flatten_lpi_states()
PATCH 1/1 ACPI: processor: idle: Add missing bounds check in flatten_lpi_states()
The inner loop in flatten_lpi_states() that combines composite LPI states can increment flat_state_cnt multiple times within the loop.
The condition that guards this (checks bounds against ACPI_PROCESSOR _MAX_POWER) occurs at the top of the outer loop. flat_state_cnt might exceed ACPI_PROCESSOR_MAX_POWER if it is incremented multiple times within the inner loop between outer loop iterations.
Add the same bounds check inside the inner loop to prevent flat_state_cnt from exceeding ACPI_PROCESSOR_MAX_POWER there as well
---
Hi,
I'm still fairly new to kernel contributions, just looking for small fixes on the weekend to familarize myself with contributions, so hopefully I am not making a mistake here. I was looking at other issues and came across the processor_idle function. While reading it, I noticed that the guard from L1048 to L1052 only checks at the beginning of the outer loop iteration and seems to be written with a singular increment in mind to check whether flat_state_cnt exceeds ACPI_PROCESSOR_MAX_POWER each time it loops around.
The nested loop at L1064 possibly increments flat_state_cnt multiple times depending on the composite_states_size. I'm not too familiar with this but flat_state_cnt looks to me like it might hypothetically increment more than once and exceed ACPI_PROCESSOR_MAX_POWER.
drivers/acpi/processor_idle.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/acpi/processor_idle.c +6 -0
@@ -1062,6 +1062,12 @@ static unsigned int flatten_lpi_states(struct acpi_processor *pr, }for (i =0 ; i < prev_level->composite_states_size; i++) { +if (flat_state_cnt >=ACPI_PROCESSOR_MAX_POWER ) { +pr_warn ("Limiting number of LPI states to max (%d)\n" , +ACPI_PROCESSOR_MAX_POWER ); +pr_warn ("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n" ); +break ; + } p = prev_level->composite_states[i];if (t->index <= p->enable_parent_state &&combine_lpi_states (p, t, flpi)) {
Signature · 1 line
2.50.1 (Apple Git-155)
Rafael J. Wysocki
maintainerQuoted 38 lines · Jingkai Tan
The inner loop in
flatten_lpi_states()that combines composite LPI states can incrementflat_state_cntmultiple times within the loop.The condition that guards this (checks bounds against
ACPI_PROCESSOR_MAX_POWER) occurs at the top of the outer loop.flat_state_cntmight exceedACPI_PROCESSOR_MAX_POWERif it is incremented multiple times within the inner loop between outer loop iterations.Add the same bounds check inside the inner loop to prevent
flat_state_cntfrom exceedingACPI_PROCESSOR_MAX_POWERthere as wellSigned-off-by Jingkai Tan <[email protected]>---
Hi,I'm still fairly new to kernel contributions, just looking for small fixes on the weekend to familarize myself with contributions, so hopefully I am not making a mistake here. I was looking at other issues and came across the
processor_idlefunction. While reading it, I noticed that the guard from L1048 to L1052 only checks at the beginning of the outer loop iteration and seems to be written with a singular increment in mind to check whetherflat_state_cntexceedsACPI_PROCESSOR_MAX_POWEReach time it loops around.The nested loop at L1064 possibly increments
flat_state_cntmultiple times depending on thecomposite_states_size. I'm not too familiar with this butflat_state_cntlooks to me like it might hypothetically increment more than once and exceedACPI_PROCESSOR_MAX_POWER.drivers/acpi/processor_idle.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/acpi/processor_idle.c +0
-0
drivers/acpi/processor_idle.c static unsigned int flatten_lpi_states(struct acpi_processor *pr, · line 1070
Quoted 9 lines
drivers/acpi/processor_idle.c +6
-0@@ -1062,6 +1062,12 @@ static unsigned int flatten_lpi_states(struct acpi_processor *pr, }for (i =0 ; i < prev_level->composite_states_size; i++) { +if (flat_state_cnt >=ACPI_PROCESSOR_MAX_POWER ) { +pr_warn ("Limiting number of LPI states to max (%d)\n" , +ACPI_PROCESSOR_MAX_POWER ); +pr_warn ("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n" ); +break ; + }
The issue addressed by this patch appears to be genuine to me, but (a) flat_state_cnt need not be checked before incrementing it and (b) it should be sufficient to break out of the inner loop when flat_state_cnt becomes too large after incrementing it as that would be caught by the check in the outer loop then.
Quoted 4 lines
drivers/acpi/processor_idle.c +0
-1p = prev_level->composite_states[i];if (t->index <= p->enable_parent_state &&combine_lpi_states (p, t, flpi)) { --
PATCH v2 1/1 ACPI: processor: idle: Add missing bounds check in flatten_lpi_states()
In reply to Jingkai TanThe inner loop in flatten_lpi_states() that combines composite LPI states can increment flat_state_cnt multiple times within the loop.
The condition that guards this (checks bounds against ACPI_PROCESSOR _MAX_POWER) occurs at the top of the outer loop. flat_state_cnt might exceed ACPI_PROCESSOR_MAX_POWER if it is incremented multiple times within the inner loop between outer loop iterations.
Add a bounds check after the increment inside the inner loop so that it breaks out when flat_state_cnt reaches ACPI_PROCESSOR_MAX_POWER. The existing check in the outer loop will then handle the warning.
---
Thanks for the review Rafael.
Patch v2 as per review, flat_state_cnt is now checked after increment, and the warnings are left to outer loop to handle
drivers/acpi/processor_idle.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/acpi/processor_idle.c +2 -0
@@ -1068,6 +1068,8 @@ static unsigned int flatten_lpi_states(struct acpi_processor *pr,stash_composite_state (curr_level, flpi); flat_state_cnt++; flpi++; +if (flat_state_cnt >=ACPI_PROCESSOR_MAX_POWER ) +break ; } } }
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 12 lines · Jingkai Tan
The inner loop in
flatten_lpi_states()that combines composite LPI states can incrementflat_state_cntmultiple times within the loop.The condition that guards this (checks bounds against
ACPI_PROCESSOR_MAX_POWER) occurs at the top of the outer loop.flat_state_cntmight exceedACPI_PROCESSOR_MAX_POWERif it is incremented multiple times within the inner loop between outer loop iterations.Add a bounds check after the increment inside the inner loop so that it breaks out when
flat_state_cntreachesACPI_PROCESSOR_MAX_POWER. The existing check in the outer loop will then handle the warning.
Signature · 2 lines
Regards, Sudeep
Quoted 10 lines · Sudeep Holla
Quoted 8 lines · Jingkai Tan
The inner loop in
flatten_lpi_states()that combines composite LPI states can incrementflat_state_cntmultiple times within the loop.The condition that guards this (checks bounds against
ACPI_PROCESSOR_MAX_POWER) occurs at the top of the outer loop.flat_state_cntmight exceedACPI_PROCESSOR_MAX_POWERif it is incremented multiple times within the inner loop between outer loop iterations.
Quoted 6 lines
Quoted 4 lines
Add a bounds check after the increment inside the inner loop so that it breaks out when
flat_state_cntreachesACPI_PROCESSOR_MAX_POWER. The existing check in the outer loop will then handle the warning.Reviewed-by Sudeep Holla <[email protected]>
Applied as 7.1 material, thanks!
Quoted 26 lines · Jingkai Tan
The inner loop in
flatten_lpi_states()that combines composite LPI states can incrementflat_state_cntmultiple times within the loop.The condition that guards this (checks bounds against
ACPI_PROCESSOR_MAX_POWER) occurs at the top of the outer loop.flat_state_cntmight exceedACPI_PROCESSOR_MAX_POWERif it is incremented multiple times within the inner loop between outer loop iterations.Add a bounds check after the increment inside the inner loop so that it breaks out when
flat_state_cntreachesACPI_PROCESSOR_MAX_POWER. The existing check in the outer loop will then handle the warning.Signed-off-by Jingkai Tan <[email protected]>---
Thanks for the review Rafael.Patch v2 as per review,
flat_state_cntis now checked after increment, and the warnings are left to outer loop to handledrivers/acpi/processor_idle.c | 2 ++ 1 file changed, 2 insertions(+)drivers/acpi/processor_idle.c +0
-0
drivers/acpi/processor_idle.c static unsigned int flatten_lpi_states(struct acpi_processor *pr, · line 1075
Quoted 8 lines
drivers/acpi/processor_idle.c +2
-0@@ -1068,6 +1068,8 @@ static unsigned int flatten_lpi_states(struct acpi_processor *pr,stash_composite_state (curr_level, flpi); flat_state_cnt++; flpi++; +if (flat_state_cnt >=ACPI_PROCESSOR_MAX_POWER ) +break ; } } }
Above "if (!prev_level)" switch may also need to this check.
Rafael J. Wysocki
maintainerQuoted 24 lines · lihuisong (C)
Quoted 21 lines · Jingkai Tan
The inner loop in
flatten_lpi_states()that combines composite LPI states can incrementflat_state_cntmultiple times within the loop.The condition that guards this (checks bounds against
ACPI_PROCESSOR_MAX_POWER) occurs at the top of the outer loop.flat_state_cntmight exceedACPI_PROCESSOR_MAX_POWERif it is incremented multiple times within the inner loop between outer loop iterations.Add a bounds check after the increment inside the inner loop so that it breaks out when
flat_state_cntreachesACPI_PROCESSOR_MAX_POWER. The existing check in the outer loop will then handle the warning.Signed-off-by Jingkai Tan <[email protected]>---
Thanks for the review Rafael.Patch v2 as per review,
flat_state_cntis now checked after increment, and the warnings are left to outer loop to handledrivers/acpi/processor_idle.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 14 lines
drivers/acpi/processor_idle.c static unsigned int flatten_lpi_states(struct acpi_processor *pr, · line 1075
Quoted 13 lines
drivers/acpi/processor_idle.c +2
-0@@ -1068,6 +1068,8 @@ static unsigned int flatten_lpi_states(struct acpi_processor *pr,stash_composite_state (curr_level, flpi); flat_state_cnt++; flpi++; +if (flat_state_cnt >=ACPI_PROCESSOR_MAX_POWER ) +break ; } } }Above "if (!prev_level)" switch may also need to this check.
That one is covered by the existing check AFAICS.