ARM: imx: Fix suspend/resume crash with Clang CFI
PATCH 1/1 ARM: imx: Fix suspend/resume crash with Clang CFI
Relocated suspend code in OCRAM lacks compiler-generated CFI type signatures. When CONFIG_CFI=y is active, the indirect call to imx6_suspend_in_ocram_fn triggers a strict CFI violation panic.
Annotate imx6q_suspend_finish with __nocfi to bypass CFI checking for this specific indirect call.
---
Tested on a Kobo Clara HD (i.MX6SLL SoC) running postmarketOS edge. Before this patch, suspending the device caused an immediate silent hang requiring a hard-reboot. With this patch applied, suspend and resume work successfully.
arch/arm/mach-imx/pm-imx6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-imx/pm-imx6.c +1 -1
@@ -360,7 +360,7 @@ int imx6_set_lpm(enum mxc_cpu_pwr_mode mode)return 0 ; } -static int imx6q_suspend_finish (unsigned long val) +static int __nocfi imx6q_suspend_finish(unsigned long val) {if (!imx6_suspend_in_ocram_fn) {cpu_do_idle ();
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] The __nocfi annotation on imx6q_suspend_finish creates a KCFI bypass gadget and unnecessarily removes CFI protection from standard kernel indirect calls.
Pre-existing issues:
- [High] The KCFI panic bypass for dynamically-copied assembly routines is incomplete, as identical patterns exist in other ARM platforms that will still panic when KCFI is enabled and suspend is triggered. --
--- Patch [1]: [PATCH] ARM: imx: Fix suspend/resume crash with Clang CFI --- commit aea4e29393049bd454bac337e0878d861ebe299d
Author: Yo'av Moshe <[email protected]>
ARM: imx: Fix suspend/resume crash with Clang CFI
This patch annotates imx6q_suspend_finish with __nocfi to bypass strict CFI violations during suspend when CONFIG_CFI=y is active. The crash occurs because relocated suspend code in OCRAM lacks compiler-generated CFI type signatures.
Quoted 4 lines
arch/arm/mach-imx/pm-imx6.c +0
-0
[ ... ]
arch/arm/mach-imx/pm-imx6.c int imx6_set_lpm(enum mxc_cpu_pwr_mode mode) · line 363
Quoted 6 lines
arch/arm/mach-imx/pm-imx6.c +1
-1@@ -360,7 +360,7 @@ int imx6_set_lpm(enum mxc_cpu_pwr_mode mode)return 0 ; } -static int imx6q_suspend_finish (unsigned long val) +static int __nocfi imx6q_suspend_finish(unsigned long val)
[Severity: High]
Does applying __nocfi to the entire function expose the globally writable imx6_suspend_in_ocram_fn pointer as a potential KCFI bypass gadget?
Since imx6_suspend_in_ocram_fn is defined without __ro_after_init:
arch/arm/mach-imx/pm-imx6.c:
static void (*imx6_suspend_in_ocram_fn)(void __iomem *ocram_vbase);
Could an attacker with an arbitrary write primitive overwrite this pointer and bypass KCFI execution validation when system suspend is triggered? Also, does annotating the whole function unnecessarily disable CFI protection for standard kernel calls like cpu_do_idle() and flush_cache_all() that occur in the same scope?
Quoted 4 lines
arch/arm/mach-imx/pm-imx6.c +0
-0{if (!imx6_suspend_in_ocram_fn) {cpu_do_idle ();arch/arm/mach-imx/pm-imx6.c +0
-0}else {
arch/arm/mach-imx/pm-imx6.c
Quoted 10 lines
arch/arm/mach-imx/pm-imx6.c +0
-0/* * call low level suspend function in ocram, * as we need to float DDR IO. */ local_flush_tlb_all ();/* check if need to flush internal L2 cache */ if (!((struct imx6_cpu_pm_info *) suspend_ocram_base)->l2_base.vbase)flush_cache_all ();imx6_suspend_in_ocram_fn (suspend_ocram_base);
[Severity: High]
This is a pre-existing issue, but do identical patterns exist in other ARM platforms that will still trigger a KCFI panic on suspend?
For example, mx5_suspend_enter() in arch/arm/mach-imx/pm-imx5.c calls the dynamically copied imx5_suspend_in_ocram_fn() via an indirect C call:
arch/arm/mach-imx/pm-imx5.c:mx5_suspend_enter() {
...
imx5_suspend_in_ocram_fn(suspend_ocram_base);
...
}Similar patterns exist in arch/arm/mach-at91/pm.c for at91_suspend_sram_fn() and arch/arm/mach-socfpga/pm.c for socfpga_sdram_self_refresh_in_ocram().
Because fncpy does not copy the KCFI type hash located 4 bytes before the assembly function entry, will these platforms also crash on suspend when CONFIG_CFI_CLANG=y is active?
Quoted 4 lines
arch/arm/mach-imx/pm-imx6.c +0
-0}return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 10 lines · Yo'av Moshe
Relocated suspend code in OCRAM lacks compiler-generated CFI type signatures. When
CONFIG_CFI=yis active, the indirect call toimx6_suspend_in_ocram_fntriggers a strict CFI violation panic.Annotate
imx6q_suspend_finishwith__nocfito bypass CFI checking for this specific indirect call.Signed-off-by Yo'av Moshe <[email protected]>
Thanks for the patch.
Quoted 25 lines
---
Tested on a Kobo Clara HD (i.MX6SLL SoC) running postmarketOS edge. Before this patch, suspending the device caused an immediate silent hang requiring a hard-reboot. With this patch applied, suspend and resume work successfully.arch/arm/mach-imx/pm-imx6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm/mach-imx/pm-imx6.c +1
-2@@ -360,7 +360,7 @@ int imx6_set_lpm(enum mxc_cpu_pwr_mode mode)return 0 ; } -static int imx6q_suspend_finish (unsigned long val) +static int __nocfi imx6q_suspend_finish(unsigned long val) {if (!imx6_suspend_in_ocram_fn) {cpu_do_idle (); --2.55.0
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 7 lines · Nick Desaulniers
Quoted 5 lines · Yo'av Moshe
Relocated suspend code in OCRAM lacks compiler-generated CFI type signatures. When
CONFIG_CFI=yis active, the indirect call toimx6_suspend_in_ocram_fntriggers a strict CFI violation panic.
Quoted 8 lines
Quoted 5 lines
Annotate
imx6q_suspend_finishwith__nocfito bypass CFI checking for this specific indirect call.Signed-off-by Yo'av Moshe <[email protected]>Thanks for the patch.
Reviewed-by Nick Desaulniers <[email protected]>
Ah, looks like I'm reviewing v1, but there's already been a v2 and v3. Disregard.
Quoted 31 lines
Quoted 24 lines
---
Tested on a Kobo Clara HD (i.MX6SLL SoC) running postmarketOS edge. Before this patch, suspending the device caused an immediate silent hang requiring a hard-reboot. With this patch applied, suspend and resume work successfully.arch/arm/mach-imx/pm-imx6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm/mach-imx/pm-imx6.c +1
-2@@ -360,7 +360,7 @@ int imx6_set_lpm(enum mxc_cpu_pwr_mode mode)return 0 ; } -static int imx6q_suspend_finish (unsigned long val) +static int __nocfi imx6q_suspend_finish(unsigned long val) {if (!imx6_suspend_in_ocram_fn) {cpu_do_idle (); --Quoted 2 lines
2.55.0
--
Thanks,
~Nick Desaulniers
Signature · 2 lines
Thanks, ~Nick Desaulniers