Skip to contentSkip to conversation

ACPI: processor: idle: Add missing bounds check in flatten_lpi_states()

PATCH Superseded Jingkai Tan 4 participants 7 messages Feb 15 – Mar 9 ACPI

lore
Share & tools
lore
#0 lore raw

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
index 81f372c64074..a27dab1ad78e 100644
@@ -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)
#1 lore raw
Quoted 38 lines · Jingkai Tan

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 +0 -0
index 81f372c64074..a27dab1ad78e 100644

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 -1
                        p = prev_level->composite_states[i];
                        if (t->index <= p->enable_parent_state &&
                            combine_lpi_states(p, t, flpi)) {
--
#2 lore raw

PATCH v2 1/1 ACPI: processor: idle: Add missing bounds check in flatten_lpi_states()

In reply to Jingkai Tan

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 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
index 81f372c64074..dc4a24560d1f 100644
@@ -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)
Reviewed-by on v2 1/1
#3 lore raw
Quoted 12 lines · Jingkai Tan

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 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.

Signature · 2 lines
Regards,
Sudeep
maintainer Applied
#4 lore raw
Quoted 10 lines · Sudeep Holla
Quoted 8 lines · Jingkai Tan

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.

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_cnt reaches ACPI_PROCESSOR_MAX_POWER. The existing check in the outer loop will then handle the warning.

Applied as 7.1 material, thanks!

#5 lore raw
In reply to Jingkai Tan
Quoted 26 lines · Jingkai Tan

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 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 +0 -0
index 81f372c64074..dc4a24560d1f 100644

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.

#6 lore raw
Quoted 24 lines · lihuisong (C)
Quoted 21 lines · Jingkai Tan

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 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(+)
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
index 81f372c64074..dc4a24560d1f 100644
@@ -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.