Skip to content

Commit 35d8945

Browse files
Rongronggg9tsbogend
authored andcommitted
MIPS: Loongson64: env: Check UARTs passed by LEFI cautiously
Some firmware does not set nr_uarts properly and passes empty items. Iterate at most min(system->nr_uarts, MAX_UARTS) items to prevent out-of-bounds access, and ignore UARTs with addr 0 silently. Meanwhile, our DT only works with UPIO_MEM but theoretically firmware may pass other IO types, so explicitly check against that. Tested on Loongson-LS3A4000-7A1000-NUC-SE. Fixes: 3989ed4 ("MIPS: Loongson64: env: Fixup serial clock-frequency when using LEFI") Cc: stable@vger.kernel.org Reviewed-by: Yao Zi <me@ziyao.cc> Signed-off-by: Rong Zhang <rongrong@oss.cipunited.com> Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
1 parent 6de23f8 commit 35d8945

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

  • arch/mips/loongson64

arch/mips/loongson64/env.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#include <linux/dma-map-ops.h>
1818
#include <linux/export.h>
1919
#include <linux/libfdt.h>
20+
#include <linux/minmax.h>
2021
#include <linux/pci_ids.h>
22+
#include <linux/serial_core.h>
2123
#include <linux/string_choices.h>
2224
#include <asm/bootinfo.h>
2325
#include <loongson.h>
@@ -106,9 +108,23 @@ static void __init lefi_fixup_fdt(struct system_loongson *system)
106108

107109
is_loongson64g = (read_c0_prid() & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64G;
108110

109-
for (i = 0; i < system->nr_uarts; i++) {
111+
for (i = 0; i < min(system->nr_uarts, MAX_UARTS); i++) {
110112
uartdev = &system->uarts[i];
111113

114+
/*
115+
* Some firmware does not set nr_uarts properly and passes empty
116+
* items. Ignore them silently.
117+
*/
118+
if (uartdev->uart_base == 0)
119+
continue;
120+
121+
/* Our DT only works with UPIO_MEM. */
122+
if (uartdev->iotype != UPIO_MEM) {
123+
pr_warn("Ignore UART 0x%llx with iotype %u passed by firmware\n",
124+
uartdev->uart_base, uartdev->iotype);
125+
continue;
126+
}
127+
112128
ret = lefi_fixup_fdt_serial(fdt_buf, uartdev->uart_base,
113129
uartdev->uartclk);
114130
/*

0 commit comments

Comments
 (0)