firmware/clk: arm_scpi hardening (leak, OPP bounds, cpufreq)
Subsystems (2)
cover firmware/clk: arm_scpi hardening (leak, OPP bounds, cpufreq)
Hi,
This series hardens the OF SCPI firmware and clock paths against a few real defects found while reviewing linux-next:
1) device_node leak in scpi_dev_domain_id() after of_parse_phandle_with_args()
2) DVFS OPP count from SCP trusted beyond MAX_DVFS_OPPS (OOB read / bad OPP table size)
3) DVFS index used as clock rate without an upper bound check
4) scpi-cpufreq registered once only, and cleared on register failure so remove() does not unregister an ERR_PTR
Please review.
Thanks,
Xixin Liu
---
Xixin Liu (4):
firmware: arm_scpi: fix device_node leak in scpi_dev_domain_id
firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS
clk: scpi: bound-check DVFS index in scpi_dvfs_recalc_rate
clk: scpi: register scpi-cpufreq once and clear on failure
drivers/clk/clk-scpi.c | 8 ++++++-- drivers/firmware/arm_scpi.c | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/4 firmware: arm_scpi: fix device_node leak in scpi_dev_domain_id
of_parse_phandle_with_args() takes a reference on clkspec.np that must be released with of_node_put(). scpi_dev_domain_id() returned clkspec.args[0] without dropping that reference, so every domain lookup leaked a device node. Paths such as scpi_dvfs_info() / cpufreq init call this per CPU, so the leak accumulates over time.
Save the domain id, of_node_put(clkspec.np), then return the saved value.
--- drivers/firmware/arm_scpi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/firmware/arm_scpi.c +4 -1
@@ -660,12 +660,15 @@ static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)static int scpi_dev_domain_id (struct device *dev) {struct of_phandle_args clkspec; +int domain;if (of_parse_phandle_with_args (dev->of_node,"clocks" ,"#clock-cells" ,0 , &clkspec))return -EINVAL ; -return clkspec.args[0 ]; + domain = clkspec.args[0 ]; +of_node_put (clkspec.np); +return domain; }static struct scpi_dvfs_info *scpi_dvfs_info (struct device *dev)
Signature · 1 line
2.53.0
PATCH 2/4 firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS
In reply to Xixin Liuscpi_dvfs_get_info() already rejected a zero opp_count, but still trusted any larger value from the SCP firmware. The shared-memory reply only holds MAX_DVFS_OPPS entries in buf.opps[]; a bigger count over-reads that array and then sizes the allocated OPP table incorrectly (garbage OPPs / OOB).
Reject zero and out-of-range counts in one check and return -EINVAL.
--- drivers/firmware/arm_scpi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/arm_scpi.c +2 -2
@@ -630,8 +630,8 @@ static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)if (ret)return ERR_PTR (ret); -if (!buf.opp_count) -return ERR_PTR (-ENOENT ); +if (!buf.opp_count || buf.opp_count >MAX_DVFS_OPPS ) +return ERR_PTR (-EINVAL ); info =kmalloc (sizeof (*info),GFP_KERNEL );if (!info)
Signature · 1 line
2.53.0
Sudeep Holla
maintainerRe: [PATCH v1 2/4] firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS
Quoted 7 lines · Xixin Liu
scpi_dvfs_get_info()already rejected a zeroopp_count, but still trusted any larger value from the SCP firmware. The shared-memory reply only holdsMAX_DVFS_OPPSentries in buf.opps[]; a bigger count over-reads that array and then sizes the allocated OPP table incorrectly (garbage OPPs / OOB).Reject zero and out-of-range counts in one check and return
-EINVAL.
This fails to apply, you need to rebase to the latest.
Signature · 2 lines
Regards, Sudeep
Sudeep Holla
maintainerRe: [PATCH v1 2/4] firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS
In reply to Xixin LiuQuoted 7 lines · Xixin Liu
scpi_dvfs_get_info()already rejected a zeroopp_count, but still trusted any larger value from the SCP firmware. The shared-memory reply only holdsMAX_DVFS_OPPSentries in buf.opps[]; a bigger count over-reads that array and then sizes the allocated OPP table incorrectly (garbage OPPs / OOB).Reject zero and out-of-range counts in one check and return
-EINVAL.
This issue of out-of-bounds read is present since the original SCPI DVFS implementation. So please include the below fixes tag:
Signature · 2 lines
Regards, Sudeep
PATCH 3/4 clk: scpi: bound-check DVFS index in scpi_dvfs_recalc_rate
In reply to Xixin Liudvfs_get_idx() may return an out-of-range index if the SCP firmware is buggy or returns a stale value. Only negative indexes were rejected, so a large index walked past info->opps and could treat garbage as a clock rate (KASAN OOB / wrong frequency to consumers).
Treat indexes >= opp count as invalid and return 0, same as idx < 0.
--- drivers/clk/clk-scpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-scpi.c +1 -1
@@ -85,7 +85,7 @@ static unsigned long scpi_dvfs_recalc_rate(struct clk_hw *hw,int idx = clk->scpi_ops->dvfs_get_idx (clk->id);const struct scpi_opp *opp; -if (idx <0 ) +if (idx <0 || idx >= clk->info->count)return 0 ; opp = clk->info->opps + idx;
Signature · 1 line
2.53.0
Sudeep Holla
maintainerRe: [PATCH v1 3/4] clk: scpi: bound-check DVFS index in scpi_dvfs_recalc_rate
Quoted 7 lines · Xixin Liu
dvfs_get_idx()may return an out-of-range index if the SCP firmware is buggy or returns a stale value. Only negative indexes were rejected, so a large index walked pastinfo->oppsand could treat garbage as a clock rate (KASAN OOB / wrong frequency to consumers).Treat indexes >= opp count as invalid and return 0, same as idx < 0.
Again this issue of out-of-bounds read is in the original SCPI clock driver. So include the below fixes tag:
Signature · 2 lines
Regards, Sudeep
PATCH 4/4 clk: scpi: register scpi-cpufreq once and clear on failure
In reply to Xixin Liuscpi_clk_probe() walks clock children and, for each DVFS provider, calls platform_device_register_simple("scpi-cpufreq"). Two related bugs:
1) Multiple DVFS children each spawned another virtual cpufreq device. If cpufreq_dev is already set, continue and skip a second register.
2) On IS_ERR, the pointer still held ERR_PTR (e.g. -ENOMEM). remove() treats any non-NULL cpufreq_dev as live and would call platform_device_unregister() on the error pointer (oops). Clear cpufreq_dev to NULL after the failure warn so remove() skips it.
--- drivers/clk/clk-scpi.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/clk/clk-scpi.c +5 -1
@@ -283,10 +283,14 @@ static int scpi_clocks_probe(struct platform_device *pdev)if (match->data != &scpi_dvfs_ops)continue ;/* Add the virtual cpufreq device if it's DVFS clock provider */ +if (cpufreq_dev) +continue ; cpufreq_dev =platform_device_register_simple ("scpi-cpufreq" ,-1 ,NULL ,0 ); -if (IS_ERR (cpufreq_dev)) +if (IS_ERR (cpufreq_dev)) {pr_warn ("unable to register cpufreq device" ); + cpufreq_dev =NULL ; + } }return 0 ; }
Signature · 1 line
2.53.0
Sudeep Holla
maintainerRe: [PATCH v1 4/4] clk: scpi: register scpi-cpufreq once and clear on failure
Quoted 3 lines · Xixin Liu
scpi_clk_probe()walks clock children and, for each DVFS provider, callsplatform_device_register_simple("scpi-cpufreq"). Two related bugs:
The function named above appears to be scpi_clocks_probe(), not scpi_clk_probe(). Fix that.
Quoted 3 lines
1) Multiple DVFS children each spawned another virtual cpufreq device. If
cpufreq_devis already set, continue and skip a second register.
The registration uses id -1, so every device must be named "scpi-cpufreq". A second device_add() cannot register that name and must return -EEXIST; platform_device_register_full() then drops the second pdev.
So the above information could be misleading.
Can you state it as each additional DVFS child attempts a duplicate registration and overwrites the first valid pointer with ERR_PTR(-EEXIST), rather than saying that each child spawns another device?
Quoted 5 lines
2) On
IS_ERR, the pointer still heldERR_PTR(e.g.-ENOMEM).remove()treats any non-NULLcpufreq_devas live and would callplatform_device_unregister()on the error pointer (oops). Clearcpufreq_devto NULL after the failure warn soremove()skips it.
This also fixes bugs introduced by the original virtual device support and by moving registration inside the child loop. So
Signature · 2 lines
Regards, Sudeep
v2 cover firmware/clk: arm_scpi hardening (leak, OPP bounds, cpufreq)
In reply to Xixin LiuHi Sudeep,
Thanks for reviewing v1.
This v2 hardens the OF SCPI firmware and clock paths against a few real defects found while reviewing linux-next:
1) device_node leak in scpi_dev_domain_id() after of_parse_phandle_with_args()
2) DVFS OPP count from SCP trusted beyond MAX_DVFS_OPPS (OOB read / bad OPP table size)
3) DVFS index used as clock rate without an upper bound check
4) scpi-cpufreq registered once only, and cleared on register failure so remove() does not unregister an ERR_PTR
5) use PLATFORM_DEVID_NONE instead of bare -1 for the scpi-cpufreq platform device id (readability; Fixes the original registration)
Changes since v1:
- 2/5: add Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
- 3/5: add Fixes: cd52c2a4b5c4 ("clk: add support for clocks provided by SCP(System Control Processor)")
- 4/5: correct commit message (scpi_clocks_probe; multi-DVFS child overwrites the first good cpufreq_dev with ERR_PTR(-EEXIST)) and add Fixes: 9490f01e2471 / 67bcc2c5f1da
All per maintainer feedback; code unchanged.
- add 5/5: clk: scpi: use PLATFORM_DEVID_NONE for scpi-cpufreq (readability; Fixes: 9490f01e2471)
- Rebased onto linux-next next-20260727 (0d33d21e47d9); 2/5 context updated for kmalloc_obj() (logic unchanged).
Please review.
Thanks,
Xixin Liu
---
Xixin Liu (5):
firmware: arm_scpi: fix device_node leak in scpi_dev_domain_id
firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS
clk: scpi: bound-check DVFS index in scpi_dvfs_recalc_rate
clk: scpi: register scpi-cpufreq once and clear on failure
clk: scpi: use PLATFORM_DEVID_NONE for scpi-cpufreq
drivers/clk/clk-scpi.c | 10 ++++++++-- drivers/firmware/arm_scpi.c | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/5 firmware: arm_scpi: fix device_node leak in scpi_dev_domain_id
of_parse_phandle_with_args() takes a reference on clkspec.np that must be released with of_node_put(). scpi_dev_domain_id() returned clkspec.args[0] without dropping that reference, so every domain lookup leaked a device node. Paths such as scpi_dvfs_info() / cpufreq init call this per CPU, so the leak accumulates over time.
Save the domain id, of_node_put(clkspec.np), then return the saved value.
--- drivers/firmware/arm_scpi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/firmware/arm_scpi.c +4 -1
@@ -661,12 +661,15 @@ static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)static int scpi_dev_domain_id (struct device *dev) {struct of_phandle_args clkspec; +int domain;if (of_parse_phandle_with_args (dev->of_node,"clocks" ,"#clock-cells" ,0 , &clkspec))return -EINVAL ; -return clkspec.args[0 ]; + domain = clkspec.args[0 ]; +of_node_put (clkspec.np); +return domain; }static struct scpi_dvfs_info *scpi_dvfs_info (struct device *dev)
Signature · 1 line
2.53.0
PATCH v2 2/5 firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS
In reply to Xixin Liuscpi_dvfs_get_info() already rejected a zero opp_count, but still trusted any larger value from the SCP firmware. The shared-memory reply only holds MAX_DVFS_OPPS entries in buf.opps[]; a bigger count over-reads that array and then sizes the allocated OPP table incorrectly (garbage OPPs / OOB). The missing upper bound dates back to the original SCPI DVFS support.
Reject zero and out-of-range counts in one check and return -EINVAL.
--- drivers/firmware/arm_scpi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/arm_scpi.c +2 -2
@@ -631,8 +631,8 @@ static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)if (ret)return ERR_PTR (ret); -if (!buf.opp_count) -return ERR_PTR (-ENOENT ); +if (!buf.opp_count || buf.opp_count >MAX_DVFS_OPPS ) +return ERR_PTR (-EINVAL ); info =kmalloc_obj (*info);if (!info)
Signature · 1 line
2.53.0
PATCH v2 3/5 clk: scpi: bound-check DVFS index in scpi_dvfs_recalc_rate
In reply to Xixin Liudvfs_get_idx() may return an out-of-range index if the SCP firmware is buggy or returns a stale value. Only negative indexes were rejected, so a large index walked past info->opps and could treat garbage as a clock rate (KASAN OOB / wrong frequency to consumers). The missing upper bound dates back to the original SCPI clock driver.
Treat indexes >= opp count as invalid and return 0, same as idx < 0.
--- drivers/clk/clk-scpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-scpi.c +1 -1
@@ -85,7 +85,7 @@ static unsigned long scpi_dvfs_recalc_rate(struct clk_hw *hw,int idx = clk->scpi_ops->dvfs_get_idx (clk->id);const struct scpi_opp *opp; -if (idx <0 ) +if (idx <0 || idx >= clk->info->count)return 0 ; opp = clk->info->opps + idx;
Signature · 1 line
2.53.0
PATCH v2 4/5 clk: scpi: register scpi-cpufreq once and clear on failure
In reply to Xixin Liuscpi_clocks_probe() walks clock children and, for each DVFS provider, calls platform_device_register_simple("scpi-cpufreq", -1, ...). Two related bugs:
1) Registration uses id = -1 and the fixed name "scpi-cpufreq". A second registration of the same name returns -EEXIST and overwrites the first good cpufreq_dev with ERR_PTR(-EEXIST). When cpufreq_dev is already set, continue so only the first successful registration remains.
2) On IS_ERR, cpufreq_dev still held the ERR_PTR; remove() would then call platform_device_unregister() on it (oops). Clear cpufreq_dev to NULL after the failure warn so remove() skips it.
--- drivers/clk/clk-scpi.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
drivers/clk/clk-scpi.c +5 -1
@@ -284,10 +284,14 @@ static int scpi_clocks_probe(struct platform_device *pdev)if (match->data != &scpi_dvfs_ops)continue ;/* Add the virtual cpufreq device if it's DVFS clock provider */ +if (cpufreq_dev) +continue ; cpufreq_dev =platform_device_register_simple ("scpi-cpufreq" ,-1 ,NULL ,0 ); -if (IS_ERR (cpufreq_dev)) +if (IS_ERR (cpufreq_dev)) {pr_warn ("unable to register cpufreq device" ); + cpufreq_dev =NULL ; + } }return 0 ; }
Signature · 1 line
2.53.0
PATCH v2 5/5 clk: scpi: use PLATFORM_DEVID_NONE for scpi-cpufreq
In reply to Xixin LiuReplace the magic -1 passed to platform_device_register_simple() with PLATFORM_DEVID_NONE. The constant is defined as (-1) in platform_device.h; this is a readability cleanup only and does not change behavior.
--- drivers/clk/clk-scpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-scpi.c +1 -1
@@ -287,7 +287,7 @@ static int scpi_clocks_probe(struct platform_device *pdev)if (cpufreq_dev)continue ; cpufreq_dev =platform_device_register_simple ("scpi-cpufreq" , --1 ,NULL ,0 ); +PLATFORM_DEVID_NONE ,NULL ,0 );if (IS_ERR (cpufreq_dev)) {pr_warn ("unable to register cpufreq device" ); cpufreq_dev =NULL ;
Signature · 1 line
2.53.0
Sudeep Holla
maintainerRe: [PATCH v2 5/5] clk: scpi: use PLATFORM_DEVID_NONE for scpi-cpufreq
Quoted 6 lines · Xixin Liu
Replace the magic -1 passed to
platform_device_register_simple()withPLATFORM_DEVID_NONE. The constant is defined as (-1) inplatform_device.h; this is a readability cleanup only and does not change behavior.Fixes 9490f01e2471 ("clk: scpi: add support for cpufreq virtual device")
This is not a fix, just a clean. I will drop this when applying.
Signature · 2 lines
Regards, Sudeep
Re: [PATCH v2 5/5] clk: scpi: use PLATFORM_DEVID_NONE for scpi-cpufreq
Hi Sudeep,
Just a gentle ping on this v2 series from 2026-07-28. Dropping the 5/5 cleanup on apply is fine with me. Could you please apply the rest when you have a chance?
Thanks,
Xixin Liu
Quoted 15 lines · Xixin Liu
Thanks for reviewing v1.
This v2 hardens the OF SCPI firmware and clock paths against a few real defects found while reviewing linux-next:
1)
device_nodeleak inscpi_dev_domain_id()afterof_parse_phandle_with_args()
2) DVFS OPP count from SCP trusted beyondMAX_DVFS_OPPS(OOB read / bad OPP table size)
3) DVFS index used as clock rate without an upper bound check
4) scpi-cpufreq registered once only, and cleared on register failure soremove()does not unregister anERR_PTR
5) usePLATFORM_DEVID_NONEinstead of bare -1 for the scpi-cpufreq platform device id (readability; Fixes the original registration)
Quoted 1 line
[...]
Applied to sudeep.holla/linux (for-next/scmi/fixes), thanks!
[1/5] firmware: arm_scpi: fix device_node leak in scpi_dev_domain_id
https://git.kernel.org/sudeep.holla/c/65320b642025 [2/5] firmware: arm_scpi: reject DVFS OPP count above MAX_DVFS_OPPS https://git.kernel.org/sudeep.holla/c/32471d84a487 [3/5] clk: scpi: bound-check DVFS index in scpi_dvfs_recalc_rate https://git.kernel.org/sudeep.holla/c/70f4b78d560e [4/5] clk: scpi: register scpi-cpufreq once and clear on failure https://git.kernel.org/sudeep.holla/c/ab06cf8152da [5/5] clk: scpi: use PLATFORM_DEVID_NONE for scpi-cpufreq https://git.kernel.org/sudeep.holla/c/86d923a882f4
Signature · 2 lines
Regards, Sudeep
Hi Sudeep,
Thanks for applying the series.
Thanks,
Xixin Liu