arm64: mm: use u32 for FDT size in fixmap_remap_fdt()
PATCH 1/1 arm64: mm: use u32 for FDT size in fixmap_remap_fdt()
fixmap_remap_fdt() uses a signed int for the FDT size, so a malformed totalsize bigger than INT_MAX wrongly passes the MAX_FDT_SIZE check. Then create_mapping_noalloc() is called with a huge size and triggers a BUG_ON() in the MMU code, with no diagnostic about the malformed FDT.
Change the FDT size from int to u32, which is the return type of fdt_totalsize(). So a malformed totalsize no longer wrongly passes the MAX_FDT_SIZE check, and setup_machine_fdt() prints a pr_crit diagnostic for it, not a BUG_ON in the MMU code.
--- arch/arm64/include/asm/mmu.h | 2 +- arch/arm64/kernel/setup.c | 4 ++-- arch/arm64/mm/fixmap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
arch/arm64/include/asm/mmu.h +1 -1
@@ -68,7 +68,7 @@ extern void create_mapping_noalloc(phys_addr_t phys, unsigned long virt,extern void create_pgd_mapping (struct mm_struct *mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,bool page_mappings_only); -extern void *fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot); +extern void *fixmap_remap_fdt (phys_addr_t dt_phys,u32 *size,pgprot_t prot);extern void mark_linear_text_alias_ro (void );extern int split_kernel_leaf_mapping (unsigned long start,unsigned long end);extern void linear_map_maybe_split_to_ptes (void );
arch/arm64/kernel/setup.c +2 -2
@@ -169,7 +169,7 @@ static void __init smp_build_mpidr_hash(void)static void __init setup_machine_fdt (phys_addr_t dt_phys) { -int size =0 ; +u32 size =0 ;void *dt_virt =fixmap_remap_fdt (dt_phys, &size,PAGE_KERNEL );const char *name;
@@ -182,7 +182,7 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)*/ if (!early_init_dt_scan (dt_virt, dt_phys)) {pr_crit ("\n" -"Error: invalid device tree blob: PA=%pa, VA=%px, size=%d bytes\n" +"Error: invalid device tree blob: PA=%pa, VA=%px, size=%u bytes\n" "The dtb must be 8-byte aligned and must not exceed 2 MB in size.\n" "\nPlease check your bootloader.\n" , &dt_phys, dt_virt, size);
arch/arm64/mm/fixmap.c +2 -2
@@ -134,11 +134,11 @@ void __set_fixmap(enum fixed_addresses idx, } } -void *__init fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot) +void *__init fixmap_remap_fdt (phys_addr_t dt_phys,u32 *size,pgprot_t prot) {const u64 dt_virt_base =__fix_to_virt (FIX_FDT );phys_addr_t dt_phys_base; -int offset; +u32 offset;void *dt_virt;/*
Signature · 1 line
2.43.0
Quoted 59 lines · Sang-Heon Jeon
fixmap_remap_fdt()uses a signed int for the FDT size, so a malformed totalsize bigger thanINT_MAXwrongly passes theMAX_FDT_SIZEcheck. Thencreate_mapping_noalloc()is called with a huge size and triggers aBUG_ON()in the MMU code, with no diagnostic about the malformed FDT.Change the FDT size from int to u32, which is the return type of
fdt_totalsize(). So a malformed totalsize no longer wrongly passes theMAX_FDT_SIZEcheck, andsetup_machine_fdt()prints apr_critdiagnostic for it, not aBUG_ONin the MMU code.Fixes 61bd93ce801b ("arm64: use fixmap region for permanent FDT mapping")Signed-off-by Sang-Heon Jeon <[email protected]>--- arch/arm64/include/asm/mmu.h | 2 +- arch/arm64/kernel/setup.c | 4 ++-- arch/arm64/mm/fixmap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)arch/arm64/include/asm/mmu.h +1
-1@@ -68,7 +68,7 @@ extern void create_mapping_noalloc(phys_addr_t phys, unsigned long virt,extern void create_pgd_mapping (struct mm_struct *mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,bool page_mappings_only); -extern void *fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot); +extern void *fixmap_remap_fdt (phys_addr_t dt_phys,u32 *size,pgprot_t prot);extern void mark_linear_text_alias_ro (void );extern int split_kernel_leaf_mapping (unsigned long start,unsigned long end);extern void linear_map_maybe_split_to_ptes (void );arch/arm64/kernel/setup.c +2
-2@@ -169,7 +169,7 @@ static void __init smp_build_mpidr_hash(void)static void __init setup_machine_fdt (phys_addr_t dt_phys) { -int size =0 ; +u32 size =0 ;void *dt_virt =fixmap_remap_fdt (dt_phys, &size,PAGE_KERNEL );const char *name;@@ -182,7 +182,7 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)*/ if (!early_init_dt_scan (dt_virt, dt_phys)) {pr_crit ("\n" -"Error: invalid device tree blob: PA=%pa, VA=%px, size=%d bytes\n" +"Error: invalid device tree blob: PA=%pa, VA=%px, size=%u bytes\n" "The dtb must be 8-byte aligned and must not exceed 2 MB in size.\n" "\nPlease check your bootloader.\n" , &dt_phys, dt_virt, size);arch/arm64/mm/fixmap.c +0
-0
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 141
Quoted 10 lines
arch/arm64/mm/fixmap.c +2
-2@@ -134,11 +134,11 @@ void __set_fixmap(enum fixed_addresses idx, } } -void *__init fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot) +void *__init fixmap_remap_fdt (phys_addr_t dt_phys,u32 *size,pgprot_t prot) {const u64 dt_virt_base =__fix_to_virt (FIX_FDT );phys_addr_t dt_phys_base; -int offset; +u32 offset;
Also sashiko found another issue with malformed total size. And I think that can be resolved by the following changes.
*size = fdt_totalsize(dt_virt);
+ if (*size < sizeof(struct fdt_header))
+ return NULL;
Before sending a separate patch about malformed totalsize. First, I'd rather check reviewers' opinions.
1. Do we need to care about malformed totalsize? (including addressed by the current patch)
2. Does the above change need to be merged with the current patch or not?
Looking forward to your feedback.
Best Regards,
Sang-Heon Jeon
Quoted 6 lines
arch/arm64/mm/fixmap.c +0
-1void *dt_virt;/* -- 2.43.0
Quoted 64 lines · Sang-Heon Jeon
Quoted 62 lines · Sang-Heon Jeon
fixmap_remap_fdt()uses a signed int for the FDT size, so a malformed totalsize bigger thanINT_MAXwrongly passes theMAX_FDT_SIZEcheck. Thencreate_mapping_noalloc()is called with a huge size and triggers aBUG_ON()in the MMU code, with no diagnostic about the malformed FDT.Change the FDT size from int to u32, which is the return type of
fdt_totalsize(). So a malformed totalsize no longer wrongly passes theMAX_FDT_SIZEcheck, andsetup_machine_fdt()prints apr_critdiagnostic for it, not aBUG_ONin the MMU code.Fixes 61bd93ce801b ("arm64: use fixmap region for permanent FDT mapping")Signed-off-by Sang-Heon Jeon <[email protected]>--- arch/arm64/include/asm/mmu.h | 2 +- arch/arm64/kernel/setup.c | 4 ++-- arch/arm64/mm/fixmap.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)arch/arm64/include/asm/mmu.h +1
-1@@ -68,7 +68,7 @@ extern void create_mapping_noalloc(phys_addr_t phys, unsigned long virt,extern void create_pgd_mapping (struct mm_struct *mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,bool page_mappings_only); -extern void *fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot); +extern void *fixmap_remap_fdt (phys_addr_t dt_phys,u32 *size,pgprot_t prot);extern void mark_linear_text_alias_ro (void );extern int split_kernel_leaf_mapping (unsigned long start,unsigned long end);extern void linear_map_maybe_split_to_ptes (void );arch/arm64/kernel/setup.c +2
-2@@ -169,7 +169,7 @@ static void __init smp_build_mpidr_hash(void)static void __init setup_machine_fdt (phys_addr_t dt_phys) { -int size =0 ; +u32 size =0 ;void *dt_virt =fixmap_remap_fdt (dt_phys, &size,PAGE_KERNEL );const char *name;@@ -182,7 +182,7 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)*/ if (!early_init_dt_scan (dt_virt, dt_phys)) {pr_crit ("\n" -"Error: invalid device tree blob: PA=%pa, VA=%px, size=%d bytes\n" +"Error: invalid device tree blob: PA=%pa, VA=%px, size=%u bytes\n" "The dtb must be 8-byte aligned and must not exceed 2 MB in size.\n" "\nPlease check your bootloader.\n" , &dt_phys, dt_virt, size);arch/arm64/mm/fixmap.c +0
-0@@ -134,11 +134,11 @@ void __set_fixmap(enum fixed_addresses idx, } }
Quoted 10 lines
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 141
Quoted 7 lines
arch/arm64/mm/fixmap.c +2
-2-void *__init fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot) +void *__init fixmap_remap_fdt (phys_addr_t dt_phys,u32 *size,pgprot_t prot) {const u64 dt_virt_base =__fix_to_virt (FIX_FDT );phys_addr_t dt_phys_base; -int offset; +u32 offset;Also sashiko found another issue with malformed total size. And I think that can be resolved by the following changes.
Add missing sashiko review link :
https://sashiko.dev/#/patchset/20260514171304.2034930-1-ekffu200098%40gmail.com
Quoted 22 lines
*size = fdt_totalsize(dt_virt);+ if (*size <
sizeof(struct fdt_header))
+ return NULL;Before sending a separate patch about malformed totalsize. First, I'd rather check reviewers' opinions.
1. Do we need to care about malformed totalsize? (including addressed by the current patch)
2. Does the above change need to be merged with the current patch or not?Looking forward to your feedback.
Best Regards,
Sang-Heon JeonQuoted 6 lines
arch/arm64/mm/fixmap.c +0
-1void *dt_virt;/* -- 2.43.0