From: Roger Pau Monne Date: Thu, 13 Mar 2025 11:19:48 +0000 (+0100) Subject: x86/ioremap: prevent additions against the NULL pointer X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=e10a2db72dc8ac3db857c642eddee5a9ca108106;p=people%2Froyger%2Fxen.git x86/ioremap: prevent additions against the NULL pointer This was reported by clang UBSAN as: UBSAN: Undefined behaviour in arch/x86/mm.c:6297:40 applying zero offset to null pointer [...] Xen call trace: [] R common/ubsan/ubsan.c#ubsan_epilogue+0xa/0xc0 [] F __ubsan_handle_pointer_overflow+0xcb/0x100 [] F ioremap_wc+0xc8/0xe0 [] F video_init+0xd0/0x180 [] F console_init_preirq+0x3d/0x220 [] F __start_xen+0x68e/0x5530 [] F __high_start+0x8e/0x90 Fix both ioremap{,_wc}() to not add the offset if the returned pointer from __vmap() is NULL. Signed-off-by: Roger Pau Monné --- diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index bfdc8fb019..4af6c4ce86 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6277,7 +6277,9 @@ void __iomem *ioremap(paddr_t pa, size_t len) unsigned int offs = pa & (PAGE_SIZE - 1); unsigned int nr = PFN_UP(offs + len); - va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT) + offs; + va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT); + if ( va ) + va += offs; } return (void __force __iomem *)va; @@ -6294,7 +6296,7 @@ void __iomem *__init ioremap_wc(paddr_t pa, size_t len) va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_WC, VMAP_DEFAULT); - return (void __force __iomem *)(va + offs); + return (void __force __iomem *)(va ? va + offs : va); } int create_perdomain_mapping(struct domain *d, unsigned long va,