firmware: arm_scmi: Lazy clock rates and bound iterator fixes
cover firmware: arm_scmi: Lazy clock rates and bound iterator fixes
Hi all,
This patch series:
- Fixes an out-of-bound access in lazy clock rate handling,
- Synchronizes bound-iterator cleanup naming between documentation and code.
Note that the first two patches fix the same symptom; either or both can be applied.
Thanks for your comments!
Geert Uytterhoeven (3):
firmware: arm_scmi: Fix OOB in scmi_clock_describe_rates_get_lazy()
firmware: arm_scmi: Fix bound iterators returning too many items
firmware: arm_scmi: Use proper iter_response_bound_cleanup() name
drivers/firmware/arm_scmi/clock.c | 9 ++++++--- drivers/firmware/arm_scmi/driver.c | 15 ++++++++++----- drivers/firmware/arm_scmi/protocols.h | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-)
Signature · 12 lines
2.43.0
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus TorvaldsPATCH 1/3 firmware: arm_scmi: Fix OOB in scmi_clock_describe_rates_get_lazy()
Lazy discovery of discrete rates works as follows:
A. Grab the first three rates,
B. Grab the last rate, if there are more than three rates.
It is up to the SCMI provider implementation to decide how many rates are returned in response to a single CLOCK_DESCRIBE_RATES command. Each rate received is stored in the scmi_clock_rates.rates[] array, and .num_rates is updated accordingly.
When more than 3 rates have been received after step A, the last rate may have been received already, and stored in scmi_clock_rates.rates[] (which has space for scmi_clock_desc.tot_rates entries). Hence grabbing the last rate again will store it a second time, beyond the end of the array.
Fix this by only grabbing the last rate when we don't already have it.
---
This bug caused random "kernel BUG at drivers/base/devres.c:135!" crashes during boot on R-Car X5H.
Example for a clock with 8 rates, which are all returned in response to a single CLOCK_DESCRIBE_RATES command:
scmi_clock_describe_rates_get_lazy: Grabbing rates 0..2
iter_clk_describe_update_state: Returned 8 remaining 0
iter_clk_describe_update_state: Allocating 8 rates
iter_clk_describe_process_response: rates[0] = 33333333
iter_clk_describe_process_response: rates[1] = 66666666
iter_clk_describe_process_response: rates[2] = 133333333
iter_clk_describe_process_response: rates[3] = 266666666
iter_clk_describe_process_response: rates[4] = 355555555
iter_clk_describe_process_response: rates[5] = 533333333
iter_clk_describe_process_response: rates[6] = 711111111
iter_clk_describe_process_response: rates[7] = 1066666666
^^^^^^^^^^
scmi_clock_describe_rates_get_lazy: Grabbing rates 7..7
iter_clk_describe_update_state: Returned 1 remaining 0
iter_clk_describe_process_response: rates[8] = 1066666666
^ ^^^^^^^^^^
Out of bounds access! ------------------------+ |
Same value as [7] ---------------------------------+
drivers/firmware/arm_scmi/clock.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)drivers/firmware/arm_scmi/clock.c +5 -2
@@ -593,8 +593,11 @@ scmi_clock_describe_rates_get_lazy(const struct scmi_protocol_handle *ph,if (ret) goto out; -/* If discrete grab the last value, which should be the max */ -if (clkd->r.rate_discrete && clkd->tot_rates >3 ) { +/* +* If discrete and we don't already have it, grab the last value, which +* should be the max +*/ +if (clkd->r.rate_discrete && clkd->tot_rates > clkd->r.num_rates) { first = clkd->tot_rates -1 ; last = clkd->tot_rates -1 ; ret = ph->hops->iter_response_run_bound (iter, &first, &last);
Signature · 1 line
2.43.0
PATCH 2/3 firmware: arm_scmi: Fix bound iterators returning too many items
In reply to Geert UytterhoevenWhen using a bound-iterator with an upper bound, commands are sent, and responses are received, until the upper bound is reached. However, it is up to the SCMI provider implementation to decide how many rates are returned in response to a single CLOCK_DESCRIBE_RATES command. If the last response contains rates beyond the specified upper bound, they are still passed up for further processing. This may lead to buffer overflows in unprepared callsites.
While the imprecise bound handling may have been intentional (it was mentioned in the commit message introducing the code), it is still confusing for users, and may cause hard to debug crashes. Fix this by strictly enforcing the upper bound.
Note that this may cause an increase in the number of CLOCK_DESCRIBE_RATES commands issued, as retrieving the last rate may no longer be done inadvertentently, but require its own command.
---
This caused random "kernel BUG at drivers/base/devres.c:135!" crashes during boot on R-Car X5H, as lazy clock rate discovery does not handle correctly receiving more rates than expected.
Example for a clock with 8 rates, which are all returned in response to a single CLOCK_DESCRIBE_RATES command:
scmi_clock_describe_rates_get_lazy: Grabbing rates 0..2
iter_clk_describe_update_state: Returned 8 remaining 0
iter_clk_describe_update_state: Allocating 8 rates
iter_clk_describe_process_response: rates[0] = 33333333
iter_clk_describe_process_response: rates[1] = 66666666
iter_clk_describe_process_response: rates[2] = 133333333
iter_clk_describe_process_response: rates[3] = 266666666
iter_clk_describe_process_response: rates[4] = 355555555
iter_clk_describe_process_response: rates[5] = 533333333
iter_clk_describe_process_response: rates[6] = 711111111
iter_clk_describe_process_response: rates[7] = 1066666666
^^^^^^^^^^Rates [3] to [7] are received, despite being outside the bound.
scmi_clock_describe_rates_get_lazy: Grabbing rates 7..7 iter_clk_describe_update_state: Returned 1 remaining 0 iter_clk_describe_process_response: rates[8] = 1066666666
^ ^^^^^^^^^^
Out of bounds access! ------------------------+ |
Same value as [7] ---------------------------------+
--- drivers/firmware/arm_scmi/driver.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/firmware/arm_scmi/driver.c +7 -2
@@ -1820,6 +1820,7 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *enconst struct scmi_protocol_handle *ph;struct scmi_iterator_state *st;struct scmi_iterator *i; +unsigned int n;if (!iter)return -EINVAL ;
@@ -1852,13 +1853,17 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *enreturn -EINVAL ; } -for (st->loop_idx =0 ; st->loop_idx < st->num_returned; st->loop_idx++) { +if (end) + n =min (st->num_returned, *end - st->desc_index +1 ); +else + n = st->num_returned; +for (st->loop_idx =0 ; st->loop_idx < n; st->loop_idx++) { ret = iops->process_response (ph, i->resp, st, i->priv);if (ret)return ret; } - st->desc_index += st->num_returned; + st->desc_index += n; ph->xops->reset_rx_to_maxsz (ph, i->t);/* * check for both returned and remaining to avoid infinite
Signature · 1 line
2.43.0
PATCH 3/3 firmware: arm_scmi: Use proper iter_response_bound_cleanup() name
In reply to Geert UytterhoevenThe documentation speaks of the "iter_response_bound_cleanup()" protocol helper, while the actual helper is called "iter_response_cleanup()". Settle on the former name, because the helper is only needed when using bound-iterators.
--- drivers/firmware/arm_scmi/clock.c | 2 +- drivers/firmware/arm_scmi/driver.c | 6 +++--- drivers/firmware/arm_scmi/protocols.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/firmware/arm_scmi/clock.c +1 -1
@@ -604,7 +604,7 @@ scmi_clock_describe_rates_get_lazy(const struct scmi_protocol_handle *ph, } out: - ph->hops->iter_response_cleanup (iter); + ph->hops->iter_response_bound_cleanup (iter);return ret; }
drivers/firmware/arm_scmi/driver.c +3 -3
@@ -1875,7 +1875,7 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *enreturn 0 ; } -static void scmi_iterator_cleanup (void *iter) +static void scmi_iterator_bound_cleanup (void *iter) {struct scmi_iterator *i = iter;
@@ -1888,7 +1888,7 @@ static int scmi_iterator_run(void *iter)int ret; ret =__scmi_iterator_run (iter,NULL ,NULL ); -scmi_iterator_cleanup (iter); +scmi_iterator_bound_cleanup (iter);return ret; }
@@ -2078,7 +2078,7 @@ static const struct scmi_proto_helpers_ops helpers_ops = { .iter_response_init = scmi_iterator_init, .iter_response_run = scmi_iterator_run, .iter_response_run_bound = scmi_iterator_run_bound, - .iter_response_cleanup = scmi_iterator_cleanup, + .iter_response_bound_cleanup = scmi_iterator_bound_cleanup, .protocol_msg_check = scmi_protocol_msg_check, .fastchannel_init = scmi_common_fastchannel_init, .fastchannel_db_ring = scmi_common_fastchannel_db_ring,
drivers/firmware/arm_scmi/protocols.h +1 -1
@@ -286,7 +286,7 @@ struct scmi_proto_helpers_ops {int (*iter_response_run)(void *iter);int (*iter_response_run_bound)(void *iter,unsigned int *start,unsigned int *end); -void (*iter_response_cleanup)(void *iter); +void (*iter_response_bound_cleanup)(void *iter);int (*protocol_msg_check)(const struct scmi_protocol_handle *ph,u32 message_id,u32 *attributes); void (*fastchannel_init)(const struct scmi_protocol_handle *ph,
Signature · 1 line
2.43.0
Cristian Marussi
reviewerQuoted 1 line · Geert Uytterhoeven
Hi all,
Hi Geert,
Quoted 5 lines
This patch series:
- Fixes an out-of-bound access in lazy clock rate handling,
- Synchronizes bound-iterator cleanup naming between documentation and code.
thanks for this !
I was just chasing down exactly the same issue, since it was flagged by our CI on a rockchip board (together with some KASAN splat...)...but I had still to manage to get my hands directly on that board to start debugging properly ... so ...
... very happy that you beat me at this:P !
While waiting for the board and trying to figure out what could cause the fatal issue I spotted something more to be rectified in the core of the iterators, BUT I dont think it would have solved the issue like your fixes.
In a nutshell, it was the possibility of an integer undeflow due to an unchecked subtraction between unsigned.
---8<---
commit 65bd4a11333098fbf4c60f3bc59c971be1cd259d (mygitlab/scmi_dev, scmi_dev)
Author: Cristian Marussi <[email protected]> Date: Mon Mar 23 08:19:32 2026 +0000
[TODO] FIX Iterator boundary checking
[TODO] FIX Iterator boundary checking
drivers/firmware/arm_scmi/driver.c +1 -1
@@ -1845,7 +1845,7 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *enif (ret)return ret; -if (st->num_returned > st->max_resources - st->desc_index) { +if (st->num_returned + st->desc_index > st->max_resources) {dev_err (ph->dev,"No. of resources can't exceed %d\n" , st->max_resources);
---8<----
Anyway, next dsys I will test all of this with your series, but since my original series indeed was on hold now due to these issues AND because still lacking clock-MAINTs acks, I am not sure if:
- we'll merge your fixes into my series while maintaining of course your authorship (instead of applying the series on top)
- Sudeep will still queue any of this for this cycle
Thanks a lot for the debug and fixes to my cr...y stuff :P
Cristian