powercap: dtpm: Fix out-of-bounds read in the set_pd_power_limit() callbacks
Subsystems (2)
cover powercap: dtpm: Fix out-of-bounds read in the set_pd_power_limit() callbacks
The set_pd_power_limit() callbacks in dtpm_cpu and dtpm_devfreq scan the EM perf state table for the first state whose power exceeds the requested limit, then use table[i - 1]. If the very first perf state already exceeds the limit, the loop breaks at i == 0 and table[-1] is read out of bounds.
The powercap core clamps the requested limit to dtpm->power_min, but that clamp can be computed from stale data: in dtpm_cpu the number of online CPUs may have grown since power_min was last updated, and in dtpm_devfreq the EM table may have been updated at runtime via em_dev_update_perf_domain(). In both cases the clamped limit can still be below the first state's power, making the underflow reachable.
Start the scan at index 1 so the lowest perf state is used as the fallback when even it exceeds the requested limit.
No functional dependency, but minor context offsets assume the dtpm NULL-guard series posted earlier [1].
[1] https://lore.kernel.org/linux-pm/[email protected]/
Sivan Zohar-Kotzer (2):
powercap: dtpm_cpu: Fix out-of-bounds read in set_pd_power_limit()
powercap: dtpm_devfreq: Fix out-of-bounds read in set_pd_power_limit()
drivers/powercap/dtpm_cpu.c | 2 +- drivers/powercap/dtpm_devfreq.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 1/2 powercap: dtpm_cpu: Fix out-of-bounds read in set_pd_power_limit()
From: Sivan Zohar-Kotzer <[email protected]>
The loop in set_pd_power_limit() looks for the first perf state whose power exceeds the requested limit, then uses table[i - 1]. If the very first perf state already exceeds the limit, the loop breaks at i == 0 and table[-1] is read out of bounds.
The powercap core clamps the requested limit to dtpm->power_min, but power_min was computed by update_pd_power_uw() with the number of online CPUs at that time. If CPUs have come online since the last update, the clamped limit can still be below table[0].power * nr_cpus, making the underflow reachable.
Start the scan at index 1 so the lowest perf state is used as the fallback when even it exceeds the requested limit.
--- drivers/powercap/dtpm_cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/powercap/dtpm_cpu.c +1 -1
@@ -54,7 +54,7 @@ static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)rcu_read_lock (); table =em_perf_state_from_pd (pd); -for (i =0 ; i < pd->nr_perf_states; i++) { +for (i =1 ; i < pd->nr_perf_states; i++) { power = table[i].power * nr_cpus;
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 2/2 powercap: dtpm_devfreq: Fix out-of-bounds read in set_pd_power_limit()
In reply to Elazar LeibovichFrom: Sivan Zohar-Kotzer <[email protected]>
The loop in set_pd_power_limit() looks for the first perf state whose power exceeds the requested limit, then uses table[i - 1]. If the very first perf state already exceeds the limit, the loop breaks at i == 0 and table[-1] is read out of bounds.
The powercap core clamps the requested limit to dtpm->power_min, but power_min is only captured by update_pd_power_uw() at setup time. If the energy model table is updated at runtime via em_dev_update_perf_domain() and the power of the lowest perf state grows past the stale power_min, the clamped limit can still be below table[0].power, making the underflow reachable.
Start the scan at index 1 so the lowest perf state is used as the fallback when even it exceeds the requested limit.
--- drivers/powercap/dtpm_devfreq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/powercap/dtpm_devfreq.c +1 -1
@@ -68,7 +68,7 @@ static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)rcu_read_lock (); table =em_perf_state_from_pd (pd); -for (i =0 ; i < pd->nr_perf_states; i++) { +for (i =1 ; i < pd->nr_perf_states; i++) {if (table[i].power > power_limit)break ; }
Signature · 1 line
2.50.1 (Apple Git-155)
Hi,
Quoted 6 lines · Elazar Leibovich
The powercap core clamps the requested limit to
dtpm->power_min, but that clamp can be computed from stale data: indtpm_cputhe number of online CPUs may have grown sincepower_minwas last updated, and indtpm_devfreqthe EM table may have been updated at runtime viaem_dev_update_perf_domain(). In both cases the clamped limit can still be below the first state's power, making the underflow reachable.
The underflow is also reachable without any race, by setting a power limit on a non-leaf node of the DTPM hierarchy.
__set_power_limit_uw() distributes the limit of a node among its children proportionally to their weights, and the weights are computed from power_max only. The share of a child is not clamped to its own power_min, so when the limit of the parent is close to its power_min, a child whose power_min is high compared to its power_max gets a share below the power of its lowest performance state. set_pd_power_limit() then breaks at i == 0 and reads table[-1].
For instance, with two CPU performance domains under the same parent, one with a 80-1200 range and the other one with a 400-3000 range, the parent's range is 480-4200. Writing 500 to the parent's constraint_0_power_limit_uw gives a share of about 357 to the second domain, below its minimum of 400. The hierarchy created by drivers/soc/rockchip/dtpm.c (the CPU clusters and the GPU under the 'package' node) has this shape. Whether a given limit triggers it depends on the energy model values of the platform.
So this series also fixes an out-of-bounds read deterministically reachable from sysfs, by a single write to the power limit of a parent node.
I've posted a separate patch fixing the distribution itself, so a child doesn't get less than its minimum power when the parent's limit allows it [1]. It doesn't replace this series, which is still needed for the stale power_min cases you described.
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Mikhail