diff mbox series

[1/1] riscv: add NULL check before calling strlen in the riscv cpu's get_desc()

Message ID tencent_6178F4CBD2326AD3FE5FBAA496630920D405@qq.com
State Accepted
Commit 9578e74571596eb8f1a74b46cbb2ddf6ed5dee39
Delegated to: Andes
Headers show
Series [1/1] riscv: add NULL check before calling strlen in the riscv cpu's get_desc() | expand

Commit Message

Hanyuan Zhao May 6, 2024, 9:10 a.m. UTC
Without the NULL check, if the devicetree that u-boot loads does not have a
compatible property then a store access fault will be raised and force the
machine to reset, due to the NULL pointer we passed to strlen. This commit
adds this check and will return -ENOSPC to indicate the get_desc failed.

Signed-off-by: Hanyuan Zhao <zhaohy22@mails.tsinghua.edu.cn>
---
 drivers/cpu/riscv_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Leo Liang May 14, 2024, 10:07 a.m. UTC | #1
On Mon, May 06, 2024 at 05:10:06PM +0800, Hanyuan Zhao wrote:
> Without the NULL check, if the devicetree that u-boot loads does not have a
> compatible property then a store access fault will be raised and force the
> machine to reset, due to the NULL pointer we passed to strlen. This commit
> adds this check and will return -ENOSPC to indicate the get_desc failed.
> 
> Signed-off-by: Hanyuan Zhao <zhaohy22@mails.tsinghua.edu.cn>
> ---
>  drivers/cpu/riscv_cpu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
diff mbox series

Patch

diff --git a/drivers/cpu/riscv_cpu.c b/drivers/cpu/riscv_cpu.c
index 9b1950efe0..d39a943cb8 100644
--- a/drivers/cpu/riscv_cpu.c
+++ b/drivers/cpu/riscv_cpu.c
@@ -24,7 +24,7 @@  static int riscv_cpu_get_desc(const struct udevice *dev, char *buf, int size)
 	const char *cpu;
 
 	cpu = dev_read_string(dev, "compatible");
-	if (size < (strlen(cpu) + 1))
+	if (!cpu || size < (strlen(cpu) + 1))
 		return -ENOSPC;
 
 	strcpy(buf, cpu);