Enable UBSAN on ppc
cover Enable UBSAN on ppc
Extend Andrew's UBSAN enablement work to ppc.
Andrew Cooper (1):
PPC: Activate UBSAN in testing
Shawn Anastasio (1):
xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN
automation/gitlab-ci/build.yaml | 3 +++ xen/arch/ppc/Kconfig | 1 + xen/arch/ppc/include/asm/processor.h | 2 ++ xen/arch/ppc/opal.c | 18 +++++++++++------- xen/arch/ppc/stubs.c | 2 +- 5 files changed, 18 insertions(+), 8 deletions(-)
--
2.30.2
PATCH 1/2 xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN
Fix two misaligned reads from the FDT in the opal setup code to avoid tripping UBSAN failures. Without this change, UBSAN-enabled builds on PPC will fail on boot before the serial console is even initialized.
--- xen/arch/ppc/opal.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/ppc/opal.c +11 -7
@@ -34,8 +34,9 @@ static void opal_putchar(char c)void __init boot_opal_init (const void *fdt) {int opal_node; -const __be64 *opal_base; -const __be64 *opal_entry; +const __be64 *opal_base_p; +const __be64 *opal_entry_p; +__be64 opal_base, opal_entry;if (fdt_check_header (fdt) <0 ) {
@@ -54,17 +55,20 @@ void __init boot_opal_init(const void *fdt)die (); } - opal_base =fdt_getprop (fdt, opal_node,"opal-base-address" ,NULL ); - opal_entry =fdt_getprop (fdt, opal_node,"opal-entry-address" ,NULL ); -if ( !opal_base || !opal_entry ) + opal_base_p =fdt_getprop (fdt, opal_node,"opal-base-address" ,NULL ); + opal_entry_p =fdt_getprop (fdt, opal_node,"opal-entry-address" ,NULL ); +if ( !opal_base_p || !opal_entry_p ) {early_printk ("Failed to get opal-base-address/opal-entry-address " "property from DT!\n" );die (); } - opal.base =be64_to_cpu (*opal_base); - opal.entry =be64_to_cpu (*opal_entry); +memcpy (&opal_base, opal_base_p,sizeof (opal_base)); +memcpy (&opal_entry, opal_entry_p,sizeof (opal_entry)); + + opal.base =be64_to_cpu (opal_base); + opal.entry =be64_to_cpu (opal_entry);early_printk_init (opal_putchar);
Signature · 1 line
2.30.2
Re: [PATCH 1/2] xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN
Quoted 39 lines · Shawn Anastasio
Fix two misaligned reads from the FDT in the opal setup code to avoid tripping UBSAN failures. Without this change, UBSAN-enabled builds on PPC will fail on boot before the serial console is even initialized.
Reported-by Andrew Cooper <[email protected]>Signed-off-by Shawn Anastasio <[email protected]>--- xen/arch/ppc/opal.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)xen/arch/ppc/opal.c +6
-5@@ -34,8 +34,9 @@ static void opal_putchar(char c)void __init boot_opal_init (const void *fdt) {int opal_node; -const __be64 *opal_base; -const __be64 *opal_entry; +const __be64 *opal_base_p; +const __be64 *opal_entry_p; +__be64 opal_base, opal_entry;if (fdt_check_header (fdt) <0 ) {@@ -54,17 +55,20 @@ void __init boot_opal_init(const void *fdt)die (); } - opal_base =fdt_getprop (fdt, opal_node,"opal-base-address" ,NULL ); - opal_entry =fdt_getprop (fdt, opal_node,"opal-entry-address" ,NULL ); -if ( !opal_base || !opal_entry ) + opal_base_p =fdt_getprop (fdt, opal_node,"opal-base-address" ,NULL ); + opal_entry_p =fdt_getprop (fdt, opal_node,"opal-entry-address" ,NULL ); +if ( !opal_base_p || !opal_entry_p ) {early_printk ("Failed to get opal-base-address/opal-entry-address " "property from DT!\n" );
xen/arch/ppc/opal.c void __init boot_opal_init(const void *fdt) · line 71
Quoted 10 lines
xen/arch/ppc/opal.c +5
-2die (); } - opal.base =be64_to_cpu (*opal_base); - opal.entry =be64_to_cpu (*opal_entry); +memcpy (&opal_base, opal_base_p,sizeof (opal_base)); +memcpy (&opal_entry, opal_entry_p,sizeof (opal_entry)); + + opal.base =be64_to_cpu (opal_base); + opal.entry =be64_to_cpu (opal_entry);
Thanks for getting to the bottom of this.
However, you can use get_unaligned_*() and friends which is probably a bit nicer, and doesn't involve the extra local variables.
~Andrew
Re: [PATCH 1/2] xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN
Quoted 52 lines · Andrew Cooper
Quoted 39 lines · Shawn Anastasio
Fix two misaligned reads from the FDT in the opal setup code to avoid tripping UBSAN failures. Without this change, UBSAN-enabled builds on PPC will fail on boot before the serial console is even initialized.
Reported-by Andrew Cooper <[email protected]>Signed-off-by Shawn Anastasio <[email protected]>--- xen/arch/ppc/opal.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)xen/arch/ppc/opal.c +6
-5@@ -34,8 +34,9 @@ static void opal_putchar(char c)void __init boot_opal_init (const void *fdt) {int opal_node; -const __be64 *opal_base; -const __be64 *opal_entry; +const __be64 *opal_base_p; +const __be64 *opal_entry_p; +__be64 opal_base, opal_entry;if (fdt_check_header (fdt) <0 ) {@@ -54,17 +55,20 @@ void __init boot_opal_init(const void *fdt)die (); } - opal_base =fdt_getprop (fdt, opal_node,"opal-base-address" ,NULL ); - opal_entry =fdt_getprop (fdt, opal_node,"opal-entry-address" ,NULL ); -if ( !opal_base || !opal_entry ) + opal_base_p =fdt_getprop (fdt, opal_node,"opal-base-address" ,NULL ); + opal_entry_p =fdt_getprop (fdt, opal_node,"opal-entry-address" ,NULL ); +if ( !opal_base_p || !opal_entry_p ) {early_printk ("Failed to get opal-base-address/opal-entry-address " "property from DT!\n" );xen/arch/ppc/opal.c void __init boot_opal_init(const void *fdt) · line 71
Quoted 10 lines
xen/arch/ppc/opal.c +5
-2die (); } - opal.base =be64_to_cpu (*opal_base); - opal.entry =be64_to_cpu (*opal_entry); +memcpy (&opal_base, opal_base_p,sizeof (opal_base)); +memcpy (&opal_entry, opal_entry_p,sizeof (opal_entry)); + + opal.base =be64_to_cpu (opal_base); + opal.entry =be64_to_cpu (opal_entry);Thanks for getting to the bottom of this.
Quoted 2 lines
However, you can use get_unaligned_*() and friends which is probably a bit nicer, and doesn't involve the extra local variables.
Sorry, the other thing to say is that if PPC is generally fine with unaligned accesses, then you might want to follow what x86 does.
We use -fno-sanitize=alignment generally, because there's a whole pile of things which are misaigned and spec'd that way for backwards compatibility.
~Andrew
Re: [PATCH 1/2] xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN
Quoted 6 lines · Andrew Cooper
Sorry, the other thing to say is that if PPC is generally fine with unaligned accesses, then you might want to follow what x86 does.
We use -fno-sanitize=alignment generally, because there's a whole pile of things which are misaigned and spec'd that way for backwards compatibility.
Oh perfect -- I did do an initial grep to see if this was done but couldn't immediately find it. The Power ISA does guarantee that unaligned word/doubleword reads are handled transparently by the hardware so enabling this seems like a reasonable approach on PPC as well. I'll submit a v2 that does this.
Quoted 1 line
~Andrew
Thanks,
Shawn
PATCH 2/2 PPC: Activate UBSAN in testing
In reply to Shawn AnastasioFrom: Andrew Cooper <[email protected]>
---
Changes from Andrew's v1:
- Define dump_execution_state in the same manner as risc-v
automation/gitlab-ci/build.yaml | 3 +++ xen/arch/ppc/Kconfig | 1 + xen/arch/ppc/include/asm/processor.h | 2 ++ xen/arch/ppc/stubs.c | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-)
automation/gitlab-ci/build.yaml +3 -0
@@ -352,6 +352,9 @@ debian-12-ppc64le-gcc-debug: CONTAINER: debian:12-ppc64le KBUILD_DEFCONFIG: ppc64_defconfig HYPERVISOR_ONLY: y + EXTRA_XEN_CONFIG: | + CONFIG_UBSAN=y + CONFIG_UBSAN_FATAL=y debian-12-riscv64-gcc-debug: extends: .gcc-riscv64-cross-build-debug
xen/arch/ppc/Kconfig +1 -0
@@ -2,6 +2,7 @@ config PPC def_bool y select FUNCTION_ALIGNMENT_4B select HAS_DEVICE_TREE + select HAS_UBSAN select HAS_VMAP config PPC64
xen/arch/ppc/include/asm/processor.h +2 -0
@@ -219,6 +219,8 @@ static inline void noreturn die(void)*/ #define cpu_relax () asm volatile ( "or %r1, %r1, %r1; or %r2, %r2, %r2" ) +#define dump_execution_state () run_in_exception_handler(show_execution_state) +#endif /* __ASSEMBLY__ */ #endif /* _ASM_PPC_PROCESSOR_H */
xen/arch/ppc/stubs.c +1 -1
@@ -47,7 +47,7 @@ void send_timer_event(struct vcpu *v)void show_execution_state (const struct cpu_user_regs *regs) { -BUG_ON ("unimplemented" ); +printk ("TODO: Implement show_execution_state(regs)\n" ); }void arch_hypercall_tasklet_result (struct vcpu *v,long res)
--
2.30.2